Skip to content

Commit

Permalink
NodeSelector change does not trigger reconcile (#186)
Browse files Browse the repository at this point in the history
* NodeSelector change does not trigger reconcile

When `nodeSelector` is changed and it matches a new node, this node ignores it.

This issue was caused by reconcile predicate `UpdateFunc` callback depending on
matching the node with both the new and the old policy `nodeSelector`. This
caused `UpdateFunc` to not work properly.

Now we check our node only against the new (updated) policy `nodeSelector`.

Signed-off-by: HlinaCZ <[email protected]>
Signed-off-by: Petr Horacek <[email protected]>
  • Loading branch information
pitiK3U authored and phoracek committed Sep 27, 2019
1 parent 756917b commit 30dcc90
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func forThisNodePredicate(cl client.Client) predicate.Funcs {
return nodeSelectorMatchesThisNode(cl, deleteEvent.Object)
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
return nodeSelectorMatchesThisNode(cl, updateEvent.ObjectOld) &&
nodeSelectorMatchesThisNode(cl, updateEvent.ObjectNew)
return nodeSelectorMatchesThisNode(cl, updateEvent.ObjectNew)
},
GenericFunc: func(genericEvent event.GenericEvent) bool {
return nodeSelectorMatchesThisNode(cl, genericEvent.Object)
Expand Down
61 changes: 61 additions & 0 deletions test/e2e/node_selector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package e2e

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

nmstatev1alpha1 "github.com/nmstate/kubernetes-nmstate/pkg/apis/nmstate/v1alpha1"
)

var _ = Describe("NodeSelector", func() {
br1Up := nmstatev1alpha1.State(`interfaces:
- name: br1
type: linux-bridge
state: up
bridge:
options:
stp:
enabled: false
port:
- name: eth1
`)
br1Absent := nmstatev1alpha1.State(`interfaces:
- name: br1
type: linux-bridge
state: absent
`)
nonexistentNodeSelector := map[string]string{"nonexistentKey": "nonexistentValue"}

Context("when policy is set with node selector not matching any nodes", func() {
BeforeEach(func() {
setDesiredStateWithPolicyAndNodeSelector("br1", br1Up, nonexistentNodeSelector)
})

AfterEach(func() {
setDesiredStateWithPolicy("br1", br1Absent)
for _, node := range nodes {
interfacesNameForNode(node).ShouldNot(ContainElement("br1"))
}

deletePolicy("br1")
})

It("should not update any nodes", func() {
for _, node := range nodes {
interfacesNameForNode(node).ShouldNot(ContainElement("br1"))
}
})

Context("and we remove the node selector", func() {
BeforeEach(func() {
setDesiredStateWithPolicyAndNodeSelector("br1", br1Up, map[string]string{})
})

It("should update all nodes", func() {
for _, node := range nodes {
interfacesNameForNode(node).Should(ContainElement("br1"))
}
})
})
})
})
7 changes: 6 additions & 1 deletion test/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ func waitForDaemonSet(t *testing.T, kubeclient kubernetes.Interface, namespace,
return nil
}

func setDesiredStateWithPolicy(name string, desiredState nmstatev1alpha1.State) {
func setDesiredStateWithPolicyAndNodeSelector(name string, desiredState nmstatev1alpha1.State, nodeSelector map[string]string) {
policy := nmstatev1alpha1.NodeNetworkConfigurationPolicy{}
policy.Name = name
key := types.NamespacedName{Name: name}
Eventually(func() error {
err := framework.Global.Client.Get(context.TODO(), key, &policy)
policy.Spec.DesiredState = desiredState
policy.Spec.NodeSelector = nodeSelector
if err != nil {
if apierrors.IsNotFound(err) {
return framework.Global.Client.Create(context.TODO(), &policy, &framework.CleanupOptions{})
Expand All @@ -158,6 +159,10 @@ func setDesiredStateWithPolicy(name string, desiredState nmstatev1alpha1.State)
}, ReadTimeout, ReadInterval).ShouldNot(HaveOccurred())
}

func setDesiredStateWithPolicy(name string, desiredState nmstatev1alpha1.State) {
setDesiredStateWithPolicyAndNodeSelector(name, desiredState, map[string]string{})
}

func updateDesiredStateAtNode(node string, desiredState nmstatev1alpha1.State) {
key := types.NamespacedName{Name: node}
state := nmstatev1alpha1.NodeNetworkState{}
Expand Down

0 comments on commit 30dcc90

Please sign in to comment.