Skip to content
Permalink
Browse files
feat(kube-run): add --kube-config, fix --kube-context opts
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Mar 22, 2022
1 parent 588eb2d commit 8014d982bc62c62e5fc031bc99ad32dbf70ef2c5
Showing with 23 additions and 5 deletions.
  1. +7 −3 cmd/werf/common/common.go
  2. +16 −2 cmd/werf/kube_run/kube_run.go
@@ -352,18 +352,22 @@ func SetupKubeConfig(cmdData *CmdData, cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(cmdData.KubeConfig, "kube-config", "", "", "Kubernetes config file path (default $WERF_KUBE_CONFIG, or $WERF_KUBECONFIG, or $KUBECONFIG)")

cmdData.KubeConfigPathMergeList = new([]string)
kubeConfigPathMergeListStr := getFirstExistingEnvVarAsString("WERF_KUBE_CONFIG", "WERF_KUBECONFIG", "KUBECONFIG")
kubeConfigPathMergeListStr := GetFirstExistingKubeConfigEnvVar()
for _, path := range filepath.SplitList(kubeConfigPathMergeListStr) {
*cmdData.KubeConfigPathMergeList = append(*cmdData.KubeConfigPathMergeList, path)
}
}

func GetFirstExistingKubeConfigEnvVar() string {
return GetFirstExistingEnvVarAsString("WERF_KUBE_CONFIG", "WERF_KUBECONFIG", "KUBECONFIG")
}

func SetupKubeConfigBase64(cmdData *CmdData, cmd *cobra.Command) {
cmdData.KubeConfigBase64 = new(string)
cmd.PersistentFlags().StringVarP(cmdData.KubeConfigBase64, "kube-config-base64", "", getFirstExistingEnvVarAsString("WERF_KUBE_CONFIG_BASE64", "WERF_KUBECONFIG_BASE64", "KUBECONFIG_BASE64"), "Kubernetes config data as base64 string (default $WERF_KUBE_CONFIG_BASE64 or $WERF_KUBECONFIG_BASE64 or $KUBECONFIG_BASE64)")
cmd.PersistentFlags().StringVarP(cmdData.KubeConfigBase64, "kube-config-base64", "", GetFirstExistingEnvVarAsString("WERF_KUBE_CONFIG_BASE64", "WERF_KUBECONFIG_BASE64", "KUBECONFIG_BASE64"), "Kubernetes config data as base64 string (default $WERF_KUBE_CONFIG_BASE64 or $WERF_KUBECONFIG_BASE64 or $KUBECONFIG_BASE64)")
}

func getFirstExistingEnvVarAsString(envNames ...string) string {
func GetFirstExistingEnvVarAsString(envNames ...string) string {
for _, envName := range envNames {
if v := os.Getenv(envName); v != "" {
return v
@@ -154,8 +154,8 @@ func NewCmd() *cobra.Command {
common.SetupLogProjectDir(&commonCmdData, cmd)

common.SetupSynchronization(&commonCmdData, cmd)
common.SetupKubeConfig(&commonCmdData, cmd)
// TODO(ilya-lesikov): doesn't work, need to be passed to `werf kubectl` somehow
// common.SetupKubeConfig(&commonCmdData, cmd)
// common.SetupKubeConfigBase64(&commonCmdData, cmd)
common.SetupKubeContext(&commonCmdData, cmd)

@@ -277,7 +277,7 @@ func runMain(ctx context.Context) error {

// TODO(ilya-lesikov): doesn't work, need to be passed to `werf kubectl` somehow
// common.SetupOndemandKubeInitializer(*commonCmdData.KubeContext, *commonCmdData.KubeConfig, *commonCmdData.KubeConfigBase64, *commonCmdData.KubeConfigPathMergeList)
common.SetupOndemandKubeInitializer(*commonCmdData.KubeContext, "", "", nil)
common.SetupOndemandKubeInitializer(*commonCmdData.KubeContext, *commonCmdData.KubeConfig, "", *commonCmdData.KubeConfigPathMergeList)
if err := common.GetOndemandKubeInitializer().Init(ctx); err != nil {
return err
}
@@ -421,6 +421,20 @@ func run(ctx context.Context, pod, secret, namespace string, werfConfig *config.
args = append(args, "-t")
}

if *commonCmdData.KubeContext != "" {
args = append(args, "--context", *commonCmdData.KubeContext)
}

if *commonCmdData.KubeConfig != "" {
args = append(args, "--kubeconfig", *commonCmdData.KubeConfig)
}

if kubeConf := common.GetFirstExistingKubeConfigEnvVar(); kubeConf != "" {
if err := os.Setenv("KUBECONFIG", kubeConf); err != nil {
return fmt.Errorf("unable to set $KUBECONFIG env var: %w", err)
}
}

var overrides map[string]interface{}
if cmdData.Overrides != "" {
if err := json.Unmarshal([]byte(cmdData.Overrides), &overrides); err != nil {

0 comments on commit 8014d98

Please sign in to comment.