/
pod_controller.go
287 lines (233 loc) · 8.8 KB
/
pod_controller.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2023 The Kanister 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 kube
import (
"context"
"io"
"time"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/log"
)
var (
ErrPodControllerNotInitialized = errors.New("pod has not been initialized")
ErrPodControllerPodAlreadyStarted = errors.New("pod has already been started")
ErrPodControllerPodNotReady = errors.New("pod is not yet ready")
ErrPodControllerPodNotStarted = errors.New("pod is not yet started")
PodControllerDefaultStopTime = 30 * time.Second
PodControllerInfiniteStopTime = 0 * time.Second
)
// PodController specifies interface needed for starting, stopping pod and operations with it
//
// The purpose of this interface is to provide single mechanism of pod manipulation, reduce number of parameters which
// caller needs to pass (since we keep pod related things internally) and eliminate human errors.
type PodController interface {
PodName() string
Pod() *corev1.Pod
StartPod(ctx context.Context) error
WaitForPodReady(ctx context.Context) error
WaitForPodCompletion(ctx context.Context) error
StopPod(ctx context.Context, timeout time.Duration, gracePeriodSeconds int64) error
StreamPodLogs(ctx context.Context) (io.ReadCloser, error)
GetCommandExecutor() (PodCommandExecutor, error)
GetFileWriter() (PodFileWriter, error)
}
// podController keeps Kubernetes Client and PodOptions needed for creating a Pod.
// It implements the PodControllerProcessor interface.
// All communication with kubernetes API are done via PodControllerProcessor interface, which could be overridden for testing purposes.
type podController struct {
cli kubernetes.Interface
podOptions *PodOptions
pod *corev1.Pod
podReady bool
podName string
pcp PodControllerProcessor
}
type PodControllerOption func(p *podController)
// WithPodControllerProcessor provides mechanism for passing fake implementation of PodControllerProcessor for testing purposes.
func WithPodControllerProcessor(processor PodControllerProcessor) PodControllerOption {
return func(p *podController) {
p.pcp = processor
}
}
// NewPodController returns a new PodController given Kubernetes Client and PodOptions
func NewPodController(cli kubernetes.Interface, options *PodOptions, opts ...PodControllerOption) PodController {
r := &podController{
cli: cli,
podOptions: options,
}
for _, opt := range opts {
opt(r)
}
// If pod controller processor has not been set by PodControllerOption, we create default implementation here
if r.pcp == nil {
r.pcp = &podControllerProcessor{
cli: cli,
}
}
return r
}
// NewPodControllerForExistingPod returns a new PodController for the given
// running pod.
// Invocation of StartPod of returned PodController instance will fail, since
// the pod is expected to be running already.
// Note:
// If the pod is not in the ready state, it will wait for up to
// KANISTER_POD_READY_WAIT_TIMEOUT (15 minutes by default) until the pod becomes ready.
func NewPodControllerForExistingPod(cli kubernetes.Interface, pod *corev1.Pod) (PodController, error) {
err := WaitForPodReady(context.Background(), cli, pod.Namespace, pod.Name)
if err != nil {
return nil, err
}
pc := &podController{
cli: cli,
pcp: &podControllerProcessor{
cli: cli,
},
pod: pod,
podName: pod.Name,
}
options := &PodOptions{
Name: pod.Name,
Namespace: pod.Namespace,
ContainerName: pod.Spec.Containers[0].Name,
}
pc.podOptions = options
pc.podReady = true
return pc, nil
}
func (p *podController) PodName() string {
return p.podName
}
func (p *podController) Pod() *corev1.Pod {
return p.pod
}
// StartPod creates pod and in case of success, it stores pod name for further use.
func (p *podController) StartPod(ctx context.Context) error {
if p.podName != "" {
return errors.Wrap(ErrPodControllerPodAlreadyStarted, "Failed to create pod")
}
if p.cli == nil || p.podOptions == nil {
return errors.Wrap(ErrPodControllerNotInitialized, "Failed to create pod")
}
pod, err := p.pcp.CreatePod(ctx, p.podOptions)
if err != nil {
log.WithError(err).Print("Failed to create pod", field.M{"PodName": p.podOptions.Name, "Namespace": p.podOptions.Namespace})
return errors.Wrap(err, "Failed to create pod")
}
p.pod = pod
p.podName = pod.Name
return nil
}
// WaitForPodReady waits for pod readiness (actually it waits until pod exit the pending state)
// Requires pod to be started otherwise will immediately fail.
func (p *podController) WaitForPodReady(ctx context.Context) error {
if p.podName == "" {
return ErrPodControllerPodNotStarted
}
if err := p.pcp.WaitForPodReady(ctx, p.pod.Namespace, p.pod.Name); err != nil {
log.WithError(err).Print("Pod failed to become ready in time", field.M{"PodName": p.podName, "Namespace": p.podOptions.Namespace})
return errors.Wrap(err, "Pod failed to become ready in time")
}
p.podReady = true
return nil
}
// WaitForPodCompletion waits for a pod to reach a terminal (either succeeded or failed) state.
func (p *podController) WaitForPodCompletion(ctx context.Context) error {
if p.podName == "" {
return ErrPodControllerPodNotStarted
}
if !p.podReady {
return ErrPodControllerPodNotReady
}
if err := p.pcp.WaitForPodCompletion(ctx, p.pod.Namespace, p.pod.Name); err != nil {
log.WithError(err).Print("Pod failed to complete in time", field.M{"PodName": p.podName, "Namespace": p.podOptions.Namespace})
return errors.Wrap(err, "Pod failed to complete in time")
}
p.podReady = false
return nil
}
// StopPod stops the pod which was previously started, otherwise it will return ErrPodControllerPodNotStarted error.
// stopTimeout is used to limit execution time
// gracePeriodSeconds is used to specify pod deletion grace period. If set to zero, pod should be deleted immediately
func (p *podController) StopPod(ctx context.Context, stopTimeout time.Duration, gracePeriodSeconds int64) error {
if p.podName == "" {
return ErrPodControllerPodNotStarted
}
if stopTimeout != PodControllerInfiniteStopTime {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, stopTimeout)
defer cancel()
}
if err := p.pcp.DeletePod(ctx, p.pod.Namespace, p.podName, metav1.DeleteOptions{GracePeriodSeconds: &gracePeriodSeconds}); err != nil {
log.WithError(err).Print("Failed to delete pod", field.M{"PodName": p.podName, "Namespace": p.pod.Namespace})
return err
}
p.podReady = false
p.podName = ""
p.pod = nil
return nil
}
func (p *podController) StreamPodLogs(ctx context.Context) (io.ReadCloser, error) {
if p.podName == "" {
return nil, ErrPodControllerPodNotStarted
}
return StreamPodLogs(ctx, p.cli, p.pod.Namespace, p.pod.Name, ContainerNameFromPodOptsOrDefault(p.podOptions))
}
// GetCommandExecutor returns PodCommandExecutor instance which is configured to execute commands within pod controlled
// by this controller.
// If pod is not created or not ready yet, it will fail with an appropriate error.
// Container will be decided based on the result of getContainerName function
func (p *podController) GetCommandExecutor() (PodCommandExecutor, error) {
if p.podName == "" {
return nil, ErrPodControllerPodNotStarted
}
if !p.podReady {
return nil, ErrPodControllerPodNotReady
}
pce := &podCommandExecutor{
cli: p.cli,
namespace: p.pod.Namespace,
podName: p.podName,
containerName: ContainerNameFromPodOptsOrDefault(p.podOptions),
}
pce.pcep = &podCommandExecutorProcessor{
cli: p.cli,
}
return pce, nil
}
// GetFileWriter returns PodFileWriter instance which is configured to write file to pod controlled by this controller.
// If pod is not created or not ready yet, it will fail with an appropriate error.
// Container will be decided based on the result of getContainerName function
func (p *podController) GetFileWriter() (PodFileWriter, error) {
if p.podName == "" {
return nil, ErrPodControllerPodNotStarted
}
if !p.podReady {
return nil, ErrPodControllerPodNotReady
}
pfw := &podFileWriter{
cli: p.cli,
namespace: p.podOptions.Namespace,
podName: p.podName,
containerName: ContainerNameFromPodOptsOrDefault(p.podOptions),
}
pfw.fileWriterProcessor = &podFileWriterProcessor{
cli: p.cli,
}
return pfw, nil
}