Skip to content

Commit b264149

Browse files
committed
Update OperatorCondition controller to watch deps
Problem: Updates to deployments can remove the OPERATOR_CONDITION_NAME environment variable. Solution: Update the OperatorCondition controller to watch for Deployment events, if the deployment is owned by a CSV, trigger the reconciler.
1 parent 4a9d02b commit b264149

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

pkg/controller/operators/operatorcondition_controller.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type OperatorConditionReconciler struct {
3838

3939
// SetupWithManager adds the OperatorCondition Reconciler reconciler to the given controller manager.
4040
func (r *OperatorConditionReconciler) SetupWithManager(mgr ctrl.Manager) error {
41+
deploymentHandler := handler.EnqueueRequestsFromMapFunc(r.mapToOperatorCondition)
4142
handler := &handler.EnqueueRequestForOwner{
4243
IsController: true,
4344
OwnerType: &operatorsv1.OperatorCondition{},
@@ -47,9 +48,30 @@ func (r *OperatorConditionReconciler) SetupWithManager(mgr ctrl.Manager) error {
4748
For(&operatorsv1.OperatorCondition{}).
4849
Watches(&source.Kind{Type: &rbacv1.Role{}}, handler).
4950
Watches(&source.Kind{Type: &rbacv1.RoleBinding{}}, handler).
51+
Watches(&source.Kind{Type: &appsv1.Deployment{}}, deploymentHandler).
5052
Complete(r)
5153
}
5254

55+
func (r *OperatorConditionReconciler) mapToOperatorCondition(obj client.Object) (requests []reconcile.Request) {
56+
if obj == nil {
57+
return nil
58+
}
59+
60+
owner := ownerutil.GetOwnerByKind(obj, operatorsv1alpha1.ClusterServiceVersionKind)
61+
if owner == nil {
62+
return nil
63+
}
64+
65+
return []reconcile.Request{
66+
{
67+
NamespacedName: types.NamespacedName{
68+
Name: owner.Name,
69+
Namespace: obj.GetNamespace(),
70+
},
71+
},
72+
}
73+
}
74+
5375
// NewOperatorConditionReconciler constructs and returns an OperatorConditionReconciler.
5476
// As a side effect, the given scheme has operator discovery types added to it.
5577
func NewOperatorConditionReconciler(cli client.Client, log logr.Logger, scheme *runtime.Scheme) (*OperatorConditionReconciler, error) {

pkg/controller/operators/operatorcondition_controller_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ var _ = Describe("OperatorCondition", func() {
287287
Expect(k8sClient.Create(ctx, operatorCondition)).To(Succeed())
288288
})
289289

290-
It("injects the OperatorCondition name into the deployment's Environment Variables", func() {
290+
It("should always inject the OperatorCondition Environment Variable into containers defined in the deployment", func() {
291291
deployment := &appsv1.Deployment{}
292292
Eventually(func() error {
293293
err := k8sClient.Get(ctx, types.NamespacedName{Name: operatorCondition.Spec.Deployments[0], Namespace: namespace.GetName()}, deployment)
@@ -313,6 +313,42 @@ var _ = Describe("OperatorCondition", func() {
313313
Value: operatorCondition.GetName(),
314314
}))
315315
}
316+
317+
// Remove the container's Environment Variables defined in the deployment
318+
Eventually(func() error {
319+
err := k8sClient.Get(ctx, types.NamespacedName{Name: operatorCondition.Spec.Deployments[0], Namespace: namespace.GetName()}, deployment)
320+
if err != nil {
321+
return err
322+
}
323+
deployment.Spec.Template.Spec.Containers[0].Env = nil
324+
return k8sClient.Update(ctx, deployment)
325+
}, timeout, interval).Should(BeNil())
326+
327+
// Ensure that the OPERATOR_CONDITION_NAME Environment Variable is recreated
328+
Eventually(func() error {
329+
err := k8sClient.Get(ctx, types.NamespacedName{Name: operatorCondition.Spec.Deployments[0], Namespace: namespace.GetName()}, deployment)
330+
if err != nil {
331+
return err
332+
}
333+
if len(deployment.Spec.Template.Spec.Containers) != 1 {
334+
return fmt.Errorf("Deployment should contain a single container")
335+
}
336+
for _, container := range deployment.Spec.Template.Spec.Containers {
337+
if len(container.Env) == 0 {
338+
return fmt.Errorf("env vars should exist")
339+
}
340+
}
341+
return nil
342+
}, timeout, interval).Should(BeNil())
343+
344+
Expect(len(deployment.Spec.Template.Spec.Containers)).Should(Equal(1))
345+
for _, container := range deployment.Spec.Template.Spec.Containers {
346+
Expect(len(container.Env)).Should(Equal(1))
347+
Expect(container.Env).Should(ContainElement(corev1.EnvVar{
348+
Name: "OPERATOR_CONDITION_NAME",
349+
Value: operatorCondition.GetName(),
350+
}))
351+
}
316352
})
317353
})
318354
})

0 commit comments

Comments
 (0)