Skip to content

Commit e342737

Browse files
committed
Move container and pod overrides from GetAllContainers/getPodTemplateSpec to GetPodTemplateSpec
Signed-off-by: Philippe Martin <[email protected]>
1 parent 63502cd commit e342737

File tree

3 files changed

+68
-55
lines changed

3 files changed

+68
-55
lines changed

pkg/devfile/generator/generators.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func GetPodTemplateSpec(devfileObj parser.DevfileObj, podTemplateParams PodTempl
238238
if err != nil {
239239
return nil, err
240240
}
241+
241242
podTemplateSpecParams := podTemplateSpecParams{
242243
ObjectMeta: podTemplateParams.ObjectMeta,
243244
InitContainers: initContainers,
@@ -254,7 +255,71 @@ func GetPodTemplateSpec(devfileObj parser.DevfileObj, podTemplateParams PodTempl
254255
if err != nil {
255256
return nil, err
256257
}
257-
return getPodTemplateSpec(globalAttributes, components, podTemplateSpecParams)
258+
259+
podTemplateSpec, err := getPodTemplateSpec(podTemplateSpecParams)
260+
if err != nil {
261+
return nil, err
262+
}
263+
264+
// TODO: apply here patches for Pod Security Admission
265+
266+
if needsPodOverrides(globalAttributes, components) {
267+
patchedPodTemplateSpec, err := applyPodOverrides(globalAttributes, components, podTemplateSpec)
268+
if err != nil {
269+
return nil, err
270+
}
271+
patchedPodTemplateSpec.ObjectMeta = podTemplateSpecParams.ObjectMeta
272+
podTemplateSpec = patchedPodTemplateSpec
273+
}
274+
275+
podTemplateSpec.Spec.Containers, err = applyContainerOverrides(devfileObj, podTemplateSpec.Spec.Containers)
276+
if err != nil {
277+
return nil, err
278+
}
279+
podTemplateSpec.Spec.InitContainers, err = applyContainerOverrides(devfileObj, podTemplateSpec.Spec.InitContainers)
280+
if err != nil {
281+
return nil, err
282+
}
283+
284+
return podTemplateSpec, nil
285+
}
286+
287+
func applyContainerOverrides(devfileObj parser.DevfileObj, containers []corev1.Container) ([]corev1.Container, error) {
288+
containerComponents, err := devfileObj.Data.GetComponents(common.DevfileOptions{
289+
ComponentOptions: common.ComponentOptions{
290+
ComponentType: v1.ContainerComponentType,
291+
},
292+
})
293+
if err != nil {
294+
return nil, err
295+
}
296+
297+
getContainerByName := func(name string) (*corev1.Container, bool) {
298+
for _, container := range containers {
299+
if container.Name == name {
300+
return &container, true
301+
}
302+
}
303+
return nil, false
304+
}
305+
306+
result := make([]corev1.Container, 0, len(containers))
307+
for _, comp := range containerComponents {
308+
container, found := getContainerByName(comp.Name)
309+
if !found {
310+
continue
311+
}
312+
if comp.Attributes.Exists(ContainerOverridesAttribute) {
313+
patched, err := containerOverridesHandler(comp, container)
314+
if err != nil {
315+
return nil, err
316+
}
317+
result = append(result, *patched)
318+
} else {
319+
result = append(result, *container)
320+
}
321+
}
322+
return result, nil
258323
}
259324

260325
// PVCParams is a struct to create PVC

pkg/devfile/generator/generators_test.go

-35
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ func TestGetContainers(t *testing.T) {
4949

5050
containerNames := []string{"testcontainer1", "testcontainer2", "testcontainer3"}
5151
containerImages := []string{"image1", "image2", "image3"}
52-
defaultPullPolicy := corev1.PullAlways
53-
defaultEnv := []corev1.EnvVar{
54-
{Name: "PROJECTS_ROOT", Value: "/projects"},
55-
{Name: "PROJECT_SOURCE", Value: "/projects/test-project"},
56-
}
5752

5853
trueMountSources := true
5954
falseMountSources := false
@@ -305,36 +300,6 @@ func TestGetContainers(t *testing.T) {
305300
name: "Simulating error case, check if error matches",
306301
wantErr: &errMatches,
307302
},
308-
{
309-
name: "container with container-overrides",
310-
containerComponents: []v1.Component{
311-
{
312-
Name: containerNames[0],
313-
ComponentUnion: v1.ComponentUnion{
314-
Container: &v1.ContainerComponent{
315-
Container: v1.Container{
316-
Image: containerImages[0],
317-
},
318-
},
319-
},
320-
Attributes: attributes.Attributes{}.FromMap(map[string]interface{}{
321-
"container-overrides": map[string]interface{}{"securityContext": map[string]int64{"runAsGroup": 3000}},
322-
}, nil),
323-
},
324-
},
325-
wantContainerName: containerNames[0],
326-
wantContainerImage: containerImages[0],
327-
wantContainerEnv: defaultEnv,
328-
wantContainerOverrideData: &corev1.Container{
329-
Name: containerNames[0],
330-
Image: containerImages[0],
331-
Env: defaultEnv,
332-
ImagePullPolicy: defaultPullPolicy,
333-
SecurityContext: &corev1.SecurityContext{
334-
RunAsGroup: pointer.Int64(3000),
335-
},
336-
},
337-
},
338303
}
339304
for _, tt := range tests {
340305
t.Run(tt.name, func(t *testing.T) {

pkg/devfile/generator/utils.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ type podTemplateSpecParams struct {
241241
}
242242

243243
// getPodTemplateSpec gets a pod template spec that can be used to create a deployment spec
244-
func getPodTemplateSpec(globalAttributes attributes.Attributes, components []v1.Component, podTemplateSpecParams podTemplateSpecParams) (*corev1.PodTemplateSpec, error) {
244+
func getPodTemplateSpec(podTemplateSpecParams podTemplateSpecParams) (*corev1.PodTemplateSpec, error) {
245245
podTemplateSpec := &corev1.PodTemplateSpec{
246246
ObjectMeta: podTemplateSpecParams.ObjectMeta,
247247
Spec: corev1.PodSpec{
@@ -250,14 +250,6 @@ func getPodTemplateSpec(globalAttributes attributes.Attributes, components []v1.
250250
Volumes: podTemplateSpecParams.Volumes,
251251
},
252252
}
253-
if needsPodOverrides(globalAttributes, components) {
254-
patchedPodTemplateSpec, err := applyPodOverrides(globalAttributes, components, podTemplateSpec)
255-
if err != nil {
256-
return nil, err
257-
}
258-
patchedPodTemplateSpec.ObjectMeta = podTemplateSpecParams.ObjectMeta
259-
podTemplateSpec = patchedPodTemplateSpec
260-
}
261253

262254
return podTemplateSpec, nil
263255
}
@@ -736,16 +728,7 @@ func getAllContainers(devfileObj parser.DevfileObj, options common.DevfileOption
736728
return nil, err
737729
}
738730
}
739-
// Check if there is an override attribute
740-
if comp.Attributes.Exists(ContainerOverridesAttribute) {
741-
patched, err := containerOverridesHandler(comp, container)
742-
if err != nil {
743-
return nil, err
744-
}
745-
containers = append(containers, *patched)
746-
} else {
747-
containers = append(containers, *container)
748-
}
731+
containers = append(containers, *container)
749732
}
750733
return containers, nil
751734
}

0 commit comments

Comments
 (0)