Skip to content

Commit

Permalink
added test cases for ensuring common resources
Browse files Browse the repository at this point in the history
Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn committed May 22, 2024
1 parent 4570753 commit 10293ff
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 103 deletions.
15 changes: 3 additions & 12 deletions api/v1/discovery_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package v1
import (
"fmt"

"github.com/stolostron/discovery/pkg/common"
admissionregistration "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -102,22 +103,12 @@ func (r *DiscoveredCluster) Default() {

var _ webhook.Validator = &DiscoveredCluster{}

// isSupportedClusterType checks if the cluster type is supported for the import
func isSupportedClusterType(clusterType string) bool {
supportedTypes := map[string]bool{
"MultiClusterEngineHCP": true,
"ROSA": true,
}

return supportedTypes[clusterType]
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *DiscoveredCluster) ValidateCreate() (admission.Warnings, error) {
discoveredclusterLog.Info("validate create", "Name", r.Name, "Type", r.Spec.Type)

// Validate resource
if !isSupportedClusterType(r.Spec.Type) && r.Spec.ImportAsManagedCluster {
if !common.IsSupportedClusterType(r.Spec.Type) && r.Spec.ImportAsManagedCluster {
err := fmt.Errorf(
"cannot create DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters of type '%s'. "+
"Only ROSA type clusters support auto import", r.Name, r.Spec.Type)
Expand All @@ -135,7 +126,7 @@ func (r *DiscoveredCluster) ValidateUpdate(old runtime.Object) (admission.Warnin

// Validate resource
oldDiscoveredCluster := old.(*DiscoveredCluster)
if !isSupportedClusterType(oldDiscoveredCluster.Spec.Type) && r.Spec.ImportAsManagedCluster {
if !common.IsSupportedClusterType(oldDiscoveredCluster.Spec.Type) && r.Spec.ImportAsManagedCluster {
err := fmt.Errorf(
"cannot update DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters of type '%s'. "+
"Only ROSA type clusters support auto import", r.Name, r.Spec.Type)
Expand Down
132 changes: 72 additions & 60 deletions controllers/discoveredcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pkg/errors"
klusterletconfigv1alpha1 "github.com/stolostron/cluster-lifecycle-api/klusterletconfig/v1alpha1"
discovery "github.com/stolostron/discovery/api/v1"
"github.com/stolostron/discovery/pkg/common"
utils "github.com/stolostron/discovery/util"
recon "github.com/stolostron/discovery/util/reconciler"
agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1"
Expand Down Expand Up @@ -92,15 +93,20 @@ func (r *DiscoveredClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re
*/
if !dc.Spec.IsManagedCluster && dc.Spec.ImportAsManagedCluster {
if !utils.IsAnnotationTrue(dc, utils.AnnotationPreviouslyAutoImported) {
if dc.Spec.Type == "ROSA" {
if res, err := r.EnsureROSA(ctx, dc); err != nil {
switch dc.Spec.Type {
case "MultiClusterEngineHCP":
if res, err := r.EnsureMultiClusterEngineHCP(ctx, dc); err != nil {
return res, err
}

} else if dc.Spec.Type == "MultiClusterEngineHCP" {
if res, err := r.EnsureMultiClusterEngineHCP(ctx, dc); err != nil {
case "ROSA":
if res, err := r.EnsureROSA(ctx, dc); err != nil {
return res, err
}

default:
logf.Info("Unknown cluster type. Skipping automatic import.", "Name", dc.Spec.DisplayName,
"Type", dc.Spec.Type)
}
} else {
logf.Info(
Expand Down Expand Up @@ -218,7 +224,8 @@ func (r *DiscoveredClusterReconciler) CreateManagedCluster(nn types.NamespacedNa
Annotations: annotations,
},
Spec: clusterapiv1.ManagedClusterSpec{
HubAcceptsClient: true,
HubAcceptsClient: true,
LeaseDurationSeconds: 60,
},
}
}
Expand Down Expand Up @@ -291,6 +298,63 @@ func (r *DiscoveredClusterReconciler) EnsureAutoImportSecret(ctx context.Context
return ctrl.Result{}, nil
}

/*
EnsureCommonResources ...
*/
func (r *DiscoveredClusterReconciler) EnsureCommonResources(ctx context.Context,
dc *discovery.DiscoveredCluster, isHCP bool) (ctrl.Result, error) {
if res, err := r.EnsureManagedCluster(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure ManagedCluster created", "Name", dc.Spec.DisplayName)
return res, err
}

if isHCP {
// Ensure that the KlusterletConfig CRD exists.
crdName := "klusterletconfigs.config.open-cluster-management.io"
if res, err := r.EnsureCRDExist(ctx, crdName); err != nil {
if !apierrors.IsNotFound(err) {
logf.Error(err, "failed to ensure custom resource definition exist", "Name", crdName)
return res, err
}
} else {
if res, err := r.EnsureKlusterletConfig(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure KlusterletConfig created", "Name", dc.GetNamespace()+"-config")
return res, err
}
}
}

// Ensure that the KlusterletAddOnConfig CRD exists. In standalone MCE mode, the CRD is not deployed.
crdName := "klusterletaddonconfigs.agent.open-cluster-management.io"
if res, err := r.EnsureCRDExist(ctx, crdName); err != nil {
if !apierrors.IsNotFound(err) {
logf.Error(err, "failed to ensure custom resource definition exist", "Name", crdName)
return res, err
}
} else {
if res, err := r.EnsureKlusterletAddonConfig(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure KlusterletAddonConfig created", "Name", dc.Spec.DisplayName)
return res, err
}
}

if !isHCP {
// Ensure that the DiscoveredCluster credentials are available on the cluster.
if res, err := r.EnsureDiscoveredClusterCredentialExists(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure DiscoveredCluster credential Secret exist", "Name",
dc.Spec.DisplayName)
return res, err
}

if res, err := r.EnsureAutoImportSecret(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure auto import Secret created", "Name", dc.Spec.DisplayName)
return res, err
}
}

return ctrl.Result{}, nil
}

// EnsureCRDExist checks if a Custom Resource Definition (CRD) with the specified name exists in the cluster.
// If the CRD exists, it returns indicating that the reconciliation should continue without requeueing.
// If the CRD doesn't exist, it logs a message indicating that the CRD was not found and returns.
Expand Down Expand Up @@ -453,64 +517,12 @@ func (r *DiscoveredClusterReconciler) EnsureROSA(ctx context.Context, dc *discov
return res, err
}

if res, err := r.EnsureManagedCluster(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure ManagedCluster created", "Name", dc.Spec.DisplayName)
return res, err
}

// Ensure that the KlusterletAddOnConfig CRD exists. In standalone MCE mode, the CRD is not deployed.
crdName := "klusterletaddonconfigs.agent.open-cluster-management.io"

if res, err := r.EnsureCRDExist(ctx, crdName); err != nil {
if !apierrors.IsNotFound(err) {
logf.Error(err, "failed to ensure custom resource definition exist", "Name", crdName)
return res, err
}
} else {
if res, err := r.EnsureKlusterletAddonConfig(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure KlusterletAddonConfig created", "Name", dc.Spec.DisplayName)
return res, err
}
}

// Ensure that the DiscoveredCluster credentials are available on the cluster.
if res, err := r.EnsureDiscoveredClusterCredentialExists(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure DiscoveredCluster credential Secret exist", "Name",
dc.Spec.DisplayName)
return res, err
}

if res, err := r.EnsureAutoImportSecret(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure auto import Secret created", "Name", dc.Spec.DisplayName)
return res, err
}

return ctrl.Result{}, nil
return r.EnsureCommonResources(ctx, dc, false)
}

func (r *DiscoveredClusterReconciler) EnsureMultiClusterEngineHCP(ctx context.Context, dc *discovery.DiscoveredCluster,
) (ctrl.Result, error) {
if res, err := r.EnsureManagedCluster(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure ManagedCluster created", "Name", dc.Spec.DisplayName)
return res, err
}

// Ensure that the KlusterletAddOnConfig CRD exists. In standalone MCE mode, the CRD is not deployed.
crdName := "klusterletconfigs.config.open-cluster-management.io"

if res, err := r.EnsureCRDExist(ctx, crdName); err != nil {
if !apierrors.IsNotFound(err) {
logf.Error(err, "failed to ensure custom resource definition exist", "Name", crdName)
return res, err
}
} else {
if res, err := r.EnsureKlusterletConfig(ctx, *dc); err != nil {
logf.Error(err, "failed to ensure KlusterletConfig created", "Name", dc.Spec.DisplayName)
return res, err
}
}

return ctrl.Result{}, nil
return r.EnsureCommonResources(ctx, dc, true)
}

// SetupWithManager ...
Expand Down Expand Up @@ -538,5 +550,5 @@ func (r *DiscoveredClusterReconciler) ShouldReconcile(obj metav1.Object) bool {
return false
}

return dc.Spec.Type == "ROSA" || dc.Spec.Type == "MultiClusterEngineHCP"
return common.IsSupportedClusterType(dc.Spec.Type)
}
Loading

0 comments on commit 10293ff

Please sign in to comment.