Skip to content

Commit 46681e3

Browse files
authored
config3alphato3: Default domain to empty string (#5015)
Update convertConfig3AlphaTo3() to use an empty string as a domain name when the read config has no domain information. Check the existence of "domain" field in the config before attempting a type assertion. This helps avoid panic: ``` panic: interface conversion: interface {} is nil, not string ``` Signed-off-by: Sunny <[email protected]>
1 parent 16b7118 commit 46681e3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

internal/cmd/operator-sdk/alpha/config3alphato3/convert_config_3-alpha_to_3.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ func convertConfig3AlphaTo3(cfgBytes []byte) (_ []byte, err error) {
6666
isMultigroup := hasMultigroup && multigroupObj.(bool)
6767

6868
if obj, hasRes := cfgObj["resources"]; hasRes {
69-
domain := cfgObj["domain"].(string)
69+
// Default to empty domain if no domain information is found.
70+
domain := ""
71+
if domainObj, ok := cfgObj["domain"]; ok {
72+
domain = domainObj.(string)
73+
}
74+
7075
resObjs := obj.([]interface{})
7176
resources := make([]resource.Resource, len(resObjs))
7277

internal/cmd/operator-sdk/alpha/config3alphato3/convert_config_3-alpha_to_3_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var _ = Describe("ConvertConfig3AlphaTo3", func() {
3636
Entry("no resources", noResourcesConfig, noResourcesConfigExp),
3737
Entry("basic", basicConfig, basicConfigExp),
3838
Entry("complex", complexConfig, complexConfigExp),
39+
Entry("no domain", noDomainConfig, noDomainConfigExp),
3940
)
4041
})
4142

@@ -68,6 +69,30 @@ version: "3"
6869
`
6970
)
7071

72+
const (
73+
noDomainConfig = `layout: ansible.sdk.operatorframework.io/v1
74+
projectName: memcached-operator
75+
resources:
76+
- crdVersion: v1
77+
group: cache
78+
kind: Memcached
79+
version: v1alpha1
80+
version: 3-alpha`
81+
noDomainConfigExp = `layout: ansible.sdk.operatorframework.io/v1
82+
projectName: memcached-operator
83+
resources:
84+
- api:
85+
crdVersion: v1
86+
# TODO(user): Uncomment the below line if this resource's CRD is namespace scoped, else delete it.
87+
# namespaced: true
88+
# TODO(user): Uncomment the below line if this resource implements a controller, else delete it.
89+
# controller: true
90+
group: cache
91+
kind: Memcached
92+
version: v1alpha1
93+
version: "3"`
94+
)
95+
7196
const (
7297
noResourcesConfig = `domain: example.com
7398
layout: ansible.sdk.operatorframework.io/v1

0 commit comments

Comments
 (0)