|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors |
| 3 | + |
| 4 | +package packager2 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "runtime" |
| 11 | + "slices" |
| 12 | + |
| 13 | + "helm.sh/helm/v3/pkg/action" |
| 14 | + "helm.sh/helm/v3/pkg/cli" |
| 15 | + "helm.sh/helm/v3/pkg/storage/driver" |
| 16 | + |
| 17 | + "github.com/zarf-dev/zarf/src/api/v1alpha1" |
| 18 | + "github.com/zarf-dev/zarf/src/config" |
| 19 | + "github.com/zarf-dev/zarf/src/pkg/cluster" |
| 20 | + "github.com/zarf-dev/zarf/src/pkg/message" |
| 21 | + "github.com/zarf-dev/zarf/src/pkg/packager/actions" |
| 22 | + "github.com/zarf-dev/zarf/src/pkg/packager/filters" |
| 23 | + "github.com/zarf-dev/zarf/src/types" |
| 24 | +) |
| 25 | + |
| 26 | +// RemoveOptions are the options for Remove. |
| 27 | +type RemoveOptions struct { |
| 28 | + Cluster *cluster.Cluster |
| 29 | + OptionalComponents string |
| 30 | + Pkg v1alpha1.ZarfPackage |
| 31 | +} |
| 32 | + |
| 33 | +// Remove removes a package that was already deployed onto a cluster, uninstalling all installed helm charts. |
| 34 | +func Remove(ctx context.Context, opt RemoveOptions) error { |
| 35 | + // If components were provided; just remove the things we were asked to remove |
| 36 | + filter := filters.Combine( |
| 37 | + filters.ByLocalOS(runtime.GOOS), |
| 38 | + filters.BySelectState(opt.OptionalComponents), |
| 39 | + ) |
| 40 | + components, err := filter.Apply(opt.Pkg) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + // Check that cluster is configured if required. |
| 45 | + requiresCluster := false |
| 46 | + componentIdx := map[string]v1alpha1.ZarfComponent{} |
| 47 | + for _, component := range components { |
| 48 | + componentIdx[component.Name] = component |
| 49 | + if component.RequiresCluster() { |
| 50 | + if opt.Cluster == nil { |
| 51 | + return fmt.Errorf("component %s requires cluster access but none was configured", component.Name) |
| 52 | + } |
| 53 | + requiresCluster = true |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Get or build the secret for the deployed package |
| 58 | + depPkg := &types.DeployedPackage{} |
| 59 | + if requiresCluster { |
| 60 | + depPkg, err = opt.Cluster.GetDeployedPackage(ctx, opt.Pkg.Metadata.Name) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("unable to load the secret for the package we are attempting to remove: %s", err.Error()) |
| 63 | + } |
| 64 | + } else { |
| 65 | + // If we do not need the cluster, create a deployed components object based on the info we have |
| 66 | + depPkg.Name = opt.Pkg.Metadata.Name |
| 67 | + depPkg.Data = opt.Pkg |
| 68 | + for _, component := range components { |
| 69 | + depPkg.DeployedComponents = append(depPkg.DeployedComponents, types.DeployedComponent{Name: component.Name}) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + reverseDepComps := depPkg.DeployedComponents |
| 74 | + slices.Reverse(reverseDepComps) |
| 75 | + for i, depComp := range reverseDepComps { |
| 76 | + // Only remove the component if it was requested or if we are removing the whole package. |
| 77 | + comp, ok := componentIdx[depComp.Name] |
| 78 | + if !ok { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + err := func() error { |
| 83 | + err := actions.Run(ctx, comp.Actions.OnRemove.Defaults, comp.Actions.OnRemove.Before, nil) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("unable to run the before action: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + reverseInstalledCharts := depComp.InstalledCharts |
| 89 | + slices.Reverse(reverseInstalledCharts) |
| 90 | + if opt.Cluster != nil { |
| 91 | + for _, chart := range reverseInstalledCharts { |
| 92 | + settings := cli.New() |
| 93 | + actionConfig := &action.Configuration{} |
| 94 | + // TODO (phillebaba): Get credentials from cluster instead of reading again. |
| 95 | + err := actionConfig.Init(settings.RESTClientGetter(), chart.Namespace, "", nil) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + client := action.NewUninstall(actionConfig) |
| 100 | + client.KeepHistory = false |
| 101 | + client.Wait = true |
| 102 | + client.Timeout = config.ZarfDefaultTimeout |
| 103 | + _, err = client.Run(chart.ChartName) |
| 104 | + if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) { |
| 105 | + return fmt.Errorf("unable to uninstall the helm chart %s in the namespace %s: %w", chart.ChartName, chart.Namespace, err) |
| 106 | + } |
| 107 | + if errors.Is(err, driver.ErrReleaseNotFound) { |
| 108 | + message.Warnf("Helm release for helm chart '%s' in the namespace '%s' was not found. Was it already removed?", chart.ChartName, chart.Namespace) |
| 109 | + } |
| 110 | + |
| 111 | + // Pop the removed helm chart from the installed charts slice. |
| 112 | + depPkg.DeployedComponents[len(depPkg.DeployedComponents)-i].InstalledCharts = depComp.InstalledCharts[:len(depComp.InstalledCharts)-1] |
| 113 | + err = opt.Cluster.UpdateDeployedPackage(ctx, *depPkg) |
| 114 | + if err != nil { |
| 115 | + // We warn and ignore errors because we may have removed the cluster that this package was inside of |
| 116 | + message.Warnf("Unable to update the secret for package %s, this may be normal if the cluster was removed: %s", depPkg.Name, err.Error()) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + err = actions.Run(ctx, comp.Actions.OnRemove.Defaults, comp.Actions.OnRemove.After, nil) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("unable to run the after action: %w", err) |
| 124 | + } |
| 125 | + err = actions.Run(ctx, comp.Actions.OnRemove.Defaults, comp.Actions.OnRemove.OnSuccess, nil) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("unable to run the success action: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + // Pop the removed component from deploy components slice. |
| 131 | + if opt.Cluster != nil { |
| 132 | + depPkg.DeployedComponents = depPkg.DeployedComponents[:len(depPkg.DeployedComponents)-1] |
| 133 | + err = opt.Cluster.UpdateDeployedPackage(ctx, *depPkg) |
| 134 | + if err != nil { |
| 135 | + // We warn and ignore errors because we may have removed the cluster that this package was inside of |
| 136 | + message.Warnf("Unable to update the secret for package %s, this may be normal if the cluster was removed: %s", depPkg.Name, err.Error()) |
| 137 | + } |
| 138 | + } |
| 139 | + return nil |
| 140 | + }() |
| 141 | + if err != nil { |
| 142 | + err := actions.Run(ctx, comp.Actions.OnRemove.Defaults, comp.Actions.OnRemove.OnFailure, nil) |
| 143 | + if err != nil { |
| 144 | + // TODO: I think we should return an error if failure action fails. Does this break existing use cases? |
| 145 | + return fmt.Errorf("unable to run the failure action: %w", err) |
| 146 | + } |
| 147 | + return nil |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // All the installed components were deleted, therefore this package is no longer actually deployed |
| 152 | + if opt.Cluster != nil && len(depPkg.DeployedComponents) == 0 { |
| 153 | + err := opt.Cluster.DeleteDeployedPackage(ctx, depPkg.Name) |
| 154 | + if err != nil { |
| 155 | + message.Warnf("Unable to delete the secret for package %s, tis may be normal if the cluster was removed: %s", depPkg.Name, err.Error()) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
0 commit comments