Skip to content

Commit 2259f01

Browse files
authored
fix(operator): idempotent sa image pull secrets (#637)
1 parent 70a3f03 commit 2259f01

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1616
- Added support for service-level affinities
1717

1818
### Fixed
19+
- (Openshift only) - High CPU usage for the operator pod due to continues reconciles
1920
- Fixed a bug where the scheduler would not re-try updating podgroup status after failure
2021
- Fixed a bug where ray workloads gang scheduling would ignore `minReplicas` if autoscaling was not set
2122
- KAI Config wrong statuses when prometheus operand is enabled

pkg/operator/operands/binder/binder_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/NVIDIA/KAI-scheduler/pkg/operator/operands/common/test_utils"
2020

2121
appsv1 "k8s.io/api/apps/v1"
22+
v1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
"k8s.io/utils/ptr"
2325
"sigs.k8s.io/controller-runtime/pkg/client"
2426
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -93,6 +95,60 @@ var _ = Describe("Binder", func() {
9395
Expect(deployment.Labels).To(HaveKeyWithValue("foo", "bar"))
9496
Expect(deployment.Spec.Template.Labels).To(HaveKeyWithValue("kai", "scheduler"))
9597
})
98+
99+
It("sets CDI flag if set in cluser policy", func(ctx context.Context) {
100+
clusterPolicy := &nvidiav1.ClusterPolicy{
101+
ObjectMeta: metav1.ObjectMeta{
102+
Name: "test",
103+
},
104+
Spec: nvidiav1.ClusterPolicySpec{
105+
CDI: nvidiav1.CDIConfigSpec{
106+
Enabled: ptr.To(true),
107+
Default: ptr.To(true),
108+
},
109+
},
110+
}
111+
112+
Expect(fakeKubeClient.Create(ctx, clusterPolicy)).To(Succeed())
113+
objects, err := b.DesiredState(ctx, fakeKubeClient, kaiConfig)
114+
Expect(err).To(BeNil())
115+
116+
deploymentT := test_utils.FindTypeInObjects[*appsv1.Deployment](objects)
117+
Expect(deploymentT).NotTo(BeNil())
118+
Expect((*deploymentT).Spec.Template.Spec.Containers[0].Args).To(ContainElement("--cdi-enabled=true"))
119+
})
120+
})
121+
122+
Context("Reservation Service Account", func() {
123+
It("will not remove current image pull secrets", func(ctx context.Context) {
124+
kaiConfig.Spec.Global.ImagePullSecrets = []string{"test-secret"}
125+
126+
reservationSA := &v1.ServiceAccount{
127+
ObjectMeta: metav1.ObjectMeta{
128+
Namespace: *kaiConfig.Spec.Binder.ResourceReservation.Namespace,
129+
Name: *kaiConfig.Spec.Binder.ResourceReservation.ServiceAccountName,
130+
},
131+
ImagePullSecrets: []v1.LocalObjectReference{
132+
{Name: "existing"},
133+
},
134+
}
135+
Expect(fakeKubeClient.Create(ctx, reservationSA)).To(Succeed())
136+
objects, err := b.DesiredState(ctx, fakeKubeClient, kaiConfig)
137+
Expect(err).To(BeNil())
138+
139+
var newReservationSA *v1.ServiceAccount
140+
for _, obj := range objects {
141+
sa, ok := obj.(*v1.ServiceAccount)
142+
if ok && sa.Name == reservationSA.Name {
143+
newReservationSA = sa
144+
}
145+
}
146+
147+
Expect(newReservationSA).NotTo(BeNil())
148+
Expect(newReservationSA.ImagePullSecrets).To(HaveLen(2))
149+
Expect(newReservationSA.ImagePullSecrets).To(ContainElement(v1.LocalObjectReference{Name: "existing"}))
150+
Expect(newReservationSA.ImagePullSecrets).To(ContainElement(v1.LocalObjectReference{Name: "test-secret"}))
151+
})
96152
})
97153
})
98154
})

pkg/operator/operands/binder/resources.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,23 @@ func resourceReservationServiceAccount(
141141

142142
sa.Name = *kaiConfig.Spec.Binder.ResourceReservation.ServiceAccountName
143143
sa.Namespace = *kaiConfig.Spec.Binder.ResourceReservation.Namespace
144-
sa.ImagePullSecrets = kaiConfigUtils.GetGlobalImagePullSecrets(kaiConfig.Spec.Global)
144+
145+
imagePullSecrets := make(map[string]bool)
146+
for _, secret := range sa.ImagePullSecrets {
147+
imagePullSecrets[secret.Name] = true
148+
}
149+
150+
for _, secret := range kaiConfigUtils.GetGlobalImagePullSecrets(kaiConfig.Spec.Global) {
151+
if !imagePullSecrets[secret.Name] {
152+
imagePullSecrets[secret.Name] = true
153+
}
154+
}
155+
156+
sa.ImagePullSecrets = make([]v1.LocalObjectReference, 0, len(imagePullSecrets))
157+
for secretName := range imagePullSecrets {
158+
sa.ImagePullSecrets = append(sa.ImagePullSecrets, v1.LocalObjectReference{Name: secretName})
159+
}
160+
145161
return []client.Object{sa}, nil
146162
}
147163

0 commit comments

Comments
 (0)