Skip to content

Commit 08b4d40

Browse files
Simon Emmsroboquat
authored andcommitted
[installer]: allow customization of envvars
1 parent a9b6cf2 commit 08b4d40

File tree

3 files changed

+177
-1
lines changed

3 files changed

+177
-1
lines changed

install/installer/pkg/common/customize.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,31 @@ func CustomizeAnnotation(ctx *RenderContext, component string, typeMeta metav1.T
7171
}
7272

7373
func CustomizeEnvvar(ctx *RenderContext, component string, existingEnvvars []corev1.EnvVar) []corev1.EnvVar {
74-
return existingEnvvars
74+
// Use a map so we can remove duplicated keys
75+
envvars := make(map[string]corev1.EnvVar, 0)
76+
77+
// Apply the customizations - envvars only need to match name
78+
if ctx.Config.Customization != nil {
79+
for _, customization := range *ctx.Config.Customization {
80+
if customization.Metadata.Name == component || customization.Metadata.Name == "*" {
81+
for _, e := range customization.Spec.Env {
82+
envvars[e.Name] = e
83+
}
84+
}
85+
}
86+
}
87+
88+
// Always apply existing envvars
89+
for _, e := range existingEnvvars {
90+
envvars[e.Name] = e
91+
}
92+
93+
// Convert map back slice
94+
output := make([]corev1.EnvVar, 0, len(envvars))
95+
for _, e := range envvars {
96+
output = append(output, e)
97+
}
98+
return output
7599
}
76100

77101
func CustomizeLabel(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingLabels ...func() map[string]string) map[string]string {

install/installer/pkg/common/customize_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gitpod-io/gitpod/installer/pkg/config/v1"
1212
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
1313
"github.com/stretchr/testify/require"
14+
corev1 "k8s.io/api/core/v1"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
)
1617

@@ -379,3 +380,149 @@ func TestCustomizeLabel(t *testing.T) {
379380
})
380381
}
381382
}
383+
384+
func TestCustomizeEnvvar(t *testing.T) {
385+
testCases := []struct {
386+
Name string
387+
Customization []config.Customization
388+
Component string
389+
ExistingEnnvars []corev1.EnvVar
390+
Expect []corev1.EnvVar
391+
}{
392+
393+
{
394+
Name: "no customization",
395+
Customization: nil,
396+
Component: "component",
397+
Expect: []corev1.EnvVar{},
398+
},
399+
{
400+
Customization: []config.Customization{},
401+
Name: "empty customization",
402+
Component: "component",
403+
Expect: []corev1.EnvVar{},
404+
},
405+
{
406+
Customization: []config.Customization{
407+
{
408+
TypeMeta: common.TypeMetaDeployment,
409+
Metadata: metav1.ObjectMeta{
410+
Name: "component2",
411+
},
412+
Spec: config.CustomizationSpec{
413+
Env: []corev1.EnvVar{
414+
{
415+
Name: "key1",
416+
Value: "value1",
417+
},
418+
},
419+
},
420+
},
421+
},
422+
Name: "ignore different name envvars",
423+
Component: "component",
424+
Expect: []corev1.EnvVar{},
425+
},
426+
{
427+
Customization: []config.Customization{
428+
{
429+
TypeMeta: common.TypeMetaDeployment,
430+
Metadata: metav1.ObjectMeta{
431+
Name: "component",
432+
},
433+
Spec: config.CustomizationSpec{
434+
Env: []corev1.EnvVar{
435+
{
436+
Name: "key1",
437+
Value: "value1",
438+
},
439+
},
440+
},
441+
},
442+
},
443+
Name: "add in name envvars",
444+
Component: "component",
445+
Expect: []corev1.EnvVar{
446+
{
447+
Name: "key1",
448+
Value: "value1",
449+
},
450+
},
451+
},
452+
{
453+
Customization: []config.Customization{
454+
{
455+
Metadata: metav1.ObjectMeta{
456+
Name: "*",
457+
},
458+
Spec: config.CustomizationSpec{
459+
Env: []corev1.EnvVar{
460+
{
461+
Name: "key1",
462+
Value: "original",
463+
},
464+
{
465+
Name: "key2",
466+
Value: "original",
467+
},
468+
{
469+
Name: "key3",
470+
Value: "override",
471+
},
472+
},
473+
},
474+
},
475+
{
476+
Metadata: metav1.ObjectMeta{
477+
Name: "component",
478+
},
479+
Spec: config.CustomizationSpec{
480+
Env: []corev1.EnvVar{
481+
{
482+
Name: "key1",
483+
Value: "override",
484+
},
485+
},
486+
},
487+
},
488+
},
489+
ExistingEnnvars: []corev1.EnvVar{
490+
{
491+
Name: "key3",
492+
Value: "original",
493+
},
494+
},
495+
Name: "full-house envvars",
496+
Component: "component",
497+
Expect: []corev1.EnvVar{
498+
{
499+
Name: "key1",
500+
Value: "override",
501+
},
502+
{
503+
Name: "key2",
504+
Value: "original",
505+
},
506+
{
507+
Name: "key3",
508+
Value: "original",
509+
},
510+
},
511+
},
512+
}
513+
514+
for _, testCase := range testCases {
515+
t.Run(testCase.Name, func(t *testing.T) {
516+
ctx, err := common.NewRenderContext(config.Config{
517+
Customization: &testCase.Customization,
518+
}, versions.Manifest{}, "test_namespace")
519+
require.NoError(t, err)
520+
521+
result := common.CustomizeEnvvar(ctx, testCase.Component, testCase.ExistingEnnvars)
522+
523+
if !reflect.DeepEqual(testCase.Expect, result) {
524+
t.Errorf("expected %v but got %v", testCase.Expect, result)
525+
}
526+
})
527+
}
528+
}

install/installer/pkg/config/v1/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,9 @@ type OAuth struct {
334334
type Customization struct {
335335
metav1.TypeMeta `json:",inline"`
336336
Metadata metav1.ObjectMeta `json:"metadata"`
337+
Spec CustomizationSpec `json:"spec,omitempty"`
338+
}
339+
340+
type CustomizationSpec struct {
341+
Env []corev1.EnvVar `json:"env"`
337342
}

0 commit comments

Comments
 (0)