-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvolumes.go
207 lines (179 loc) · 5.37 KB
/
volumes.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
/*
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"
api "github.com/flux-framework/flux-operator/api/v1alpha2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// Shared function to return consistent set of volume mounts
func getVolumeMounts(cluster *api.MiniCluster) []corev1.VolumeMount {
mounts := []corev1.VolumeMount{
// The empty volume for Flux will go here
{
Name: cluster.Spec.Flux.Container.Name,
MountPath: cluster.Spec.Flux.Container.MountPath,
ReadOnly: false,
},
// Entrypoints will go here
{
Name: cluster.EntrypointConfigMapName(),
MountPath: "/flux_operator/",
ReadOnly: true,
},
}
return mounts
}
// getVolumes that are shared between MiniCluster and statefulset
func getVolumes(cluster *api.MiniCluster) []corev1.Volume {
// Runner start scripts
makeExecutable := int32(0777)
runnerStartScripts := []corev1.KeyToPath{}
// Prepare a custom "wait.sh" for each container based on index
for i, container := range cluster.Spec.Containers {
// For now, only Flux runners get the custom wait.sh script
if container.RunFlux {
startScript := corev1.KeyToPath{
Key: fmt.Sprintf("wait-%d", i),
Path: fmt.Sprintf("wait-%d.sh", i),
Mode: &makeExecutable,
}
runnerStartScripts = append(runnerStartScripts, startScript)
}
// A non flux container can also handle custom logic, if command is provided
if container.GenerateEntrypoint() {
startScript := corev1.KeyToPath{
Key: fmt.Sprintf("start-%d", i),
Path: fmt.Sprintf("start-%d.sh", i),
Mode: &makeExecutable,
}
runnerStartScripts = append(runnerStartScripts, startScript)
}
}
// /flux_operator/curve.cert
curveKey := corev1.KeyToPath{
Key: CurveCertKey,
Path: "curve.cert",
}
// Add the flux init script
fluxScript := corev1.KeyToPath{
Key: cluster.Spec.Flux.Container.Name,
Path: "flux-init.sh",
Mode: &makeExecutable,
}
runnerStartScripts = append(runnerStartScripts, fluxScript)
runnerStartScripts = append(runnerStartScripts, curveKey)
// Defaults volumes we always write - entrypoint and empty volume
volumes := []corev1.Volume{
{
Name: cluster.Spec.Flux.Container.Name,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: cluster.EntrypointConfigMapName(),
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
// Namespace based on the cluster
LocalObjectReference: corev1.LocalObjectReference{
Name: cluster.EntrypointConfigMapName(),
},
// /flux_operator/wait-<index>.sh
Items: runnerStartScripts,
},
},
},
}
// Add volumes that already exist (not created by the Flux Operator)
// These are unique names and path/claim names across containers
// This can be a claim, secret, or config map
existingVolumes := getExistingVolumes(cluster.ExistingContainerVolumes())
volumes = append(volumes, existingVolumes...)
return volumes
}
// Get Existing volumes for the MiniCluster
func getExistingVolumes(existing map[string]api.ContainerVolume) []corev1.Volume {
volumes := []corev1.Volume{}
for volumeName, volumeMeta := range existing {
var newVolume corev1.Volume
if volumeMeta.SecretName != "" {
newVolume = corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: volumeMeta.SecretName,
},
},
}
} else if volumeMeta.EmptyDir {
// The Flux Operator supports default and memory
medium := corev1.StorageMediumDefault
if volumeMeta.EmptyDirMedium == "memory" {
medium = corev1.StorageMediumMemory
}
newVolume = corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: medium,
},
},
}
if volumeMeta.EmptyDirSizeLimit != "" {
sizeLimit := resource.MustParse(volumeMeta.EmptyDirSizeLimit)
newVolume.VolumeSource.EmptyDir.SizeLimit = &sizeLimit
}
} else if volumeMeta.HostPath != "" {
newVolume = corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
// Empty path for type means no checks are done
HostPath: &corev1.HostPathVolumeSource{
Path: volumeMeta.Path,
},
},
}
} else if volumeMeta.ConfigMapName != "" {
// Prepare items as key to path
items := []corev1.KeyToPath{}
for key, path := range volumeMeta.Items {
newItem := corev1.KeyToPath{
Key: key,
Path: path,
}
items = append(items, newItem)
}
// This is a config map volume with items
newVolume = corev1.Volume{
Name: volumeMeta.ConfigMapName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: volumeMeta.ConfigMapName,
},
Items: items,
},
},
}
} else {
// Fall back to persistent volume claim
newVolume = corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: volumeMeta.ClaimName,
},
},
}
}
volumes = append(volumes, newVolume)
}
return volumes
}