forked from chaosblade-io/chaosblade-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
158 lines (146 loc) · 4.26 KB
/
client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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 channel
import (
"bytes"
"fmt"
"io"
"net/url"
"strings"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// Client contains the kubernetes client, operator client and kubeconfig
type Client struct {
kubernetes.Interface
client.Client
Config *rest.Config
}
// NewClientFunc returns the controller client
func NewClientFunc() manager.NewClientFunc {
return func(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) {
// Create the Client for Write operations.
c, err := client.New(config, options)
if err != nil {
return nil, err
}
cli := &Client{}
cli.Interface = kubernetes.NewForConfigOrDie(config)
cli.Client = &client.DelegatingClient{
Reader: &client.DelegatingReader{
CacheReader: cache,
ClientReader: c,
},
Writer: c,
StatusClient: c,
}
cli.Config = config
return cli, nil
}
}
type IOStreams struct {
In io.Reader
Out io.Writer
ErrOut io.Writer
}
type StreamOptions struct {
IOStreams
Stdin bool
TTY bool
OutDecoder func(bytes []byte) interface{}
ErrDecoder func(bytes []byte) interface{}
}
type ExecOptions struct {
StreamOptions
PodName string
PodNamespace string
ContainerName string
Command []string
IgnoreOutput bool
}
// Exec command in pod
func (c *Client) Exec(options *ExecOptions) interface{} {
logFields := logrus.WithFields(logrus.Fields{
"command": options.Command,
"podName": options.PodName,
"podNamespace": options.PodNamespace,
"container": options.ContainerName,
})
logFields.Infof("Exec command in pod")
request := c.CoreV1().RESTClient().Post().
Resource("pods").
Name(options.PodName).
Namespace(options.PodNamespace).
SubResource("exec").
VersionedParams(
&corev1.PodExecOptions{
Container: options.ContainerName,
Command: options.Command,
Stdin: options.Stdin,
Stdout: true,
Stderr: true,
TTY: options.TTY,
}, scheme.ParameterCodec)
output := bytes.NewBuffer([]byte{})
options.Out = output
errput := bytes.NewBuffer([]byte{})
options.ErrOut = errput
err := execute("POST", request.URL(), c.Config, options)
errMsg := strings.TrimSpace(errput.String())
outMsg := strings.TrimSpace(output.String())
execLog := logFields.WithFields(logrus.Fields{
"err": errMsg,
"out": outMsg,
})
if errMsg != "" {
execLog.Infof("get err message")
return options.ErrDecoder(errput.Bytes())
}
if err != nil {
execLog.WithError(err).Errorln("Invoke exec command error")
return options.ErrDecoder([]byte(err.Error()))
}
if outMsg != "" {
execLog.Infof("get output message")
return options.OutDecoder(output.Bytes())
}
if options.IgnoreOutput {
return nil
}
return options.ErrDecoder([]byte(fmt.Sprintf("cannot get output of pods/%s/exec, maybe kubelet cannot be accessed or container not found",
options.PodName)))
}
// "172.21.1.11:8080/api/v1/namespaces/default/pods/my-nginx-3855515330-l1uqk/exec
// ?container=my-nginx&stdin=1&stdout=1&stderr=1&tty=1&command=%2Fbin%2Fbash"
func execute(method string, url *url.URL, config *rest.Config, options *ExecOptions) error {
exec, err := remotecommand.NewSPDYExecutor(config, method, url)
if err != nil {
return err
}
return exec.Stream(remotecommand.StreamOptions{
Stdin: options.StreamOptions.In,
Stdout: options.StreamOptions.Out,
Stderr: options.StreamOptions.ErrOut,
Tty: options.StreamOptions.TTY,
})
}