Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e: Easier to manage namespaces #1707

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions e2e/deployers/applicationset.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ import (

type ApplicationSet struct{}

// Deploy creates an ApplicationSet on the hub cluster, creating the workload on one of the managed clusters.
func (a ApplicationSet) Deploy(ctx types.Context) error {
name := ctx.Name()
log := ctx.Logger()
namespace := util.ArgocdNamespace
managementNamespace := ctx.ManagementNamespace()

log.Info("Deploying workload")
log.Infof("Deploying applicationset in namespace %q", managementNamespace)

err := CreateManagedClusterSetBinding(McsbName, namespace)
err := CreateManagedClusterSetBinding(McsbName, managementNamespace)
if err != nil {
return err
}

err = CreatePlacement(ctx, name, namespace)
err = CreatePlacement(ctx, name, managementNamespace)
if err != nil {
return err
}

err = CreatePlacementDecisionConfigMap(ctx, name, namespace)
err = CreatePlacementDecisionConfigMap(ctx, name, managementNamespace)
if err != nil {
return err
}
Expand All @@ -40,37 +41,38 @@ func (a ApplicationSet) Deploy(ctx types.Context) error {
return err
}

// Undeploy deletes an ApplicationSet from the hub cluster, deleting the workload from the managed clusters.
func (a ApplicationSet) Undeploy(ctx types.Context) error {
name := ctx.Name()
log := ctx.Logger()
namespace := util.ArgocdNamespace
managementNamespace := ctx.ManagementNamespace()

log.Info("Undeploying workload")
log.Infof("Undeploying applicationset in namespace %q", managementNamespace)

err := DeleteApplicationSet(ctx, a)
if err != nil {
return err
}

err = DeleteConfigMap(ctx, name, namespace)
err = DeleteConfigMap(ctx, name, managementNamespace)
if err != nil {
return err
}

err = DeletePlacement(ctx, name, namespace)
err = DeletePlacement(ctx, name, managementNamespace)
if err != nil {
return err
}

// multiple appsets could use the same mcsb in argocd ns.
// so delete mcsb if only 1 appset is in argocd ns
lastAppset, err := isLastAppsetInArgocdNs(namespace)
lastAppset, err := isLastAppsetInArgocdNs(managementNamespace)
if err != nil {
return err
}

if lastAppset {
err = DeleteManagedClusterSetBinding(ctx, McsbName, namespace)
err = DeleteManagedClusterSetBinding(ctx, McsbName, managementNamespace)
if err != nil {
return err
}
Expand Down
19 changes: 10 additions & 9 deletions e2e/deployers/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func CreateSubscription(ctx types.Context, s Subscription) error {
name := ctx.Name()
log := ctx.Logger()
w := ctx.Workload()
namespace := name
managementNamespace := ctx.ManagementNamespace()

labels := make(map[string]string)
labels[AppLabelKey] = name
Expand All @@ -156,7 +156,7 @@ func CreateSubscription(ctx types.Context, s Subscription) error {
subscription := &subscriptionv1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Namespace: managementNamespace,
Labels: labels,
Annotations: annotations,
},
Expand Down Expand Up @@ -191,12 +191,12 @@ func CreateSubscription(ctx types.Context, s Subscription) error {
func DeleteSubscription(ctx types.Context, s Subscription) error {
name := ctx.Name()
log := ctx.Logger()
namespace := name
managementNamespace := ctx.ManagementNamespace()

subscription := &subscriptionv1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Namespace: managementNamespace,
},
}

Expand Down Expand Up @@ -276,12 +276,13 @@ func CreateApplicationSet(ctx types.Context, a ApplicationSet) error {
name := ctx.Name()
log := ctx.Logger()
w := ctx.Workload()
namespace := util.ArgocdNamespace
managementNamespace := ctx.ManagementNamespace()
appNamespace := ctx.AppNamespace()

appset := &argocdv1alpha1hack.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Namespace: managementNamespace,
},
Spec: argocdv1alpha1hack.ApplicationSetSpec{
Generators: []argocdv1alpha1hack.ApplicationSetGenerator{
Expand Down Expand Up @@ -309,7 +310,7 @@ func CreateApplicationSet(ctx types.Context, a ApplicationSet) error {
},
Destination: argocdv1alpha1hack.ApplicationDestination{
Server: "{{server}}",
Namespace: name,
Namespace: appNamespace,
},
Project: "default",
SyncPolicy: &argocdv1alpha1hack.SyncPolicy{
Expand Down Expand Up @@ -353,12 +354,12 @@ func CreateApplicationSet(ctx types.Context, a ApplicationSet) error {
func DeleteApplicationSet(ctx types.Context, a ApplicationSet) error {
name := ctx.Name()
log := ctx.Logger()
namespace := util.ArgocdNamespace
managementNamespace := ctx.ManagementNamespace()

appset := &argocdv1alpha1hack.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Namespace: managementNamespace,
},
}

Expand Down
30 changes: 15 additions & 15 deletions e2e/deployers/discoveredapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func (d DiscoveredApp) GetNamespace() string {
return util.RamenOpsNamespace
}

// Deploy creates a workload on the first managed cluster.
func (d DiscoveredApp) Deploy(ctx types.Context) error {
name := ctx.Name()
log := ctx.Logger()
namespace := name
appNamespace := ctx.AppNamespace()

log.Info("Deploying workload")
log.Infof("Deploying workload in namespace %q", appNamespace)

// create namespace in both dr clusters
if err := util.CreateNamespaceAndAddAnnotation(namespace); err != nil {
if err := util.CreateNamespaceAndAddAnnotation(appNamespace); err != nil {
return err
}

Expand All @@ -51,7 +51,7 @@ func (d DiscoveredApp) Deploy(ctx types.Context) error {
return err
}

cmd := exec.Command("kubectl", "apply", "-k", tempDir, "-n", namespace,
cmd := exec.Command("kubectl", "apply", "-k", tempDir, "-n", appNamespace,
"--context", drpolicy.Spec.DRClusters[0], "--timeout=5m")

if out, err := cmd.Output(); err != nil {
Expand All @@ -62,7 +62,7 @@ func (d DiscoveredApp) Deploy(ctx types.Context) error {
return err
}

if err = WaitWorkloadHealth(ctx, util.Ctx.C1.Client, namespace); err != nil {
if err = WaitWorkloadHealth(ctx, util.Ctx.C1.Client, appNamespace); err != nil {
return err
}

Expand All @@ -71,12 +71,12 @@ func (d DiscoveredApp) Deploy(ctx types.Context) error {
return nil
}

// Undeploy deletes the workload from the managed clusters.
func (d DiscoveredApp) Undeploy(ctx types.Context) error {
name := ctx.Name()
log := ctx.Logger()
namespace := name // this namespace is in dr clusters
appNamespace := ctx.AppNamespace()

log.Info("Undeploying workload")
log.Infof("Undeploying workload in namespace %q", appNamespace)

drpolicy, err := util.GetDRPolicy(util.Ctx.Hub.Client, util.DefaultDRPolicyName)
if err != nil {
Expand All @@ -86,26 +86,26 @@ func (d DiscoveredApp) Undeploy(ctx types.Context) error {
log.Infof("Deleting discovered apps on cluster %q", drpolicy.Spec.DRClusters[0])

// delete app on both clusters
if err := DeleteDiscoveredApps(ctx, namespace, drpolicy.Spec.DRClusters[0]); err != nil {
if err := DeleteDiscoveredApps(ctx, appNamespace, drpolicy.Spec.DRClusters[0]); err != nil {
return err
}

log.Infof("Deletting discovered apps on cluster %q", drpolicy.Spec.DRClusters[1])

if err := DeleteDiscoveredApps(ctx, namespace, drpolicy.Spec.DRClusters[1]); err != nil {
if err := DeleteDiscoveredApps(ctx, appNamespace, drpolicy.Spec.DRClusters[1]); err != nil {
return err
}

log.Infof("Deleting namespace %q on cluster %q", namespace, drpolicy.Spec.DRClusters[0])
log.Infof("Deleting namespace %q on cluster %q", appNamespace, drpolicy.Spec.DRClusters[0])

// delete namespace on both clusters
if err := util.DeleteNamespace(util.Ctx.C1.Client, namespace, log); err != nil {
if err := util.DeleteNamespace(util.Ctx.C1.Client, appNamespace, log); err != nil {
return err
}

log.Infof("Deleting namespace %q on cluster %q", namespace, drpolicy.Spec.DRClusters[1])
log.Infof("Deleting namespace %q on cluster %q", appNamespace, drpolicy.Spec.DRClusters[1])

if err := util.DeleteNamespace(util.Ctx.C2.Client, namespace, log); err != nil {
if err := util.DeleteNamespace(util.Ctx.C2.Client, appNamespace, log); err != nil {
return err
}

Expand Down
25 changes: 13 additions & 12 deletions e2e/deployers/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (s Subscription) GetNamespace() string {
return ""
}

// Deploy creates a Subscription on the hub cluster, creating the workload on one of the managed clusters.
func (s Subscription) Deploy(ctx types.Context) error {
// Generate a Placement for the Workload
// Use the global Channel
Expand All @@ -32,22 +33,22 @@ func (s Subscription) Deploy(ctx types.Context) error {
// Address namespace/label/suffix as needed for various resources
name := ctx.Name()
log := ctx.Logger()
namespace := name
managementNamespace := ctx.ManagementNamespace()

log.Info("Deploying workload")
log.Infof("Deploying subscription in namespace %q", managementNamespace)

// create subscription namespace
err := util.CreateNamespace(util.Ctx.Hub.Client, namespace)
err := util.CreateNamespace(util.Ctx.Hub.Client, managementNamespace)
if err != nil {
return err
}

err = CreateManagedClusterSetBinding(McsbName, namespace)
err = CreateManagedClusterSetBinding(McsbName, managementNamespace)
if err != nil {
return err
}

err = CreatePlacement(ctx, name, namespace)
err = CreatePlacement(ctx, name, managementNamespace)
if err != nil {
return err
}
Expand All @@ -57,33 +58,33 @@ func (s Subscription) Deploy(ctx types.Context) error {
return err
}

return waitSubscriptionPhase(ctx, namespace, name, subscriptionv1.SubscriptionPropagated)
return waitSubscriptionPhase(ctx, managementNamespace, name, subscriptionv1.SubscriptionPropagated)
}

// Delete Subscription, Placement, Binding
// Undeploy deletes a subscription from the hub cluster, deleting the workload from the managed clusters.
func (s Subscription) Undeploy(ctx types.Context) error {
name := ctx.Name()
log := ctx.Logger()
namespace := name
managementNamespace := ctx.ManagementNamespace()

log.Info("Undeploying workload")
log.Infof("Undeploying subscription in namespace %q", managementNamespace)

err := DeleteSubscription(ctx, s)
if err != nil {
return err
}

err = DeletePlacement(ctx, name, namespace)
err = DeletePlacement(ctx, name, managementNamespace)
if err != nil {
return err
}

err = DeleteManagedClusterSetBinding(ctx, McsbName, namespace)
err = DeleteManagedClusterSetBinding(ctx, McsbName, managementNamespace)
if err != nil {
return err
}

return util.DeleteNamespace(util.Ctx.Hub.Client, namespace, log)
return util.DeleteNamespace(util.Ctx.Hub.Client, managementNamespace, log)
}

func (s Subscription) IsWorkloadSupported(w types.Workload) bool {
Expand Down
Loading
Loading