mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-11-04 11:56:11 +08:00 
			
		
		
		
	Delete custom resources during uninstall
Remove Kustomizations, GitRepositories and HelmRepositories before deleting the toolkit controllers and CRDs.
This commit is contained in:
		@ -19,10 +19,12 @@ package main
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/manifoldco/promptui"
 | 
						"github.com/manifoldco/promptui"
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1"
 | 
				
			||||||
 | 
						sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var uninstallCmd = &cobra.Command{
 | 
					var uninstallCmd = &cobra.Command{
 | 
				
			||||||
@ -33,24 +35,24 @@ var uninstallCmd = &cobra.Command{
 | 
				
			|||||||
  tk uninstall --dry-run --namespace=gitops-system
 | 
					  tk uninstall --dry-run --namespace=gitops-system
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Uninstall all components and delete custom resource definitions
 | 
					  # Uninstall all components and delete custom resource definitions
 | 
				
			||||||
  tk uninstall --crds --namespace=gitops-system
 | 
					  tk uninstall --resources --crds --namespace=gitops-system
 | 
				
			||||||
`,
 | 
					`,
 | 
				
			||||||
	RunE: uninstallCmdRun,
 | 
						RunE: uninstallCmdRun,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	uninstallCRDs      bool
 | 
						uninstallCRDs      bool
 | 
				
			||||||
	uninstallKustomizations bool
 | 
						uninstallResources bool
 | 
				
			||||||
	uninstallDryRun    bool
 | 
						uninstallDryRun    bool
 | 
				
			||||||
	uninstallSilent    bool
 | 
						uninstallSilent    bool
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	uninstallCmd.Flags().BoolVarP(&uninstallKustomizations, "kustomizations", "", false,
 | 
						uninstallCmd.Flags().BoolVar(&uninstallResources, "resources", false,
 | 
				
			||||||
		"removes all Kustomizations previously installed")
 | 
							"removes custom resources such as Kustomizations, GitRepositories and HelmRepositories")
 | 
				
			||||||
	uninstallCmd.Flags().BoolVarP(&uninstallCRDs, "crds", "", false,
 | 
						uninstallCmd.Flags().BoolVar(&uninstallCRDs, "crds", false,
 | 
				
			||||||
		"removes all CRDs previously installed")
 | 
							"removes all CRDs previously installed")
 | 
				
			||||||
	uninstallCmd.Flags().BoolVarP(&uninstallDryRun, "dry-run", "", false,
 | 
						uninstallCmd.Flags().BoolVar(&uninstallDryRun, "dry-run", false,
 | 
				
			||||||
		"only print the object that would be deleted")
 | 
							"only print the object that would be deleted")
 | 
				
			||||||
	uninstallCmd.Flags().BoolVarP(&uninstallSilent, "silent", "s", false,
 | 
						uninstallCmd.Flags().BoolVarP(&uninstallSilent, "silent", "s", false,
 | 
				
			||||||
		"delete components without asking for confirmation")
 | 
							"delete components without asking for confirmation")
 | 
				
			||||||
@ -75,18 +77,19 @@ func uninstallCmdRun(cmd *cobra.Command, args []string) error {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if uninstallKustomizations {
 | 
						if uninstallResources {
 | 
				
			||||||
		logger.Actionf("uninstalling kustomizations")
 | 
							logger.Actionf("uninstalling custom resources")
 | 
				
			||||||
		command := fmt.Sprintf("kubectl -n %s delete kustomizations --all --timeout=%s %s",
 | 
							for _, kind := range []string{
 | 
				
			||||||
			namespace, timeout.String(), dryRun)
 | 
								kustomizev1.KustomizationKind,
 | 
				
			||||||
 | 
								sourcev1.GitRepositoryKind,
 | 
				
			||||||
 | 
								sourcev1.HelmRepositoryKind,
 | 
				
			||||||
 | 
							} {
 | 
				
			||||||
 | 
								command := fmt.Sprintf("kubectl -n %s delete %s --all --timeout=%s %s",
 | 
				
			||||||
 | 
									namespace, kind, timeout.String(), dryRun)
 | 
				
			||||||
			if _, err := utils.execCommand(ctx, ModeOS, command); err != nil {
 | 
								if _, err := utils.execCommand(ctx, ModeOS, command); err != nil {
 | 
				
			||||||
				return fmt.Errorf("uninstall failed")
 | 
									return fmt.Errorf("uninstall failed")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		// TODO: use the kustomizations snapshots to create a list of objects
 | 
					 | 
				
			||||||
		// that are subject to deletion and wait for all of them to be terminated
 | 
					 | 
				
			||||||
		logger.Waitingf("waiting on GC")
 | 
					 | 
				
			||||||
		time.Sleep(30 * time.Second)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	kinds := "namespace,clusterroles,clusterrolebindings"
 | 
						kinds := "namespace,clusterroles,clusterrolebindings"
 | 
				
			||||||
 | 
				
			|||||||
@ -17,7 +17,7 @@ tk uninstall [flags]
 | 
				
			|||||||
  tk uninstall --dry-run --namespace=gitops-system
 | 
					  tk uninstall --dry-run --namespace=gitops-system
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Uninstall all components and delete custom resource definitions
 | 
					  # Uninstall all components and delete custom resource definitions
 | 
				
			||||||
  tk uninstall --crds --namespace=gitops-system
 | 
					  tk uninstall --resources --crds --namespace=gitops-system
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -27,7 +27,7 @@ tk uninstall [flags]
 | 
				
			|||||||
      --crds        removes all CRDs previously installed
 | 
					      --crds        removes all CRDs previously installed
 | 
				
			||||||
      --dry-run     only print the object that would be deleted
 | 
					      --dry-run     only print the object that would be deleted
 | 
				
			||||||
  -h, --help        help for uninstall
 | 
					  -h, --help        help for uninstall
 | 
				
			||||||
      --kustomizations   removes all Kustomizations previously installed
 | 
					      --resources   removes custom resources such as Kustomizations, GitRepositories and HelmRepositories
 | 
				
			||||||
  -s, --silent      delete components without asking for confirmation
 | 
					  -s, --silent      delete components without asking for confirmation
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user