From a03d3e34ac4fc9f31d77647c6273f6b7da35d145 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Thu, 25 Jun 2020 12:18:55 -0400 Subject: [PATCH] Adding init flad to skip adding repos The stable charts repository is nearing the end of its life. Once that has happened those who attempt to run helm init will get an error. This flag provides a means to still use Helm v2 after the stable charts repository is gone. A secondary situation is that Helm v2 can be used in environmnets without a network connection. Closes #7868 Signed-off-by: Matt Farina --- cmd/helm/init.go | 12 +++++++++-- cmd/helm/installer/init.go | 23 ++++++++++++++++++++++ cmd/helm/installer/init_test.go | 35 +++++++++++++++++++++++++++++++++ docs/helm/helm_init.md | 3 ++- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/cmd/helm/init.go b/cmd/helm/init.go index 3425130ea..28ea9edeb 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -80,6 +80,7 @@ type initCmd struct { dryRun bool forceUpgrade bool skipRefresh bool + skipRepos bool out io.Writer client helm.Interface home helmpath.Home @@ -118,6 +119,7 @@ func newInitCmd(out io.Writer) *cobra.Command { f.BoolVarP(&i.clientOnly, "client-only", "c", false, "If set does not install Tiller") f.BoolVar(&i.dryRun, "dry-run", false, "Do not install local or remote") f.BoolVar(&i.skipRefresh, "skip-refresh", false, "Do not refresh (download) the local repository cache") + f.BoolVar(&i.skipRepos, "skip-repos", false, "Skip adding the stable and local repositories") f.BoolVar(&i.wait, "wait", false, "Block until Tiller is running and ready to receive requests") // TODO: replace TLS flags with pkg/helm/environment.AddFlagsTLS() in Helm 3 @@ -258,8 +260,14 @@ func (i *initCmd) run() error { return nil } - if err := installer.Initialize(i.home, i.out, i.skipRefresh, settings, stableRepositoryURL, localRepositoryURL); err != nil { - return fmt.Errorf("error initializing: %s", err) + if i.skipRepos { + if err := installer.InitializeWithoutRepos(i.home, i.out); err != nil { + return fmt.Errorf("error initializing: %s", err) + } + } else { + if err := installer.Initialize(i.home, i.out, i.skipRefresh, settings, stableRepositoryURL, localRepositoryURL); err != nil { + return fmt.Errorf("error initializing: %s", err) + } } fmt.Fprintf(i.out, "$HELM_HOME has been configured at %s.\n", settings.Home) diff --git a/cmd/helm/installer/init.go b/cmd/helm/installer/init.go index 7731a4a98..23cdd4716 100644 --- a/cmd/helm/installer/init.go +++ b/cmd/helm/installer/init.go @@ -51,6 +51,29 @@ func Initialize(home helmpath.Home, out io.Writer, skipRefresh bool, settings he return ensureRepoFileFormat(home.RepositoryFile(), out) } +// InitializeWithoutRepos initializes local config without adding repos +// +// Returns an error if the command failed. +func InitializeWithoutRepos(home helmpath.Home, out io.Writer) error { + if err := ensureDirectories(home, out); err != nil { + return err + } + + // Adding an empty repositories file + repoFile := home.RepositoryFile() + if fi, err := os.Stat(repoFile); err != nil { + fmt.Fprintf(out, "Creating %s \n", repoFile) + f := repo.NewRepoFile() + if err := f.WriteFile(repoFile, 0644); err != nil { + return err + } + } else if fi.IsDir() { + return fmt.Errorf("%s must be a file, not a directory", repoFile) + } + + return ensureRepoFileFormat(home.RepositoryFile(), out) +} + // ensureDirectories checks to see if $HELM_HOME exists. // // If $HELM_HOME does not exist, this function will create it. diff --git a/cmd/helm/installer/init_test.go b/cmd/helm/installer/init_test.go index 1d53687e6..7af6348b4 100644 --- a/cmd/helm/installer/init_test.go +++ b/cmd/helm/installer/init_test.go @@ -68,6 +68,41 @@ func TestInitialize(t *testing.T) { } } +func TestInitializeWithoutRepos(t *testing.T) { + home, err := ioutil.TempDir("", "helm_home") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(home) + + b := bytes.NewBuffer(nil) + hh := helmpath.Home(home) + + if err := InitializeWithoutRepos(hh, b); err != nil { + t.Error(err) + } + + expectedDirs := []string{hh.String(), hh.Repository(), hh.Cache()} + for _, dir := range expectedDirs { + if fi, err := os.Stat(dir); err != nil { + t.Errorf("%s", err) + } else if !fi.IsDir() { + t.Errorf("%s is not a directory", fi) + } + } + + if fi, err := os.Stat(hh.RepositoryFile()); err != nil { + t.Error(err) + } else if fi.IsDir() { + t.Errorf("%s should not be a directory", fi) + } + + // Make sure the local repository was not added + if fi, err := os.Stat(hh.LocalRepository(LocalRepositoryIndexFile)); err == nil { + t.Errorf("%s should not be found", fi) + } +} + func TestEnsureHome(t *testing.T) { home, err := ioutil.TempDir("", "helm_home") if err != nil { diff --git a/docs/helm/helm_init.md b/docs/helm/helm_init.md index 64f8bcf62..5917c325a 100644 --- a/docs/helm/helm_init.md +++ b/docs/helm/helm_init.md @@ -47,6 +47,7 @@ helm init [flags] --replicas int Amount of tiller instances to run on the cluster (default 1) --service-account string Name of service account --skip-refresh Do not refresh (download) the local repository cache + --skip-repos Skip adding the stable and local repositories --stable-repo-url string URL for stable repository (default "https://kubernetes-charts.storage.googleapis.com") -i, --tiller-image string Override Tiller image --tiller-tls Install Tiller with TLS enabled @@ -75,4 +76,4 @@ helm init [flags] * [helm](helm.md) - The Helm package manager for Kubernetes. -###### Auto generated by spf13/cobra on 16-May-2019 +###### Auto generated by spf13/cobra on 25-Jun-2020