@@ -24,7 +24,6 @@ package controller
2424
2525import (
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.
0 commit comments