From bc1fcc608e55be0fc2aecc3997996b2457c68a58 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Fri, 14 Apr 2017 13:54:47 -0700 Subject: [PATCH] switch kube subnet manager to PATCH --- Documentation/kube-flannel-rbac.yml | 7 ++++++- subnet/kube/kube.go | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Documentation/kube-flannel-rbac.yml b/Documentation/kube-flannel-rbac.yml index 9f328b2db7..d66465cdd4 100644 --- a/Documentation/kube-flannel-rbac.yml +++ b/Documentation/kube-flannel-rbac.yml @@ -20,8 +20,13 @@ rules: - nodes verbs: - list - - update - watch + - apiGroups: + - "" + resources: + - nodes/status + verbs: + - patch --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 diff --git a/subnet/kube/kube.go b/subnet/kube/kube.go index 9d38502797..5261da8aea 100644 --- a/subnet/kube/kube.go +++ b/subnet/kube/kube.go @@ -29,12 +29,14 @@ import ( "github.com/golang/glog" "golang.org/x/net/context" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/client/cache" clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" "k8s.io/kubernetes/pkg/client/restclient" "k8s.io/kubernetes/pkg/controller/framework" "k8s.io/kubernetes/pkg/runtime" utilruntime "k8s.io/kubernetes/pkg/util/runtime" + "k8s.io/kubernetes/pkg/util/strategicpatch" "k8s.io/kubernetes/pkg/util/wait" "k8s.io/kubernetes/pkg/watch" ) @@ -231,7 +233,31 @@ func (ksm *kubeSubnetManager) AcquireLease(ctx context.Context, network string, n.Annotations[backendDataAnnotation] = string(bd) n.Annotations[backendPublicIPAnnotation] = attrs.PublicIP.String() n.Annotations[subnetKubeManagedAnnotation] = "true" - n, err = ksm.client.Core().Nodes().Update(n) + + var oldNode, newNode v1.Node + if err := api.Scheme.Convert(cacheNode, &oldNode, nil); err != nil { + return nil, err + } + if err := api.Scheme.Convert(n, &newNode, nil); err != nil { + return nil, err + } + + oldData, err := json.Marshal(oldNode) + if err != nil { + return nil, err + } + + newData, err := json.Marshal(newNode) + if err != nil { + return nil, err + } + + patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{}) + if err != nil { + return nil, fmt.Errorf("failed to create patch for node %q: %v", ksm.nodeName, err) + } + + _, err = ksm.client.Core().Nodes().Patch(ksm.nodeName, api.StrategicMergePatchType, patchBytes, "status") if err != nil { return nil, err }