Skip to content

Commit 924c1ac

Browse files
authored
Merge pull request #505 from prashanth26/regression/issue-448
Changes missed out by PR #492
2 parents 91c52d5 + 02d7d4b commit 924c1ac

File tree

10 files changed

+454
-303
lines changed

10 files changed

+454
-303
lines changed

pkg/controller/controller_utils.go

Lines changed: 3 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ package controller
2424

2525
import (
2626
"encoding/binary"
27-
"encoding/json"
2827
"fmt"
2928
"hash/fnv"
3029
"math/rand"
@@ -33,32 +32,27 @@ import (
3332
"sync/atomic"
3433
"time"
3534

36-
"k8s.io/apimachinery/pkg/api/errors"
37-
"k8s.io/apimachinery/pkg/api/validation"
38-
3935
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
4036
machineapi "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/typed/machine/v1alpha1"
37+
fakemachineapi "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/typed/machine/v1alpha1/fake"
4138
annotationsutils "github.com/gardener/machine-controller-manager/pkg/util/annotations"
42-
conditionutils "github.com/gardener/machine-controller-manager/pkg/util/conditions"
4339
hashutil "github.com/gardener/machine-controller-manager/pkg/util/hash"
44-
taintutils "github.com/gardener/machine-controller-manager/pkg/util/taints"
4540
v1 "k8s.io/api/core/v1"
41+
"k8s.io/apimachinery/pkg/api/errors"
4642
"k8s.io/apimachinery/pkg/api/meta"
43+
"k8s.io/apimachinery/pkg/api/validation"
4744
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4845
"k8s.io/apimachinery/pkg/labels"
4946
"k8s.io/apimachinery/pkg/runtime"
5047
"k8s.io/apimachinery/pkg/types"
5148
"k8s.io/apimachinery/pkg/util/clock"
5249
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
5350
"k8s.io/apimachinery/pkg/util/sets"
54-
"k8s.io/apimachinery/pkg/util/strategicpatch"
5551
"k8s.io/apimachinery/pkg/util/wait"
5652
clientset "k8s.io/client-go/kubernetes"
5753
"k8s.io/client-go/tools/cache"
5854
"k8s.io/client-go/tools/record"
5955
clientretry "k8s.io/client-go/util/retry"
60-
61-
fakemachineapi "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/typed/machine/v1alpha1/fake"
6256
"k8s.io/klog"
6357
)
6458

@@ -904,193 +898,6 @@ func FilterMachineSets(ISes []*v1alpha1.MachineSet, filterFn filterIS) []*v1alph
904898
return filtered
905899
}
906900

907-
// AddOrUpdateTaintOnNode add taints to the node. If taint was added into node, it'll issue API calls
908-
// to update nodes; otherwise, no API calls. Return error if any.
909-
func AddOrUpdateTaintOnNode(c clientset.Interface, nodeName string, taints ...*v1.Taint) error {
910-
if len(taints) == 0 {
911-
return nil
912-
}
913-
firstTry := true
914-
return clientretry.RetryOnConflict(Backoff, func() error {
915-
var err error
916-
var oldNode *v1.Node
917-
// First we try getting node from the API server cache, as it's cheaper. If it fails
918-
// we get it from etcd to be sure to have fresh data.
919-
if firstTry {
920-
oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{ResourceVersion: "0"})
921-
firstTry = false
922-
} else {
923-
oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
924-
}
925-
if err != nil {
926-
return err
927-
}
928-
929-
var newNode *v1.Node
930-
oldNodeCopy := oldNode
931-
updated := false
932-
for _, taint := range taints {
933-
curNewNode, ok, err := taintutils.AddOrUpdateTaint(oldNodeCopy, taint)
934-
if err != nil {
935-
return fmt.Errorf("Failed to update taint of node")
936-
}
937-
updated = updated || ok
938-
newNode = curNewNode
939-
oldNodeCopy = curNewNode
940-
}
941-
if !updated {
942-
return nil
943-
}
944-
return UpdateNodeTaints(c, nodeName, oldNode, newNode)
945-
})
946-
}
947-
948-
// RemoveTaintOffNode is for cleaning up taints temporarily added to node,
949-
// won't fail if target taint doesn't exist or has been removed.
950-
// If passed a node it'll check if there's anything to be done, if taint is not present it won't issue
951-
// any API calls.
952-
func RemoveTaintOffNode(c clientset.Interface, nodeName string, node *v1.Node, taints ...*v1.Taint) error {
953-
if len(taints) == 0 {
954-
return nil
955-
}
956-
// Short circuit for limiting amount of API calls.
957-
if node != nil {
958-
match := false
959-
for _, taint := range taints {
960-
if taintutils.TaintExists(node.Spec.Taints, taint) {
961-
match = true
962-
break
963-
}
964-
}
965-
if !match {
966-
return nil
967-
}
968-
}
969-
970-
firstTry := true
971-
return clientretry.RetryOnConflict(Backoff, func() error {
972-
var err error
973-
var oldNode *v1.Node
974-
// First we try getting node from the API server cache, as it's cheaper. If it fails
975-
// we get it from etcd to be sure to have fresh data.
976-
if firstTry {
977-
oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{ResourceVersion: "0"})
978-
firstTry = false
979-
} else {
980-
oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
981-
}
982-
if err != nil {
983-
return err
984-
}
985-
986-
var newNode *v1.Node
987-
oldNodeCopy := oldNode
988-
updated := false
989-
for _, taint := range taints {
990-
curNewNode, ok, err := taintutils.RemoveTaint(oldNodeCopy, taint)
991-
if err != nil {
992-
return fmt.Errorf("Failed to remove taint of node")
993-
}
994-
updated = updated || ok
995-
newNode = curNewNode
996-
oldNodeCopy = curNewNode
997-
}
998-
if !updated {
999-
return nil
1000-
}
1001-
return UpdateNodeTaints(c, nodeName, oldNode, newNode)
1002-
})
1003-
}
1004-
1005-
// PatchNodeTaints is for updating the node taints from oldNode to the newNode
1006-
// It makes a TwoWayMergePatch by comparing the two objects
1007-
// It calls the Patch() method to do the final patch
1008-
func PatchNodeTaints(c clientset.Interface, nodeName string, oldNode *v1.Node, newNode *v1.Node) error {
1009-
oldData, err := json.Marshal(oldNode)
1010-
if err != nil {
1011-
return fmt.Errorf("failed to marshal old node %#v for node %q: %v", oldNode, nodeName, err)
1012-
}
1013-
1014-
newTaints := newNode.Spec.Taints
1015-
newNodeClone := oldNode.DeepCopy()
1016-
newNodeClone.Spec.Taints = newTaints
1017-
newData, err := json.Marshal(newNodeClone)
1018-
if err != nil {
1019-
return fmt.Errorf("failed to marshal new node %#v for node %q: %v", newNodeClone, nodeName, err)
1020-
}
1021-
1022-
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
1023-
if err != nil {
1024-
return fmt.Errorf("failed to create patch for node %q: %v", nodeName, err)
1025-
}
1026-
1027-
_, err = c.CoreV1().Nodes().Patch(string(nodeName), types.StrategicMergePatchType, patchBytes)
1028-
return err
1029-
}
1030-
1031-
// UpdateNodeTaints is for updating the node taints from oldNode to the newNode
1032-
// using the nodes Update() method
1033-
func UpdateNodeTaints(c clientset.Interface, nodeName string, oldNode *v1.Node, newNode *v1.Node) error {
1034-
newNodeClone := oldNode.DeepCopy()
1035-
newNodeClone.Spec.Taints = newNode.Spec.Taints
1036-
1037-
_, err := c.CoreV1().Nodes().Update(newNodeClone)
1038-
if err != nil {
1039-
return fmt.Errorf("failed to create update taints for node %q: %v", nodeName, err)
1040-
}
1041-
1042-
return nil
1043-
}
1044-
1045-
// GetNodeCondition get the nodes condition matching the specified type
1046-
func GetNodeCondition(c clientset.Interface, nodeName string, conditionType v1.NodeConditionType) (*v1.NodeCondition, error) {
1047-
node, err := c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
1048-
if err != nil {
1049-
return nil, err
1050-
}
1051-
return conditionutils.GetNodeCondition(node, conditionType), nil
1052-
}
1053-
1054-
// AddOrUpdateConditionsOnNode adds a condition to the node's status
1055-
func AddOrUpdateConditionsOnNode(c clientset.Interface, nodeName string, condition v1.NodeCondition) error {
1056-
firstTry := true
1057-
return clientretry.RetryOnConflict(Backoff, func() error {
1058-
var err error
1059-
var oldNode *v1.Node
1060-
// First we try getting node from the API server cache, as it's cheaper. If it fails
1061-
// we get it from etcd to be sure to have fresh data.
1062-
if firstTry {
1063-
oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{ResourceVersion: "0"})
1064-
firstTry = false
1065-
} else {
1066-
oldNode, err = c.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
1067-
}
1068-
1069-
if err != nil {
1070-
return err
1071-
}
1072-
1073-
var newNode *v1.Node
1074-
oldNodeCopy := oldNode
1075-
newNode = conditionutils.AddOrUpdateCondition(oldNodeCopy, condition)
1076-
return UpdateNodeConditions(c, nodeName, oldNode, newNode)
1077-
})
1078-
}
1079-
1080-
// UpdateNodeConditions is for updating the node conditions from oldNode to the newNode
1081-
// using the nodes Update() method
1082-
func UpdateNodeConditions(c clientset.Interface, nodeName string, oldNode *v1.Node, newNode *v1.Node) error {
1083-
newNodeClone := oldNode.DeepCopy()
1084-
newNodeClone.Status.Conditions = newNode.Status.Conditions
1085-
1086-
_, err := c.CoreV1().Nodes().UpdateStatus(newNodeClone)
1087-
if err != nil {
1088-
return fmt.Errorf("failed to create update conditions for node %q: %v", nodeName, err)
1089-
}
1090-
1091-
return nil
1092-
}
1093-
1094901
// WaitForCacheSync is a wrapper around cache.WaitForCacheSync that generates log messages
1095902
// indicating that the controller identified by controllerName is waiting for syncs, followed by
1096903
// either a successful or failed sync.

pkg/controller/deployment_rollback.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"time"
2828

2929
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
30+
"github.com/gardener/machine-controller-manager/pkg/util/nodeops"
3031
v1 "k8s.io/api/core/v1"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
"k8s.io/apimachinery/pkg/labels"
@@ -176,7 +177,7 @@ func (dc *controller) removeTaintNodesBackingMachineSet(machineSet *v1alpha1.Mac
176177
continue
177178
}
178179

179-
err = RemoveTaintOffNode(
180+
err = nodeops.RemoveTaintOffNode(
180181
dc.targetCoreClient,
181182
machine.Status.Node,
182183
node,

pkg/controller/deployment_rolling.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import (
2727
"sort"
2828
"time"
2929

30+
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
31+
"github.com/gardener/machine-controller-manager/pkg/util/nodeops"
3032
v1 "k8s.io/api/core/v1"
3133
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3234
"k8s.io/apimachinery/pkg/labels"
3335
"k8s.io/apimachinery/pkg/types"
34-
"k8s.io/utils/integer"
35-
36-
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
3736
"k8s.io/klog"
37+
"k8s.io/utils/integer"
3838
)
3939

4040
var (
@@ -322,7 +322,7 @@ func (dc *controller) taintNodesBackingMachineSets(MachineSets []*v1alpha1.Machi
322322
// to avoid scheduling on older machines
323323
for _, machine := range filteredMachines {
324324
if machine.Status.Node != "" {
325-
err = AddOrUpdateTaintOnNode(
325+
err = nodeops.AddOrUpdateTaintOnNode(
326326
dc.targetCoreClient,
327327
machine.Status.Node,
328328
taint,

pkg/controller/machine_util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"encoding/json"
2727

2828
"github.com/gardener/machine-controller-manager/pkg/apis/machine/validation"
29+
"github.com/gardener/machine-controller-manager/pkg/util/nodeops"
2930
apierrors "k8s.io/apimachinery/pkg/api/errors"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/klog"
@@ -520,7 +521,7 @@ func (c *controller) UpdateNodeTerminationCondition(machine *v1alpha1.Machine) e
520521
}
521522

522523
// check if condition already exists
523-
cond, err := GetNodeCondition(c.targetCoreClient, nodeName, NodeTerminationCondition)
524+
cond, err := nodeops.GetNodeCondition(c.targetCoreClient, nodeName, NodeTerminationCondition)
524525
if err != nil {
525526
if apierrors.IsNotFound(err) {
526527
return nil
@@ -536,7 +537,7 @@ func (c *controller) UpdateNodeTerminationCondition(machine *v1alpha1.Machine) e
536537
setTerminationReasonByPhase(machine.Status.CurrentStatus.Phase, &terminationCondition)
537538
}
538539

539-
err = AddOrUpdateConditionsOnNode(c.targetCoreClient, nodeName, terminationCondition)
540+
err = nodeops.AddOrUpdateConditionsOnNode(c.targetCoreClient, nodeName, terminationCondition)
540541
if apierrors.IsNotFound(err) {
541542
return nil
542543
}

pkg/util/conditions/conditions.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)