finalize

package
v1.2.2 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: 1 module, 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 FinalizeCmd = &cobra.Command{
	Use:     "finalize [type1[,type2,...]] [name1,name2 [name3 ...]]",
	Aliases: []string{"fin", "f"},
	Args:    cobra.ArbitraryArgs,
	GroupID: cmdgroups.ClusterInteraction,
	Short:   "Remove finalizers from k8s resources",
	Long: `This command can remove all or only specific finalizers from specific or all resources.

Examples:

	> kpu finalize secret foo -n bar
	Removes all finalizers from the secret 'foo' in the namespace 'bar'.
	
	> kpu finalize --all -n bar
	Removes all finalizers from all resources in the namespace 'bar'.
	
	> kpu finalize -f foo.bar.baz/foobar --all -A
	Removes the finalizer 'foo.bar.baz/foobar' from all resources in all namespaces.
	Note that this affects only namespace-scoped resources due to the default value 'namespaced' for --scope.

	> kpu finalize secret,configmap foo bar -f foo.bar.baz/foobar -f foo.bar.baz/asdf
	Removes the finalizers 'foo.bar.baz/foobar' and 'foo.bar.baz/asdf' from the secrets and configmaps
	named 'foo' and 'bar' in the default namespace.
	`,
	Run: func(cmd *cobra.Command, args []string) {
		ValidateFinalizeCommand(args)

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

		var resourceTypes []string
		if len(args) > 0 {

			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, scope, 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 remove all ")
			if len(finalizers) > 0 {
				p.WriteString(utils.NaturalLanguageJoin(finalizers, ", ", true))
				p.WriteString(" ")
			}
			p.WriteString("finalizers from 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 {
			changed, err := k.RemoveFinalizers(cmd.Context(), &obj, finalizers...)
			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 errOccurred {
			os.Exit(1)
		}
	},
}

FinalizeCmd represents the finalize command

Functions

func ValidateFinalizeCommand

func ValidateFinalizeCommand(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