This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add clean up capability It cleans up Helm v2: - Configuration (Helm home) - Release data - Tiller deployment Signed-off-by: Martin Hickey <[email protected]> * Update after review comments #33 (review) #33 (comment) Signed-off-by: Martin Hickey <[email protected]>
- Loading branch information
Showing
12 changed files
with
579 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
Copyright The Helm Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"helm-2to3/pkg/v2" | ||
) | ||
|
||
/*var ( | ||
settings *EnvSettings | ||
)*/ | ||
|
||
func newCleanupCmd(out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "cleanup", | ||
Short: "cleanup Helm v2 configuration, release data and Tiller deployment", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
RunE: runCleanup, | ||
} | ||
|
||
flags := cmd.Flags() | ||
settings.AddFlags(flags) | ||
|
||
return cmd | ||
} | ||
|
||
func runCleanup(cmd *cobra.Command, args []string) error { | ||
return Cleanup() | ||
} | ||
|
||
// Cleanup will delete all release data for in specified namespace and owner label. It will remove | ||
// the Tiller server deployed as per namespace and owner label. It is also delete the Helm gv2 home directory | ||
// which contains the Helm configuration. Helm v2 will be unusable after this operation. | ||
func Cleanup() error { | ||
if settings.dryRun { | ||
fmt.Printf("NOTE: This is in dry-run mode, the following actions will not be executed.\n") | ||
fmt.Printf("Run without --dry-run to take the actions described below:\n\n") | ||
} | ||
|
||
fmt.Printf("WARNING: Helm v2 Configuration, Release Data and Tiller Deployment will be removed.\n") | ||
fmt.Printf("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.\n") | ||
fmt.Printf("Helm v2 will not be usable afterwards.\n\n") | ||
|
||
doCleanup, err := askConfirmation() | ||
if err != nil { | ||
return err | ||
} | ||
if !doCleanup { | ||
return fmt.Errorf("Cleanup will not proceed as the user didn't answer (Y|y) in order to continue") | ||
} | ||
|
||
fmt.Printf("\nHelm v2 data will be cleaned up.\n") | ||
|
||
fmt.Printf("[Helm 2] Releases will be deleted.\n") | ||
retrieveOptions := v2.RetrieveOptions{ | ||
ReleaseName: "", | ||
TillerNamespace: settings.tillerNamespace, | ||
TillerLabel: settings.label, | ||
TillerOutCluster: settings.tillerOutCluster, | ||
StorageType: settings.releaseStorage, | ||
} | ||
err = v2.DeleteAllReleaseVersions(retrieveOptions, settings.dryRun) | ||
if err != nil { | ||
return err | ||
} | ||
if !settings.dryRun { | ||
fmt.Printf("[Helm 2] Releases deleted.\n") | ||
} | ||
|
||
if !settings.tillerOutCluster { | ||
fmt.Printf("[Helm 2] Tiller service in \"%s\" namespace will be removed.\n", settings.tillerNamespace) | ||
err = v2.RemoveTiller(settings.tillerNamespace, settings.dryRun) | ||
if err != nil { | ||
return err | ||
} | ||
if !settings.dryRun { | ||
fmt.Printf("[Helm 2] Tiller service in \"%s\" namespace removed.\n", settings.tillerNamespace) | ||
} | ||
} | ||
|
||
err = v2.RemoveHomeFolder(settings.dryRun) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !settings.dryRun { | ||
fmt.Printf("Helm v2 data was cleaned up successfully.\n") | ||
} | ||
return nil | ||
} | ||
|
||
func askConfirmation() (bool, error) { | ||
fmt.Printf("[Cleanup/confirm] Are you sure you want to cleanup Helm v2 data? [y/N]: ") | ||
|
||
scanner := bufio.NewScanner(os.Stdin) | ||
scanner.Scan() | ||
if err := scanner.Err(); err != nil { | ||
return false, errors.Wrap(err, "couldn't read from standard input") | ||
} | ||
answer := scanner.Text() | ||
if strings.ToLower(answer) == "y" || strings.ToLower(answer) == "yes" { | ||
return true, nil | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright The Helm Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type EnvSettings struct { | ||
tillerNamespace string | ||
releaseStorage string | ||
label string | ||
dryRun bool | ||
tillerOutCluster bool | ||
} | ||
|
||
func New() *EnvSettings { | ||
envSettings := EnvSettings{} | ||
return &envSettings | ||
} | ||
|
||
// AddFlags binds flags to the given flagset. | ||
func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVarP(&s.tillerNamespace, "tiller-ns", "t", "kube-system", "namespace of Tiller") | ||
fs.BoolVar(&s.dryRun, "dry-run", false, "simulate a command") | ||
fs.StringVarP(&s.label, "label", "l", "OWNER=TILLER", "label to select tiller resources by") | ||
fs.BoolVar(&s.tillerOutCluster, "tiller-out-cluster", false, "when Tiller is not running in the cluster e.g. Tillerless") | ||
fs.StringVarP(&s.releaseStorage, "release-storage", "s", "secrets", "v2 release storage type/object. It can be 'secrets' or 'configmaps'. This is only used with the 'tiller-out-cluster' flag") | ||
} |
Oops, something went wrong.