Skip to content

Commit

Permalink
Bug 1906198: Accept any name for the nmstate resource
Browse files Browse the repository at this point in the history
Currently, the operator will only reconcile an nmstate resource that
has a name 'nmstate'. This patch makes it possible to add the resource
as any name, but subsequent resources added will be ignored.

Signed-off-by: Brad P. Crochet <[email protected]>
  • Loading branch information
bcrochet committed Mar 3, 2021
1 parent 66b9357 commit e3b5c60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
20 changes: 13 additions & 7 deletions controllers/nmstate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ func (r *NMStateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
_ = r.Log.WithValues("nmstate", req.NamespacedName)

// We won't create more than one kubernetes-nmstate handler
if req.Name != names.NMStateResourceName {
r.Log.Info("Ignoring NMState.nmstate.io without default name")
return ctrl.Result{}, nil
}

// Fetch the NMState instance
instanceList := &nmstatev1beta1.NMStateList{}
err := r.Client.List(context.TODO(), instanceList, &client.ListOptions{})
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed listing all NMState instances")
}
instance := &nmstatev1beta1.NMState{}
err := r.Client.Get(context.TODO(), req.NamespacedName, instance)
err = r.Client.Get(context.TODO(), req.NamespacedName, instance)
if err != nil {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile req.
Expand All @@ -83,6 +82,13 @@ func (r *NMStateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{}, err
}

// We only want one instance of NMState. Ignore anything after that.
if len(instanceList.Items) > 0 && instanceList.Items[0].Name != req.Name {
r.Log.Info("Ignoring NMState.nmstate.io because one already exists and does not match existing name")
err = r.Client.Delete(context.TODO(), instance, &client.DeleteOptions{})
return ctrl.Result{}, nil
}

err = r.applyCRDs(instance)
if err != nil {
errors.Wrap(err, "failed applying CRDs")
Expand Down
13 changes: 11 additions & 2 deletions controllers/nmstate_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var _ = Describe("NMState controller reconcile", func() {
s := scheme.Scheme
s.AddKnownTypes(nmstatev1beta1.GroupVersion,
&nmstatev1beta1.NMState{},
&nmstatev1beta1.NMStateList{},
)
objs := []runtime.Object{&nmstate}
// Create a fake client to mock API calls.
Expand All @@ -71,18 +72,26 @@ var _ = Describe("NMState controller reconcile", func() {
Expect(err).ToNot(HaveOccurred())
})

Context("when CR is wrong name", func() {
Context("when additional CR is created", func() {
var (
request ctrl.Request
)
BeforeEach(func() {
request.Name = "not-present-node"
request.Name = "nmstate-two"
})
It("should return empty result", func() {
result, err := reconciler.Reconcile(request)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
})
It("and should delete the second one", func() {
_, err := reconciler.Reconcile(request)
Expect(err).ToNot(HaveOccurred())
nmstateList := &nmstatev1beta1.NMStateList{}
err = cl.List(context.TODO(), nmstateList, &client.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(len(nmstateList.Items)).To(Equal(1))
})
})
Context("when an nmstate is found", func() {
var (
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/operator/nmstate_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ var _ = Describe("NMState operator", func() {
Expect(err).ToNot(HaveOccurred(), "List daemon sets in namespace nmstate succeeds")
Expect(ds.Items).To(HaveLen(1), "and has only one item")
})
Context("and the CR is created with a wrong name", func() {
Context("and another CR is created with a different name", func() {
var nmstate = defaultNMState
nmstate.Name = "wrong-name"
nmstate.Name = "different-name"
BeforeEach(func() {
err := testenv.Client.Create(context.TODO(), &nmstate)
Expect(err).ToNot(HaveOccurred(), "NMState CR with incorrect name is created without error")
Expect(err).ToNot(HaveOccurred(), "NMState CR with different name is ignored")
})
AfterEach(func() {
err := testenv.Client.Delete(context.TODO(), &nmstate, &client.DeleteOptions{})
Expand Down

0 comments on commit e3b5c60

Please sign in to comment.