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

Commit

Permalink
feat: create crs from provider credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
faiq committed Mar 29, 2024
1 parent 00a0eb2 commit 96f6fd9
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s crsStrategy) apply(
)
}

if err = utils.EnsureCRSForClusterFromConfigMaps(ctx, cm.Name, s.client, targetCluster, cm); err != nil {
if err = utils.EnsureCRSForClusterFromObjects(ctx, cm.Name, s.client, targetCluster, cm); err != nil {
return fmt.Errorf(
"failed to apply cluster-autoscaler installation ClusterResourceSet: %w",
err,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/generic/lifecycle/cni/calico/strategy_crs.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s crsStrategy) ensureCNICRSForCluster(
)
}

if err := utils.EnsureCRSForClusterFromConfigMaps(ctx, cm.Name, s.client, cluster, tigeraConfigMap, cm); err != nil {
if err := utils.EnsureCRSForClusterFromObjects(ctx, cm.Name, s.client, cluster, tigeraConfigMap, cm); err != nil {
return fmt.Errorf(
"failed to apply Calico CNI installation ClusterResourceSet: %w",
err,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/generic/lifecycle/cni/cilium/strategy_crs.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s crsStrategy) apply(
)
}

if err := utils.EnsureCRSForClusterFromConfigMaps(ctx, cm.Name, s.client, cluster, cm); err != nil {
if err := utils.EnsureCRSForClusterFromObjects(ctx, cm.Name, s.client, cluster, cm); err != nil {
return fmt.Errorf(
"failed to apply Cilium CNI installation ClusterResourceSet: %w",
err,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/generic/lifecycle/cpi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *CPIHandler) AfterControlPlaneInitialized(
)
return
}
err = lifecycleutils.EnsureCRSForClusterFromConfigMaps(ctx, cm.Name, c.client, &req.Cluster, cm)
err = lifecycleutils.EnsureCRSForClusterFromObjects(ctx, cm.Name, c.client, &req.Cluster, cm)
if err != nil {
log.Error(
err,
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/generic/lifecycle/csi/aws-ebs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (a *AWSEBS) createStorageClasses(ctx context.Context,
if err != nil {
return err
}
return lifecycleutils.EnsureCRSForClusterFromConfigMaps(
return lifecycleutils.EnsureCRSForClusterFromObjects(
ctx,
"aws-storageclass-crs",
a.client,
Expand Down Expand Up @@ -140,7 +140,7 @@ func (a *AWSEBS) handleCRSApply(ctx context.Context,
err,
)
}
err = lifecycleutils.EnsureCRSForClusterFromConfigMaps(
err = lifecycleutils.EnsureCRSForClusterFromObjects(
ctx,
cm.Name,
a.client,
Expand Down
33 changes: 32 additions & 1 deletion pkg/handlers/generic/lifecycle/csi/nutanix-csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -75,6 +76,36 @@ func (n *NutanixCSI) Apply(
default:
return fmt.Errorf("stategy %s not implemented", strategy)
}
if provider.Credentials != nil {
sec := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: provider.Credentials.Name,
Name: provider.Credentials.Namespace,
},
}
err := n.client.Get(
ctx,
ctrlclient.ObjectKeyFromObject(sec),
sec,
)
if err != nil {
return err
}
err = lifecycleutils.EnsureCRSForClusterFromObjects(
ctx,
fmt.Sprintf("nutanix-csi-credentials-crs-%s", req.Cluster.Name),
n.client,
&req.Cluster,
sec,
)
if err != nil {
return err
}
}
return n.createStorageClasses(
ctx,
provider.StorageClassConfig,
Expand Down Expand Up @@ -163,7 +194,7 @@ func (n *NutanixCSI) createStorageClasses(ctx context.Context,
if err != nil {
return err
}
return lifecycleutils.EnsureCRSForClusterFromConfigMaps(
return lifecycleutils.EnsureCRSForClusterFromObjects(
ctx,
"nutanix-storageclass-crs",
n.client,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/generic/lifecycle/nfd/strategy_crs.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s crsStrategy) apply(
)
}

if err := utils.EnsureCRSForClusterFromConfigMaps(ctx, cm.Name, s.client, cluster, cm); err != nil {
if err := utils.EnsureCRSForClusterFromObjects(ctx, cm.Name, s.client, cluster, cm); err != nil {
return fmt.Errorf(
"failed to apply NFD installation ClusterResourceSet: %w",
err,
Expand Down
29 changes: 23 additions & 6 deletions pkg/handlers/generic/lifecycle/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,35 @@ var (
}
)

func EnsureCRSForClusterFromConfigMaps(
func EnsureCRSForClusterFromObjects(
ctx context.Context,
crsName string,
c ctrlclient.Client,
cluster *clusterv1.Cluster,
configMaps ...*corev1.ConfigMap,
objects ...runtime.Object,
) error {
resources := make([]crsv1.ResourceRef, 0, len(configMaps))
for _, cm := range configMaps {
resources := make([]crsv1.ResourceRef, 0, len(objects))
for _, obj := range objects {
var name string
var kind crsv1.ClusterResourceSetResourceKind
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
sec, secOk := obj.(*corev1.Secret)
if !secOk {
return fmt.Errorf(
"cannot create ClusterResourceSet with obj %v only secrets and configmaps are supported",
obj,
)
}
name = sec.Name
kind = crsv1.SecretClusterResourceSetResourceKind
} else {
name = cm.Name
kind = crsv1.ConfigMapClusterResourceSetResourceKind
}
resources = append(resources, crsv1.ResourceRef{
Kind: string(crsv1.ConfigMapClusterResourceSetResourceKind),
Name: cm.Name,
Name: name,
Kind: string(kind),
})
}

Expand Down

0 comments on commit 96f6fd9

Please sign in to comment.