From 68677afbaa2b949ea5ecd6fa76b704f33ebb6c1e Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Mon, 22 Aug 2022 18:43:19 +0300 Subject: [PATCH] fix(cleanup): ignore WERF_KUBE_CONTEXT env var, support option --scan-only-context Use werf cleanup `--scan-only-context` option (or `--kube-context` alias) or `WERF_SCAN_ONLY_CONTEXT` environment variable to scan for deployed images only in the specified kube config context. By default werf cleanup will scan all kube contexts available in the kube config. Signed-off-by: Timofey Kirillov --- cmd/werf/cleanup/cleanup.go | 14 +++++++++++--- cmd/werf/common/cleanup_namespaces_scan.go | 10 +++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/werf/cleanup/cleanup.go b/cmd/werf/cleanup/cleanup.go index 9a3307bd8d..22763d97f4 100644 --- a/cmd/werf/cleanup/cleanup.go +++ b/cmd/werf/cleanup/cleanup.go @@ -3,6 +3,7 @@ package cleanup import ( "context" "fmt" + "os" "github.com/spf13/cobra" @@ -23,6 +24,10 @@ import ( var commonCmdData common.CmdData +var cmdData struct { + ScanContextOnly string +} + func NewCmd(ctx context.Context) *cobra.Command { ctx = common.NewContextWithCmdData(ctx, &commonCmdData) cmd := common.SetCommandContext(ctx, &cobra.Command{ @@ -83,7 +88,6 @@ It is safe to run this command periodically (daily is enough) by automated clean common.SetupSynchronization(&commonCmdData, cmd) common.SetupKubeConfig(&commonCmdData, cmd) common.SetupKubeConfigBase64(&commonCmdData, cmd) - common.SetupKubeContext(&commonCmdData, cmd) common.SetupWithoutKube(&commonCmdData, cmd) common.SetupKeepStagesBuiltWithinLastNHours(&commonCmdData, cmd) @@ -95,6 +99,10 @@ It is safe to run this command periodically (daily is enough) by automated clean common.SetupDockerServerStoragePath(&commonCmdData, cmd) common.SetupPlatform(&commonCmdData, cmd) + // aliases, but only WERF_SCAN_ONLY_CONTEXT env var is supported + cmd.PersistentFlags().StringVarP(&cmdData.ScanContextOnly, "scan-context-only", "", os.Getenv("WERF_SCAN_CONTEXT_ONLY"), "Scan for used images only in the specified kube context, scan all contexts from kube config otherwise (default false or $WERF_SCAN_CONTEXT_ONLY)") + cmd.PersistentFlags().StringVarP(&cmdData.ScanContextOnly, "kube-context", "", os.Getenv("WERF_SCAN_CONTEXT_ONLY"), "Scan for used images only in the specified kube context, scan all contexts from kube config otherwise (default false or $WERF_SCAN_CONTEXT_ONLY)") + return cmd } @@ -140,7 +148,7 @@ func runCleanup(ctx context.Context) error { } }() - common.SetupOndemandKubeInitializer(*commonCmdData.KubeContext, *commonCmdData.KubeConfig, *commonCmdData.KubeConfigBase64, *commonCmdData.KubeConfigPathMergeList) + common.SetupOndemandKubeInitializer(cmdData.ScanContextOnly, *commonCmdData.KubeConfig, *commonCmdData.KubeConfigBase64, *commonCmdData.KubeConfigPathMergeList) if err := common.GetOndemandKubeInitializer().Init(ctx); err != nil { return err } @@ -234,7 +242,7 @@ It is worth noting that auto-cleaning is enabled by default, and manual use is u var kubernetesContextClients []*kube.ContextClient var kubernetesNamespaceRestrictionByContext map[string]string if !(*commonCmdData.WithoutKube || werfConfig.Meta.Cleanup.DisableKubernetesBasedPolicy) { - kubernetesContextClients, err = common.GetKubernetesContextClients(&commonCmdData) + kubernetesContextClients, err = common.GetKubernetesContextClients(*commonCmdData.KubeConfig, *commonCmdData.KubeConfigBase64, *commonCmdData.KubeConfigPathMergeList, cmdData.ScanContextOnly) if err != nil { return fmt.Errorf("unable to get Kubernetes clusters connections: %w", err) } diff --git a/cmd/werf/common/cleanup_namespaces_scan.go b/cmd/werf/common/cleanup_namespaces_scan.go index 9bd1c07f94..4a0599eb9f 100644 --- a/cmd/werf/common/cleanup_namespaces_scan.go +++ b/cmd/werf/common/cleanup_namespaces_scan.go @@ -15,21 +15,21 @@ func SetupScanContextNamespaceOnly(cmdData *CmdData, cmd *cobra.Command) { cmd.Flags().BoolVarP(cmdData.ScanContextNamespaceOnly, "scan-context-namespace-only", "", util.GetBoolEnvironmentDefaultFalse("WERF_SCAN_CONTEXT_NAMESPACE_ONLY"), "Scan for used images only in namespace linked with context for each available context in kube-config (or only for the context specified with option --kube-context). When disabled will scan all namespaces in all contexts (or only for the context specified with option --kube-context). (Default $WERF_SCAN_CONTEXT_NAMESPACE_ONLY)") } -func GetKubernetesContextClients(cmdData *CmdData) ([]*kube.ContextClient, error) { +func GetKubernetesContextClients(configPath, configDataBase64 string, configPathMergeList []string, kubeContext string) ([]*kube.ContextClient, error) { var res []*kube.ContextClient - if contextClients, err := kube.GetAllContextsClients(kube.GetAllContextsClientsOptions{ConfigPath: *cmdData.KubeConfig, ConfigDataBase64: *cmdData.KubeConfigBase64, ConfigPathMergeList: *cmdData.KubeConfigPathMergeList}); err != nil { + if contextClients, err := kube.GetAllContextsClients(kube.GetAllContextsClientsOptions{ConfigPath: configPath, ConfigDataBase64: configDataBase64, ConfigPathMergeList: configPathMergeList}); err != nil { return nil, err } else { - if *cmdData.KubeContext != "" { + if kubeContext != "" { for _, cc := range contextClients { - if cc.ContextName == *cmdData.KubeContext { + if cc.ContextName == kubeContext { res = append(res, cc) break } } if len(res) == 0 { - return nil, fmt.Errorf("cannot find specified kube context %q", *cmdData.KubeContext) + return nil, fmt.Errorf("cannot find specified kube context %q", kubeContext) } } else { res = contextClients