Skip to content

Commit 6af3c0a

Browse files
immanuwellweyfonk
authored andcommitted
fix: inherit node selectors for GitRepo sync jobs
AI assistance was limited to drafting this small patch and its unit coverage; the behavior was reviewed against the existing GitJob scheduling code. Assisted-by: ChatGPT Signed-off-by: immanuwell <pchpr.00@list.ru>
1 parent cff530b commit 6af3c0a

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

internal/cmd/controller/gitops/reconciler/gitjob.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"maps"
78
"os"
89
"slices"
910
"strconv"
@@ -198,6 +199,11 @@ func (r *GitJobReconciler) newGitJob(ctx context.Context, obj *v1alpha1.GitRepo)
198199
jobSpec.Template.Spec.Tolerations,
199200
fleetControllerDeployment.Spec.Template.Spec.Tolerations...,
200201
)
202+
if jobSpec.Template.Spec.NodeSelector == nil {
203+
jobSpec.Template.Spec.NodeSelector = map[string]string{}
204+
}
205+
maps.Copy(jobSpec.Template.Spec.NodeSelector, fleetControllerDeployment.Spec.Template.Spec.NodeSelector)
206+
201207
job := &batchv1.Job{
202208
ObjectMeta: metav1.ObjectMeta{
203209
Annotations: map[string]string{

internal/cmd/controller/gitops/reconciler/gitjob_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"maps"
1011
"os"
1112
"slices"
1213
"strings"
@@ -595,6 +596,7 @@ func TestNewJob(t *testing.T) {
595596
strictHostKeyChecks bool
596597
clientObjects []runtime.Object
597598
deploymentTolerations []corev1.Toleration
599+
deploymentNodeSelector map[string]string
598600
expectedInitContainers []corev1.Container
599601
expectedContainers []corev1.Container
600602
expectedVolumes []corev1.Volume
@@ -1741,6 +1743,9 @@ func TestNewJob(t *testing.T) {
17411743
Operator: "Exists",
17421744
},
17431745
},
1746+
deploymentNodeSelector: map[string]string{
1747+
"node-role.kubernetes.io/fleet": "true",
1748+
},
17441749
clientObjects: []runtime.Object{
17451750
&corev1.ConfigMap{
17461751
ObjectMeta: metav1.ObjectMeta{
@@ -1760,7 +1765,7 @@ func TestNewJob(t *testing.T) {
17601765
for name, test := range tests {
17611766
t.Run(name, func(t *testing.T) {
17621767
r := GitJobReconciler{
1763-
Client: getFakeClient(test.deploymentTolerations, test.clientObjects...),
1768+
Client: getFakeClient(test.deploymentTolerations, test.deploymentNodeSelector, test.clientObjects...),
17641769
Scheme: scheme,
17651770
Image: "test",
17661771
Clock: RealClock{},
@@ -1867,6 +1872,12 @@ func TestNewJob(t *testing.T) {
18671872
if !cmp.Equal(expectedTolerations, job.Spec.Template.Spec.Tolerations) {
18681873
t.Fatalf("job tolerations differ. Expecting: %v and found: %v", test.deploymentTolerations, job.Spec.Template.Spec.Tolerations)
18691874
}
1875+
1876+
expectedNodeSelector := map[string]string{"kubernetes.io/os": "linux"}
1877+
maps.Copy(expectedNodeSelector, test.deploymentNodeSelector)
1878+
if !cmp.Equal(expectedNodeSelector, job.Spec.Template.Spec.NodeSelector) {
1879+
t.Fatalf("job node selector differs. Expecting: %v and found: %v", expectedNodeSelector, job.Spec.Template.Spec.NodeSelector)
1880+
}
18701881
})
18711882
}
18721883
}
@@ -2380,7 +2391,7 @@ func TestGenerateJob_EnvVars(t *testing.T) {
23802391
}
23812392

23822393
r := GitJobReconciler{
2383-
Client: getFakeClient([]corev1.Toleration{}),
2394+
Client: getFakeClient([]corev1.Toleration{}, nil),
23842395
Image: "test",
23852396
Clock: RealClock{},
23862397
SystemNamespace: config.DefaultNamespace,
@@ -2747,18 +2758,18 @@ ignore this line as well`,
27472758
}
27482759
}
27492760

2750-
func getFakeClient(tolerations []corev1.Toleration, objs ...runtime.Object) client.Client {
2761+
func getFakeClient(tolerations []corev1.Toleration, nodeSelector map[string]string, objs ...runtime.Object) client.Client {
27512762
scheme := runtime.NewScheme()
27522763
utilruntime.Must(corev1.AddToScheme(scheme))
27532764
utilruntime.Must(appsv1.AddToScheme(scheme))
27542765

27552766
return fake.NewClientBuilder().
27562767
WithScheme(scheme).
27572768
WithRuntimeObjects(objs...).
2758-
WithRuntimeObjects(getFleetControllerDeployment(tolerations)).Build()
2769+
WithRuntimeObjects(getFleetControllerDeployment(tolerations, nodeSelector)).Build()
27592770
}
27602771

2761-
func getFleetControllerDeployment(tolerations []corev1.Toleration) *appsv1.Deployment {
2772+
func getFleetControllerDeployment(tolerations []corev1.Toleration, nodeSelector map[string]string) *appsv1.Deployment {
27622773
return &appsv1.Deployment{
27632774
ObjectMeta: metav1.ObjectMeta{
27642775
Name: config.ManagerConfigName,
@@ -2767,7 +2778,8 @@ func getFleetControllerDeployment(tolerations []corev1.Toleration) *appsv1.Deplo
27672778
Spec: appsv1.DeploymentSpec{
27682779
Template: corev1.PodTemplateSpec{
27692780
Spec: corev1.PodSpec{
2770-
Tolerations: tolerations,
2781+
Tolerations: tolerations,
2782+
NodeSelector: nodeSelector,
27712783
},
27722784
},
27732785
},

0 commit comments

Comments
 (0)