Skip to content

Commit 98eb466

Browse files
prashanth26hardikdr
authored andcommitted
Minor changes
1. Renamed PreferNoSchedule to PreferNoScheduleKey 2. Fixed map key check
1 parent ae0d9fd commit 98eb466

File tree

6 files changed

+68
-45
lines changed

6 files changed

+68
-45
lines changed

pkg/controller/controller_utils.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,9 @@ func RemoveTaintOffNode(c clientset.Interface, nodeName string, node *v1.Node, t
881881
})
882882
}
883883

884-
// PatchNodeTaints patches node's taints.
884+
// PatchNodeTaints is for updating the node taints from oldNode to the newNode
885+
// It makes a TwoWayMergePatch by comparing the two objects
886+
// It calls the Patch() method to do the final patch
885887
func PatchNodeTaints(c clientset.Interface, nodeName string, oldNode *v1.Node, newNode *v1.Node) error {
886888
oldData, err := json.Marshal(oldNode)
887889
if err != nil {
@@ -905,13 +907,18 @@ func PatchNodeTaints(c clientset.Interface, nodeName string, oldNode *v1.Node, n
905907
return err
906908
}
907909

908-
// UpdateNodeTaints updates node's taints.
910+
// UpdateNodeTaints is for updating the node taints from oldNode to the newNode
911+
// using the nodes Update() method
909912
func UpdateNodeTaints(c clientset.Interface, nodeName string, oldNode *v1.Node, newNode *v1.Node) error {
910913
newNodeClone := oldNode.DeepCopy()
911914
newNodeClone.Spec.Taints = newNode.Spec.Taints
912915

913916
_, err := c.Core().Nodes().Update(newNodeClone)
914-
return err
917+
if err != nil {
918+
return fmt.Errorf("failed to create update taints for node %q: %v", nodeName, err)
919+
}
920+
921+
return nil
915922
}
916923

917924
// WaitForCacheSync is a wrapper around cache.WaitForCacheSync that generates log messages

pkg/controller/deployment_rollback.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ func (dc *controller) rollback(d *v1alpha1.MachineDeployment, isList []*v1alpha1
6262
glog.V(4).Infof("Found machine set %q with desired revision %d", is.Name, v)
6363

6464
// Remove PreferNoSchedule taints from nodes which were backing the machineSet
65-
err = dc.removeTaintNodesBackingMachineSets(is)
65+
err = dc.removeTaintNodesBackingMachineSet(
66+
is,
67+
&v1.Taint{
68+
Key: PreferNoScheduleKey,
69+
Value: "True",
70+
Effect: "PreferNoSchedule",
71+
},
72+
)
6673
if err != nil {
67-
glog.Warningf("Failed to remove taints %s off nodes. Error: %s", PreferNoSchedule, err)
74+
glog.Warningf("Failed to remove taints %s off nodes. Error: %s", PreferNoScheduleKey, err)
6875
}
6976

7077
// rollback by copying podTemplate.Spec from the machine set
@@ -130,16 +137,16 @@ func (dc *controller) updateMachineDeploymentAndClearRollbackTo(d *v1alpha1.Mach
130137
return err
131138
}
132139

133-
// removeTaintNodesBackingMachineSets removes taints from all nodes backing the machineSets
134-
func (dc *controller) removeTaintNodesBackingMachineSets(machineSet *v1alpha1.MachineSet) error {
140+
// removeTaintNodesBackingMachineSet removes taints from all nodes backing the machineSets
141+
func (dc *controller) removeTaintNodesBackingMachineSet(machineSet *v1alpha1.MachineSet, taint *v1.Taint) error {
135142

136-
if machineSet.Annotations[PreferNoSchedule] == "" {
143+
if _, exists := machineSet.Annotations[taint.Key]; !exists {
137144
// No taint exists
138-
glog.Warningf("No taint exits on machineSet: %s. Hence not removing.", machineSet.Name)
145+
glog.Warningf("No taint exists on machineSet: %s. Hence not removing.", machineSet.Name)
139146
return nil
140147
}
141148

142-
glog.V(2).Infof("Trying to untaint MachineSet object %q with %s to enable scheduling of pods", machineSet.Name, PreferNoSchedule)
149+
glog.V(2).Infof("Trying to untaint MachineSet object %q with %s to enable scheduling of pods", machineSet.Name, taint.Key)
143150
selector, err := metav1.LabelSelectorAsSelector(machineSet.Spec.Selector)
144151
if err != nil {
145152
return err
@@ -159,12 +166,6 @@ func (dc *controller) removeTaintNodesBackingMachineSets(machineSet *v1alpha1.Ma
159166
return err
160167
}
161168

162-
taints := v1.Taint{
163-
Key: PreferNoSchedule,
164-
Value: "True",
165-
Effect: "PreferNoSchedule",
166-
}
167-
168169
// Iterate through all machines and remove the PreferNoSchedule taint
169170
// to avoid scheduling on older machines
170171
for _, machine := range filteredMachines {
@@ -179,7 +180,7 @@ func (dc *controller) removeTaintNodesBackingMachineSets(machineSet *v1alpha1.Ma
179180
dc.targetCoreClient,
180181
machine.Status.Node,
181182
node,
182-
&taints,
183+
taint,
183184
)
184185
if err != nil {
185186
glog.Warningf("Node taint removal failed for node: %s, Error: %s", machine.Status.Node, err)
@@ -202,7 +203,7 @@ func (dc *controller) removeTaintNodesBackingMachineSets(machineSet *v1alpha1.Ma
202203
}
203204

204205
msCopy := machineSet.DeepCopy()
205-
delete(msCopy.Annotations, PreferNoSchedule)
206+
delete(msCopy.Annotations, taint.Key)
206207

207208
machineSet, err = dc.controlMachineClient.MachineSets(msCopy.Namespace).Update(msCopy)
208209

@@ -219,7 +220,7 @@ func (dc *controller) removeTaintNodesBackingMachineSets(machineSet *v1alpha1.Ma
219220
// Break out of loop when update succeeds
220221
break
221222
}
222-
glog.V(2).Infof("Removed taint %s from MachineSet object %q", PreferNoSchedule, machineSet.Name)
223+
glog.V(2).Infof("Removed taint %s from MachineSet object %q", taint.Key, machineSet.Name)
223224

224225
return nil
225226
}

pkg/controller/deployment_rollback_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import (
2121
. "github.com/onsi/ginkgo/extensions/table"
2222
. "github.com/onsi/gomega"
2323
corev1 "k8s.io/api/core/v1"
24+
v1 "k8s.io/api/core/v1"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
)
2728

2829
var _ = Describe("deployment_rollback", func() {
2930

30-
Describe("#removeTaintNodesBackingMachineSets", func() {
31+
Describe("#removeTaintNodesBackingMachineSet", func() {
3132
type setup struct {
3233
nodes []*corev1.Node
3334
machineSets []*machinev1.MachineSet
@@ -67,7 +68,7 @@ var _ = Describe("deployment_rollback", func() {
6768
nil,
6869
nil,
6970
map[string]string{
70-
PreferNoSchedule: "True",
71+
PreferNoScheduleKey: "True",
7172
},
7273
)
7374

@@ -93,7 +94,13 @@ var _ = Describe("deployment_rollback", func() {
9394
defer trackers.Stop()
9495
waitForCacheSync(stop, controller)
9596

96-
err := controller.removeTaintNodesBackingMachineSets(data.action)
97+
err := controller.removeTaintNodesBackingMachineSet(
98+
data.action,
99+
&v1.Taint{
100+
Key: PreferNoScheduleKey,
101+
Value: "True",
102+
Effect: "PreferNoSchedule",
103+
})
97104

98105
if !data.expect.err {
99106
Expect(err).To(BeNil())
@@ -129,7 +136,7 @@ var _ = Describe("deployment_rollback", func() {
129136
&corev1.NodeSpec{
130137
Taints: []corev1.Taint{
131138
corev1.Taint{
132-
Key: PreferNoSchedule,
139+
Key: PreferNoScheduleKey,
133140
Value: "True",
134141
Effect: "PreferNoSchedule",
135142
},
@@ -154,7 +161,7 @@ var _ = Describe("deployment_rollback", func() {
154161
nil,
155162
nil,
156163
map[string]string{
157-
PreferNoSchedule: "True",
164+
PreferNoScheduleKey: "True",
158165
},
159166
)[0],
160167
expect: expect{

pkg/controller/deployment_rolling.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ func (dc *controller) rolloutRolling(d *v1alpha1.MachineDeployment, isList []*v1
5050
}
5151
allISs := append(oldISs, newIS)
5252

53-
err = dc.taintNodesBackingMachineSets(oldISs)
53+
err = dc.taintNodesBackingMachineSets(
54+
oldISs, &v1.Taint{
55+
Key: PreferNoScheduleKey,
56+
Value: "True",
57+
Effect: "PreferNoSchedule",
58+
},
59+
)
5460
if err != nil {
55-
glog.Warningf("Failed to add %s on all nodes. Error: %s", PreferNoSchedule, err)
61+
glog.Warningf("Failed to add %s on all nodes. Error: %s", PreferNoScheduleKey, err)
5662
}
5763

5864
// Scale up, if we can.
@@ -253,15 +259,16 @@ func (dc *controller) scaleDownOldMachineSetsForRollingUpdate(allISs []*v1alpha1
253259
}
254260

255261
// taintNodesBackingMachineSets taints all nodes backing the machineSets
256-
func (dc *controller) taintNodesBackingMachineSets(MachineSets []*v1alpha1.MachineSet) error {
262+
func (dc *controller) taintNodesBackingMachineSets(MachineSets []*v1alpha1.MachineSet, taint *v1.Taint) error {
257263

258264
for _, machineSet := range MachineSets {
259265

260-
if machineSet.Annotations[PreferNoSchedule] != "" {
266+
if _, exists := machineSet.Annotations[taint.Key]; exists {
267+
// Taint exists, hence just continue
261268
continue
262269
}
263270

264-
glog.V(3).Infof("Trying to taint MachineSet object %q with %s to avoid scheduling of pods", machineSet.Name, PreferNoSchedule)
271+
glog.V(3).Infof("Trying to taint MachineSet object %q with %s to avoid scheduling of pods", machineSet.Name, taint.Key)
265272
selector, err := metav1.LabelSelectorAsSelector(machineSet.Spec.Selector)
266273
if err != nil {
267274
return err
@@ -281,20 +288,14 @@ func (dc *controller) taintNodesBackingMachineSets(MachineSets []*v1alpha1.Machi
281288
return err
282289
}
283290

284-
taints := v1.Taint{
285-
Key: PreferNoSchedule,
286-
Value: "True",
287-
Effect: "PreferNoSchedule",
288-
}
289-
290291
// Iterate through all machines and place the PreferNoSchedule taint
291292
// to avoid scheduling on older machines
292293
for _, machine := range filteredMachines {
293294
if machine.Status.Node != "" {
294295
err = AddOrUpdateTaintOnNode(
295296
dc.targetCoreClient,
296297
machine.Status.Node,
297-
&taints,
298+
taint,
298299
)
299300
if err != nil {
300301
glog.Warningf("Node tainting failed for node: %s, %s", machine.Status.Node, err)
@@ -319,7 +320,7 @@ func (dc *controller) taintNodesBackingMachineSets(MachineSets []*v1alpha1.Machi
319320
if msCopy.Annotations == nil {
320321
msCopy.Annotations = make(map[string]string, 0)
321322
}
322-
msCopy.Annotations[PreferNoSchedule] = "True"
323+
msCopy.Annotations[taint.Key] = "True"
323324

324325
_, err = dc.controlMachineClient.MachineSets(msCopy.Namespace).Update(msCopy)
325326
if err != nil && time.Now().Before(retryDeadline) {
@@ -335,7 +336,7 @@ func (dc *controller) taintNodesBackingMachineSets(MachineSets []*v1alpha1.Machi
335336
// Break out of loop when update succeeds
336337
break
337338
}
338-
glog.V(2).Infof("Tainted MachineSet object %q with %s to avoid scheduling of pods", machineSet.Name, PreferNoSchedule)
339+
glog.V(2).Infof("Tainted MachineSet object %q with %s to avoid scheduling of pods", machineSet.Name, taint.Key)
339340
}
340341

341342
return nil

pkg/controller/deployment_rolling_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
. "github.com/onsi/ginkgo/extensions/table"
2222
. "github.com/onsi/gomega"
2323
corev1 "k8s.io/api/core/v1"
24+
v1 "k8s.io/api/core/v1"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
)
@@ -91,8 +92,14 @@ var _ = Describe("deployment_rolling", func() {
9192
defer trackers.Stop()
9293
waitForCacheSync(stop, controller)
9394

94-
err := controller.taintNodesBackingMachineSets(data.action)
95-
95+
err := controller.taintNodesBackingMachineSets(
96+
data.action,
97+
&v1.Taint{
98+
Key: PreferNoScheduleKey,
99+
Value: "True",
100+
Effect: "PreferNoSchedule",
101+
},
102+
)
96103
if !data.expect.err {
97104
Expect(err).To(BeNil())
98105
} else {
@@ -164,15 +171,15 @@ var _ = Describe("deployment_rolling", func() {
164171
nil,
165172
nil,
166173
map[string]string{
167-
PreferNoSchedule: "True",
174+
PreferNoScheduleKey: "True",
168175
},
169176
),
170177
nodes: newNodes(
171178
1,
172179
&corev1.NodeSpec{
173180
Taints: []corev1.Taint{
174181
corev1.Taint{
175-
Key: PreferNoSchedule,
182+
Key: PreferNoScheduleKey,
176183
Value: "True",
177184
Effect: "PreferNoSchedule",
178185
},

pkg/controller/deployment_util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ const (
106106
// is deployment.spec.replicas + maxSurge. Used by the underlying machine sets to estimate their
107107
// proportions in case the deployment has surge replicas.
108108
MaxReplicasAnnotation = "deployment.kubernetes.io/max-replicas"
109-
// PreferNoSchedule is used to identify machineSet nodes on which PreferNoSchedule taint is added on
109+
// PreferNoScheduleKey is used to identify machineSet nodes on which PreferNoSchedule taint is added on
110110
// older machineSets during a rolling update
111-
PreferNoSchedule = "deployment.machine.sapcloud.io/prefer-no-schedule"
111+
PreferNoScheduleKey = "deployment.machine.sapcloud.io/prefer-no-schedule"
112112

113113
// RollbackRevisionNotFound is not found rollback event reason
114114
RollbackRevisionNotFound = "DeploymentRollbackRevisionNotFound"
@@ -347,7 +347,7 @@ var annotationsToSkip = map[string]bool{
347347
DesiredReplicasAnnotation: true,
348348
MaxReplicasAnnotation: true,
349349
LastReplicaUpdate: true,
350-
PreferNoSchedule: true,
350+
PreferNoScheduleKey: true,
351351
}
352352

353353
// skipCopyAnnotation returns true if we should skip copying the annotation with the given annotation key

0 commit comments

Comments
 (0)