Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

feat(cleanup): Cleanup of a release #132

Merged
merged 4 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Flags:
--kube-context string name of the kubeconfig context to use
--kubeconfig string path to the kubeconfig file
-l, --label string label to select Tiller resources by (default "OWNER=TILLER")
--name string the release name. When it is specified, the named release and its versions will be removed only. Should not be used with other cleanup operations
--release-cleanup if set, release data cleanup performed
-s, --release-storage string v2 release storage type/object. It can be 'secrets' or 'configmaps'. This is only used with the 'tiller-out-cluster' flag (default "secrets")
--tiller-cleanup if set, Tiller cleanup performed
Expand All @@ -172,6 +173,7 @@ It will clean:
- Tiller deployment

Clean up can be done individually also, by setting one or all of the following flags: `--config-cleanup`, `--release-cleanup` and `--tiller-cleanup`.
Cleanup of a release and its versions is done by setting `--name` flag. This is a singular operation and is not to be used with the other cleanup operations.
If none of these flag are set, then all cleanup is performed.

For cleanup it uses the default Helm v2 home folder.
Expand Down
67 changes: 56 additions & 11 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -31,13 +32,15 @@ import (

var (
configCleanup bool
releaseName string
releaseCleanup bool
tillerCleanup bool
)

type CleanupOptions struct {
ConfigCleanup bool
DryRun bool
ReleaseName string
ReleaseCleanup bool
StorageType string
TillerCleanup bool
Expand All @@ -60,6 +63,7 @@ func newCleanupCmd(out io.Writer) *cobra.Command {
settings.AddFlags(flags)

flags.BoolVar(&configCleanup, "config-cleanup", false, "if set, configuration cleanup performed")
flags.StringVar(&releaseName, "name", "", "the release name. When it is specified, the named release and its versions will be removed only. Should not be used with other cleanup operations")
flags.BoolVar(&releaseCleanup, "release-cleanup", false, "if set, release data cleanup performed")
flags.BoolVar(&tillerCleanup, "tiller-cleanup", false, "if set, Tiller cleanup performed")

Expand All @@ -71,6 +75,7 @@ func runCleanup(cmd *cobra.Command, args []string) error {
ConfigCleanup: configCleanup,
DryRun: settings.DryRun,
ReleaseCleanup: releaseCleanup,
ReleaseName: releaseName,
StorageType: settings.ReleaseStorage,
TillerCleanup: tillerCleanup,
TillerLabel: settings.Label,
Expand All @@ -92,10 +97,17 @@ func runCleanup(cmd *cobra.Command, args []string) error {
func Cleanup(cleanupOptions CleanupOptions, kubeConfig common.KubeConfig) error {
var message strings.Builder

if !cleanupOptions.ConfigCleanup && !cleanupOptions.ReleaseCleanup && !cleanupOptions.TillerCleanup {
cleanupOptions.ConfigCleanup = true
if cleanupOptions.ReleaseName != "" {
if cleanupOptions.ConfigCleanup || cleanupOptions.TillerCleanup {
return errors.New("cleanup of a specific release is a singular operation. Other operations like configuration cleanup or Tiller cleanup are not allowed in conjunction with the operation")
}
cleanupOptions.ReleaseCleanup = true
cleanupOptions.TillerCleanup = true
} else {
if !cleanupOptions.ConfigCleanup && !cleanupOptions.ReleaseCleanup && !cleanupOptions.TillerCleanup {
cleanupOptions.ConfigCleanup = true
cleanupOptions.ReleaseCleanup = true
cleanupOptions.TillerCleanup = true
}
}

if cleanupOptions.DryRun {
Expand All @@ -109,16 +121,22 @@ func Cleanup(cleanupOptions CleanupOptions, kubeConfig common.KubeConfig) error
fmt.Fprint(&message, "\"Helm v2 Configuration\" ")
}
if cleanupOptions.ReleaseCleanup {
fmt.Fprint(&message, "\"Release Data\" ")
if cleanupOptions.ReleaseName == "" {
fmt.Fprint(&message, "\"Release Data\" ")
} else {
fmt.Fprint(&message, fmt.Sprintf("\"Release '%s' Data\" ", cleanupOptions.ReleaseName))
}
}
if cleanupOptions.TillerCleanup {
fmt.Fprint(&message, "\"Release Data\" ")
fmt.Fprint(&message, "\"Tiller\" ")
}
fmt.Fprintln(&message, "will be removed. ")
if cleanupOptions.ReleaseCleanup {
if cleanupOptions.ReleaseCleanup && cleanupOptions.ReleaseName == "" {
fmt.Fprintln(&message, "This will clean up all releases managed by Helm v2. It will not be possible to restore them if you haven't made a backup of the releases.")
}
fmt.Fprintln(&message, "Helm v2 may not be usable afterwards.")
if cleanupOptions.ReleaseName == "" {
fmt.Fprintln(&message, "Helm v2 may not be usable afterwards.")
}

fmt.Println(message.String())

Expand All @@ -134,20 +152,47 @@ func Cleanup(cleanupOptions CleanupOptions, kubeConfig common.KubeConfig) error
log.Printf("\nHelm v2 data will be cleaned up.\n")

if cleanupOptions.ReleaseCleanup {
log.Println("[Helm 2] Releases will be deleted.")
if cleanupOptions.ReleaseName == "" {
log.Println("[Helm 2] Releases will be deleted.")
} else {
log.Printf("[Helm 2] Release '%s' will be deleted.\n", cleanupOptions.ReleaseName)
}
retrieveOptions := v2.RetrieveOptions{
ReleaseName: "",
ReleaseName: cleanupOptions.ReleaseName,
TillerNamespace: cleanupOptions.TillerNamespace,
TillerLabel: cleanupOptions.TillerLabel,
TillerOutCluster: cleanupOptions.TillerOutCluster,
StorageType: cleanupOptions.StorageType,
}
err = v2.DeleteAllReleaseVersions(retrieveOptions, kubeConfig, cleanupOptions.DryRun)
if cleanupOptions.ReleaseName == "" {
err = v2.DeleteAllReleaseVersions(retrieveOptions, kubeConfig, cleanupOptions.DryRun)
} else {
// Get the releases versions as its the versions that are deleted
v2Releases, err := v2.GetReleaseVersions(retrieveOptions, kubeConfig)
if err != nil {
return err
}
versions := []int32{}
v2RelVerLen := len(v2Releases)
for i := 0; i < v2RelVerLen; i++ {
v2Release := v2Releases[i]
versions = append(versions, v2Release.Version)
}
deleteOptions := v2.DeleteOptions{
DryRun: cleanupOptions.DryRun,
Versions: versions,
}
err = v2.DeleteReleaseVersions(retrieveOptions, deleteOptions, kubeConfig)
}
if err != nil {
return err
}
if !cleanupOptions.DryRun {
log.Println("[Helm 2] Releases deleted.")
if cleanupOptions.ReleaseName == "" {
log.Println("[Helm 2] Releases deleted.")
} else {
log.Printf("[Helm 2] Release '%s' deleted.\n", cleanupOptions.ReleaseName)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions completion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ commands:
- dry-run
- l
- label
- name
- release-cleanup
- s
- release-storage
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "2to3"
version: "0.4.1"
version: "0.5.0"
usage: "migrate and cleanup Helm v2 configuration and releases in-place to Helm v3"
description: "migrate and cleanup Helm v2 configuration and releases in-place to Helm v3"
command: "$HELM_PLUGIN_DIR/bin/2to3"
Expand Down