Skip to content

Commit e316b87

Browse files
committed
Set an object reconcilers to "ignore" mode if its corresponding type is removed from the HNCConfiguration Spec
If a type is removed from the HNCConfiguration Spec, we will set the corresponding object reconciler to "ignore" mode. Ideally, we should shut down the corresponding object reconciler. Gracefully terminating an object reconciler is still under development (kubernetes-sigs/controller-runtime#764). Once the feature is released, we will see if we can shut down the object reconciler instead of setting it to "ignore" mode.
1 parent d49fbcd commit e316b87

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

incubator/hnc/pkg/reconcilers/hnc_config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,29 @@ func (r *ConfigReconciler) syncObjectReconcilers(ctx context.Context, inst *api.
162162
}
163163
}
164164

165+
// If a type exists in the forest but not exists in the Spec, we will
166+
// set the mode of corresponding object reconciler to "ignore".
167+
// TODO: Ideally, we should shut down the corresponding object
168+
// reconciler. Gracefully terminating an object reconciler is still under
169+
// development (https://github.com/kubernetes-sigs/controller-runtime/issues/764).
170+
// We will revisit the code below once the feature is released.
171+
for _, ts := range r.Forest.GetTypeSyncers() {
172+
exist := false
173+
for _, t := range inst.Spec.Types {
174+
if ts.GetGVK() == schema.FromAPIVersionAndKind(t.APIVersion, t.Kind) {
175+
exist = true
176+
break
177+
}
178+
}
179+
if exist {
180+
continue
181+
}
182+
// The type does not exist in the Spec. Ignore subsequent reconciliations.
183+
if err := ts.SetMode(ctx, api.Ignore, r.Log); err != nil {
184+
return err // retry the reconciliation
185+
}
186+
}
187+
165188
return nil
166189
}
167190

incubator/hnc/pkg/reconcilers/hnc_config_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,28 @@ var _ = Describe("HNCConfiguration", func() {
190190
Eventually(hasObject(ctx, "ResourceQuota", barName, "foo-resource-quota")).Should(BeTrue())
191191
Expect(objectInheritedFrom(ctx, "ResourceQuota", barName, "foo-resource-quota")).Should(Equal(fooName))
192192
})
193+
194+
It("should stop propagating objects if a type is first set to propagate mode then removed from the spec", func() {
195+
addToHNCConfig(ctx, "v1", "Secret", api.Propagate)
196+
setParent(ctx, barName, fooName)
197+
makeObject(ctx, "Secret", fooName, "foo-sec")
198+
199+
// "foo-sec" should propagate from foo to bar.
200+
Eventually(hasObject(ctx, "Secret", barName, "foo-sec")).Should(BeTrue())
201+
Expect(objectInheritedFrom(ctx, "Secret", barName, "foo-sec")).Should(Equal(fooName))
202+
203+
removeHNCConfigType(ctx, "v1", "Secret")
204+
// Give foo another secret.
205+
makeObject(ctx, "Secret", fooName, "foo-sec-2")
206+
207+
// Foo should have "foo-sec-2" because we created there.
208+
Eventually(hasObject(ctx, "Secret", fooName, "foo-sec-2")).Should(BeTrue())
209+
// Sleep to give "foo-sec-2" a chance to propagate from foo to bar, if it could.
210+
time.Sleep(sleepTime)
211+
// "foo-role-2" should not propagate from foo to bar because the reconciliation request is ignored.
212+
Expect(hasObject(ctx, "Secret", barName, "foo-sec-2")()).Should(BeFalse())
213+
214+
})
193215
})
194216

195217
func hasTypeWithMode(apiVersion, kind string, mode api.SynchronizationMode, config *api.HNCConfiguration) func() bool {
@@ -271,3 +293,22 @@ func updateHNCConfigSpec(ctx context.Context, prevApiVersion, newApiVersion, pre
271293
return updateHNCConfig(ctx, c)
272294
}).Should(Succeed())
273295
}
296+
297+
func removeHNCConfigType(ctx context.Context, apiVersion, kind string) {
298+
Eventually(func() error {
299+
c := getHNCConfig(ctx)
300+
i := 0
301+
for ; i < len(c.Spec.Types); i++ {
302+
if c.Spec.Types[i].APIVersion == apiVersion && c.Spec.Types[i].Kind == kind {
303+
break
304+
}
305+
}
306+
// The type does not exist. Nothing to remove.
307+
if i == len(c.Spec.Types) {
308+
return nil
309+
}
310+
c.Spec.Types[i] = c.Spec.Types[len(c.Spec.Types)-1]
311+
c.Spec.Types = c.Spec.Types[:len(c.Spec.Types)-1]
312+
return updateHNCConfig(ctx, c)
313+
}).Should(Succeed())
314+
}

0 commit comments

Comments
 (0)