mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Merge pull request #90561 from cici37/util
Copy patchNodeStatus logic to cloud-provider
This commit is contained in:
commit
e8d6d3768e
@ -10,7 +10,6 @@ go_library(
|
|||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/controller:go_default_library",
|
"//pkg/controller:go_default_library",
|
||||||
"//pkg/util/node:go_default_library",
|
|
||||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
|
@ -39,7 +39,6 @@ import (
|
|||||||
cloudproviderapi "k8s.io/cloud-provider/api"
|
cloudproviderapi "k8s.io/cloud-provider/api"
|
||||||
cloudnodeutil "k8s.io/cloud-provider/node/helpers"
|
cloudnodeutil "k8s.io/cloud-provider/node/helpers"
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
nodeutil "k8s.io/kubernetes/pkg/util/node"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// labelReconcileInfo lists Node labels to reconcile, and how to reconcile them.
|
// labelReconcileInfo lists Node labels to reconcile, and how to reconcile them.
|
||||||
@ -280,7 +279,7 @@ func (cnc *CloudNodeController) updateNodeAddress(ctx context.Context, node *v1.
|
|||||||
}
|
}
|
||||||
newNode := node.DeepCopy()
|
newNode := node.DeepCopy()
|
||||||
newNode.Status.Addresses = nodeAddresses
|
newNode.Status.Addresses = nodeAddresses
|
||||||
_, _, err = nodeutil.PatchNodeStatus(cnc.kubeClient.CoreV1(), types.NodeName(node.Name), node, newNode)
|
_, _, err = cloudnodeutil.PatchNodeStatus(cnc.kubeClient.CoreV1(), types.NodeName(node.Name), node, newNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Error patching node with cloud ip addresses = [%v]", err)
|
klog.Errorf("Error patching node with cloud ip addresses = [%v]", err)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ go_library(
|
|||||||
"address.go",
|
"address.go",
|
||||||
"conditions.go",
|
"conditions.go",
|
||||||
"labels.go",
|
"labels.go",
|
||||||
|
"status.go",
|
||||||
"taints.go",
|
"taints.go",
|
||||||
],
|
],
|
||||||
importmap = "k8s.io/kubernetes/vendor/k8s.io/cloud-provider/node/helpers",
|
importmap = "k8s.io/kubernetes/vendor/k8s.io/cloud-provider/node/helpers",
|
||||||
@ -20,6 +21,7 @@ go_library(
|
|||||||
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/util/retry:go_default_library",
|
"//staging/src/k8s.io/client-go/util/retry:go_default_library",
|
||||||
"//vendor/k8s.io/klog:go_default_library",
|
"//vendor/k8s.io/klog:go_default_library",
|
||||||
],
|
],
|
||||||
|
140
staging/src/k8s.io/cloud-provider/node/helpers/status.go
Normal file
140
staging/src/k8s.io/cloud-provider/node/helpers/status.go
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
NOTE: the contents of this file has been copied from k8s.io/kubernetes/pkg/util/node. The reason for duplicating this code is to remove
|
||||||
|
dependencies for cloud controller manager.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/api/equality"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||||
|
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PatchNodeStatus patches node status.
|
||||||
|
func PatchNodeStatus(c v1core.CoreV1Interface, nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) (*v1.Node, []byte, error) {
|
||||||
|
patchBytes, err := preparePatchBytesforNodeStatus(nodeName, oldNode, newNode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedNode, err := c.Nodes().Patch(context.TODO(), string(nodeName), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "status")
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to patch status %q for node %q: %v", patchBytes, nodeName, err)
|
||||||
|
}
|
||||||
|
return updatedNode, patchBytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func preparePatchBytesforNodeStatus(nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) ([]byte, error) {
|
||||||
|
oldData, err := json.Marshal(oldNode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to Marshal oldData for node %q: %v", nodeName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NodeStatus.Addresses is incorrectly annotated as patchStrategy=merge, which
|
||||||
|
// will cause strategicpatch.CreateTwoWayMergePatch to create an incorrect patch
|
||||||
|
// if it changed.
|
||||||
|
manuallyPatchAddresses := (len(oldNode.Status.Addresses) > 0) && !equality.Semantic.DeepEqual(oldNode.Status.Addresses, newNode.Status.Addresses)
|
||||||
|
|
||||||
|
// Reset spec to make sure only patch for Status or ObjectMeta is generated.
|
||||||
|
// Note that we don't reset ObjectMeta here, because:
|
||||||
|
// 1. This aligns with Nodes().UpdateStatus().
|
||||||
|
// 2. Some component does use this to update node annotations.
|
||||||
|
diffNode := newNode.DeepCopy()
|
||||||
|
diffNode.Spec = oldNode.Spec
|
||||||
|
if manuallyPatchAddresses {
|
||||||
|
diffNode.Status.Addresses = oldNode.Status.Addresses
|
||||||
|
}
|
||||||
|
newData, err := json.Marshal(diffNode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to Marshal newData for node %q: %v", nodeName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to CreateTwoWayMergePatch for node %q: %v", nodeName, err)
|
||||||
|
}
|
||||||
|
if manuallyPatchAddresses {
|
||||||
|
patchBytes, err = fixupPatchForNodeStatusAddresses(patchBytes, newNode.Status.Addresses)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to fix up NodeAddresses in patch for node %q: %v", nodeName, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return patchBytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixupPatchForNodeStatusAddresses adds a replace-strategy patch for Status.Addresses to
|
||||||
|
// the existing patch
|
||||||
|
func fixupPatchForNodeStatusAddresses(patchBytes []byte, addresses []v1.NodeAddress) ([]byte, error) {
|
||||||
|
// Given patchBytes='{"status": {"conditions": [ ... ], "phase": ...}}' and
|
||||||
|
// addresses=[{"type": "InternalIP", "address": "10.0.0.1"}], we need to generate:
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// "status": {
|
||||||
|
// "conditions": [ ... ],
|
||||||
|
// "phase": ...,
|
||||||
|
// "addresses": [
|
||||||
|
// {
|
||||||
|
// "type": "InternalIP",
|
||||||
|
// "address": "10.0.0.1"
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// "$patch": "replace"
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
var patchMap map[string]interface{}
|
||||||
|
if err := json.Unmarshal(patchBytes, &patchMap); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
addrBytes, err := json.Marshal(addresses)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var addrArray []interface{}
|
||||||
|
if err := json.Unmarshal(addrBytes, &addrArray); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
addrArray = append(addrArray, map[string]interface{}{"$patch": "replace"})
|
||||||
|
|
||||||
|
status := patchMap["status"]
|
||||||
|
if status == nil {
|
||||||
|
status = map[string]interface{}{}
|
||||||
|
patchMap["status"] = status
|
||||||
|
}
|
||||||
|
statusMap, ok := status.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected data in patch")
|
||||||
|
}
|
||||||
|
statusMap["addresses"] = addrArray
|
||||||
|
|
||||||
|
return json.Marshal(patchMap)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user