Skip to content

Commit 36a90fb

Browse files
fix: nil pointer when one of the addons is not defined on the stage or env level
1 parent b2bed9c commit 36a90fb

File tree

2 files changed

+228
-2
lines changed

2 files changed

+228
-2
lines changed

internal/project/cluster.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,26 @@ func (c *Cluster) SetDefaultAddons(config *ProjectConfig) {
136136
}
137137

138138
// AddonProperties returns the addon properties for the cluster merged with the environment and stage properties
139-
func (c *Cluster) AddonProperties(config *ProjectConfig, env, stage string) map[string]*ClusterAddon {
139+
func (c *Cluster) AddonProperties(config *ProjectConfig, env, stg string) map[string]*ClusterAddon {
140140
properties := c.Addons
141141
for addonName, addon := range c.Addons {
142142
if !addon.Enabled {
143143
// addon was disabled on the cluster level, we skip it
144144
continue
145145
}
146+
envAddonProps := map[string]any{}
147+
if env := config.GetEnvironment(env).GetAddon(addonName); env != nil {
148+
envAddonProps = env.Properties
149+
}
150+
stageAddonProps := map[string]any{}
151+
if stg := config.GetStage(env, stg).GetAddon(addonName); stg != nil {
152+
stageAddonProps = stg.Properties
153+
}
146154
addonProps := map[string]any{}
147155
for key, property := range config.ParsedAddons[addonName].Properties {
148156
addonProps[key] = property.Default
149157
}
150-
properties[addonName].Properties = utils.MergeMaps(addonProps, config.GetStage(env, stage).GetAddon(addonName).Properties, config.GetEnvironment(env).GetAddon(addonName).Properties, c.GetAddon(addonName).Properties)
158+
properties[addonName].Properties = utils.MergeMaps(addonProps, envAddonProps, stageAddonProps, c.GetAddon(addonName).Properties)
151159
}
152160
return properties
153161
}

internal/project/cluster_test.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,3 +806,221 @@ func TestCluster_SetDefaultAddons(t *testing.T) {
806806
})
807807
}
808808
}
809+
810+
func TestCluster_AddonProperties(t *testing.T) {
811+
type fields struct {
812+
Name string
813+
Addons map[string]*ClusterAddon
814+
Properties map[string]string
815+
}
816+
type args struct {
817+
config *ProjectConfig
818+
env string
819+
stg string
820+
}
821+
tests := []struct {
822+
name string
823+
fields fields
824+
args args
825+
want map[string]*ClusterAddon
826+
}{
827+
{
828+
name: "one addon, cluster has highest priority",
829+
fields: fields{
830+
Name: "cluster1",
831+
Addons: map[string]*ClusterAddon{
832+
"addon1": {
833+
Enabled: true,
834+
Properties: map[string]any{
835+
"property1": "value1",
836+
},
837+
},
838+
},
839+
},
840+
args: args{
841+
config: &ProjectConfig{
842+
Addons: map[string]Addon{
843+
"addon1": {
844+
Group: "group1",
845+
DefaultEnabled: true,
846+
},
847+
},
848+
ParsedAddons: map[string]template.TemplateManifest{
849+
"addon1": {
850+
Properties: map[string]template.Property{
851+
"property1": {
852+
Required: true,
853+
Default: "Hello World",
854+
Type: template.PropertyTypeString,
855+
Description: "property1",
856+
},
857+
},
858+
},
859+
},
860+
Environments: map[string]*Environment{
861+
"env1": {
862+
Stages: map[string]*Stage{
863+
"stage1": {
864+
Addons: map[string]*ClusterAddon{
865+
"addon1": {
866+
Enabled: true,
867+
Properties: map[string]any{},
868+
},
869+
},
870+
},
871+
},
872+
},
873+
},
874+
},
875+
env: "env1",
876+
stg: "stage1",
877+
},
878+
want: map[string]*ClusterAddon{
879+
"addon1": {
880+
Enabled: true,
881+
Properties: map[string]any{"property1": "value1"},
882+
},
883+
},
884+
},
885+
{
886+
name: "one addon, stage has highest priority",
887+
fields: fields{
888+
Name: "cluster1",
889+
Addons: map[string]*ClusterAddon{
890+
"addon1": {
891+
Enabled: true,
892+
Properties: map[string]any{},
893+
},
894+
},
895+
},
896+
args: args{
897+
config: &ProjectConfig{
898+
Addons: map[string]Addon{
899+
"addon1": {
900+
Group: "group1",
901+
DefaultEnabled: true,
902+
},
903+
},
904+
ParsedAddons: map[string]template.TemplateManifest{
905+
"addon1": {
906+
Properties: map[string]template.Property{
907+
"property1": {
908+
Required: true,
909+
Default: "Hello World",
910+
Type: template.PropertyTypeString,
911+
Description: "property1",
912+
},
913+
},
914+
},
915+
},
916+
Environments: map[string]*Environment{
917+
"env1": {
918+
Addons: map[string]*ClusterAddon{
919+
"addon1": {
920+
Enabled: true,
921+
Properties: map[string]any{
922+
"property1": "value1",
923+
},
924+
},
925+
},
926+
Stages: map[string]*Stage{
927+
"stage1": {
928+
Addons: map[string]*ClusterAddon{
929+
"addon1": {
930+
Enabled: true,
931+
Properties: map[string]any{
932+
"property1": "value1",
933+
},
934+
},
935+
},
936+
},
937+
},
938+
},
939+
},
940+
},
941+
env: "env1",
942+
stg: "stage1",
943+
},
944+
want: map[string]*ClusterAddon{
945+
"addon1": {
946+
Enabled: true,
947+
Properties: map[string]any{"property1": "value1"},
948+
},
949+
},
950+
},
951+
{
952+
name: "one addon, env has highest priority",
953+
fields: fields{
954+
Name: "cluster1",
955+
Addons: map[string]*ClusterAddon{
956+
"addon1": {
957+
Enabled: true,
958+
Properties: map[string]any{},
959+
},
960+
},
961+
},
962+
args: args{
963+
config: &ProjectConfig{
964+
Addons: map[string]Addon{
965+
"addon1": {
966+
Group: "group1",
967+
DefaultEnabled: true,
968+
},
969+
},
970+
ParsedAddons: map[string]template.TemplateManifest{
971+
"addon1": {
972+
Properties: map[string]template.Property{
973+
"property1": {
974+
Required: true,
975+
Default: "Hello World",
976+
Type: template.PropertyTypeString,
977+
Description: "property1",
978+
},
979+
},
980+
},
981+
},
982+
Environments: map[string]*Environment{
983+
"env1": {
984+
Addons: map[string]*ClusterAddon{
985+
"addon1": {
986+
Enabled: true,
987+
Properties: map[string]any{
988+
"property1": "value1",
989+
},
990+
},
991+
},
992+
Stages: map[string]*Stage{
993+
"stage1": {
994+
Addons: map[string]*ClusterAddon{},
995+
},
996+
},
997+
},
998+
},
999+
},
1000+
env: "env1",
1001+
stg: "stage1",
1002+
},
1003+
want: map[string]*ClusterAddon{
1004+
"addon1": {
1005+
Enabled: true,
1006+
Properties: map[string]any{"property1": "value1"},
1007+
},
1008+
},
1009+
},
1010+
}
1011+
for _, tt := range tests {
1012+
t.Run(tt.name, func(t *testing.T) {
1013+
c := &Cluster{
1014+
Name: tt.fields.Name,
1015+
Addons: tt.fields.Addons,
1016+
Properties: tt.fields.Properties,
1017+
}
1018+
got := c.AddonProperties(tt.args.config, tt.args.env, tt.args.stg)
1019+
diff := cmp.Diff(tt.want, got)
1020+
if diff != "" {
1021+
t.Errorf("Cluster.AddonProperties() mismatch (-want +got):\n%s", diff)
1022+
return
1023+
}
1024+
})
1025+
}
1026+
}

0 commit comments

Comments
 (0)