Skip to content

Commit

Permalink
Merge pull request #2110 from rohantmp/fixExternalDelete
Browse files Browse the repository at this point in the history
fix: external delete should check status field for service uid
  • Loading branch information
FabianKramm committed Sep 9, 2024
2 parents fe1fbd0 + ff3fe67 commit b379167
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/cli/delete_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"time"

managementv1 "github.com/loft-sh/api/v4/pkg/apis/management/v1"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/find"
"github.com/loft-sh/vcluster/pkg/cli/flags"
Expand All @@ -25,8 +26,6 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

const VirtualClusterServiceUIDLabel = "vcluster.loft.sh/service-uid"

type DeleteOptions struct {
Driver string

Expand Down Expand Up @@ -121,10 +120,13 @@ func DeleteHelm(ctx context.Context, options *DeleteOptions, globalFlags *flags.

// try to delete the vCluster in the platform
if vClusterService != nil {
cmd.log.Debugf("deleting vcluster in platform")
err = cmd.deleteVClusterInPlatform(ctx, vClusterService)
if err != nil {
return err
}
} else {
cmd.log.Warn("vcluster service not found, could not delete in platform")
}

// try to delete the pvc
Expand Down Expand Up @@ -241,15 +243,21 @@ func (cmd *deleteHelm) deleteVClusterInPlatform(ctx context.Context, vClusterSer
return nil
}

virtualClusterInstances, err := managementClient.Loft().ManagementV1().VirtualClusterInstances(corev1.NamespaceAll).List(ctx, metav1.ListOptions{
LabelSelector: VirtualClusterServiceUIDLabel + "=" + string(vClusterService.UID),
})
virtualClusterInstances, err := managementClient.Loft().ManagementV1().VirtualClusterInstances(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
cmd.log.Debugf("Error retrieving vcluster instances: %v", err)
return nil
}

toDelete := []managementv1.VirtualClusterInstance{}
for _, virtualClusterInstance := range virtualClusterInstances.Items {
if virtualClusterInstance.Status.ServiceUID != "" && virtualClusterInstance.Status.ServiceUID == string(vClusterService.UID) {
toDelete = append(toDelete, virtualClusterInstance)
}
}
cmd.log.Debugf("found %d matching virtualclusterinstances", len(toDelete))

for _, virtualClusterInstance := range toDelete {
cmd.log.Infof("Delete virtual cluster instance %s/%s in platform", virtualClusterInstance.Namespace, virtualClusterInstance.Name)
err = managementClient.Loft().ManagementV1().VirtualClusterInstances(virtualClusterInstance.Namespace).Delete(ctx, virtualClusterInstance.Name, metav1.DeleteOptions{})
if err != nil {
Expand Down

0 comments on commit b379167

Please sign in to comment.