-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
145 lines (126 loc) · 3.53 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright 2020 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"bytes"
"context"
"io"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
// ExecIntoPod is used to select
func ExecIntoPod(client kubernetes.Interface, dpl *appsv1.Deployment, cmd string) error {
command := []string{"/bin/bash", "-c", cmd}
pod, err := getDeploymentPod(client, dpl)
if err != nil {
return err
}
if _, stderr, err := exec(client, command, pod, dpl.Namespace); err != nil {
return errors.Wrapf(err, "failed to exec, %s", StringValueFallback(stderr, "no error message"))
}
return nil
}
// run exec command on pod
func exec(cs kubernetes.Interface, command []string, pod, ns string) (*string, *string, error) {
req := cs.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod).
Namespace(ns).
SubResource("exec")
req.VersionedParams(&corev1.PodExecOptions{
Command: command,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
cfg, err := config.GetConfig()
if err != nil {
return nil, nil, err
}
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL())
if err != nil {
return nil, nil, errors.Wrap(err, "error while creating executor")
}
var stdout, stderr bytes.Buffer
var stdin io.Reader
err = exec.Stream(remotecommand.StreamOptions{
Stdin: stdin,
Stdout: &stdout,
Stderr: &stderr,
Tty: false,
})
if err != nil {
return String(stdout.String()), String(stderr.String()), err
}
return String(stdout.String()), String(stderr.String()), nil
}
func getDeploymentPod(cl kubernetes.Interface, dpl *appsv1.Deployment) (string, error) {
name := dpl.Name
ns := dpl.Namespace
api := cl.CoreV1()
listOptions := metav1.ListOptions{
LabelSelector: "deployment=" + name,
}
podList, err := api.Pods(ns).List(context.Background(), listOptions)
if err != nil {
return "", err
}
podListItems := podList.Items
if len(podListItems) == 0 {
return "", nil
}
return podListItems[0].Name, nil
}
// String is a utility function converting a string to a *string
func String(s string) *string {
if s == "" {
return nil
}
return &s
}
// StringValue is a utility function converting a *string to a string
func StringValue(s *string) string {
if s == nil {
return ""
}
return *s
}
// StringValueFallback is a utility function converting a converting a *string to a string with a fallback value
func StringValueFallback(s *string, fb string) string {
if s == nil {
return fb
}
return *s
}
// Int is a utility function converting an int to an int pointer
func Int(i int) *int {
return &i
}
// IntValue is a utility function converting an *int32 to a value
func IntValue(i *int) int {
if i == nil {
return 0
}
return *i
}
// Int32 is a utility function converting an int32 to a pointer
func Int32(i int32) *int32 {
return &i
}