Skip to content

Commit 70a8862

Browse files
authored
fix: trigger pod rollout if supergraphschema changes after it was initially created (#44)
1 parent 7cbcb48 commit 70a8862

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

internal/controllers/supergraph_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ func (r *SuperGraphReconciler) reconcile(ctx context.Context, supergraph infrav1
275275
deploymentTemplate.Spec.Template.Labels = make(map[string]string)
276276
}
277277

278+
if deploymentTemplate.Spec.Template.Annotations == nil {
279+
deploymentTemplate.Spec.Template.Annotations = make(map[string]string)
280+
}
281+
278282
deploymentTemplate.Spec.Selector = &metav1.LabelSelector{
279283
MatchLabels: map[string]string{
280284
"app.kubernetes.io/instance": "apollo-router",
@@ -283,6 +287,8 @@ func (r *SuperGraphReconciler) reconcile(ctx context.Context, supergraph infrav1
283287
},
284288
}
285289

290+
deploymentTemplate.Spec.Template.Annotations["apollo-controller/schema-checksum"] = graphschema.Status.ObservedSHA256Checksum
291+
286292
if deploymentTemplate.Spec.Replicas == nil {
287293
deploymentTemplate.Spec.Replicas = &replicas
288294
}

internal/controllers/supergraph_controller_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

@@ -124,6 +125,7 @@ var _ = Describe("SuperGraph controller", func() {
124125
},
125126
Spec: v1beta1.SuperGraphSchemaSpec{},
126127
}
128+
127129
Expect(k8sClient.Create(ctx, schema)).Should(Succeed())
128130
configMapName := fmt.Sprintf("supergraph-schema-%s", schemaName)
129131
instanceLookupKey := types.NamespacedName{Name: schemaName, Namespace: "default"}
@@ -135,6 +137,8 @@ var _ = Describe("SuperGraph controller", func() {
135137
}
136138

137139
schema.Status.ConfigMap.Name = configMapName
140+
schema.Status.ObservedSHA256Checksum = "dummy-check"
141+
138142
return k8sClient.Status().Update(ctx, schema)
139143
}, timeout, interval).Should(Not(HaveOccurred()))
140144

@@ -223,11 +227,54 @@ var _ = Describe("SuperGraph controller", func() {
223227
"app.kubernetes.io/name": "apollo-router",
224228
}))
225229

230+
Expect(reconciledInstance.Spec.Template.ObjectMeta.Annotations).To(Equal(map[string]string{
231+
"apollo-controller/schema-checksum": schema.Status.ObservedSHA256Checksum,
232+
}))
233+
226234
Expect(reconciledInstance.Spec.Template.Spec.Containers[0].Name).To(Equal("router"))
227235
Expect(reconciledInstance.Spec.Template.Spec.Containers[0].Image).To(Equal("ghcr.io/apollographql/router:v2.9.0"))
228236
Expect(reconciledInstance.OwnerReferences[0].Name).Should(Equal(supergraphName))
229237
})
230238

239+
It("updates the deployment if the referenced schema checksum changes", func() {
240+
ctx := context.Background()
241+
242+
schema = &v1beta1.SuperGraphSchema{
243+
ObjectMeta: metav1.ObjectMeta{
244+
Name: schemaName,
245+
Namespace: "default",
246+
},
247+
Spec: v1beta1.SuperGraphSchemaSpec{},
248+
}
249+
instanceLookupKey := types.NamespacedName{Name: schemaName, Namespace: "default"}
250+
251+
Eventually(func() error {
252+
err := k8sClient.Get(ctx, instanceLookupKey, schema)
253+
if err != nil {
254+
return err
255+
}
256+
257+
schema.Status.ObservedSHA256Checksum = "dummy-check-v2"
258+
return k8sClient.Status().Update(ctx, schema)
259+
}, timeout, interval).Should(Not(HaveOccurred()))
260+
261+
instanceLookupKey = types.NamespacedName{Name: fmt.Sprintf("apollo-router-%s", supergraphName), Namespace: "default"}
262+
reconciledInstance := &appsv1.Deployment{}
263+
264+
Eventually(func() error {
265+
err := k8sClient.Get(ctx, instanceLookupKey, reconciledInstance)
266+
if err != nil {
267+
return err
268+
}
269+
270+
if reconciledInstance.Spec.Template.ObjectMeta.Annotations["apollo-controller/schema-checksum"] != "dummy-check-v2" {
271+
return errors.New("schema checksum not updated")
272+
}
273+
274+
return nil
275+
}, timeout, interval).Should(BeNil())
276+
})
277+
231278
It("cleans up", func() {
232279
ctx := context.Background()
233280
Expect(k8sClient.Delete(ctx, supergraph)).Should(Succeed())

0 commit comments

Comments
 (0)