Skip to content

Commit

Permalink
✨ Check if there any managed clusters exists before hub cleanup (#431)
Browse files Browse the repository at this point in the history
* Check is there any managed clusters exists before hub cleanup

Signed-off-by: Rokibul Hasan <[email protected]>

* Update error message.

Signed-off-by: Rokibul Hasan <[email protected]>

* Update condition

Signed-off-by: Rokibul Hasan <[email protected]>

* Update e2e test

Signed-off-by: Rokibul Hasan <[email protected]>

* use meta.IsStatusConditionTrue

Signed-off-by: Rokibul Hasan <[email protected]>

---------

Signed-off-by: Rokibul Hasan <[email protected]>
  • Loading branch information
RokibulHasan7 authored Jun 3, 2024
1 parent a46f3ea commit 6267edc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/cmd/clean/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ package init
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/rest"
"log"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -63,6 +67,17 @@ func (o *Options) Run() error {
return err
}

// check if any managed cluster exist or not
exist, err := isManagedClusterExist(config)
if err != nil {
return err
}

if exist {
fmt.Fprintf(o.Streams.Out, "Please detach all managed clusters from the hub control plane\n")
return nil
}

//Clean other resources
kubeClient, apiExtensionsClient, _, err := helpers.GetClients(f)
if err != nil {
Expand Down Expand Up @@ -200,3 +215,21 @@ func puregeOperator(client kubernetes.Interface, extensionClient apiextensionscl

return utilerrors.NewAggregate(errs)
}

func isManagedClusterExist(config *rest.Config) (bool, error) {
clusterClient, err := clusterclientset.NewForConfig(config)
if err != nil {
return false, err
}
managedClusters, err := clusterClient.ClusterV1().ManagedClusters().List(context.Background(), metav1.ListOptions{})
if err != nil {
return false, err
}

for _, cluster := range managedClusters.Items {
if meta.IsStatusConditionTrue(cluster.Status.Conditions, clusterapiv1.ManagedClusterConditionAvailable) {
return true, nil
}
}
return false, nil
}
24 changes: 24 additions & 0 deletions test/e2e/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package util
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
"time"

"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -36,6 +38,28 @@ func WaitNamespaceDeleted(restcfg *rest.Config, namespace string) error {
})
}

func WaitForManagedClusterAvailableStatusToChange(restcfg *rest.Config, clusterName string) error {
klusterClient, err := clusterclient.NewForConfig(restcfg)
if err != nil {
return err
}
return wait.PollUntilContextCancel(context.TODO(), 1*time.Second, true, func(ctx context.Context) (bool, error) {
managedCluster, err := klusterClient.ClusterV1().ManagedClusters().Get(context.TODO(), clusterName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}

if !meta.IsStatusConditionTrue(managedCluster.Status.Conditions, clusterapiv1.ManagedClusterConditionAvailable) {
return true, nil
}

return false, nil
})
}

func DeleteClusterCSRs(restcfg *rest.Config) error {
clientset, err := kubernetes.NewForConfig(restcfg)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func initE2E() (*TestE2eConfig, error) {
if err != nil {
return err
}

// wait for managed cluster Available status to be "False"
err = WaitForManagedClusterAvailableStatusToChange(e2eConf.Cluster().Hub().KubeConfig(), e2eConf.Cluster().ManagedCluster1().Name())
if err != nil {
return err
}
err = e2eConf.Clusteradm().Clean(
"--context", e2eConf.Cluster().Hub().Context(),
"--purge-operator=false",
Expand Down

0 comments on commit 6267edc

Please sign in to comment.