diff --git a/cmd/vclusterctl/cmd/delete.go b/cmd/vclusterctl/cmd/delete.go index c96f9ab8a..fc1438b20 100644 --- a/cmd/vclusterctl/cmd/delete.go +++ b/cmd/vclusterctl/cmd/delete.go @@ -39,6 +39,7 @@ type DeleteCmd struct { DeleteNamespace bool DeleteConfigMap bool AutoDeleteNamespace bool + IgnoreNotFound bool rawConfig *clientcmdapi.Config restConfig *rest.Config @@ -80,6 +81,7 @@ vcluster delete test --namespace test cobraCmd.Flags().BoolVar(&cmd.KeepPVC, "keep-pvc", false, "If enabled, vcluster will not delete the persistent volume claim of the vcluster") cobraCmd.Flags().BoolVar(&cmd.DeleteNamespace, "delete-namespace", false, "If enabled, vcluster will delete the namespace of the vcluster. In the case of multi-namespace mode, will also delete all other namespaces created by vcluster") cobraCmd.Flags().BoolVar(&cmd.AutoDeleteNamespace, "auto-delete-namespace", true, "If enabled, vcluster will delete the namespace of the vcluster if it was created by vclusterctl. In the case of multi-namespace mode, will also delete all other namespaces created by vcluster") + cobraCmd.Flags().BoolVar(&cmd.IgnoreNotFound, "ignore-not-found", false, "If enabled, vcluster will not error out in case the target vcluster does not exist") return cobraCmd } @@ -97,7 +99,14 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error { vClusterName := args[0] vCluster, proVCluster, err := find.GetVCluster(ctx, proClient, cmd.Context, vClusterName, cmd.Namespace, cmd.Project, cmd.log) if err != nil { - return err + if !cmd.IgnoreNotFound { + return err + } + var errorNotFound *find.VclusterNotFoundError + if !errors.As(err, &errorNotFound) { + return err + } + return nil } else if proVCluster != nil { return cmd.deleteProVCluster(cobraCmd.Context(), proClient, proVCluster) } diff --git a/cmd/vclusterctl/cmd/find/find.go b/cmd/vclusterctl/cmd/find/find.go index 714068b0c..1578e8f4c 100644 --- a/cmd/vclusterctl/cmd/find/find.go +++ b/cmd/vclusterctl/cmd/find/find.go @@ -45,6 +45,14 @@ const ( StatusUnknown Status = "Unknown" ) +type VclusterNotFoundError struct { + Name string +} + +func (e *VclusterNotFoundError) Error() string { + return fmt.Sprintf("couldn't find vcluster %s", e.Name) +} + func SwitchContext(kubeConfig *clientcmdapi.Config, otherContext string) error { kubeConfig.CurrentContext = otherContext return clientcmd.ModifyConfig(clientcmd.NewDefaultClientConfigLoadingRules(), *kubeConfig, false) @@ -72,7 +80,7 @@ func GetVCluster(ctx context.Context, proClient pro.Client, context, name, names // figure out what we want to return if len(ossVClusters) == 0 && len(proVClusters) == 0 { - return nil, nil, fmt.Errorf("couldn't find vcluster %s", name) + return nil, nil, &VclusterNotFoundError{Name: name} } else if len(ossVClusters) == 1 && len(proVClusters) == 0 { return &ossVClusters[0], nil, nil } else if len(proVClusters) == 1 && len(ossVClusters) == 0 {