Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodes updated with labels matching update selector should be added to the queue in daemonset controller #920

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/controller/daemonset/daemonset_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ func (e *nodeEventHandler) Update(evt event.UpdateEvent, q workqueue.RateLimitin
ds := &dsList.Items[i]
oldShouldRun, oldShouldContinueRunning := nodeShouldRunDaemonPod(oldNode, ds)
currentShouldRun, currentShouldContinueRunning := nodeShouldRunDaemonPod(curNode, ds)
if (oldShouldRun != currentShouldRun) || (oldShouldContinueRunning != currentShouldContinueRunning) {
if (oldShouldRun != currentShouldRun) || (oldShouldContinueRunning != currentShouldContinueRunning) ||
(NodeShouldUpdateBySelector(oldNode, ds) != NodeShouldUpdateBySelector(curNode, ds)) {
klog.V(6).Infof("update node: %s triggers DaemonSet %s/%s to reconcile.", curNode.Name, ds.GetNamespace(), ds.GetName())
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: ds.GetName(),
Namespace: ds.GetNamespace(),
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/daemonset/daemonset_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,24 @@ func findUpdatedPodsOnNode(ds *appsv1alpha1.DaemonSet, podsOnNode []*corev1.Pod,
}
return newPod, oldPod, true
}

// NodeShouldUpdateBySelector checks if the node is selected to upgrade for ds's gray update selector.
// This function does not check NodeShouldRunDaemonPod
func NodeShouldUpdateBySelector(node *corev1.Node, ds *appsv1alpha1.DaemonSet) bool {
switch ds.Spec.UpdateStrategy.Type {
case appsv1alpha1.OnDeleteDaemonSetStrategyType:
return false
case appsv1alpha1.RollingUpdateDaemonSetStrategyType:
if ds.Spec.UpdateStrategy.RollingUpdate == nil || ds.Spec.UpdateStrategy.RollingUpdate.Selector == nil {
return false
}
selector, err := metav1.LabelSelectorAsSelector(ds.Spec.UpdateStrategy.RollingUpdate.Selector)
if err != nil {
// this should not happen if the DaemonSet passed validation
return false
}
return !selector.Empty() && selector.Matches(labels.Set(node.Labels))
default:
return false
}
}
66 changes: 66 additions & 0 deletions pkg/controller/daemonset/daemonset_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,69 @@ func TestGetMaxCreateNum(t *testing.T) {
})
}
}

func TestNodeShouldUpdateBySelector(t *testing.T) {
for _, tt := range []struct {
Title string
Node *corev1.Node
Ds *appsv1alpha1.DaemonSet
Expected bool
}{
{
"node with no label",
newNode("node1", nil),
newDaemonSet("ds1"),
false,
},
{
"node with label, not selected",
newNode("node1", map[string]string{
"key1": "value1",
}),
func() *appsv1alpha1.DaemonSet {
ds := newDaemonSet("ds1")
ds.Spec.UpdateStrategy = newStandardRollingUpdateStrategy(map[string]string{
"key1": "value2",
})
return ds
}(),
false,
},
{
"node with label, selected",
newNode("node1", map[string]string{
"key1": "value1",
}),
func() *appsv1alpha1.DaemonSet {
ds := newDaemonSet("ds1")
ds.Spec.UpdateStrategy = newStandardRollingUpdateStrategy(map[string]string{
"key1": "value1",
})
return ds
}(),
true,
},
} {
t.Logf("\t%s", tt.Title)
should := NodeShouldUpdateBySelector(tt.Node, tt.Ds)
if should != tt.Expected {
t.Errorf("NodeShouldUpdateBySelector() = %v, want %v", should, tt.Expected)
}
}
}

func newStandardRollingUpdateStrategy(matchLabels map[string]string) appsv1alpha1.DaemonSetUpdateStrategy {
one := intstr.FromInt(1)
strategy := appsv1alpha1.DaemonSetUpdateStrategy{
Type: appsv1alpha1.RollingUpdateDaemonSetStrategyType,
RollingUpdate: &appsv1alpha1.RollingUpdateDaemonSet{
MaxUnavailable: &one,
Selector: nil,
Type: appsv1alpha1.StandardRollingUpdateType,
},
}
if len(matchLabels) > 0 {
strategy.RollingUpdate.Selector = &metav1.LabelSelector{MatchLabels: matchLabels}
}
return strategy
}