Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
feat: set a default storage class if 1 provider and 1 storage class c…
Browse files Browse the repository at this point in the history
…onfig
  • Loading branch information
faiq committed Mar 29, 2024
1 parent 96f6fd9 commit bb5fb37
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pkg/handlers/generic/lifecycle/csi/aws-ebs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (a *AWSEBS) Apply(
ctx context.Context,
provider v1alpha1.CSIProvider,
defaultStorageConfig *v1alpha1.DefaultStorage,
hasOneProviderAndOneStorageClass bool,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
) error {
strategy := provider.Strategy
Expand All @@ -71,19 +72,21 @@ func (a *AWSEBS) Apply(
ctx,
provider.StorageClassConfig,
&req.Cluster,
hasOneProviderAndOneStorageClass,
defaultStorageConfig,
)
}

func (a *AWSEBS) createStorageClasses(ctx context.Context,
configs []v1alpha1.StorageClassConfig,
cluster *clusterv1.Cluster,
hasOneProviderAndOneStorageClass bool,
defaultStorageConfig *v1alpha1.DefaultStorage,
) error {
allStorageClasses := make([]runtime.Object, 0, len(configs))
for _, c := range configs {
setAsDefault := c.Name == defaultStorageConfig.StorageClassConfigName &&
v1alpha1.CSIProviderAWSEBS == defaultStorageConfig.ProviderName
v1alpha1.CSIProviderAWSEBS == defaultStorageConfig.ProviderName || hasOneProviderAndOneStorageClass
allStorageClasses = append(allStorageClasses, lifecycleutils.CreateStorageClass(
c,
a.config.GlobalOptions.DefaultsNamespace(),
Expand Down
13 changes: 12 additions & 1 deletion pkg/handlers/generic/lifecycle/csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CSIProvider interface {
context.Context,
v1alpha1.CSIProvider,
*v1alpha1.DefaultStorage,
bool, // this is a bool which keeps track of the case where a user has only one provider and one storageclass
*runtimehooksv1.AfterControlPlaneInitializedRequest,
) error
}
Expand Down Expand Up @@ -98,6 +99,10 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
)
return
}
hasOneProviderAndOneStorageClass := len(csiProviders.Providers) == 1 &&
len(csiProviders.Providers[0].StorageClassConfig) == 1 &&
csiProviders.DefaultStorage == nil

for _, provider := range csiProviders.Providers {
handler, ok := c.ProviderHandler[provider.Name]
if !ok {
Expand All @@ -110,7 +115,13 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
continue
}
log.Info(fmt.Sprintf("Creating csi provider %s", provider))
err = handler.Apply(ctx, provider, csiProviders.DefaultStorage, req)
err = handler.Apply(
ctx,
provider,
csiProviders.DefaultStorage,
hasOneProviderAndOneStorageClass,
req,
)
if err != nil {
log.Error(
err,
Expand Down
6 changes: 5 additions & 1 deletion pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (n *NutanixCSI) Apply(
ctx context.Context,
provider v1alpha1.CSIProvider,
defaultStorageConfig *v1alpha1.DefaultStorage,
hasOneProviderAndOneStorageClass bool,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
) error {
strategy := provider.Strategy
Expand Down Expand Up @@ -110,6 +111,7 @@ func (n *NutanixCSI) Apply(
ctx,
provider.StorageClassConfig,
&req.Cluster,
hasOneProviderAndOneStorageClass,
defaultStorageConfig,
)
}
Expand Down Expand Up @@ -169,12 +171,14 @@ func (n *NutanixCSI) handleHelmAddonApply(
func (n *NutanixCSI) createStorageClasses(ctx context.Context,
configs []v1alpha1.StorageClassConfig,
cluster *clusterv1.Cluster,
hasOneProviderAndOneStorageClass bool,
defaultStorageConfig *v1alpha1.DefaultStorage,
) error {
allStorageClasses := make([]runtime.Object, 0, len(configs))
for _, c := range configs {
setAsDefault := c.Name == defaultStorageConfig.StorageClassConfigName &&
v1alpha1.CSIProviderNutanix == defaultStorageConfig.ProviderName
v1alpha1.CSIProviderNutanix == defaultStorageConfig.ProviderName ||
hasOneProviderAndOneStorageClass
allStorageClasses = append(allStorageClasses, lifecycleutils.CreateStorageClass(
c,
n.config.GlobalOptions.DefaultsNamespace(),
Expand Down

0 comments on commit bb5fb37

Please sign in to comment.