/
schedule.go
345 lines (283 loc) · 9.32 KB
/
schedule.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 pods
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/submariner-io/shipyard/test/e2e/framework"
"github.com/submariner-io/subctl/internal/cli"
"github.com/submariner-io/subctl/internal/constants"
"github.com/submariner-io/subctl/pkg/image"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/utils/ptr"
)
type schedulingType int
const (
InvalidScheduling schedulingType = iota
GatewayNode
NonGatewayNode
CustomNode
)
type networkingType bool
const (
HostNetworking networkingType = true
PodNetworking networkingType = false
)
type Scheduling struct {
ScheduleOn schedulingType
NodeName string
Networking networkingType
}
type Config struct {
Name string
ClientSet kubernetes.Interface
Scheduling Scheduling
Namespace string
Command string
Timeout uint
ImageRepositoryInfo image.RepositoryInfo
}
type Scheduled struct {
Pod *v1.Pod
Config *Config
PodOutput string
}
func ScheduleAndAwaitCompletion(config *Config) (string, error) {
if config.Scheduling.ScheduleOn == InvalidScheduling {
config.Scheduling.ScheduleOn = GatewayNode
}
if config.Namespace == "" {
config.Namespace = constants.OperatorNamespace
}
if err := checkNSLabels(config); err != nil {
return "", err
}
np := &Scheduled{Config: config}
if err := np.schedule(); err != nil {
return "", err
}
defer np.Delete()
if err := np.AwaitCompletion(); err != nil {
return "", err
}
return np.PodOutput, nil
}
func Schedule(config *Config) (*Scheduled, error) {
if config.Scheduling.ScheduleOn == InvalidScheduling {
config.Scheduling.ScheduleOn = GatewayNode
}
if config.Namespace == "" {
config.Namespace = constants.OperatorNamespace
}
if err := checkNSLabels(config); err != nil {
return nil, err
}
np := &Scheduled{Config: config}
if err := np.schedule(); err != nil {
return nil, err
}
return np, nil
}
func (np *Scheduled) schedule() error {
if np.Config.Scheduling.ScheduleOn == CustomNode && np.Config.Scheduling.NodeName == "" {
return fmt.Errorf("CustomNode is specified for scheduling, but nodeName is missing")
}
networkPod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: np.Config.Name,
Labels: map[string]string{
"app": np.Config.Name,
constants.TransientLabel: constants.TrueLabel,
},
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
HostNetwork: bool(np.Config.Scheduling.Networking),
Containers: []v1.Container{
{
Name: np.Config.Name,
Image: np.Config.ImageRepositoryInfo.GetNettestImage(),
Command: []string{"sh", "-c", "$(COMMAND) >/dev/termination-log 2>&1 || exit 0"},
Env: []v1.EnvVar{
{Name: "COMMAND", Value: np.Config.Command},
},
},
},
Tolerations: []v1.Toleration{{Operator: v1.TolerationOpExists}},
},
}
if np.Config.Scheduling.Networking == HostNetworking {
networkPod.Spec.Containers[0].SecurityContext = &v1.SecurityContext{
Capabilities: &v1.Capabilities{
Add: []v1.Capability{"NET_ADMIN", "NET_RAW"},
Drop: []v1.Capability{"all"},
},
// Some containers which run os like rhel/fedora runs tcpdump
// as specific user id "72". So it needs pods to be privileged
// Also setting the runAsUser prevent the pods from starting with
// random user id
Privileged: ptr.To(true),
RunAsUser: ptr.To(int64(0)),
}
}
if np.Config.Scheduling.ScheduleOn == CustomNode {
networkPod.Spec.NodeName = np.Config.Scheduling.NodeName
} else {
networkPod.Spec.Affinity = nodeAffinity(np.Config.Scheduling.ScheduleOn)
}
pc := np.Config.ClientSet.CoreV1().Pods(np.Config.Namespace)
var err error
np.Pod, err = pc.Create(context.TODO(), &networkPod, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "error creating Pod")
}
err = np.awaitUntilScheduled()
if err != nil {
np.Delete()
return err
}
return nil
}
func (np *Scheduled) Delete() {
pc := np.Config.ClientSet.CoreV1().Pods(np.Config.Namespace)
_ = pc.Delete(context.TODO(), np.Pod.Name, metav1.DeleteOptions{})
}
//nolint:wrapcheck // No need to wrap errors here.
func (np *Scheduled) awaitUntilScheduled() error {
pods := np.Config.ClientSet.CoreV1().Pods(np.Config.Namespace)
pod, errmsg, err := framework.AwaitResultOrError("await pod ready",
func() (interface{}, error) {
return pods.Get(context.TODO(), np.Pod.Name, metav1.GetOptions{})
}, func(result interface{}) (bool, string, error) {
pod := result.(*v1.Pod)
if pod.Status.Phase != v1.PodRunning && pod.Status.Phase != v1.PodSucceeded {
statusStr, _ := json.MarshalIndent(pod.Status, "", " ")
if pod.Status.Phase != v1.PodPending {
return false, "", fmt.Errorf("expected pod phase %v or %v. Actual pod status: %s",
v1.PodPending, v1.PodRunning, statusStr)
}
return false, fmt.Sprintf("Pod %q is still pending: Pod status: %s", pod.Name, statusStr), nil
}
return true, "", nil // pod is either running or has completed its execution
})
if wait.Interrupted(err) {
return errors.New(errmsg)
}
if err != nil {
return err
}
np.Pod = pod.(*v1.Pod)
return nil
}
//nolint:wrapcheck // No need to wrap errors here.
func (np *Scheduled) AwaitCompletion() error {
pods := np.Config.ClientSet.CoreV1().Pods(np.Config.Namespace)
_, errorMsg, err := framework.AwaitResultOrError(
fmt.Sprintf("await pod %q finished", np.Pod.Name), func() (interface{}, error) {
return pods.Get(context.TODO(), np.Pod.Name, metav1.GetOptions{})
}, func(result interface{}) (bool, string, error) {
np.Pod = result.(*v1.Pod)
switch np.Pod.Status.Phase { //nolint:exhaustive // 'missing cases in switch' - OK
case v1.PodSucceeded:
return true, "", nil
case v1.PodFailed:
return true, "", nil
default:
return false, fmt.Sprintf("Pod status is %v", np.Pod.Status.Phase), nil
}
})
if err != nil {
return errors.Wrapf(err, errorMsg)
}
finished := np.Pod.Status.Phase == v1.PodSucceeded || np.Pod.Status.Phase == v1.PodFailed
if finished {
np.PodOutput = np.Pod.Status.ContainerStatuses[0].State.Terminated.Message
}
return nil
}
func nodeAffinity(scheduling schedulingType) *v1.Affinity {
var nodeSelTerms []v1.NodeSelectorTerm
switch scheduling {
case GatewayNode:
nodeSelTerms = addNodeSelectorTerm(nodeSelTerms, framework.GatewayLabel,
v1.NodeSelectorOpIn, []string{"true"})
case NonGatewayNode:
nodeSelTerms = addNodeSelectorTerm(nodeSelTerms, framework.GatewayLabel,
v1.NodeSelectorOpDoesNotExist, nil)
nodeSelTerms = addNodeSelectorTerm(nodeSelTerms, framework.GatewayLabel,
v1.NodeSelectorOpNotIn, []string{"true"})
case InvalidScheduling:
case CustomNode:
}
return &v1.Affinity{
NodeAffinity: &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
NodeSelectorTerms: nodeSelTerms,
},
},
}
}
func addNodeSelectorTerm(nodeSelTerms []v1.NodeSelectorTerm, label string,
op v1.NodeSelectorOperator, values []string,
) []v1.NodeSelectorTerm {
return append(nodeSelTerms, v1.NodeSelectorTerm{MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: label,
Operator: op,
Values: values,
},
}})
}
func checkNSLabels(config *Config) error {
if config.Namespace == constants.OperatorNamespace {
// The default operator namespace has the proper pod security set up via OCP SCC so no need to check for a
// pod-security label. Also this avoids a warning with OCP 4.10.x which doesn't automatically set the pod-security
// label as later versions do.
return nil
}
ns, err := config.ClientSet.CoreV1().Namespaces().Get(context.TODO(), config.Namespace, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error fetching %s namespace", config.Namespace))
}
expectedLabels := map[string]string{
"pod-security.kubernetes.io/enforce": "privileged",
"pod-security.kubernetes.io/audit": "privileged",
"pod-security.kubernetes.io/warn": "privileged",
}
foundOne := false
for key, expectedLabel := range expectedLabels {
actualLabel, found := ns.Labels[key]
if found && actualLabel == expectedLabel {
foundOne = true
break
}
}
if foundOne {
return nil
}
labelStr := ""
for key, val := range expectedLabels {
labelStr += fmt.Sprintf(" %s=%s", key, val) + "\n"
}
status := cli.NewReporter()
status.Warning("Starting with Kubernetes 1.23, the Pod Security admission controller expects namespaces to have security labels."+
" Without these, you will see warnings in subctl's output. subctl should work fine, but you can avoid the warnings and ensure "+
"correct behavior by adding at least one of these labels to the namespace %q:\n%s", config.Namespace, labelStr)
return nil
}