Skip to content

Commit

Permalink
Merge pull request #1458 from mariuskimmina/ignore-not-found
Browse files Browse the repository at this point in the history
feat(cli): add ignore-not-found flag
  • Loading branch information
FabianKramm authored Jan 24, 2024
2 parents eace09a + 656bde2 commit 962436f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cmd/vclusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type DeleteCmd struct {
DeleteNamespace bool
DeleteConfigMap bool
AutoDeleteNamespace bool
IgnoreNotFound bool

rawConfig *clientcmdapi.Config
restConfig *rest.Config
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/vclusterctl/cmd/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 962436f

Please sign in to comment.