Skip to content

Commit

Permalink
Merge pull request #3761 from danj-replicated/storage_type_safety
Browse files Browse the repository at this point in the history
Check if Extensions.Storage exists before accessing Type key
  • Loading branch information
twz123 authored Nov 28, 2023
2 parents c95dbfd + e80c996 commit 82202b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/k0s/v1beta1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (c *ClusterConfig) UnmarshalJSON(data []byte) error {
if jc.Spec.Extensions == nil {
jc.Spec.Extensions = DefaultExtensions()
}
if jc.Spec.Extensions.Storage == nil {
jc.Spec.Extensions.Storage = DefaultStorageExtension()
}
if jc.Spec.Network == nil {
jc.Spec.Network = DefaultNetwork()
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/k0s/v1beta1/clusterconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ spec:
telemetry: null
konnectivity: null
`
extensionsYamlData := `
apiVersion: k0s.k0sproject.io/v1beta1
kind: ClusterConfig
metadata:
name: foobar
spec:
extensions:
storage: null
`

c, err := ConfigFromString(yamlData)
assert.NoError(t, err)
Expand All @@ -210,6 +219,10 @@ spec:
assert.Equal(t, DefaultInstallSpec(), c.Spec.Install)
assert.Equal(t, DefaultClusterTelemetry(), c.Spec.Telemetry)
assert.Equal(t, DefaultKonnectivitySpec(), c.Spec.Konnectivity)

e, err := ConfigFromString(extensionsYamlData)
assert.NoError(t, err)
assert.Equal(t, DefaultExtensions(), e.Spec.Extensions)
}

func TestWorkerProfileConfig(t *testing.T) {
Expand Down
12 changes: 8 additions & 4 deletions pkg/apis/k0s/v1beta1/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,17 @@ func (e *ClusterExtensions) Validate() []error {
return errs
}

func DefaultStorageExtension() *StorageExtension {
return &StorageExtension{
Type: ExternalStorage,
CreateDefaultStorageClass: false,
}
}

// DefaultExtensions default values
func DefaultExtensions() *ClusterExtensions {
return &ClusterExtensions{
Storage: &StorageExtension{
Type: ExternalStorage,
CreateDefaultStorageClass: false,
},
Storage: DefaultStorageExtension(),
Helm: &HelmExtensions{
ConcurrencyLevel: 5,
},
Expand Down

0 comments on commit 82202b3

Please sign in to comment.