github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/packager/helm/destroy.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package helm contains operations for working with helm charts.
     5  package helm
     6  
     7  import (
     8  	"regexp"
     9  
    10  	"github.com/Racer159/jackal/src/pkg/cluster"
    11  	"github.com/Racer159/jackal/src/pkg/message"
    12  	"helm.sh/helm/v3/pkg/action"
    13  )
    14  
    15  // Destroy removes JackalInitPackage charts from the cluster and optionally all Jackal-installed charts.
    16  func Destroy(purgeAllJackalInstallations bool) {
    17  	spinner := message.NewProgressSpinner("Removing Jackal-installed charts")
    18  	defer spinner.Stop()
    19  
    20  	h := Helm{}
    21  
    22  	// Initially load the actionConfig without a namespace
    23  	err := h.createActionConfig("", spinner)
    24  	if err != nil {
    25  		// Don't fatal since this is a removal action
    26  		spinner.Errorf(err, "Unable to initialize the K8s client")
    27  		return
    28  	}
    29  
    30  	// Match a name that begins with "jackal-"
    31  	// Explanation: https://regex101.com/r/3yzKZy/1
    32  	jackalPrefix := regexp.MustCompile(`(?m)^jackal-`)
    33  
    34  	// Get a list of all releases in all namespaces
    35  	list := action.NewList(h.actionConfig)
    36  	list.All = true
    37  	list.AllNamespaces = true
    38  	// Uninstall in reverse order
    39  	list.ByDate = true
    40  	list.SortReverse = true
    41  	releases, err := list.Run()
    42  	if err != nil {
    43  		// Don't fatal since this is a removal action
    44  		spinner.Errorf(err, "Unable to get the list of installed charts")
    45  	}
    46  
    47  	// Iterate over all releases
    48  	for _, release := range releases {
    49  		if !purgeAllJackalInstallations && release.Namespace != cluster.JackalNamespaceName {
    50  			// Don't process releases outside the jackal namespace unless purge all is true
    51  			continue
    52  		}
    53  		// Filter on jackal releases
    54  		if jackalPrefix.MatchString(release.Name) {
    55  			spinner.Updatef("Uninstalling helm chart %s/%s", release.Namespace, release.Name)
    56  			if err = h.RemoveChart(release.Namespace, release.Name, spinner); err != nil {
    57  				// Don't fatal since this is a removal action
    58  				spinner.Errorf(err, "Unable to uninstall the chart")
    59  			}
    60  		}
    61  	}
    62  
    63  	spinner.Success()
    64  }