Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/cmd/controller/gitops/reconciler/gitjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"os"
"slices"
"strconv"
Expand Down Expand Up @@ -198,6 +199,11 @@ func (r *GitJobReconciler) newGitJob(ctx context.Context, obj *v1alpha1.GitRepo)
jobSpec.Template.Spec.Tolerations,
fleetControllerDeployment.Spec.Template.Spec.Tolerations...,
)
if jobSpec.Template.Spec.NodeSelector == nil {
jobSpec.Template.Spec.NodeSelector = map[string]string{}
}
maps.Copy(jobSpec.Template.Spec.NodeSelector, fleetControllerDeployment.Spec.Template.Spec.NodeSelector)

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
Expand Down
24 changes: 18 additions & 6 deletions internal/cmd/controller/gitops/reconciler/gitjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"os"
"slices"
"strings"
Expand Down Expand Up @@ -595,6 +596,7 @@ func TestNewJob(t *testing.T) {
strictHostKeyChecks bool
clientObjects []runtime.Object
deploymentTolerations []corev1.Toleration
deploymentNodeSelector map[string]string
expectedInitContainers []corev1.Container
expectedContainers []corev1.Container
expectedVolumes []corev1.Volume
Expand Down Expand Up @@ -1741,6 +1743,9 @@ func TestNewJob(t *testing.T) {
Operator: "Exists",
},
},
deploymentNodeSelector: map[string]string{
"node-role.kubernetes.io/fleet": "true",
},
clientObjects: []runtime.Object{
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -1760,7 +1765,7 @@ func TestNewJob(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
r := GitJobReconciler{
Client: getFakeClient(test.deploymentTolerations, test.clientObjects...),
Client: getFakeClient(test.deploymentTolerations, test.deploymentNodeSelector, test.clientObjects...),
Scheme: scheme,
Image: "test",
Clock: RealClock{},
Expand Down Expand Up @@ -1867,6 +1872,12 @@ func TestNewJob(t *testing.T) {
if !cmp.Equal(expectedTolerations, job.Spec.Template.Spec.Tolerations) {
t.Fatalf("job tolerations differ. Expecting: %v and found: %v", test.deploymentTolerations, job.Spec.Template.Spec.Tolerations)
}

expectedNodeSelector := map[string]string{"kubernetes.io/os": "linux"}
maps.Copy(expectedNodeSelector, test.deploymentNodeSelector)
if !cmp.Equal(expectedNodeSelector, job.Spec.Template.Spec.NodeSelector) {
t.Fatalf("job node selector differs. Expecting: %v and found: %v", expectedNodeSelector, job.Spec.Template.Spec.NodeSelector)
}
})
}
}
Expand Down Expand Up @@ -2380,7 +2391,7 @@ func TestGenerateJob_EnvVars(t *testing.T) {
}

r := GitJobReconciler{
Client: getFakeClient([]corev1.Toleration{}),
Client: getFakeClient([]corev1.Toleration{}, nil),
Image: "test",
Clock: RealClock{},
SystemNamespace: config.DefaultNamespace,
Expand Down Expand Up @@ -2747,18 +2758,18 @@ ignore this line as well`,
}
}

func getFakeClient(tolerations []corev1.Toleration, objs ...runtime.Object) client.Client {
func getFakeClient(tolerations []corev1.Toleration, nodeSelector map[string]string, objs ...runtime.Object) client.Client {
scheme := runtime.NewScheme()
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(appsv1.AddToScheme(scheme))

return fake.NewClientBuilder().
WithScheme(scheme).
WithRuntimeObjects(objs...).
WithRuntimeObjects(getFleetControllerDeployment(tolerations)).Build()
WithRuntimeObjects(getFleetControllerDeployment(tolerations, nodeSelector)).Build()
}

func getFleetControllerDeployment(tolerations []corev1.Toleration) *appsv1.Deployment {
func getFleetControllerDeployment(tolerations []corev1.Toleration, nodeSelector map[string]string) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: config.ManagerConfigName,
Expand All @@ -2767,7 +2778,8 @@ func getFleetControllerDeployment(tolerations []corev1.Toleration) *appsv1.Deplo
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Tolerations: tolerations,
Tolerations: tolerations,
NodeSelector: nodeSelector,
},
},
},
Expand Down