feat<all> expose .namespace public as Namespace, to provide easier integration for non-cli projects

Signed-off-by: mike.du <duliyang@hotmail.com>
pull/7037/head
mike.du 6 years ago
parent ff57dc608b
commit a772076456

@ -67,7 +67,7 @@ func main() {
actionConfig := new(action.Configuration) actionConfig := new(action.Configuration)
cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:]) cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:])
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), debug); err != nil { if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace, os.Getenv("HELM_DRIVER"), debug); err != nil {
debug("%+v", err) debug("%+v", err)
os.Exit(1) os.Exit(1)
} }

@ -205,7 +205,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
} }
} }
client.Namespace = settings.Namespace() client.Namespace = settings.Namespace
return client.Run(chartRequested, vals) return client.Run(chartRequested, vals)
} }

@ -186,4 +186,17 @@ func TestInstall(t *testing.T) {
} }
runTestActionCmd(t, tests) runTestActionCmd(t, tests)
settings.Namespace = "halo"
nsTests := []cmdTestCase{
// Install to halo ns without --namespace flag
{
name: "install to halo ns without --namespace flag",
cmd: "install halo testdata/testcharts/empty",
//wantError: true,
golden: "output/install-with-ns-setting.txt",
},
}
runTestActionCmd(t, nsTests)
} }

@ -51,7 +51,7 @@ func newLintCmd(out io.Writer) *cobra.Command {
if len(args) > 0 { if len(args) > 0 {
paths = args paths = args
} }
client.Namespace = settings.Namespace() client.Namespace = settings.Namespace
vals, err := valueOpts.MergeValues(getter.All(settings)) vals, err := valueOpts.MergeValues(getter.All(settings))
if err != nil { if err != nil {
return err return err

@ -46,7 +46,7 @@ func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command
Long: releaseTestHelp, Long: releaseTestHelp,
Args: require.ExactArgs(1), Args: require.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
client.Namespace = settings.Namespace() client.Namespace = settings.Namespace
rel, runErr := client.Run(args[0]) rel, runErr := client.Run(args[0])
// We only return an error if we weren't even able to get the // We only return an error if we weren't even able to get the
// release, otherwise we keep going so we can print status and logs // release, otherwise we keep going so we can print status and logs

@ -0,0 +1,6 @@
NAME: halo
LAST DEPLOYED: Fri Sep 2 22:04:05 1977
NAMESPACE: halo
STATUS: deployed
REVISION: 1
TEST SUITE: None

@ -71,7 +71,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
Long: upgradeDesc, Long: upgradeDesc,
Args: require.ExactArgs(2), Args: require.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
client.Namespace = settings.Namespace() client.Namespace = settings.Namespace
if client.Version == "" && client.Devel { if client.Version == "" && client.Devel {
debug("setting version to >0.0.0-0") debug("setting version to >0.0.0-0")

@ -38,7 +38,7 @@ import (
// EnvSettings describes all of the environment settings. // EnvSettings describes all of the environment settings.
type EnvSettings struct { type EnvSettings struct {
namespace string Namespace string
config genericclioptions.RESTClientGetter config genericclioptions.RESTClientGetter
configOnce sync.Once configOnce sync.Once
@ -61,7 +61,7 @@ type EnvSettings struct {
func New() *EnvSettings { func New() *EnvSettings {
env := EnvSettings{ env := EnvSettings{
namespace: os.Getenv("HELM_NAMESPACE"), Namespace: os.Getenv("HELM_NAMESPACE"),
KubeContext: os.Getenv("HELM_KUBECONTEXT"), KubeContext: os.Getenv("HELM_KUBECONTEXT"),
PluginsDirectory: envOr("HELM_PLUGINS", helmpath.DataPath("plugins")), PluginsDirectory: envOr("HELM_PLUGINS", helmpath.DataPath("plugins")),
RegistryConfig: envOr("HELM_REGISTRY_CONFIG", helmpath.ConfigPath("registry.json")), RegistryConfig: envOr("HELM_REGISTRY_CONFIG", helmpath.ConfigPath("registry.json")),
@ -74,7 +74,7 @@ func New() *EnvSettings {
// AddFlags binds flags to the given flagset. // AddFlags binds flags to the given flagset.
func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
fs.StringVarP(&s.namespace, "namespace", "n", s.namespace, "namespace scope for this request") fs.StringVarP(&s.Namespace, "namespace", "n", s.Namespace, "namespace scope for this request")
fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file") fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to the kubeconfig file")
fs.StringVar(&s.KubeContext, "kube-context", s.KubeContext, "name of the kubeconfig context to use") fs.StringVar(&s.KubeContext, "kube-context", s.KubeContext, "name of the kubeconfig context to use")
fs.BoolVar(&s.Debug, "debug", s.Debug, "enable verbose output") fs.BoolVar(&s.Debug, "debug", s.Debug, "enable verbose output")
@ -98,7 +98,7 @@ func (s *EnvSettings) EnvVars() map[string]string {
"HELM_REGISTRY_CONFIG": s.RegistryConfig, "HELM_REGISTRY_CONFIG": s.RegistryConfig,
"HELM_REPOSITORY_CACHE": s.RepositoryCache, "HELM_REPOSITORY_CACHE": s.RepositoryCache,
"HELM_REPOSITORY_CONFIG": s.RepositoryConfig, "HELM_REPOSITORY_CONFIG": s.RepositoryConfig,
"HELM_NAMESPACE": s.Namespace(), "HELM_NAMESPACE": s.Namespace,
"HELM_KUBECONTEXT": s.KubeContext, "HELM_KUBECONTEXT": s.KubeContext,
} }
@ -109,27 +109,24 @@ func (s *EnvSettings) EnvVars() map[string]string {
return envvars return envvars
} }
//Namespace gets the namespace from the configuration //GetNamespace return the namespace from configuration
func (s *EnvSettings) Namespace() string { func (s *EnvSettings) GetNamespace() string {
if s.namespace != "" {
return s.namespace if s.Namespace != "" {
return s.Namespace
} }
if ns, _, err := s.RESTClientGetter().ToRawKubeConfigLoader().Namespace(); err == nil { if ns, _, err := s.RESTClientGetter().ToRawKubeConfigLoader().Namespace(); err == nil {
return ns return ns
} }
return "default"
}
//set the namespace of envsetting, provide easier integration for non-cli projects return "default"
func (s *EnvSettings) SetNameSpace(ns string) {
s.namespace = ns
} }
//RESTClientGetter gets the kubeconfig from EnvSettings //RESTClientGetter gets the kubeconfig from EnvSettings
func (s *EnvSettings) RESTClientGetter() genericclioptions.RESTClientGetter { func (s *EnvSettings) RESTClientGetter() genericclioptions.RESTClientGetter {
s.configOnce.Do(func() { s.configOnce.Do(func() {
s.config = kube.GetConfig(s.KubeConfig, s.KubeContext, s.namespace) s.config = kube.GetConfig(s.KubeConfig, s.KubeContext, s.Namespace)
}) })
return s.config return s.config
} }

@ -78,8 +78,8 @@ func TestEnvSettings(t *testing.T) {
if settings.Debug != tt.debug { if settings.Debug != tt.debug {
t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug) t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug)
} }
if settings.Namespace() != tt.ns { if settings.GetNamespace() != tt.ns {
t.Errorf("expected namespace %q, got %q", tt.ns, settings.Namespace()) t.Errorf("expected namespace %q, got %q", tt.ns, settings.GetNamespace())
} }
if settings.KubeContext != tt.kcontext { if settings.KubeContext != tt.kcontext {
t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext) t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext)

Loading…
Cancel
Save