diff --git a/pkg/cli/settings.go b/pkg/cli/settings.go index c2e5d189a..268f86d6f 100644 --- a/pkg/cli/settings.go +++ b/pkg/cli/settings.go @@ -1,3 +1,19 @@ +/* +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 cli import ( @@ -9,9 +25,11 @@ import ( "strconv" ) -// Settings describes all of the settings required by the Helm client. +// Settings describes all of the configuration options required by the Helm client. type Settings struct { - Namespace string + // The Kubernetes namespace + Namespace string + // The Helm driver ("memory", "secret", or "configmap") HelmDriver string // KubeConfig is the path to the kubeconfig file KubeConfig string @@ -32,6 +50,7 @@ type Settings struct { // PluginsDirectory is the path to the plugins directory. PluginsDirectory string + // Kubernetes configuration flags config *genericclioptions.ConfigFlags } @@ -114,7 +133,3 @@ func (s *Settings) EnvVars() map[string]string { } return envvars } - -func (s *Settings) validate() error { - return nil -} diff --git a/pkg/cli/validate.go b/pkg/cli/validate.go new file mode 100644 index 000000000..67077efc3 --- /dev/null +++ b/pkg/cli/validate.go @@ -0,0 +1,75 @@ +/* +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 cli + +import "fmt" + +type MissingConfigError struct { + string +} + +func (e MissingConfigError) Error() string { + return fmt.Sprintf("missing config error: %s param missing from client configuration", e.string) +} + +// Checks whether the error yielded by the Validate function contains MissingConfigErrors. +func HasMissingConfigErrors(errs []error) bool { + return len(errs) > 0 +} + +// Ensures that the required fields are set on the Settings struct and returns a list of MissingConfigError +// for all missing fields. +func (s *Settings) Validate() []error { + errs := make([]error, 0) + + if s.Namespace == "" { + appendError(&errs, "Namespace") + } + if s.HelmDriver == "" { + appendError(&errs, "HelmDriver") + } + if s.KubeConfig == "" { + appendError(&errs, "KubeConfig") + } + if s.KubeContext == "" { + appendError(&errs, "KubeContext") + } + if s.KubeToken == "" { + appendError(&errs, "KubeToken") + } + if s.KubeAPIServer == "" { + appendError(&errs, "KubeAPIServer") + } + if s.RegistryConfig == "" { + appendError(&errs, "RegistryConfig") + } + if s.RepositoryConfig == "" { + appendError(&errs, "RepositoryConfig") + } + if s.RepositoryCache == "" { + appendError(&errs, "RepositoryCache") + } + if s.PluginsDirectory == "" { + appendError(&errs, "RepositoryCache") + } + + return errs +} + +func appendError(errs *[]error, field string) { + *errs = append(*errs, MissingConfigError{field}) +} diff --git a/pkg/cli/validate_test.go b/pkg/cli/validate_test.go new file mode 100644 index 000000000..d2e83d285 --- /dev/null +++ b/pkg/cli/validate_test.go @@ -0,0 +1,46 @@ +/* +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 cli + +import "testing" + +func TestSettingsValidation(t *testing.T) { + tests := []struct { + name string + + // input + settings Settings + + // expected + expectedErrs []error + }{ + {settings: *SettingsFromEnv(), expectedErrs: []error{}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var errsEqual = func(es1, es2 []error) bool { + return true + } + + errs := tt.settings.Validate() + if !errsEqual(errs, tt.expectedErrs) { + t.Errorf("expected errors %v, got %v", tt.expectedErrs, errs) + } + }) + } +} diff --git a/pkg/downloader/chart_downloader_test.go b/pkg/downloader/chart_downloader_test.go index abfb007ff..309a6b0fd 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -59,7 +59,7 @@ func TestResolveChartRef(t *testing.T) { Out: os.Stderr, RepositoryConfig: repoConfig, RepositoryCache: repoCache, - Getters: getter.All(&cli.EnvSettings{ + Getters: getter.All(&cli.Settings{ RepositoryConfig: repoConfig, RepositoryCache: repoCache, }), @@ -99,7 +99,7 @@ func TestResolveChartOpts(t *testing.T) { Out: os.Stderr, RepositoryConfig: repoConfig, RepositoryCache: repoCache, - Getters: getter.All(&cli.EnvSettings{ + Getters: getter.All(&cli.Settings{ RepositoryConfig: repoConfig, RepositoryCache: repoCache, }), @@ -199,7 +199,7 @@ func TestDownloadTo(t *testing.T) { Keyring: "testdata/helm-test-key.pub", RepositoryConfig: repoConfig, RepositoryCache: repoCache, - Getters: getter.All(&cli.EnvSettings{ + Getters: getter.All(&cli.Settings{ RepositoryConfig: repoConfig, RepositoryCache: repoCache, }), @@ -252,7 +252,7 @@ func TestDownloadTo_TLS(t *testing.T) { Keyring: "testdata/helm-test-key.pub", RepositoryConfig: repoConfig, RepositoryCache: repoCache, - Getters: getter.All(&cli.EnvSettings{ + Getters: getter.All(&cli.Settings{ RepositoryConfig: repoConfig, RepositoryCache: repoCache, }), @@ -299,7 +299,7 @@ func TestDownloadTo_VerifyLater(t *testing.T) { Verify: VerifyLater, RepositoryConfig: repoConfig, RepositoryCache: repoCache, - Getters: getter.All(&cli.EnvSettings{ + Getters: getter.All(&cli.Settings{ RepositoryConfig: repoConfig, RepositoryCache: repoCache, }), @@ -328,7 +328,7 @@ func TestScanReposForURL(t *testing.T) { Verify: VerifyLater, RepositoryConfig: repoConfig, RepositoryCache: repoCache, - Getters: getter.All(&cli.EnvSettings{ + Getters: getter.All(&cli.Settings{ RepositoryConfig: repoConfig, RepositoryCache: repoCache, }), diff --git a/pkg/plugin/plugin_test.go b/pkg/plugin/plugin_test.go index af0b61846..6f44ee16a 100644 --- a/pkg/plugin/plugin_test.go +++ b/pkg/plugin/plugin_test.go @@ -305,7 +305,7 @@ func TestSetupEnv(t *testing.T) { name := "pequod" base := filepath.Join("testdata/helmhome/helm/plugins", name) - s := cli.New() + s := cli.SettingsFromEnv() s.PluginsDirectory = "testdata/helmhome/helm/plugins" SetupPluginEnv(s, name, base) diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index f50d6a2b6..e1f44f247 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -45,7 +45,7 @@ func TestLoadChartRepository(t *testing.T) { r, err := NewChartRepository(&Entry{ Name: testRepository, URL: testURL, - }, getter.All(&cli.EnvSettings{})) + }, getter.All(&cli.Settings{})) if err != nil { t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) } @@ -78,7 +78,7 @@ func TestIndex(t *testing.T) { r, err := NewChartRepository(&Entry{ Name: testRepository, URL: testURL, - }, getter.All(&cli.EnvSettings{})) + }, getter.All(&cli.Settings{})) if err != nil { t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) } @@ -283,7 +283,7 @@ func TestFindChartInRepoURL(t *testing.T) { } defer srv.Close() - chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(&cli.EnvSettings{})) + chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(&cli.Settings{})) if err != nil { t.Fatalf("%v", err) } @@ -291,7 +291,7 @@ func TestFindChartInRepoURL(t *testing.T) { t.Errorf("%s is not the valid URL", chartURL) } - chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(&cli.EnvSettings{})) + chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(&cli.Settings{})) if err != nil { t.Errorf("%s", err) } @@ -302,7 +302,7 @@ func TestFindChartInRepoURL(t *testing.T) { func TestErrorFindChartInRepoURL(t *testing.T) { - g := getter.All(&cli.EnvSettings{ + g := getter.All(&cli.Settings{ RepositoryCache: ensure.TempDir(t), }) diff --git a/pkg/repo/index_test.go b/pkg/repo/index_test.go index 466a2c306..14813231e 100644 --- a/pkg/repo/index_test.go +++ b/pkg/repo/index_test.go @@ -159,7 +159,7 @@ func TestDownloadIndexFile(t *testing.T) { r, err := NewChartRepository(&Entry{ Name: testRepo, URL: srv.URL, - }, getter.All(&cli.EnvSettings{})) + }, getter.All(&cli.Settings{})) if err != nil { t.Errorf("Problem creating chart repository from %s: %v", testRepo, err) } @@ -217,7 +217,7 @@ func TestDownloadIndexFile(t *testing.T) { r, err := NewChartRepository(&Entry{ Name: testRepo, URL: srv.URL + chartRepoURLPath, - }, getter.All(&cli.EnvSettings{})) + }, getter.All(&cli.Settings{})) if err != nil { t.Errorf("Problem creating chart repository from %s: %v", testRepo, err) }