-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconnector.go
194 lines (178 loc) · 4.75 KB
/
connector.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package kubernetes
import (
"context"
"fmt"
"io"
log "go.arcalot.io/log/v2"
"go.flow.arcalot.io/deployer"
core "k8s.io/api/core/v1"
kubeErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/remotecommand"
watchTools "k8s.io/client-go/tools/watch"
)
type connector struct {
cli *kubernetes.Clientset
restClient *restclient.RESTClient
config *Config
connectionConfig restclient.Config
logger log.Logger
}
//nolint:funlen
func (c connector) Deploy(ctx context.Context, image string) (deployer.Plugin, error) {
podSpec := c.config.Pod.Spec.PodSpec
pluginContainer := c.config.Pod.Spec.PluginContainer
pluginContainer.Stdin = true
pluginContainer.Image = image
pluginContainer.Env = append(pluginContainer.Env, core.EnvVar{
Name: "PYTHON_UNBUFFERED",
Value: "1",
})
pluginContainer.Args = []string{"--atp"}
podSpec.Containers = append(
podSpec.Containers,
pluginContainer,
)
podSpec.RestartPolicy = core.RestartPolicyNever
meta := c.config.Pod.Metadata
if meta.Name == "" && meta.GenerateName == "" {
meta.GenerateName = "arcaflow-plugin-"
}
if c.config.Connection.Insecure {
c.logger.Warningf("Deploying without TLS verification, do it at your own risk.")
}
c.logger.Infof("Deploying pod from image %s...", image)
pod, err := c.cli.CoreV1().Pods(c.config.Pod.Metadata.Namespace).Create(
ctx,
&core.Pod{
ObjectMeta: meta,
Spec: podSpec,
},
metav1.CreateOptions{},
)
if err != nil {
return nil, fmt.Errorf("failed to create pod (%w)", err)
}
c.logger.Infof("Waiting for pod %s...", pod.Name)
pod, err = c.waitForPod(ctx, pod)
if err != nil {
_ = c.removePod(ctx, pod, true)
return nil, err
}
c.logger.Infof("Attaching to pod...")
req := c.restClient.Post().
Namespace(c.config.Pod.Metadata.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("attach")
req.VersionedParams(
&core.PodAttachOptions{
Container: pod.Spec.Containers[len(podSpec.Containers)-1].Name,
Stdin: true,
Stdout: true,
Stderr: true,
}, scheme.ParameterCodec,
)
podExec, err := remotecommand.NewSPDYExecutor(
&c.connectionConfig,
"POST",
req.URL(),
)
if err != nil {
_ = c.removePod(ctx, pod, true)
return nil, err
}
stdinReader, stdinWriter := io.Pipe()
stdoutReader, stdoutWriter := io.Pipe()
go func() {
defer func() {
_ = stdoutWriter.Close()
_ = stdinWriter.Close()
}()
_ = podExec.StreamWithContext(
ctx,
remotecommand.StreamOptions{
Stdin: stdinReader,
Stdout: stdoutWriter,
Stderr: stdoutWriter,
},
)
}()
c.logger.Infof("Pod start complete.")
return &connectorContainer{
pod: pod,
connector: c,
stdinWriter: stdinWriter,
stdoutReader: stdoutReader,
}, nil
}
func (c connector) waitForPod(ctx context.Context, pod *core.Pod) (*core.Pod, error) {
fieldSelector := fields.
OneTermEqualSelector("metadata.name", pod.Name).
String()
listWatch := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.FieldSelector = fieldSelector
return c.cli.
CoreV1().
Pods(c.config.Pod.Metadata.Namespace).
List(ctx, options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.FieldSelector = fieldSelector
return c.cli.
CoreV1().
Pods(c.config.Pod.Metadata.Namespace).
Watch(ctx, options)
},
}
event, err := watchTools.UntilWithSync(
ctx,
listWatch,
&core.Pod{},
nil,
c.isPodAvailableEvent,
)
if event != nil {
pod = event.Object.(*core.Pod)
}
return pod, err
}
func (c connector) isPodAvailableEvent(event watch.Event) (bool, error) {
if event.Type == watch.Deleted {
return false, kubeErrors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
}
if eventObject, ok := event.Object.(*core.Pod); ok {
switch eventObject.Status.Phase {
case core.PodFailed, core.PodSucceeded:
return true, nil
case core.PodRunning:
conditions := eventObject.Status.Conditions
for _, condition := range conditions {
if condition.Type == core.PodReady &&
condition.Status == core.ConditionTrue {
return true, nil
}
}
}
}
return false, nil
}
func (c connector) removePod(ctx context.Context, pod *core.Pod, force bool) error {
var gracePeriod *int64
if force {
t := int64(0)
gracePeriod = &t
}
return c.cli.CoreV1().Pods(c.config.Pod.Metadata.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{
GracePeriodSeconds: gracePeriod,
})
}