Skip to content

Commit

Permalink
Fix pod-overrides for container level (#162)
Browse files Browse the repository at this point in the history
Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi authored Feb 14, 2023
1 parent c9908fc commit 4f0ff68
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func getPodTemplateSpec(globalAttributes attributes.Attributes, components []v1.
Volumes: podTemplateSpecParams.Volumes,
},
}
if len(globalAttributes) != 0 && needsPodOverrides(globalAttributes, components) {
if needsPodOverrides(globalAttributes, components) {
patchedPodTemplateSpec, err := applyPodOverrides(globalAttributes, components, podTemplateSpec)
if err != nil {
return nil, err
Expand Down
52 changes: 50 additions & 2 deletions pkg/devfile/generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,17 @@ func TestGetPodTemplateSpec(t *testing.T) {
}

tests := []struct {
title string
podName string
namespace string
serviceAccount string
schedulerName string
labels map[string]string
attributes attributes.Attributes
components []v1.Component
}{
{
title: "normal pod spec",
podName: "podSpecTest",
namespace: "default",
labels: map[string]string{
Expand All @@ -653,6 +657,7 @@ func TestGetPodTemplateSpec(t *testing.T) {
},
},
{
title: "podSpec with pod-overrides attribute at devfile level",
podName: "podSpecTest",
namespace: "default",
serviceAccount: "new-service-account",
Expand All @@ -664,10 +669,50 @@ func TestGetPodTemplateSpec(t *testing.T) {
PodOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": {\"serviceAccountName\": \"new-service-account\"}}")},
},
},
{
title: "podSpec with pod-overrides attribute at container level",
podName: "podSpecTest",
namespace: "default",
serviceAccount: "new-service-account",
labels: map[string]string{
"app": "app",
"component": "frontend",
},
components: []v1.Component{
{
Name: "tools",
Attributes: attributes.Attributes{
PodOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": {\"serviceAccountName\": \"new-service-account\"}}")},
},
},
},
},
{
title: "podSpec with pod-overrides attribute at devfile and container level",
podName: "podSpecTest",
namespace: "default",
serviceAccount: "new-service-account",
schedulerName: "new-scheduler",
labels: map[string]string{
"app": "app",
"component": "frontend",
},
components: []v1.Component{
{
Name: "tools",
Attributes: attributes.Attributes{
PodOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": {\"serviceAccountName\": \"new-service-account\"}}")},
},
},
},
attributes: attributes.Attributes{
PodOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": {\"schedulerName\": \"new-scheduler\"}}")},
},
},
}

for _, tt := range tests {
t.Run(tt.podName, func(t *testing.T) {
t.Run(tt.title, func(t *testing.T) {

objectMeta := GetObjectMeta(tt.podName, tt.namespace, tt.labels, nil)
podTemplateSpecParams := podTemplateSpecParams{
Expand All @@ -677,7 +722,7 @@ func TestGetPodTemplateSpec(t *testing.T) {
InitContainers: container,
}

podTemplateSpec, err := getPodTemplateSpec(tt.attributes, nil, podTemplateSpecParams)
podTemplateSpec, err := getPodTemplateSpec(tt.attributes, tt.components, podTemplateSpecParams)
if err != nil {
t.Errorf("TestGetPodTemplateSpec() error: %s", err.Error())
}
Expand All @@ -691,6 +736,9 @@ func TestGetPodTemplateSpec(t *testing.T) {
if tt.serviceAccount != "" && podTemplateSpec.Spec.ServiceAccountName != tt.serviceAccount {
t.Errorf("TestGetPodTemplateSpec() error: expected serviceAccountName %s, actual %s", tt.serviceAccount, podTemplateSpec.Spec.ServiceAccountName)
}
if tt.schedulerName != "" && podTemplateSpec.Spec.SchedulerName != tt.schedulerName {
t.Errorf("TestGetPodTemplateSpec() error: expected schedulerName %s, actual %s", tt.schedulerName, podTemplateSpec.Spec.SchedulerName)
}
if !hasVolumeWithName("vol1", podTemplateSpec.Spec.Volumes) {
t.Errorf("TestGetPodTemplateSpec() error: volume with name: %s not found", "vol1")
}
Expand Down

0 comments on commit 4f0ff68

Please sign in to comment.