diff --git a/cmd/werf/build/main.go b/cmd/werf/build/main.go index 4bec415e9f..1c1e567584 100644 --- a/cmd/werf/build/main.go +++ b/cmd/werf/build/main.go @@ -75,8 +75,8 @@ If one or more IMAGE_NAME parameters specified, werf will build only these image common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/bundle/apply/apply.go b/cmd/werf/bundle/apply/apply.go index 0249d6cbad..316f50d27c 100644 --- a/cmd/werf/bundle/apply/apply.go +++ b/cmd/werf/bundle/apply/apply.go @@ -58,8 +58,8 @@ func NewCmd() *cobra.Command { } common.SetupEnvironment(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupStagesStorageOptions(&commonCmdData, cmd) // FIXME common.SetupFinalStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/bundle/download/download.go b/cmd/werf/bundle/download/download.go index 42b640c1b5..525860c413 100644 --- a/cmd/werf/bundle/download/download.go +++ b/cmd/werf/bundle/download/download.go @@ -43,8 +43,8 @@ func NewCmd() *cobra.Command { }, } - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupInsecureRegistry(&commonCmdData, cmd) common.SetupInsecureHelmDependencies(&commonCmdData, cmd) diff --git a/cmd/werf/bundle/export/export.go b/cmd/werf/bundle/export/export.go index e4607cd81e..cd9a436558 100644 --- a/cmd/werf/bundle/export/export.go +++ b/cmd/werf/bundle/export/export.go @@ -72,8 +72,8 @@ func NewCmd() *cobra.Command { common.SetupConfigPath(&commonCmdData, cmd) common.SetupEnvironment(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupIntrospectAfterError(&commonCmdData, cmd) diff --git a/cmd/werf/bundle/publish/publish.go b/cmd/werf/bundle/publish/publish.go index d392eb1d6f..c9cf4d9fdb 100644 --- a/cmd/werf/bundle/publish/publish.go +++ b/cmd/werf/bundle/publish/publish.go @@ -78,8 +78,8 @@ Published into container registry bundle can be rolled out by the "werf bundle" common.SetupConfigPath(&commonCmdData, cmd) common.SetupEnvironment(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupIntrospectAfterError(&commonCmdData, cmd) diff --git a/cmd/werf/bundle/render/render.go b/cmd/werf/bundle/render/render.go index 5dde1ea987..a2f5c78dff 100644 --- a/cmd/werf/bundle/render/render.go +++ b/cmd/werf/bundle/render/render.go @@ -61,8 +61,8 @@ func NewCmd() *cobra.Command { } common.SetupEnvironment(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupStagesStorageOptions(&commonCmdData, cmd) common.SetupFinalStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/ci_env/ci_env.go b/cmd/werf/ci_env/ci_env.go index eb9d8c8d57..b9256867fe 100644 --- a/cmd/werf/ci_env/ci_env.go +++ b/cmd/werf/ci_env/ci_env.go @@ -66,8 +66,8 @@ Currently supported only GitLab (gitlab) and GitHub (github) CI systems`, common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDockerConfig(&commonCmdData, cmd, "Command will copy specified or default (~/.docker) config to the temporary directory and may perform additional login with new config.") common.SetupPlatform(&commonCmdData, cmd) diff --git a/cmd/werf/cleanup/cleanup.go b/cmd/werf/cleanup/cleanup.go index abe59e09d6..e7d811bbb2 100644 --- a/cmd/werf/cleanup/cleanup.go +++ b/cmd/werf/cleanup/cleanup.go @@ -59,8 +59,8 @@ It is safe to run this command periodically (daily is enough) by automated clean common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) common.SetupCacheStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/common/common.go b/cmd/werf/common/common.go index 03a469d861..0d63459c28 100644 --- a/cmd/werf/common/common.go +++ b/cmd/werf/common/common.go @@ -11,6 +11,7 @@ import ( "time" "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/werf/logboek" "github.com/werf/logboek/pkg/level" @@ -169,9 +170,13 @@ func SetupConfigTemplatesDir(cmdData *CmdData, cmd *cobra.Command) { cmd.Flags().StringVarP(cmdData.ConfigTemplatesDir, "config-templates-dir", "", os.Getenv("WERF_CONFIG_TEMPLATES_DIR"), `Custom configuration templates directory (default $WERF_CONFIG_TEMPLATES_DIR or .werf in working directory)`) } -func SetupTmpDir(cmdData *CmdData, cmd *cobra.Command) { +type SetupTmpDirOptions struct { + Persistent bool +} + +func SetupTmpDir(cmdData *CmdData, cmd *cobra.Command, opts SetupTmpDirOptions) { cmdData.TmpDir = new(string) - cmd.Flags().StringVarP(cmdData.TmpDir, "tmp-dir", "", "", "Use specified dir to store tmp files and dirs (default $WERF_TMP_DIR or system tmp dir)") + getFlags(cmd, opts.Persistent).StringVarP(cmdData.TmpDir, "tmp-dir", "", "", "Use specified dir to store tmp files and dirs (default $WERF_TMP_DIR or system tmp dir)") } func SetupGiterminismOptions(cmdData *CmdData, cmd *cobra.Command) { @@ -210,9 +215,13 @@ func setupDevBranch(cmdData *CmdData, cmd *cobra.Command) { cmd.Flags().StringVarP(cmdData.DevBranch, "dev-branch", "", defaultValue, fmt.Sprintf("Set dev git branch name (default $WERF_DEV_BRANCH or %q)", defaultValue)) } -func SetupHomeDir(cmdData *CmdData, cmd *cobra.Command) { +type SetupHomeDirOptions struct { + Persistent bool +} + +func SetupHomeDir(cmdData *CmdData, cmd *cobra.Command, opts SetupHomeDirOptions) { cmdData.HomeDir = new(string) - cmd.Flags().StringVarP(cmdData.HomeDir, "home-dir", "", "", "Use specified dir to store werf cache files and dirs (default $WERF_HOME or ~/.werf)") + getFlags(cmd, opts.Persistent).StringVarP(cmdData.HomeDir, "home-dir", "", "", "Use specified dir to store werf cache files and dirs (default $WERF_HOME or ~/.werf)") } func SetupSSHKey(cmdData *CmdData, cmd *cobra.Command) { @@ -364,7 +373,11 @@ func GetFirstExistingKubeConfigEnvVar() string { 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", "", GetFirstExistingKubeConfigBase64EnvVar(), "Kubernetes config data as base64 string (default $WERF_KUBE_CONFIG_BASE64 or $WERF_KUBECONFIG_BASE64 or $KUBECONFIG_BASE64)") +} + +func GetFirstExistingKubeConfigBase64EnvVar() string { + return GetFirstExistingEnvVarAsString("WERF_KUBE_CONFIG_BASE64", "WERF_KUBECONFIG_BASE64", "KUBECONFIG_BASE64") } func GetFirstExistingEnvVarAsString(envNames ...string) string { @@ -1599,3 +1612,11 @@ func GetBackgroundOutputFile() string { func GetLastBackgroundErrorFile() string { return filepath.Join(werf.GetServiceDir(), "last_background_error") } + +func getFlags(cmd *cobra.Command, persistent bool) *pflag.FlagSet { + if persistent { + return cmd.PersistentFlags() + } + + return cmd.Flags() +} diff --git a/cmd/werf/compose/main.go b/cmd/werf/compose/main.go index d1cbce7b52..0e2a2829f3 100644 --- a/cmd/werf/compose/main.go +++ b/cmd/werf/compose/main.go @@ -204,8 +204,8 @@ services: common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/config/graph/graph.go b/cmd/werf/config/graph/graph.go index 59c844d61b..e60af6e5d5 100644 --- a/cmd/werf/config/graph/graph.go +++ b/cmd/werf/config/graph/graph.go @@ -112,8 +112,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupLogOptions(&commonCmdData, cmd) diff --git a/cmd/werf/config/list/list.go b/cmd/werf/config/list/list.go index 4c8b20ce8d..6b29770001 100644 --- a/cmd/werf/config/list/list.go +++ b/cmd/werf/config/list/list.go @@ -44,8 +44,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupLogOptions(&commonCmdData, cmd) diff --git a/cmd/werf/config/render/render.go b/cmd/werf/config/render/render.go index e22130e864..e3fa811867 100644 --- a/cmd/werf/config/render/render.go +++ b/cmd/werf/config/render/render.go @@ -74,8 +74,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupLogOptions(&commonCmdData, cmd) diff --git a/cmd/werf/converge/converge.go b/cmd/werf/converge/converge.go index 73f017ddf1..a4ab74677e 100644 --- a/cmd/werf/converge/converge.go +++ b/cmd/werf/converge/converge.go @@ -92,8 +92,8 @@ werf converge --repo registry.mydomain.com/web --env production`, common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupIntrospectAfterError(&commonCmdData, cmd) diff --git a/cmd/werf/dismiss/dismiss.go b/cmd/werf/dismiss/dismiss.go index 51f2336e9b..c7454e96d6 100644 --- a/cmd/werf/dismiss/dismiss.go +++ b/cmd/werf/dismiss/dismiss.go @@ -70,14 +70,14 @@ Read more info about Helm Release name, Kubernetes Namespace and how to change i }, } - common.SetupTmpDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) common.SetupConfigTemplatesDir(&commonCmdData, cmd) common.SetupConfigPath(&commonCmdData, cmd) common.SetupEnvironment(&commonCmdData, cmd) common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDir(&commonCmdData, cmd) common.SetupGitWorkTree(&commonCmdData, cmd) diff --git a/cmd/werf/export/export.go b/cmd/werf/export/export.go index 21e133819e..555dcfd990 100644 --- a/cmd/werf/export/export.go +++ b/cmd/werf/export/export.go @@ -69,8 +69,8 @@ All meta-information related to werf is removed from the exported images, and th common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/get_autogenerated_values.go b/cmd/werf/helm/get_autogenerated_values.go index 4f86775504..19492be97a 100644 --- a/cmd/werf/helm/get_autogenerated_values.go +++ b/cmd/werf/helm/get_autogenerated_values.go @@ -57,8 +57,8 @@ These values includes project name, docker images ids and other`), common.SetupGiterminismOptions(&getAutogeneratedValuedCmdData, cmd) - common.SetupTmpDir(&getAutogeneratedValuedCmdData, cmd) - common.SetupHomeDir(&getAutogeneratedValuedCmdData, cmd) + common.SetupTmpDir(&getAutogeneratedValuedCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&getAutogeneratedValuedCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&getAutogeneratedValuedCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&getAutogeneratedValuedCmdData, cmd) diff --git a/cmd/werf/helm/get_namespace.go b/cmd/werf/helm/get_namespace.go index f3b13e9039..bb4dd5c968 100644 --- a/cmd/werf/helm/get_namespace.go +++ b/cmd/werf/helm/get_namespace.go @@ -38,8 +38,8 @@ func NewGetNamespaceCmd() *cobra.Command { common.SetupGiterminismOptions(&getNamespaceCmdData, cmd) - common.SetupTmpDir(&getNamespaceCmdData, cmd) - common.SetupHomeDir(&getNamespaceCmdData, cmd) + common.SetupTmpDir(&getNamespaceCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&getNamespaceCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDockerConfig(&getNamespaceCmdData, cmd, "") common.SetupLogOptions(&getNamespaceCmdData, cmd) diff --git a/cmd/werf/helm/get_release.go b/cmd/werf/helm/get_release.go index b81d8c0669..4194cf52b4 100644 --- a/cmd/werf/helm/get_release.go +++ b/cmd/werf/helm/get_release.go @@ -39,8 +39,8 @@ func NewGetReleaseCmd() *cobra.Command { common.SetupGiterminismOptions(&getReleaseCmdData, cmd) - common.SetupTmpDir(&getReleaseCmdData, cmd) - common.SetupHomeDir(&getReleaseCmdData, cmd) + common.SetupTmpDir(&getReleaseCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&getReleaseCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDockerConfig(&getReleaseCmdData, cmd, "") common.SetupLogOptions(&getReleaseCmdData, cmd) diff --git a/cmd/werf/helm/helm.go b/cmd/werf/helm/helm.go index c54a0b847a..3c33cb951a 100644 --- a/cmd/werf/helm/helm.go +++ b/cmd/werf/helm/helm.go @@ -53,8 +53,8 @@ func NewCmd() *cobra.Command { os.Setenv("HELM_EXPERIMENTAL_OCI", "1") cmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", *helm_v3.Settings.GetNamespaceP(), "namespace scope for this request") - common.SetupTmpDir(&_commonCmdData, cmd) - common.SetupHomeDir(&_commonCmdData, cmd) + common.SetupTmpDir(&_commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&_commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupKubeConfig(&_commonCmdData, cmd) common.SetupKubeConfigBase64(&_commonCmdData, cmd) common.SetupKubeContext(&_commonCmdData, cmd) diff --git a/cmd/werf/helm/migrate2to3.go b/cmd/werf/helm/migrate2to3.go index 9518310835..79e42cb84d 100644 --- a/cmd/werf/helm/migrate2to3.go +++ b/cmd/werf/helm/migrate2to3.go @@ -78,8 +78,8 @@ func NewMigrate2To3Cmd() *cobra.Command { }, } - common.SetupTmpDir(&migrate2To3CommonCmdData, cmd) - common.SetupHomeDir(&migrate2To3CommonCmdData, cmd) + common.SetupTmpDir(&migrate2To3CommonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&migrate2To3CommonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupKubeConfig(&migrate2To3CommonCmdData, cmd) common.SetupKubeConfigBase64(&migrate2To3CommonCmdData, cmd) diff --git a/cmd/werf/helm/secret/decrypt/decrypt.go b/cmd/werf/helm/secret/decrypt/decrypt.go index 83fee37780..a393e78ac8 100644 --- a/cmd/werf/helm/secret/decrypt/decrypt.go +++ b/cmd/werf/helm/secret/decrypt/decrypt.go @@ -53,8 +53,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/encrypt/encrypt.go b/cmd/werf/helm/secret/encrypt/encrypt.go index e21c052a4b..f9781080e5 100644 --- a/cmd/werf/helm/secret/encrypt/encrypt.go +++ b/cmd/werf/helm/secret/encrypt/encrypt.go @@ -52,8 +52,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/file/decrypt/decrypt.go b/cmd/werf/helm/secret/file/decrypt/decrypt.go index c611cf1f9c..6a4a6e9c60 100644 --- a/cmd/werf/helm/secret/file/decrypt/decrypt.go +++ b/cmd/werf/helm/secret/file/decrypt/decrypt.go @@ -62,8 +62,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/file/edit/edit.go b/cmd/werf/helm/secret/file/edit/edit.go index c1f331a214..ef9dd48abd 100644 --- a/cmd/werf/helm/secret/file/edit/edit.go +++ b/cmd/werf/helm/secret/file/edit/edit.go @@ -43,8 +43,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/file/encrypt/encrypt.go b/cmd/werf/helm/secret/file/encrypt/encrypt.go index e6d342822f..f63a97c8e2 100644 --- a/cmd/werf/helm/secret/file/encrypt/encrypt.go +++ b/cmd/werf/helm/secret/file/encrypt/encrypt.go @@ -58,8 +58,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/rotate_secret_key/rotate_secret_key.go b/cmd/werf/helm/secret/rotate_secret_key/rotate_secret_key.go index 307c5ecc5a..40fa2f1f5f 100644 --- a/cmd/werf/helm/secret/rotate_secret_key/rotate_secret_key.go +++ b/cmd/werf/helm/secret/rotate_secret_key/rotate_secret_key.go @@ -50,8 +50,8 @@ Command will extract data with the old key, generate new secret data and rewrite }, } - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDir(&commonCmdData, cmd) common.SetupGitWorkTree(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/values/decrypt/decrypt.go b/cmd/werf/helm/secret/values/decrypt/decrypt.go index 37619c22c0..f2fa6dab51 100644 --- a/cmd/werf/helm/secret/values/decrypt/decrypt.go +++ b/cmd/werf/helm/secret/values/decrypt/decrypt.go @@ -67,8 +67,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/values/edit/edit.go b/cmd/werf/helm/secret/values/edit/edit.go index 2604636c4c..cb5fbfab8c 100644 --- a/cmd/werf/helm/secret/values/edit/edit.go +++ b/cmd/werf/helm/secret/values/edit/edit.go @@ -43,8 +43,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/helm/secret/values/encrypt/encrypt.go b/cmd/werf/helm/secret/values/encrypt/encrypt.go index 448f2a930c..708bf70b74 100644 --- a/cmd/werf/helm/secret/values/encrypt/encrypt.go +++ b/cmd/werf/helm/secret/values/encrypt/encrypt.go @@ -58,8 +58,8 @@ Encryption key should be in $WERF_SECRET_KEY or .werf_secret_key file`), } common.SetupDir(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/cmd/werf/host/cleanup/cleanup.go b/cmd/werf/host/cleanup/cleanup.go index 7b403d7bc6..772052b186 100644 --- a/cmd/werf/host/cleanup/cleanup.go +++ b/cmd/werf/host/cleanup/cleanup.go @@ -58,8 +58,8 @@ It is safe to run this command periodically by automated cleanup job in parallel }, } - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDockerConfig(&commonCmdData, cmd, "") common.SetupProjectName(&commonCmdData, cmd) diff --git a/cmd/werf/host/purge/purge.go b/cmd/werf/host/purge/purge.go index afe8b074a0..baf4c0d692 100644 --- a/cmd/werf/host/purge/purge.go +++ b/cmd/werf/host/purge/purge.go @@ -55,8 +55,8 @@ WARNING: Do not run this command during any other werf command is working on the }, } - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupDockerConfig(&commonCmdData, cmd, "") common.SetupProjectName(&commonCmdData, cmd) diff --git a/cmd/werf/kube_run/kube_run.go b/cmd/werf/kube_run/kube_run.go index f4731156f1..4fd00b1d74 100644 --- a/cmd/werf/kube_run/kube_run.go +++ b/cmd/werf/kube_run/kube_run.go @@ -133,8 +133,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/kubectl/kubectl.go b/cmd/werf/kubectl/kubectl.go index a95079b074..592af90869 100644 --- a/cmd/werf/kubectl/kubectl.go +++ b/cmd/werf/kubectl/kubectl.go @@ -1,26 +1,43 @@ package kubectl import ( + "context" "fmt" "os" + "strings" "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/kubectl/pkg/cmd" + "k8s.io/kubectl/pkg/cmd/plugin" "k8s.io/kubectl/pkg/cmd/util" "github.com/werf/werf/cmd/werf/common" + "github.com/werf/werf/pkg/tmp_manager" + "github.com/werf/werf/pkg/werf" +) + +var ( + commonCmdData common.CmdData + configFlags *genericclioptions.ConfigFlags ) func NewCmd() *cobra.Command { - kubectlCmd := cmd.NewDefaultKubectlCommand() + configFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() + + kubectlCmd := cmd.NewDefaultKubectlCommandWithArgs(cmd.KubectlOptions{ + PluginHandler: cmd.NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes), + Arguments: os.Args, + ConfigFlags: configFlags, + IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}, + }) + + common.SetupHomeDir(&commonCmdData, kubectlCmd, common.SetupHomeDirOptions{Persistent: true}) + common.SetupTmpDir(&commonCmdData, kubectlCmd, common.SetupTmpDirOptions{Persistent: true}) + common.SetupKubeConfigBase64(&commonCmdData, kubectlCmd) 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)) - } - } + kubeConfigFlag.Usage = "Path to the kubeconfig file to use for CLI requests (default $WERF_KUBE_CONFIG, or $WERF_KUBECONFIG, or $KUBECONFIG). Ignored if kubeconfig passed as base64." kubeContextFlag := kubectlCmd.Flag("context") kubeContextFlag.Usage = "The name of the kubeconfig context to use (default $WERF_KUBE_CONTEXT)" @@ -38,5 +55,69 @@ func NewCmd() *cobra.Command { } } + wrapPreRun(kubectlCmd) + return kubectlCmd } + +func wrapPreRun(kubectlCmd *cobra.Command) { + switch { + case kubectlCmd.PersistentPreRunE != nil: + oldFunc := kubectlCmd.PersistentPreRunE + kubectlCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if err := prePreRun(); err != nil { + return err + } + return oldFunc(cmd, args) + } + case kubectlCmd.PersistentPreRun != nil: + oldFunc := kubectlCmd.PersistentPreRun + kubectlCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { + if err := prePreRun(); err != nil { + util.CheckErr(err) + } + oldFunc(cmd, args) + } + default: + kubectlCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if err := prePreRun(); err != nil { + return err + } + return nil + } + } +} + +func prePreRun() error { + ctx := common.GetContext() + + if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil { + return fmt.Errorf("initialization error: %w", err) + } + + return setupKubeconfig(ctx) +} + +func setupKubeconfig(ctx context.Context) error { + var kubeConfigPath string + if *commonCmdData.KubeConfigBase64 != "" { + var err error + kubeConfigPath, err = tmp_manager.CreateKubeConfigFromBase64(ctx, strings.NewReader(*commonCmdData.KubeConfigBase64)) + if err != nil { + return fmt.Errorf("unable to create kubeconfig from base64: %w", err) + } + *configFlags.KubeConfig = "" + } + + if kubeConfigEnv := common.GetFirstExistingKubeConfigEnvVar(); kubeConfigEnv != "" && *configFlags.KubeConfig == "" { + kubeConfigPath = kubeConfigEnv + } + + if kubeConfigPath != "" { + if err := os.Setenv("KUBECONFIG", kubeConfigPath); err != nil { + return fmt.Errorf("unable to set $KUBECONFIG env var: %w", err) + } + } + + return nil +} diff --git a/cmd/werf/managed_images/add/add.go b/cmd/werf/managed_images/add/add.go index 06de81677b..c34df736c1 100644 --- a/cmd/werf/managed_images/add/add.go +++ b/cmd/werf/managed_images/add/add.go @@ -49,8 +49,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/managed_images/ls/ls.go b/cmd/werf/managed_images/ls/ls.go index 9606ef70f0..d1eefd61e7 100644 --- a/cmd/werf/managed_images/ls/ls.go +++ b/cmd/werf/managed_images/ls/ls.go @@ -45,8 +45,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/managed_images/rm/rm.go b/cmd/werf/managed_images/rm/rm.go index e05c35de4c..aa7882695b 100644 --- a/cmd/werf/managed_images/rm/rm.go +++ b/cmd/werf/managed_images/rm/rm.go @@ -49,8 +49,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/purge/purge.go b/cmd/werf/purge/purge.go index 5f430080fe..c707c380e9 100644 --- a/cmd/werf/purge/purge.go +++ b/cmd/werf/purge/purge.go @@ -52,8 +52,8 @@ WARNING: Images that are being used in the Kubernetes cluster will also be delet common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) common.SetupCacheStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/render/render.go b/cmd/werf/render/render.go index 24d016e4cf..176082f57f 100644 --- a/cmd/werf/render/render.go +++ b/cmd/werf/render/render.go @@ -76,8 +76,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupIntrospectAfterError(&commonCmdData, cmd) diff --git a/cmd/werf/run/run.go b/cmd/werf/run/run.go index 5f4e654084..47e032f1da 100644 --- a/cmd/werf/run/run.go +++ b/cmd/werf/run/run.go @@ -118,8 +118,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/stage/image/main.go b/cmd/werf/stage/image/main.go index f182a63714..e0c0ec4273 100644 --- a/cmd/werf/stage/image/main.go +++ b/cmd/werf/stage/image/main.go @@ -58,8 +58,8 @@ func NewCmd() *cobra.Command { common.SetupGiterminismOptions(&commonCmdData, cmd) - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupSSHKey(&commonCmdData, cmd) common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd) diff --git a/cmd/werf/synchronization/main.go b/cmd/werf/synchronization/main.go index 42cbe80772..9ae4712a76 100644 --- a/cmd/werf/synchronization/main.go +++ b/cmd/werf/synchronization/main.go @@ -54,8 +54,8 @@ func NewCmd() *cobra.Command { }, } - common.SetupTmpDir(&commonCmdData, cmd) - common.SetupHomeDir(&commonCmdData, cmd) + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) common.SetupGiterminismOptions(&commonCmdData, cmd) diff --git a/pkg/tmp_manager/common.go b/pkg/tmp_manager/common.go index 97b28d46e9..b6009d27eb 100644 --- a/pkg/tmp_manager/common.go +++ b/pkg/tmp_manager/common.go @@ -12,11 +12,13 @@ import ( const ( projectsServiceDir = "projects" dockerConfigsServiceDir = "docker_configs" + kubeConfigsServiceDir = "kubeconfigs" werfConfigRendersServiceDir = "werf_config_renders" CommonPrefix = "werf-" ProjectDirPrefix = CommonPrefix + "project-data-" DockerConfigDirPrefix = CommonPrefix + "docker-config-" + KubeConfigDirPrefix = CommonPrefix + "kubeconfig-" WerfConfigRenderPrefix = CommonPrefix + "config-render-" ) diff --git a/pkg/tmp_manager/gc.go b/pkg/tmp_manager/gc.go index f3990d33ae..f45ab91827 100644 --- a/pkg/tmp_manager/gc.go +++ b/pkg/tmp_manager/gc.go @@ -46,6 +46,21 @@ func ShouldRunAutoGC() (bool, error) { } } + createdKubeConfigsDir := filepath.Join(GetCreatedTmpDirs(), kubeConfigsServiceDir) + if _, err := os.Stat(createdKubeConfigsDir); !os.IsNotExist(err) { + var err error + createdDirs, err := ioutil.ReadDir(createdKubeConfigsDir) + if err != nil { + return false, fmt.Errorf("unable to list created kubeconfigs in %s: %w", createdKubeConfigsDir, err) + } + + for _, info := range createdDirs { + if now.Sub(info.ModTime()) > 24*time.Hour { + return true, nil + } + } + } + createdWerfConfigRenderFiles := filepath.Join(GetCreatedTmpDirs(), werfConfigRendersServiceDir) if _, err := os.Stat(createdWerfConfigRenderFiles); !os.IsNotExist(err) { var err error @@ -80,6 +95,10 @@ func RunGC(ctx context.Context, dryRun bool) error { return err } + if err := gcCreatedKubeConfigs(&pathsToRemove); err != nil { + return fmt.Errorf("unable to gc kubeconfigs: %w", err) + } + if err := gcCreatedWerfConfigRenders(&pathsToRemove); err != nil { return err } @@ -198,6 +217,31 @@ func gcCreatedDockerConfigs(pathsToRemove *[]string) error { return nil } +// Remove only these created kubeconfigs, which can be removed +func gcCreatedKubeConfigs(pathsToRemove *[]string) error { + kubeConfigs, err := getLinks(filepath.Join(GetCreatedTmpDirs(), kubeConfigsServiceDir)) + if err != nil { + return fmt.Errorf("unable to get created tmp kubeconfigs: %w", err) + } + + linksToRemove, err := getCreatedFilesToRemove(kubeConfigs) + if err != nil { + return fmt.Errorf("cannot get created tmp files to remove: %w", err) + } + + kubeConfigsToRemove, err := readLinks(linksToRemove) + if err != nil { + return fmt.Errorf("unable to read kubeconfigs links: %w", err) + } + + *pathsToRemove = append(*pathsToRemove, kubeConfigsToRemove...) + for _, link := range linksToRemove { + *pathsToRemove = append(*pathsToRemove, link.LinkPath) + } + + return nil +} + // Remove only these created werf config render files, which can be removed func gcCreatedWerfConfigRenders(pathsToRemove *[]string) error { createdWerfConfigRendersLinks, err := getLinks(filepath.Join(GetCreatedTmpDirs(), werfConfigRendersServiceDir)) diff --git a/pkg/tmp_manager/kubeconfig.go b/pkg/tmp_manager/kubeconfig.go new file mode 100644 index 0000000000..1715d6f09f --- /dev/null +++ b/pkg/tmp_manager/kubeconfig.go @@ -0,0 +1,40 @@ +package tmp_manager + +import ( + "context" + "encoding/base64" + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" +) + +func CreateKubeConfigFromBase64(ctx context.Context, base64KubeConfig io.Reader) (string, error) { + base64Decoder := base64.NewDecoder(base64.StdEncoding, base64KubeConfig) + kubeConfig, err := ioutil.ReadAll(base64Decoder) + if err != nil { + return "", fmt.Errorf("unable to base64 decode kubeconfig: %w", err) + } + + tmpDir, err := newTmpDir(KubeConfigDirPrefix) + if err != nil { + return "", fmt.Errorf("unable to create kubeconfig tmp dir: %w", err) + } + + if err := os.Chmod(tmpDir, 0o700); err != nil { + return "", fmt.Errorf("unable to create tmp kubeconfigs service dir: %w", err) + } + + kubeConfigPath := filepath.Join(tmpDir, "kubeconfig") + if err := os.WriteFile(kubeConfigPath, kubeConfig, 0o600); err != nil { + return "", fmt.Errorf("unable to write file kubeconfig: %w", err) + } + + if err := registerCreatedPath(tmpDir, filepath.Join(GetCreatedTmpDirs(), kubeConfigsServiceDir)); err != nil { + os.RemoveAll(tmpDir) + return "", fmt.Errorf("unable to register created kubeconfigs service dir: %w", err) + } + + return kubeConfigPath, nil +}