Skip to content

Commit

Permalink
fix(cleanup): ignore WERF_KUBE_CONTEXT env var, support option --scan…
Browse files Browse the repository at this point in the history
…-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 <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Aug 23, 2022
1 parent c527871 commit 68677af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 11 additions & 3 deletions cmd/werf/cleanup/cleanup.go
Expand Up @@ -3,6 +3,7 @@ package cleanup
import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"

Expand All @@ -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{
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/werf/common/cleanup_namespaces_scan.go
Expand Up @@ -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
Expand Down

0 comments on commit 68677af

Please sign in to comment.