From 7038bd07e83e715a3c289b06f901c68a705a4276 Mon Sep 17 00:00:00 2001 From: Sebastien RIBIERE Date: Thu, 1 Aug 2024 07:22:54 +0000 Subject: [PATCH 1/3] fix: list applications in all Namespaces for namespaced update Signed-off-by: Sebastien RIBIERE --- cmd/run.go | 6 ++++-- pkg/argocd/argocd.go | 2 +- pkg/argocd/argocd_test.go | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 751c29c9..aabb951f 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,7 @@ func newRunCommand() *cobra.Command { var err error if !disableKubernetes { ctx := context.Background() - cfg.KubeClient, err = getKubeConfig(ctx, cfg.ArgocdNamespace, kubeConfig) + cfg.KubeClient, err = getKubeConfig(ctx, v1.NamespaceAll, kubeConfig) if err != nil { log.Fatalf("could not create K8s client: %v", err) } @@ -127,13 +128,14 @@ 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, namespace=%s]", cfg.ApplicationsAPIKind, cfg.ClientOpts.ServerAddr, cfg.ClientOpts.AuthToken != "", cfg.ClientOpts.Insecure, cfg.ClientOpts.GRPCWeb, cfg.ClientOpts.Plaintext, + cfg.ArgocdNamespace, ) // Health server will start in a go routine and run asynchronously diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 155d01aa..433cbe12 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -30,7 +30,7 @@ func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v } func (client *k8sClient) ListApplications() ([]v1alpha1.Application, error) { - list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(client.kubeClient.Namespace).List(context.TODO(), v1.ListOptions{}) + list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{}) if err != nil { return nil, err } diff --git a/pkg/argocd/argocd_test.go b/pkg/argocd/argocd_test.go index a24cc4e7..7e8a4a2d 100644 --- a/pkg/argocd/argocd_test.go +++ b/pkg/argocd/argocd_test.go @@ -1066,7 +1066,7 @@ func TestKubernetesClient(t *testing.T) { t.Run("List applications", func(t *testing.T) { apps, err := client.ListApplications() require.NoError(t, err) - require.Len(t, apps, 1) + require.Len(t, apps, 2) assert.ElementsMatch(t, []string{"test-app1"}, []string{app1.Name}) }) From dd65cb3ec684e6f55436b518ded1cca2996b8e7e Mon Sep 17 00:00:00 2001 From: Sebastien RIBIERE Date: Tue, 13 Aug 2024 14:46:23 +0000 Subject: [PATCH 2/3] fix: Namespaced or clusterwide is now a parameter Signed-off-by: Sebastien RIBIERE --- cmd/main.go | 1 + cmd/run.go | 10 ++++++++-- cmd/test.go | 2 +- cmd/util.go | 8 ++++---- cmd/util_test.go | 2 +- pkg/argocd/argocd.go | 2 +- pkg/argocd/argocd_test.go | 2 +- pkg/kube/kubernetes.go | 6 ++++-- pkg/kube/kubernetes_test.go | 4 ++-- 9 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 81eed761..2b555093 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 aabb951f..728c469d 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -111,7 +111,11 @@ func newRunCommand() *cobra.Command { var err error if !disableKubernetes { ctx := context.Background() - cfg.KubeClient, err = getKubeConfig(ctx, v1.NamespaceAll, 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) } @@ -128,13 +132,14 @@ 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, namespace=%s]", + 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, ) @@ -247,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/argocd/argocd.go b/pkg/argocd/argocd.go index 433cbe12..155d01aa 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -30,7 +30,7 @@ func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v } func (client *k8sClient) ListApplications() ([]v1alpha1.Application, error) { - list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{}) + list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(client.kubeClient.Namespace).List(context.TODO(), v1.ListOptions{}) if err != nil { return nil, err } diff --git a/pkg/argocd/argocd_test.go b/pkg/argocd/argocd_test.go index 7e8a4a2d..a24cc4e7 100644 --- a/pkg/argocd/argocd_test.go +++ b/pkg/argocd/argocd_test.go @@ -1066,7 +1066,7 @@ func TestKubernetesClient(t *testing.T) { t.Run("List applications", func(t *testing.T) { apps, err := client.ListApplications() require.NoError(t, err) - require.Len(t, apps, 2) + require.Len(t, apps, 1) assert.ElementsMatch(t, []string{"test-app1"}, []string{app1.Name}) }) diff --git a/pkg/kube/kubernetes.go b/pkg/kube/kubernetes.go index 3ec66cfd..723aa4b6 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 56ca6204..87f9181c 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) From b91eafb206de4ae32517aaab517afc4dc7e72c11 Mon Sep 17 00:00:00 2001 From: Sebastien RIBIERE Date: Tue, 13 Aug 2024 15:39:56 +0000 Subject: [PATCH 3/3] fix: gofmt Signed-off-by: Sebastien RIBIERE --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 2b555093..c00c0b9a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -50,7 +50,7 @@ type ImageUpdaterConfig struct { GitCommitSignOff bool DisableKubeEvents bool GitCreds git.CredsStore - Namespaced bool + Namespaced bool } // newRootCommand implements the root command of argocd-image-updater