-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathclient.go
More file actions
149 lines (127 loc) · 3.73 KB
/
client.go
File metadata and controls
149 lines (127 loc) · 3.73 KB
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
package kubernetes
import (
"context"
"fmt"
"io"
"os"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
type Client struct {
client *kubernetes.Clientset
config *rest.Config
}
// NewClient constructs a struct wrapping the kubernetes client that is used by the kubernetes driver
func NewClient(kubeConfig, kubeContext string) (*Client, string, error) {
if kubeConfig == "" {
kubeConfig = os.Getenv("KUBECONFIG")
}
// create client config loading rules
var clientConfigLoadingRules *clientcmd.ClientConfigLoadingRules
if kubeConfig != "" {
clientConfigLoadingRules = &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig}
} else {
clientConfigLoadingRules = clientcmd.NewDefaultClientConfigLoadingRules()
}
// load kubernetes config
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientConfigLoadingRules,
&clientcmd.ConfigOverrides{CurrentContext: kubeContext},
)
clientConfig, err := config.ClientConfig()
if err != nil {
return nil, "", fmt.Errorf("failed to load kubernetes config: %w", err)
}
namespace, _, err := config.Namespace()
if err != nil {
return nil, "", fmt.Errorf("failed to load kubernetes namespace from config: %w", err)
}
kubeClient, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, "", err
}
return &Client{
client: kubeClient,
config: clientConfig,
}, namespace, nil
}
func (c *Client) Client() *kubernetes.Clientset {
return c.client
}
func (c *Client) Config() *rest.Config {
return c.config
}
func (c *Client) FullLogs(ctx context.Context, namespace, pod, container string) ([]byte, error) {
logs, err := c.Logs(ctx, namespace, pod, container, true)
if err != nil {
return nil, err
}
return io.ReadAll(logs)
}
func (c *Client) Logs(ctx context.Context, namespace, pod, container string, follow bool) (io.ReadCloser, error) {
return c.client.CoreV1().Pods(namespace).GetLogs(pod, &corev1.PodLogOptions{
Container: container,
Follow: follow,
}).Stream(ctx)
}
type ExecStreamOptions struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Pod string
Namespace string
Container string
Command []string
}
// Exec executes a kubectl exec with given transport round tripper and upgrader
func (c *Client) Exec(ctx context.Context, options *ExecStreamOptions) error {
client, err := kubernetes.NewForConfig(c.config)
if err != nil {
return err
}
execRequest := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(options.Pod).
Namespace(options.Namespace).
SubResource(string("exec")).
VersionedParams(&corev1.PodExecOptions{
Container: options.Container,
Command: options.Command,
Stdin: options.Stdin != nil,
Stdout: options.Stdout != nil,
Stderr: options.Stderr != nil,
}, scheme.ParameterCodec)
// Use WebSocket executor which is the default as of Kubernetes 1.31. Fall back to SPDY for older clusters.
websocketExec, err := remotecommand.NewWebSocketExecutor(c.config, "GET", execRequest.URL().String())
if err != nil {
return err
}
spdyExec, err := remotecommand.NewSPDYExecutor(c.config, "POST", execRequest.URL())
if err != nil {
return err
}
exec, err := remotecommand.NewFallbackExecutor(websocketExec, spdyExec, httpstream.IsUpgradeFailure)
if err != nil {
return err
}
errChan := make(chan error)
go func() {
errChan <- exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: options.Stdin,
Stdout: options.Stdout,
Stderr: options.Stderr,
})
}()
select {
case <-ctx.Done():
<-errChan
return nil
case err = <-errChan:
return err
}
}