Skip to content

Commit 700e3a5

Browse files
committed
feat: Add k8s client config option to scope namespace
Signed-off-by: Graham Beckley <[email protected]>
1 parent 0965eaa commit 700e3a5

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

cmd/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func runImageUpdater(cfg *ImageUpdaterConfig, warmUp bool) (argocd.ImageUpdaterR
259259
var argoClient argocd.ArgoCD
260260
switch cfg.ApplicationsAPIKind {
261261
case applicationsAPIKindK8S:
262-
argoClient, err = argocd.NewK8SClient(cfg.KubeClient)
262+
argoClient, err = argocd.NewK8SClient(cfg.KubeClient, &argocd.K8SClientOptions{AppNamespace: cfg.AppNamespace})
263263
case applicationsAPIKindArgoCD:
264264
argoClient, err = argocd.NewAPIClient(&cfg.ClientOpts)
265265
default:

pkg/argocd/argocd.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ import (
2525

2626
// Kubernetes based client
2727
type k8sClient struct {
28-
kubeClient *kube.ImageUpdaterKubernetesClient
28+
kubeClient *kube.ImageUpdaterKubernetesClient
29+
appNamespace *string
2930
}
3031

3132
// GetApplication retrieves an application by name across all namespaces.
3233
func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v1alpha1.Application, error) {
33-
// List all applications across all namespaces (using empty labelSelector)
34-
appList, err := client.ListApplications(v1.NamespaceAll)
34+
// List all applications across configured namespace or all namespaces (using empty labelSelector)
35+
appList, err := client.ListApplications("")
3536
if err != nil {
3637
return nil, fmt.Errorf("error listing applications: %w", err)
3738
}
@@ -61,7 +62,7 @@ func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v
6162

6263
// ListApplications lists all applications across all namespaces.
6364
func (client *k8sClient) ListApplications(labelSelector string) ([]v1alpha1.Application, error) {
64-
list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
65+
list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(*client.appNamespace).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
6566
if err != nil {
6667
return nil, fmt.Errorf("error listing applications: %w", err)
6768
}
@@ -99,9 +100,23 @@ func (client *k8sClient) UpdateSpec(ctx context.Context, spec *application.Appli
99100
return nil, fmt.Errorf("max retries(%d) reached while updating application: %s", maxRetries, spec.GetName())
100101
}
101102

103+
type K8SClientOptions struct {
104+
AppNamespace string
105+
}
106+
102107
// NewK8SClient creates a new kubernetes client to interact with kubernetes api-server.
103-
func NewK8SClient(kubeClient *kube.ImageUpdaterKubernetesClient) (ArgoCD, error) {
104-
return &k8sClient{kubeClient: kubeClient}, nil
108+
func NewK8SClient(kubeClient *kube.ImageUpdaterKubernetesClient, opts *K8SClientOptions) (ArgoCD, error) {
109+
// Provide default options if nil
110+
if opts == nil {
111+
opts = &K8SClientOptions{
112+
AppNamespace: v1.NamespaceAll,
113+
}
114+
}
115+
116+
return &k8sClient{
117+
kubeClient: kubeClient,
118+
appNamespace: &opts.AppNamespace,
119+
}, nil
105120
}
106121

107122
// Native

pkg/argocd/argocd_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ func TestKubernetesClient(t *testing.T) {
10231023
Namespace: "testns1",
10241024
},
10251025
ApplicationsClientset: fake.NewSimpleClientset(app1, app2),
1026-
})
1026+
}, nil)
10271027

10281028
require.NoError(t, err)
10291029

@@ -1064,7 +1064,7 @@ func TestKubernetesClient(t *testing.T) {
10641064
// Create the Kubernetes client
10651065
client, err := NewK8SClient(&kube.ImageUpdaterKubernetesClient{
10661066
ApplicationsClientset: clientset,
1067-
})
1067+
}, nil)
10681068
require.NoError(t, err)
10691069

10701070
// Test ListApplications error handling
@@ -1094,7 +1094,7 @@ func TestKubernetesClient(t *testing.T) {
10941094
// Create the Kubernetes client
10951095
client, err := NewK8SClient(&kube.ImageUpdaterKubernetesClient{
10961096
ApplicationsClientset: clientset,
1097-
})
1097+
}, nil)
10981098
require.NoError(t, err)
10991099

11001100
// Test GetApplication with multiple matching applications
@@ -1124,7 +1124,7 @@ func TestKubernetesClientUpdateSpec(t *testing.T) {
11241124

11251125
client, err := NewK8SClient(&kube.ImageUpdaterKubernetesClient{
11261126
ApplicationsClientset: clientset,
1127-
})
1127+
}, nil)
11281128
require.NoError(t, err)
11291129

11301130
appName := "test-app"
@@ -1145,7 +1145,7 @@ func TestKubernetesClientUpdateSpec(t *testing.T) {
11451145

11461146
client, err := NewK8SClient(&kube.ImageUpdaterKubernetesClient{
11471147
ApplicationsClientset: clientset,
1148-
})
1148+
}, nil)
11491149
require.NoError(t, err)
11501150

11511151
appName := "test-app"
@@ -1169,7 +1169,7 @@ func TestKubernetesClientUpdateSpec(t *testing.T) {
11691169

11701170
client, err := NewK8SClient(&kube.ImageUpdaterKubernetesClient{
11711171
ApplicationsClientset: clientset,
1172-
})
1172+
}, nil)
11731173
require.NoError(t, err)
11741174

11751175
clientset.PrependReactor("update", "applications", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
@@ -1198,7 +1198,7 @@ func TestKubernetesClientUpdateSpec(t *testing.T) {
11981198

11991199
client, err := NewK8SClient(&kube.ImageUpdaterKubernetesClient{
12001200
ApplicationsClientset: clientset,
1201-
})
1201+
}, nil)
12021202
require.NoError(t, err)
12031203

12041204
clientset.PrependReactor("update", "applications", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {

0 commit comments

Comments
 (0)