Skip to content

Commit 6527408

Browse files
committed
fix: Remove providerName from defaultStorage config
This commit removes the `providerName` from `defaultStorage` configuration, just requiring that the `storageClassConfigName` exists for one of the configured providers. This commit also adds validation to ensure that the storage class configs have unique names across providers, current behaviour could have led to conflicts if the storage class name was used in multiple provider configurations. When adding the `local-path` CSI provisioner for Docker clusters, I found that the `providerName` in default storage class config was redundant and led to unnecessary duplication of values. Removing this unnecessary field reduces the duplication from: ``` defaultStorage: providerName: local-path storageClassConfigName: local-path providers: - name: local-path storageClassConfig: - name: local-path strategy: HelmAddon ``` To: ``` defaultStorage: storageClassConfigName: local-path providers: - name: local-path storageClassConfig: - name: local-path strategy: HelmAddon ``` While this may seem like only a small change, a single line removed, it is always good to keep APIs as terse as possible while still retaining strict behaviour. I think this commit achieves that.
1 parent 25b6054 commit 6527408

25 files changed

+75
-133
lines changed

api/v1alpha1/addon_types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ type ClusterAutoscaler struct {
110110
}
111111

112112
type DefaultStorage struct {
113-
// Name of the CSI Provider for the default storage class.
114-
// +kubebuilder:validation:Required
115-
// +kubebuilder:validation:Enum=aws-ebs;nutanix;local-path
116-
ProviderName string `json:"providerName"`
117-
118113
// Name of storage class config in any of the provider objects.
119114
// +kubebuilder:validation:Required
120115
// +kubebuilder:validation:MinLength=1

api/v1alpha1/crds/caren.nutanix.com_awsclusterconfigs.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,11 @@ spec:
102102
properties:
103103
defaultStorage:
104104
properties:
105-
providerName:
106-
description: Name of the CSI Provider for the default storage class.
107-
enum:
108-
- aws-ebs
109-
type: string
110105
storageClassConfigName:
111106
description: Name of storage class config in any of the provider objects.
112107
minLength: 1
113108
type: string
114109
required:
115-
- providerName
116110
- storageClassConfigName
117111
type: object
118112
providers:

api/v1alpha1/crds/caren.nutanix.com_dockerclusterconfigs.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,11 @@ spec:
102102
properties:
103103
defaultStorage:
104104
properties:
105-
providerName:
106-
description: Name of the CSI Provider for the default storage class.
107-
enum:
108-
- local-path
109-
type: string
110105
storageClassConfigName:
111106
description: Name of storage class config in any of the provider objects.
112107
minLength: 1
113108
type: string
114109
required:
115-
- providerName
116110
- storageClassConfigName
117111
type: object
118112
providers:

api/v1alpha1/crds/caren.nutanix.com_genericclusterconfigs.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,12 @@ spec:
116116
properties:
117117
defaultStorage:
118118
properties:
119-
providerName:
120-
description: Name of the CSI Provider for the default
121-
storage class.
122-
enum:
123-
- aws-ebs
124-
- nutanix
125-
- local-path
126-
type: string
127119
storageClassConfigName:
128120
description: Name of storage class config in any of the
129121
provider objects.
130122
minLength: 1
131123
type: string
132124
required:
133-
- providerName
134125
- storageClassConfigName
135126
type: object
136127
providers:

api/v1alpha1/crds/caren.nutanix.com_nutanixclusterconfigs.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,11 @@ spec:
102102
properties:
103103
defaultStorage:
104104
properties:
105-
providerName:
106-
description: Name of the CSI Provider for the default storage class.
107-
enum:
108-
- nutanix
109-
type: string
110105
storageClassConfigName:
111106
description: Name of storage class config in any of the provider objects.
112107
minLength: 1
113108
type: string
114109
required:
115-
- providerName
116110
- storageClassConfigName
117111
type: object
118112
providers:

examples/capi-quick-start/aws-cluster-calico-crs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: ClusterResourceSet
3030
csi:
3131
defaultStorage:
32-
providerName: aws-ebs
3332
storageClassConfigName: aws-ebs
3433
providers:
3534
- name: aws-ebs

examples/capi-quick-start/aws-cluster-calico-helm-addon.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: HelmAddon
3030
csi:
3131
defaultStorage:
32-
providerName: aws-ebs
3332
storageClassConfigName: aws-ebs
3433
providers:
3534
- name: aws-ebs

examples/capi-quick-start/aws-cluster-cilium-crs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: ClusterResourceSet
3030
csi:
3131
defaultStorage:
32-
providerName: aws-ebs
3332
storageClassConfigName: aws-ebs
3433
providers:
3534
- name: aws-ebs

examples/capi-quick-start/aws-cluster-cilium-helm-addon.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: HelmAddon
3030
csi:
3131
defaultStorage:
32-
providerName: aws-ebs
3332
storageClassConfigName: aws-ebs
3433
providers:
3534
- name: aws-ebs

examples/capi-quick-start/docker-cluster-calico-crs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: ClusterResourceSet
3030
csi:
3131
defaultStorage:
32-
providerName: local-path
3332
storageClassConfigName: local-path
3433
providers:
3534
- name: local-path

examples/capi-quick-start/docker-cluster-calico-helm-addon.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: HelmAddon
3030
csi:
3131
defaultStorage:
32-
providerName: local-path
3332
storageClassConfigName: local-path
3433
providers:
3534
- name: local-path

examples/capi-quick-start/docker-cluster-cilium-crs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: ClusterResourceSet
3030
csi:
3131
defaultStorage:
32-
providerName: local-path
3332
storageClassConfigName: local-path
3433
providers:
3534
- name: local-path

examples/capi-quick-start/docker-cluster-cilium-helm-addon.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ spec:
2929
strategy: HelmAddon
3030
csi:
3131
defaultStorage:
32-
providerName: local-path
3332
storageClassConfigName: local-path
3433
providers:
3534
- name: local-path

examples/capi-quick-start/nutanix-cluster-calico-crs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ spec:
7474
strategy: ClusterResourceSet
7575
csi:
7676
defaultStorage:
77-
providerName: nutanix
7877
storageClassConfigName: nutanix-volume
7978
providers:
8079
- credentials:

examples/capi-quick-start/nutanix-cluster-calico-helm-addon.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ spec:
7474
strategy: HelmAddon
7575
csi:
7676
defaultStorage:
77-
providerName: nutanix
7877
storageClassConfigName: nutanix-volume
7978
providers:
8079
- credentials:

examples/capi-quick-start/nutanix-cluster-cilium-crs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ spec:
7474
strategy: ClusterResourceSet
7575
csi:
7676
defaultStorage:
77-
providerName: nutanix
7877
storageClassConfigName: nutanix-volume
7978
providers:
8079
- credentials:

examples/capi-quick-start/nutanix-cluster-cilium-helm-addon.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ spec:
7474
strategy: HelmAddon
7575
csi:
7676
defaultStorage:
77-
providerName: nutanix
7877
storageClassConfigName: nutanix-volume
7978
providers:
8079
- credentials:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/onsi/gomega v1.33.1
2424
github.com/spf13/pflag v1.0.5
2525
github.com/stretchr/testify v1.9.0
26+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
2627
gopkg.in/yaml.v2 v2.4.0
2728
k8s.io/api v0.29.5
2829
k8s.io/apiextensions-apiserver v0.29.5
@@ -128,7 +129,6 @@ require (
128129
go.opentelemetry.io/otel/trace v1.22.0 // indirect
129130
go.uber.org/multierr v1.11.0 // indirect
130131
golang.org/x/crypto v0.23.0 // indirect
131-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
132132
golang.org/x/mod v0.17.0 // indirect
133133
golang.org/x/net v0.25.0 // indirect
134134
golang.org/x/oauth2 v0.18.0 // indirect

hack/examples/patches/aws/csi.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
path: "/spec/topology/variables/0/value/addons/csi"
66
value:
77
defaultStorage:
8-
providerName: aws-ebs
98
storageClassConfigName: aws-ebs
109
providers:
1110
- name: aws-ebs

hack/examples/patches/docker/csi.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
path: "/spec/topology/variables/0/value/addons/csi"
66
value:
77
defaultStorage:
8-
providerName: local-path
98
storageClassConfigName: local-path
109
providers:
1110
- name: local-path

hack/examples/patches/nutanix/csi.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
path: "/spec/topology/variables/0/value/addons/csi"
66
value:
77
defaultStorage:
8-
providerName: nutanix
98
storageClassConfigName: nutanix-volume
109
providers:
1110
- name: nutanix

make/addons.mk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ update-addon.kube-vip: ; $(info $(M) updating kube-vip manifests)
6161

6262
.PHONY: generate-helm-configmap
6363
generate-helm-configmap:
64-
go run hack/tools/helm-cm/main.go -kustomize-directory="./hack/addons/kustomize" -output-file="./charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml"
64+
go run hack/tools/helm-cm/main.go -kustomize-directory="./hack/addons/kustomize" \
65+
-output-file="./charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml"
6566
./hack/addons/add-warning-helm-configmap.sh
6667

6768
# Set only the supported CSI providers for each provider.
6869
.PHONY: configure-csi-providers.%
6970
configure-csi-providers.%: CSI_JSONPATH := .spec.versions[].schema.openAPIV3Schema.properties.spec.properties.addons.properties.csi.properties
7071
configure-csi-providers.%: ; $(info $(M) configuring csi providers for $*clusterconfigs)
7172
gojq --yaml-input --yaml-output \
72-
'($(CSI_JSONPATH).providers.items.properties.name.enum, $(CSI_JSONPATH).defaultStorage.properties.providerName.enum) |= $(CSI_PROVIDERS_$(*))' \
73+
'$(CSI_JSONPATH).providers.items.properties.name.enum |= $(CSI_PROVIDERS_$(*))' \
7374
api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml > api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml.tmp
74-
cat hack/license-header.yaml.txt <(echo ---) api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml.tmp > api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml
75+
cat hack/license-header.yaml.txt <(echo ---) api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml.tmp \
76+
>api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml
7577
rm api/v1alpha1/crds/caren.nutanix.com_$(*)clusterconfigs.yaml.tmp

pkg/handlers/generic/lifecycle/csi/handler.go

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package csi
66
import (
77
"context"
88
"fmt"
9+
"slices"
910

1011
"github.com/go-logr/logr"
1112
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -98,6 +99,13 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
9899
return
99100
}
100101

102+
if err := validateUniqueStorageClassNames(csi); err != nil {
103+
log.Error(err, "")
104+
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
105+
resp.SetMessage(err.Error())
106+
return
107+
}
108+
101109
if err := validateDefaultStorage(csi); err != nil {
102110
log.Error(err, "")
103111
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
@@ -149,31 +157,37 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
149157
}
150158

151159
func validateDefaultStorage(csi v1alpha1.CSI) error {
152-
// Verify that the default storage references a defined provider, and one of the
153-
// storage class configs for that provider.
154-
{
155-
storageClassConfigsByProviderName := map[string][]v1alpha1.StorageClassConfig{}
156-
for _, provider := range csi.Providers {
157-
storageClassConfigsByProviderName[provider.Name] = provider.StorageClassConfig
158-
}
159-
configs, ok := storageClassConfigsByProviderName[csi.DefaultStorage.ProviderName]
160-
if !ok {
161-
return fmt.Errorf(
162-
"the DefaultStorage Provider name must be the name of a configured provider",
163-
)
164-
}
165-
defaultStorageClassConfigNameInProvider := false
166-
for _, config := range configs {
167-
if csi.DefaultStorage.StorageClassConfigName == config.Name {
168-
defaultStorageClassConfigNameInProvider = true
169-
break
160+
// Verify that the default storage references a defined storage class config.
161+
if !slices.ContainsFunc(csi.Providers, func(provider v1alpha1.CSIProvider) bool {
162+
return slices.ContainsFunc(
163+
provider.StorageClassConfig,
164+
func(scc v1alpha1.StorageClassConfig) bool {
165+
return scc.Name == csi.DefaultStorage.StorageClassConfigName
166+
},
167+
)
168+
}) {
169+
return fmt.Errorf(
170+
"the DefaultStorage StorageClassConfig name must be the name of a configured StorageClassConfig",
171+
)
172+
}
173+
174+
return nil
175+
}
176+
177+
func validateUniqueStorageClassNames(csi v1alpha1.CSI) error {
178+
storageClassNames := make(map[string]string)
179+
for _, provider := range csi.Providers {
180+
for _, scc := range provider.StorageClassConfig {
181+
if existingProviderDef, ok := storageClassNames[scc.Name]; ok {
182+
return fmt.Errorf(
183+
"storage class name %q must be unique and is already defined by provider %q",
184+
scc.Name,
185+
existingProviderDef,
186+
)
170187
}
171-
}
172-
if !defaultStorageClassConfigNameInProvider {
173-
return fmt.Errorf(
174-
"the DefaultStorage StorageClassConfig name must be the name of a StorageClassConfig for the default provider",
175-
)
188+
storageClassNames[scc.Name] = provider.Name
176189
}
177190
}
191+
178192
return nil
179193
}

0 commit comments

Comments
 (0)