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

feat(cli): add ignore-not-found flag #1458

Merged
merged 1 commit into from
Jan 24, 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
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
Loading