/
containers.go
191 lines (160 loc) · 5.3 KB
/
containers.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
/*
Copyright 2022-2023 Lawrence Livermore National Security, LLC
(c.f. AUTHORS, NOTICE.LLNS, COPYING)
This is part of the Flux resource manager framework.
For details, see https://github.com/flux-framework.
SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"fmt"
corev1 "k8s.io/api/core/v1"
api "github.com/flux-framework/flux-operator/api/v1alpha2"
)
// getFluxContainers prepares the flux container to run the show!
func getFluxContainer(
cluster *api.MiniCluster,
mounts []corev1.VolumeMount) (corev1.Container, error) {
// Allow dictating pulling on the level of the container
pullPolicy := corev1.PullIfNotPresent
if cluster.Spec.Flux.Container.PullAlways {
pullPolicy = corev1.PullAlways
}
resources, err := getFluxContainerResources(cluster.Spec.Flux.Container)
if err != nil {
return corev1.Container{}, err
}
initContainer := corev1.Container{
// Call this the driver container, number 0
Name: cluster.Spec.Flux.Container.Name,
Image: cluster.Spec.Flux.Container.Image,
Command: []string{"/bin/bash", "/flux_operator/flux-init.sh"},
ImagePullPolicy: pullPolicy,
WorkingDir: cluster.Spec.Flux.Container.WorkingDir,
VolumeMounts: mounts,
Stdin: true,
TTY: true,
Resources: resources,
}
return initContainer, nil
}
// getContainers gets containers for a MiniCluster job or external service
func getContainers(
specs []api.MiniClusterContainer,
defaultName string,
mounts []corev1.VolumeMount,
serviceContainer bool,
) ([]corev1.Container, error) {
// Create the containers for the pod
containers := []corev1.Container{}
// Add on application and flux runner containers
for i, container := range specs {
// Allow dictating pulling on the level of the container
pullPolicy := corev1.PullIfNotPresent
if container.PullAlways {
pullPolicy = corev1.PullAlways
}
// Fluxrunner will use the namespace name
containerName := container.Name
command := []string{}
// A Flux runner will have a wait.sh script that waits for the flux view
// to copy over, and then wraps the original command in a submit
// It inherits the cluster name
if container.RunFlux {
// wait.sh path corresponds to container identifier
waitScript := fmt.Sprintf("/flux_operator/wait-%d.sh", i)
command = []string{"/bin/bash", waitScript}
containerName = defaultName
}
// A container not running flux can only have pre/post sections
// in a custom script if we know the entrypoint.
if container.GenerateEntrypoint() && !serviceContainer {
startScript := fmt.Sprintf("/flux_operator/start-%d.sh", i)
command = []string{"/bin/bash", startScript}
}
// Prepare lifescycle commands for the container
lifecycle := createContainerLifecycle(container)
// Add on existing volumes/claims
for volumeName, volume := range container.Volumes {
mount := corev1.VolumeMount{
Name: volumeName,
MountPath: volume.Path,
ReadOnly: volume.ReadOnly,
}
mounts = append(mounts, mount)
}
// Prepare container resources
// Note that for the QoS to be guaranteed (to assign more than one per node)
// We need specific requests for memory and CPU, and not just on the pod, but the init container too.
// There is a function in API that will return True if we need resources for init,
// and then a static amount is defined
resources, err := getContainerResources(&container)
if err != nil {
return containers, err
}
addCaps := []corev1.Capability{}
for _, cap := range container.SecurityContext.AddCapabilities {
addCaps = append(addCaps, corev1.Capability(cap))
}
securityContext := corev1.SecurityContext{
Privileged: &container.SecurityContext.Privileged,
Capabilities: &corev1.Capabilities{
Add: addCaps,
},
}
newContainer := corev1.Container{
// Call this the driver container, number 0
Name: containerName,
Image: container.Image,
ImagePullPolicy: pullPolicy,
WorkingDir: container.WorkingDir,
VolumeMounts: mounts,
Stdin: true,
TTY: true,
Lifecycle: lifecycle,
Resources: resources,
SecurityContext: &securityContext,
}
// Only add command if we actually have one
if len(command) > 0 {
newContainer.Command = command
}
ports := []corev1.ContainerPort{}
envars := []corev1.EnvVar{}
// For now we will take ports and have container port == exposed port
for _, port := range container.Ports {
newPort := corev1.ContainerPort{
ContainerPort: int32(port),
Protocol: "TCP",
}
ports = append(ports, newPort)
}
// Add environment variables
for key, value := range container.Environment {
newEnvar := corev1.EnvVar{
Name: key,
Value: value,
}
envars = append(envars, newEnvar)
}
// Add environment variables that come as secrets
for envarName, envar := range container.Secrets {
newEnvar := corev1.EnvVar{
Name: envarName,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: envar.Name,
},
Key: envar.Key,
},
},
}
envars = append(envars, newEnvar)
}
newContainer.Ports = ports
newContainer.Env = envars
containers = append(containers, newContainer)
}
return containers, nil
}