From a6fb17281837c136263c432fcf1648ac57006ab2 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Mon, 16 Nov 2015 08:09:43 -0800 Subject: [PATCH] extract shouldRun predicate to a function in daemonset controller --- pkg/controller/daemon/controller.go | 46 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/pkg/controller/daemon/controller.go b/pkg/controller/daemon/controller.go index f174b1c2b1d..2742b8267aa 100644 --- a/pkg/controller/daemon/controller.go +++ b/pkg/controller/daemon/controller.go @@ -352,27 +352,13 @@ func (dsc *DaemonSetsController) manage(ds *extensions.DaemonSet) { glog.Errorf("Couldn't get list of nodes when syncing daemon set %+v: %v", ds, err) } var nodesNeedingDaemonPods, podsToDelete []string - for i, node := range nodeList.Items { - // Check if the node satisfies the daemon set's node selector. - nodeSelector := labels.Set(ds.Spec.Template.Spec.NodeSelector).AsSelector() - shouldRun := nodeSelector.Matches(labels.Set(nodeList.Items[i].Labels)) - // If the daemon set specifies a node name, check that it matches with nodeName. - nodeName := nodeList.Items[i].Name - shouldRun = shouldRun && (ds.Spec.Template.Spec.NodeName == "" || ds.Spec.Template.Spec.NodeName == nodeName) + for _, node := range nodeList.Items { + shouldRun := nodeShouldRunDaemonPod(&node, ds) + daemonPods, isRunning := nodeToDaemonPods[node.Name] - // If the node is not ready, don't run on it. - // TODO(mikedanese): remove this once daemonpods forgive nodes - shouldRun = shouldRun && api.IsNodeReady(&node) - - // If the node is unschedulable, don't run it - // TODO(mikedanese): remove this once we have the right node admitance levels. - // See https://github.com/kubernetes/kubernetes/issues/17297#issuecomment-156857375. - shouldRun = shouldRun && !node.Spec.Unschedulable - - daemonPods, isRunning := nodeToDaemonPods[nodeName] if shouldRun && !isRunning { // If daemon pod is supposed to be running on node, but isn't, create daemon pod. - nodesNeedingDaemonPods = append(nodesNeedingDaemonPods, nodeName) + nodesNeedingDaemonPods = append(nodesNeedingDaemonPods, node.Name) } else if shouldRun && len(daemonPods) > 1 { // If daemon pod is supposed to be running on node, but more than 1 daemon pod is running, delete the excess daemon pods. // Sort the daemon pods by creation time, so the the oldest is preserved. @@ -455,10 +441,7 @@ func (dsc *DaemonSetsController) updateDaemonSetStatus(ds *extensions.DaemonSet) var desiredNumberScheduled, currentNumberScheduled, numberMisscheduled int for _, node := range nodeList.Items { - nodeSelector := labels.Set(ds.Spec.Template.Spec.NodeSelector).AsSelector() - nameMatch := ds.Spec.Template.Name == "" || ds.Spec.Template.Name == node.Name - labelMatch := nodeSelector.Matches(labels.Set(node.Labels)) - shouldRun := nameMatch && labelMatch + shouldRun := nodeShouldRunDaemonPod(&node, ds) numDaemonPods := len(nodeToDaemonPods[node.Name]) @@ -523,6 +506,25 @@ func (dsc *DaemonSetsController) syncDaemonSet(key string) error { return nil } +func nodeShouldRunDaemonPod(node *api.Node, ds *extensions.DaemonSet) bool { + // Check if the node satisfies the daemon set's node selector. + nodeSelector := labels.Set(ds.Spec.Template.Spec.NodeSelector).AsSelector() + shouldRun := nodeSelector.Matches(labels.Set(node.Labels)) + // If the daemon set specifies a node name, check that it matches with node.Name. + shouldRun = shouldRun && (ds.Spec.Template.Spec.NodeName == "" || ds.Spec.Template.Spec.NodeName == node.Name) + + // If the node is not ready, don't run on it. + // TODO(mikedanese): remove this once daemonpods forgive nodes + shouldRun = shouldRun && api.IsNodeReady(node) + + // If the node is unschedulable, don't run it + // TODO(mikedanese): remove this once we have the right node admitance levels. + // See https://github.com/kubernetes/kubernetes/issues/17297#issuecomment-156857375. + shouldRun = shouldRun && !node.Spec.Unschedulable + + return shouldRun +} + // byCreationTimestamp sorts a list by creation timestamp, using their names as a tie breaker. type byCreationTimestamp []extensions.DaemonSet