diff --git a/cmd/main.go b/cmd/main.go index 81eed761..c00c0b9a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -50,6 +50,7 @@ type ImageUpdaterConfig struct { GitCommitSignOff bool DisableKubeEvents bool GitCreds git.CredsStore + Namespaced bool } // newRootCommand implements the root command of argocd-image-updater diff --git a/cmd/run.go b/cmd/run.go index 06030d22..40f8fc09 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -22,6 +22,7 @@ import ( "github.com/argoproj/argo-cd/v2/reposerver/askpass" "github.com/spf13/cobra" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "golang.org/x/sync/semaphore" ) @@ -110,7 +111,11 @@ func newRunCommand() *cobra.Command { var err error if !disableKubernetes { ctx := context.Background() - cfg.KubeClient, err = getKubeConfig(ctx, cfg.ArgocdNamespace, kubeConfig) + ns := cfg.ArgocdNamespace + if !cfg.Namespaced { + ns = v1.NamespaceAll + } + cfg.KubeClient, err = getKubeConfig(ctx, ns, cfg.Namespaced, kubeConfig) if err != nil { log.Fatalf("could not create K8s client: %v", err) } @@ -127,13 +132,15 @@ func newRunCommand() *cobra.Command { cfg.ClientOpts.AuthToken = token } - log.Infof("ArgoCD configuration: [apiKind=%s, server=%s, auth_token=%v, insecure=%v, grpc_web=%v, plaintext=%v]", + log.Infof("ArgoCD configuration: [apiKind=%s, server=%s, auth_token=%v, insecure=%v, grpc_web=%v, plaintext=%v, namespaced=%v, namespace=%s]", cfg.ApplicationsAPIKind, cfg.ClientOpts.ServerAddr, cfg.ClientOpts.AuthToken != "", cfg.ClientOpts.Insecure, cfg.ClientOpts.GRPCWeb, cfg.ClientOpts.Plaintext, + cfg.Namespaced, + cfg.ArgocdNamespace, ) // Health server will start in a go routine and run asynchronously @@ -245,6 +252,7 @@ func newRunCommand() *cobra.Command { runCmd.Flags().BoolVar(&cfg.GitCommitSignOff, "git-commit-sign-off", env.GetBoolVal("GIT_COMMIT_SIGN_OFF", false), "Whether to sign-off git commits") runCmd.Flags().StringVar(&commitMessagePath, "git-commit-message-path", defaultCommitTemplatePath, "Path to a template to use for Git commit messages") runCmd.Flags().BoolVar(&cfg.DisableKubeEvents, "disable-kube-events", env.GetBoolVal("IMAGE_UPDATER_KUBE_EVENTS", false), "Disable kubernetes events") + runCmd.Flags().BoolVar(&cfg.Namespaced, "namespaced", env.GetBoolVal("IMAGE_UPDATER_NAMESPACED", true), "Scope to only the provided namespace") return runCmd } diff --git a/cmd/test.go b/cmd/test.go index afc9cfbe..202fbde4 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -68,7 +68,7 @@ argocd-image-updater test nginx --allow-tags '^1.19.\d+(\-.*)*$' --update-strate var err error if !disableKubernetes { ctx := context.Background() - kubeClient, err = getKubeConfig(ctx, "", kubeConfig) + kubeClient, err = getKubeConfig(ctx, "", true, kubeConfig) if err != nil { log.Fatalf("could not create K8s client: %v", err) } diff --git a/cmd/util.go b/cmd/util.go index 7b6a944d..bddd6608 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -26,7 +26,7 @@ func getPrintableHealthPort(port int) string { } } -func getKubeConfig(ctx context.Context, namespace string, kubeConfig string) (*kube.KubernetesClient, error) { +func getKubeConfig(ctx context.Context, namespace string, namespaced bool, kubeConfig string) (*kube.KubernetesClient, error) { var fullKubeConfigPath string var kubeClient *kube.KubernetesClient var err error @@ -39,12 +39,12 @@ func getKubeConfig(ctx context.Context, namespace string, kubeConfig string) (*k } if fullKubeConfigPath != "" { - log.Debugf("Creating Kubernetes client from %s", fullKubeConfigPath) + log.Debugf("Creating Kubernetes client from %s for namespace '%s'", fullKubeConfigPath, namespace) } else { - log.Debugf("Creating in-cluster Kubernetes client") + log.Debugf("Creating in-cluster Kubernetes client for namespace '%s'", namespace) } - kubeClient, err = kube.NewKubernetesClientFromConfig(ctx, namespace, fullKubeConfigPath) + kubeClient, err = kube.NewKubernetesClientFromConfig(ctx, namespace, namespaced, fullKubeConfigPath) if err != nil { return nil, err } diff --git a/cmd/util_test.go b/cmd/util_test.go index 68753076..30e91756 100644 --- a/cmd/util_test.go +++ b/cmd/util_test.go @@ -41,7 +41,7 @@ func TestGetKubeConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client, err := getKubeConfig(context.TODO(), tt.namespace, tt.configPath) + client, err := getKubeConfig(context.TODO(), tt.namespace, true, tt.configPath) if tt.expectError { require.Error(t, err) } else { diff --git a/pkg/kube/kubernetes.go b/pkg/kube/kubernetes.go index 11583314..a80646b6 100644 --- a/pkg/kube/kubernetes.go +++ b/pkg/kube/kubernetes.go @@ -8,6 +8,7 @@ import ( "os" "time" + "github.com/argoproj-labs/argocd-image-updater/pkg/log" "github.com/argoproj-labs/argocd-image-updater/pkg/metrics" appv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" @@ -38,7 +39,7 @@ func NewKubernetesClient(ctx context.Context, client kubernetes.Interface, appli // NewKubernetesClient creates a new Kubernetes client object from given // configuration file. If configuration file is the empty string, in-cluster // client will be created. -func NewKubernetesClientFromConfig(ctx context.Context, namespace string, kubeconfig string) (*KubernetesClient, error) { +func NewKubernetesClientFromConfig(ctx context.Context, namespace string, namespaced bool, kubeconfig string) (*KubernetesClient, error) { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig loadingRules.ExplicitPath = kubeconfig @@ -50,7 +51,7 @@ func NewKubernetesClientFromConfig(ctx context.Context, namespace string, kubeco return nil, err } - if namespace == "" { + if namespace == "" && namespaced { namespace, _, err = clientConfig.Namespace() if err != nil { return nil, err @@ -67,6 +68,7 @@ func NewKubernetesClientFromConfig(ctx context.Context, namespace string, kubeco return nil, err } + log.Debugf("Creating kubernetes client for ns '%s'", namespace) return NewKubernetesClient(ctx, clientset, applicationsClientset, namespace), nil } diff --git a/pkg/kube/kubernetes_test.go b/pkg/kube/kubernetes_test.go index 35d69223..eb99fedf 100644 --- a/pkg/kube/kubernetes_test.go +++ b/pkg/kube/kubernetes_test.go @@ -16,14 +16,14 @@ import ( func Test_NewKubernetesClient(t *testing.T) { t.Run("Get new K8s client for remote cluster instance", func(t *testing.T) { - client, err := NewKubernetesClientFromConfig(context.TODO(), "", "../../test/testdata/kubernetes/config") + client, err := NewKubernetesClientFromConfig(context.TODO(), "", true, "../../test/testdata/kubernetes/config") require.NoError(t, err) assert.NotNil(t, client) assert.Equal(t, "default", client.Namespace) }) t.Run("Get new K8s client for remote cluster instance specified namespace", func(t *testing.T) { - client, err := NewKubernetesClientFromConfig(context.TODO(), "argocd", "../../test/testdata/kubernetes/config") + client, err := NewKubernetesClientFromConfig(context.TODO(), "argocd", true, "../../test/testdata/kubernetes/config") require.NoError(t, err) assert.NotNil(t, client) assert.Equal(t, "argocd", client.Namespace)