-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcontainers.go
More file actions
190 lines (158 loc) · 5.11 KB
/
Copy pathcontainers.go
File metadata and controls
190 lines (158 loc) · 5.11 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
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
/*
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
}
// test minicluster with init security context added PLUS mount at /tmp somewhere
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: cluster.Spec.Flux.Container.Resources,
SecurityContext: &cluster.Spec.Flux.Container.SecurityContext,
}
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{}
// Assume we can't mount the same name twice
seenMounts := map[string]bool{}
// 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.ImagePullPolicy != "" {
pullPolicy = corev1.PullPolicy(container.ImagePullPolicy)
}
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 {
var mount corev1.VolumeMount
if volume.EmptyDir {
// We assume bindings to /dev/shm
mount = corev1.VolumeMount{
Name: volumeName,
MountPath: "/dev/shm",
}
} else {
mount = corev1.VolumeMount{
Name: volumeName,
MountPath: volume.Path,
ReadOnly: volume.ReadOnly,
}
}
_, ok := seenMounts[volumeName]
if !ok {
mounts = append(mounts, mount)
seenMounts[volumeName] = true
}
}
// Create the container
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: container.Resources,
SecurityContext: &container.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
}