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

Commit

Permalink
refactor: CCM code to support both CRS and CAAPH provider (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin authored and jimmidyson committed Apr 11, 2024
1 parent 2fe9631 commit de1b808
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
21 changes: 14 additions & 7 deletions pkg/handlers/generic/lifecycle/ccm/aws/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
lifecycleutils "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
)

Expand Down Expand Up @@ -52,18 +53,18 @@ func New(
}
}

func (a *AWSCCM) EnsureCCMConfigMapForCluster(
func (a *AWSCCM) Apply(
ctx context.Context,
cluster *clusterv1.Cluster,
) (*corev1.ConfigMap, error) {
) error {
log := ctrl.LoggerFrom(ctx).WithValues(
"cluster",
cluster.Name,
)
log.Info("Creating AWS CCM ConfigMap for Cluster")
version, err := semver.ParseTolerant(cluster.Spec.Topology.Version)
if err != nil {
return nil, fmt.Errorf("failed to parse version from cluster %w", err)
return fmt.Errorf("failed to parse version from cluster %w", err)
}
minorVersion := fmt.Sprintf("%d.%d", version.Major, version.Minor)
configMapForMinorVersion := a.config.kubernetesMinorVersionToCCMConfigMapNames[minorVersion]
Expand All @@ -79,22 +80,28 @@ func (a *AWSCCM) EnsureCCMConfigMapForCluster(
err = a.client.Get(ctx, objName, ccmConfigMapForMinorVersion)
if err != nil {
log.Error(err, "failed to fetch CCM template for cluster")
return nil, fmt.Errorf(
return fmt.Errorf(
"failed to retrieve default AWS CCM manifests ConfigMap %q: %w",
objName,
err,
)
}

ccmConfigMap := generateCCMConfigMapForCluster(ccmConfigMapForMinorVersion, cluster)
if err := client.ServerSideApply(ctx, a.client, ccmConfigMap); err != nil {
if err = client.ServerSideApply(ctx, a.client, ccmConfigMap); err != nil {
log.Error(err, "failed to apply CCM configmap for cluster")
return nil, fmt.Errorf(
return fmt.Errorf(
"failed to apply AWS CCM manifests ConfigMap: %w",
err,
)
}
return ccmConfigMap, nil

err = lifecycleutils.EnsureCRSForClusterFromObjects(ctx, ccmConfigMap.Name, a.client, cluster, ccmConfigMap)
if err != nil {
return fmt.Errorf("failed to generate CCM CRS for cluster: %w", err)
}

return nil
}

func generateCCMConfigMapForCluster(
Expand Down
23 changes: 4 additions & 19 deletions pkg/handlers/generic/lifecycle/ccm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -19,15 +18,14 @@ import (
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/lifecycle"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
lifecycleutils "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
)

const (
variableRootName = "ccm"
)

type CCMProvider interface {
EnsureCCMConfigMapForCluster(context.Context, *clusterv1.Cluster) (*corev1.ConfigMap, error)
Apply(context.Context, *clusterv1.Cluster) error
}

type CCMHandler struct {
Expand Down Expand Up @@ -100,31 +98,18 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
log.Info(fmt.Sprintf("No CCM handler provided for infra kind %s", infraKind))
return
}
cm, err := handler.EnsureCCMConfigMapForCluster(ctx, &req.Cluster)
err = handler.Apply(ctx, &req.Cluster)
if err != nil {
log.Error(
err,
"failed to generate CCM configmap",
"failed to deploy CCM for cluster",
)
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
resp.SetMessage(
fmt.Sprintf("failed to generate CCM configmap: %v",
fmt.Sprintf("failed to deploy CCM for cluster: %v",
err,
),
)
return
}
err = lifecycleutils.EnsureCRSForClusterFromObjects(ctx, cm.Name, c.client, &req.Cluster, cm)
if err != nil {
log.Error(
err,
"failed to generate CCM CRS for cluster",
)
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
resp.SetMessage(
fmt.Sprintf("failed to generate CCM CRS: %v",
err,
),
)
}
}

0 comments on commit de1b808

Please sign in to comment.