del

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DeleteCmd = &cobra.Command{
	Use:     "delete type1[,type2,...] [name1,name2 [name3 ...]]",
	Aliases: []string{"del", "d"},
	Args:    cobra.MinimumNArgs(1),
	GroupID: cmdgroups.ClusterInteraction,
	Short:   "Delete k8s resources",
	Long: `This command works basically like 'kubectl delete', with two major differences:
1. It lists all affected resources and asks for confirmation beforehand (unless -y is specified).
2. It adds deletion confirmation annotations for specific API groups before attempting the deletion.

Currently, the following deletion confirmation rules are implemented:
- core.gardener.cloud => confirmation.gardener.cloud/deletion=true
- openmcp.cloud       => confirmation.openmcp.cloud/deletion=true
`,
	Run: func(cmd *cobra.Command, args []string) {
		ValidateDeleteCommand(args)

		k, err := utils.LoadKubeconfig(k8sOptions.KubeconfigPath)
		if err != nil {
			utils.Fatal(1, "error loading kubeconfig: %s", err.Error())
		}

		resourceTypes := strings.Split(args[0], ",")

		var resourceNames []string
		if len(args) > 1 {
			resourceNames = []string{}
			for _, arg := range args[1:] {
				resourceNames = append(resourceNames, strings.Split(arg, ",")...)
			}
			resourceNames = slices.DeleteFunc(resourceNames, func(s string) bool { return s == "" })
		}

		affectedResources, errs := k.ListResources(cmd.Context(), resourceTypes, resourceNames, utils.SCOPE_ALL, k8sOptions)

		if !suppressWarnings {
			for _, e := range errs {
				fmt.Fprintf(os.Stderr, "warning: %s\n", e.Error())
			}
		}
		if len(affectedResources) == 0 {
			fmt.Println("No resources would be affected by this command.")
			os.Exit(0)
		}

		if !autoConfirmation {

			p := strings.Builder{}
			p.WriteString("This will delete the following ")
			if len(affectedResources) == 1 {
				p.WriteString("resource:\n")
			} else {
				fmt.Fprint(&p, len(affectedResources))
				p.WriteString(" resources:\n")
			}
			for _, obj := range affectedResources {
				p.WriteString(utils.GVKToString(obj.GroupVersionKind()))
				p.WriteString("\t")
				ns := obj.GetNamespace()
				if ns != "" {
					p.WriteString(ns)
					p.WriteString("/")
				}
				p.WriteString(obj.GetName())
				p.WriteString("\n")
			}
			p.WriteString("Do you want to continue?")
			if !utils.PromptForConfirmation(p.String(), false) {
				fmt.Println("Command aborted.")
				os.Exit(0)
			}
		}

		errOccurred := false
		for _, obj := range affectedResources {
			var anns map[string]string
			switch obj.GroupVersionKind().Group {
			case "core.gardener.cloud":
				anns = map[string]string{
					"confirmation.gardener.cloud/deletion": "true",
				}
			case "core.openmcp.cloud":
				anns = map[string]string{
					"confirmation.openmcp.cloud/deletion": "true",
				}
			}
			if len(anns) > 0 {
				changed, err := k.PatchAnnotations(cmd.Context(), &obj, anns)
				if err != nil {
					fmt.Printf("error patching %s: %s\n", utils.ResourceIdentifier(&obj), err.Error())
					errOccurred = true
				} else {
					if changed {
						fmt.Printf("%s patched\n", utils.ResourceIdentifier(&obj))
					} else {
						fmt.Printf("%s unchanged\n", utils.ResourceIdentifier(&obj))
					}
				}
			}
			if err := k.Delete(cmd.Context(), &obj); err != nil {
				fmt.Printf("error deleting %s: %s\n", utils.ResourceIdentifier(&obj), err.Error())
				errOccurred = true
			}
		}
		if errOccurred {
			os.Exit(1)
		}
	},
}

DeleteCmd represents the delete command

Functions

func ValidateDeleteCommand

func ValidateDeleteCommand(args []string)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL