Skip to content

Commit

Permalink
feat(kubectl): respect a few global $WERF_* env vars
Browse files Browse the repository at this point in the history
Make "werf kubectl" respect $WERF_KUBE_CONFIG, $WERF_KUBECONFIG, $WERF_KUBE_CONTEXT, $WERF_SKIP_TLS_VERIFY_REGISTRY.

Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Mar 22, 2022
1 parent ad3a705 commit a2d523e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions cmd/werf/kubectl/kubectl.go
@@ -1,11 +1,42 @@
package kubectl

import (
"fmt"
"os"

"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/cmd"
"k8s.io/kubectl/pkg/cmd/util"

"github.com/werf/werf/cmd/werf/common"
)

func NewCmd() *cobra.Command {
kubectlRootCmd := cmd.NewDefaultKubectlCommand()
return kubectlRootCmd
kubectlCmd := cmd.NewDefaultKubectlCommand()

kubeConfigFlag := kubectlCmd.Flag("kubeconfig")
kubeConfigFlag.Usage = "Path to the kubeconfig file to use for CLI requests (default $WERF_KUBE_CONFIG, or $WERF_KUBECONFIG, or $KUBECONFIG)"
if kubeConfigEnvVar := common.GetFirstExistingKubeConfigEnvVar(); kubeConfigEnvVar != "" {
if err := os.Setenv("KUBECONFIG", kubeConfigEnvVar); err != nil {
util.CheckErr(fmt.Errorf("unable to set $KUBECONFIG env var: %w", err))
}
}

kubeContextFlag := kubectlCmd.Flag("context")
kubeContextFlag.Usage = "The name of the kubeconfig context to use (default $WERF_KUBE_CONTEXT)"
if werfKubeContextEnvVar := os.Getenv("WERF_KUBE_CONTEXT"); werfKubeContextEnvVar != "" {
if err := kubectlCmd.Flags().Set("context", werfKubeContextEnvVar); err != nil {
util.CheckErr(fmt.Errorf("unable to set context flag for kubectl: %w", err))
}
}

skipTlsVerifyRegistryFlag := kubectlCmd.Flag("insecure-skip-tls-verify")
skipTlsVerifyRegistryFlag.Usage = "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure (default $WERF_SKIP_TLS_VERIFY_REGISTRY)"
if isSkipTlsVerifyRegistry := common.GetBoolEnvironmentDefaultFalse("WERF_SKIP_TLS_VERIFY_REGISTRY"); isSkipTlsVerifyRegistry {
if err := kubectlCmd.Flags().Set("insecure-skip-tls-verify", "true"); err != nil {
util.CheckErr(fmt.Errorf("unable to set insecure-skip-tls-verify flag for kubectl: %w", err))
}
}

return kubectlCmd
}

0 comments on commit a2d523e

Please sign in to comment.