|
|
@ -26,21 +26,17 @@ import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
|
|
|
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
|
|
|
|
|
|
|
|
"helm.sh/helm/v3/pkg/helmpath"
|
|
|
|
"helm.sh/helm/v3/pkg/helmpath"
|
|
|
|
"helm.sh/helm/v3/pkg/kube"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 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.ConfigFlags
|
|
|
|
configOnce sync.Once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// KubeConfig is the path to the kubeconfig file
|
|
|
|
// KubeConfig is the path to the kubeconfig file
|
|
|
|
KubeConfig string
|
|
|
|
KubeConfig string
|
|
|
@ -63,8 +59,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"),
|
|
|
|
KubeToken: os.Getenv("HELM_KUBETOKEN"),
|
|
|
|
KubeToken: os.Getenv("HELM_KUBETOKEN"),
|
|
|
@ -75,7 +70,16 @@ func New() *EnvSettings {
|
|
|
|
RepositoryCache: envOr("HELM_REPOSITORY_CACHE", helmpath.CachePath("repository")),
|
|
|
|
RepositoryCache: envOr("HELM_REPOSITORY_CACHE", helmpath.CachePath("repository")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
env.Debug, _ = strconv.ParseBool(os.Getenv("HELM_DEBUG"))
|
|
|
|
env.Debug, _ = strconv.ParseBool(os.Getenv("HELM_DEBUG"))
|
|
|
|
return &env
|
|
|
|
|
|
|
|
|
|
|
|
// bind to kubernetes config flags
|
|
|
|
|
|
|
|
env.config = &genericclioptions.ConfigFlags{
|
|
|
|
|
|
|
|
Namespace: &env.namespace,
|
|
|
|
|
|
|
|
Context: &env.KubeContext,
|
|
|
|
|
|
|
|
BearerToken: &env.KubeToken,
|
|
|
|
|
|
|
|
APIServer: &env.KubeAPIServer,
|
|
|
|
|
|
|
|
KubeConfig: &env.KubeConfig,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AddFlags binds flags to the given flagset.
|
|
|
|
// AddFlags binds flags to the given flagset.
|
|
|
@ -107,42 +111,27 @@ func (s *EnvSettings) EnvVars() map[string]string {
|
|
|
|
"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(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// broken, these are populated from helm flags and not kubeconfig.
|
|
|
|
"HELM_KUBECONTEXT": s.KubeContext,
|
|
|
|
"HELM_KUBECONTEXT": s.KubeContext,
|
|
|
|
"HELM_KUBETOKEN": s.KubeToken,
|
|
|
|
"HELM_KUBETOKEN": s.KubeToken,
|
|
|
|
"HELM_KUBEAPISERVER": s.KubeAPIServer,
|
|
|
|
"HELM_KUBEAPISERVER": s.KubeAPIServer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if s.KubeConfig != "" {
|
|
|
|
if s.KubeConfig != "" {
|
|
|
|
envvars["KUBECONFIG"] = s.KubeConfig
|
|
|
|
envvars["KUBECONFIG"] = s.KubeConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return envvars
|
|
|
|
return envvars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Namespace gets the namespace from the configuration
|
|
|
|
// Namespace gets the namespace from the configuration
|
|
|
|
func (s *EnvSettings) Namespace() string {
|
|
|
|
func (s *EnvSettings) Namespace() string {
|
|
|
|
if s.namespace != "" {
|
|
|
|
if ns, _, err := s.config.ToRawKubeConfigLoader().Namespace(); err == nil {
|
|
|
|
return s.namespace
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ns, _, err := s.RESTClientGetter().ToRawKubeConfigLoader().Namespace(); err == nil {
|
|
|
|
|
|
|
|
return ns
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "default"
|
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//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() {
|
|
|
|
|
|
|
|
clientConfig := kube.GetConfig(s.KubeConfig, s.KubeContext, s.namespace)
|
|
|
|
|
|
|
|
if s.KubeToken != "" {
|
|
|
|
|
|
|
|
clientConfig.BearerToken = &s.KubeToken
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.KubeAPIServer != "" {
|
|
|
|
|
|
|
|
clientConfig.APIServer = &s.KubeAPIServer
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.config = clientConfig
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return s.config
|
|
|
|
return s.config
|
|
|
|
}
|
|
|
|
}
|
|
|
|