Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List applications in all Namespaces when trying to update #831

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/kube/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down