Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use JSON merge patch to update labels/annotations #4838

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions charts/kube-ovn/templates/ovn-CR.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ rules:
- get
- list
- update
- patch
- create
- delete
- watch
Expand Down
6 changes: 3 additions & 3 deletions cmd/daemon/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ func initChassisAnno(cfg *daemon.Configuration) error {
klog.Error(err)
return err
}
annotations := map[string]any{util.ChassisAnnotation: chassesName}
if err = util.UpdateNodeAnnotations(cfg.KubeClient.CoreV1().Nodes(), cfg.NodeName, annotations); err != nil {
klog.Errorf("failed to update chassis annotation of node %s: %v", cfg.NodeName, err)
patch := util.KVPatch{util.ChassisAnnotation: chassesName}
if err = util.PatchAnnotations(cfg.KubeClient.CoreV1().Nodes(), cfg.NodeName, patch); err != nil {
klog.Errorf("failed to patch chassis annotation of node %s: %v", cfg.NodeName, err)
return err
}

Expand Down
1 change: 1 addition & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,7 @@ rules:
- get
- list
- update
- patch
- create
- delete
- watch
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1
import (
"testing"

runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"

"github.com/brianvoe/gofakeit/v7"
"github.com/stretchr/testify/require"
Expand Down
9 changes: 3 additions & 6 deletions pkg/controller/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,9 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
}

if svcVpc = svc.Annotations[util.VpcAnnotation]; svcVpc != vpcName {
if svc.Annotations == nil {
svc.Annotations = make(map[string]string, 1)
}
svc.Annotations[util.VpcAnnotation] = vpcName
if _, err = c.config.KubeClient.CoreV1().Services(namespace).Update(context.Background(), svc, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update service %s: %v", key, err)
patch := util.KVPatch{util.VpcAnnotation: vpcName}
if err = util.PatchAnnotations(c.config.KubeClient.CoreV1().Services(namespace), svc.Name, patch); err != nil {
klog.Errorf("failed to patch service %s: %v", key, err)
return err
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/external_gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (c *Controller) removeExternalGateway() error {
return err
}
for _, node := range nodes {
labels := map[string]any{util.ExGatewayLabel: "false"}
if err = util.UpdateNodeLabels(c.config.KubeClient.CoreV1().Nodes(), node.Name, labels); err != nil {
patch := util.KVPatch{util.ExGatewayLabel: "false"}
if err = util.PatchLabels(c.config.KubeClient.CoreV1().Nodes(), node.Name, patch); err != nil {
klog.Errorf("failed to patch external gw node %s: %v", node.Name, err)
return err
}
Expand Down Expand Up @@ -234,9 +234,9 @@ func (c *Controller) getGatewayChassis(config map[string]string) ([]string, erro
klog.Errorf("failed to get gw node %s, %v", gw, err)
return nil, err
}
labels := map[string]any{util.ExGatewayLabel: "true"}
if err = util.UpdateNodeLabels(c.config.KubeClient.CoreV1().Nodes(), node.Name, labels); err != nil {
klog.Errorf("failed to update annotations of node %s: %v", node.Name, err)
patch := util.KVPatch{util.ExGatewayLabel: "true"}
if err = util.PatchLabels(c.config.KubeClient.CoreV1().Nodes(), node.Name, patch); err != nil {
klog.Errorf("failed to patch annotations of node %s: %v", node.Name, err)
return nil, err
}

Expand Down
19 changes: 7 additions & 12 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,16 +521,15 @@ func (c *Controller) initDefaultProviderNetwork() error {

excludeAnno := fmt.Sprintf(util.ProviderNetworkExcludeTemplate, c.config.DefaultProviderName)
interfaceAnno := fmt.Sprintf(util.ProviderNetworkInterfaceTemplate, c.config.DefaultProviderName)
newNodes := make([]*v1.Node, 0, len(nodes))
patchNodes := make([]string, 0, len(nodes))
for _, node := range nodes {
if len(node.Annotations) == 0 {
continue
}

var newNode *v1.Node
if node.Annotations[excludeAnno] == "true" {
pn.Spec.ExcludeNodes = append(pn.Spec.ExcludeNodes, node.Name)
newNode = node.DeepCopy()
patchNodes = append(patchNodes, node.Name)
} else if s := node.Annotations[interfaceAnno]; s != "" {
var index *int
for i := range pn.Spec.CustomInterfaces {
Expand All @@ -545,12 +544,7 @@ func (c *Controller) initDefaultProviderNetwork() error {
ci := kubeovnv1.CustomInterface{Interface: s, Nodes: []string{node.Name}}
pn.Spec.CustomInterfaces = append(pn.Spec.CustomInterfaces, ci)
}
newNode = node.DeepCopy()
}
if newNode != nil {
delete(newNode.Annotations, excludeAnno)
delete(newNode.Annotations, interfaceAnno)
newNodes = append(newNodes, newNode)
patchNodes = append(patchNodes, node.Name)
}
}

Expand All @@ -560,9 +554,10 @@ func (c *Controller) initDefaultProviderNetwork() error {
}

// update nodes only when provider network has been created successfully
for _, node := range newNodes {
if _, err := c.config.KubeClient.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update node %s: %v", node.Name, err)
patch := util.KVPatch{excludeAnno: nil, interfaceAnno: nil}
for _, node := range patchNodes {
if err := util.PatchAnnotations(c.config.KubeClient.CoreV1().Nodes(), node, patch); err != nil {
klog.Errorf("failed to patch node %s: %v", node, err)
}
}
}()
Expand Down
15 changes: 4 additions & 11 deletions pkg/controller/inspection.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package controller

import (
"context"
"fmt"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"

"github.com/kubeovn/kube-ovn/pkg/ovs"
Expand Down Expand Up @@ -48,15 +45,11 @@ func (c *Controller) inspectPod() error {
}

if !exists { // pod exists but not lsp
delete(pod.Annotations, fmt.Sprintf(util.AllocatedAnnotationTemplate, podNet.ProviderName))
delete(pod.Annotations, fmt.Sprintf(util.RoutedAnnotationTemplate, podNet.ProviderName))
patch, err := util.GenerateStrategicMergePatchPayload(oriPod, pod)
if err != nil {
klog.Errorf("failed to generate patch payload, %v", err)
return err
patch := util.KVPatch{
fmt.Sprintf(util.AllocatedAnnotationTemplate, podNet.ProviderName): nil,
fmt.Sprintf(util.RoutedAnnotationTemplate, podNet.ProviderName): nil,
}
if _, err := c.config.KubeClient.CoreV1().Pods(pod.Namespace).Patch(context.Background(), pod.Name,
types.StrategicMergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
if err = util.PatchAnnotations(c.config.KubeClient.CoreV1().Pods(pod.Namespace), pod.Name, patch); err != nil {
klog.Errorf("patch pod %s/%s failed %v during inspection", pod.Name, pod.Namespace, err)
return err
}
Expand Down
27 changes: 9 additions & 18 deletions pkg/controller/namespace.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"context"
"reflect"
"slices"
"strings"
Expand All @@ -11,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -197,32 +195,25 @@ func (c *Controller) handleAddNamespace(key string) error {
excludeIps = append(excludeIps, strings.Join(subnet.Spec.ExcludeIps, ","))
}

if len(namespace.Annotations) == 0 {
namespace.Annotations = map[string]string{}
} else if namespace.Annotations[util.LogicalSwitchAnnotation] == strings.Join(lss, ",") &&
if namespace.Annotations[util.LogicalSwitchAnnotation] == strings.Join(lss, ",") &&
namespace.Annotations[util.CidrAnnotation] == strings.Join(cidrs, ";") &&
namespace.Annotations[util.ExcludeIpsAnnotation] == strings.Join(excludeIps, ";") &&
namespace.Annotations[util.IPPoolAnnotation] == ippool {
return nil
}

namespace.Annotations[util.LogicalSwitchAnnotation] = strings.Join(lss, ",")
namespace.Annotations[util.CidrAnnotation] = strings.Join(cidrs, ";")
namespace.Annotations[util.ExcludeIpsAnnotation] = strings.Join(excludeIps, ";")

patch := util.KVPatch{
util.LogicalSwitchAnnotation: strings.Join(lss, ","),
util.CidrAnnotation: strings.Join(cidrs, ";"),
util.ExcludeIpsAnnotation: strings.Join(excludeIps, ";"),
}
if ippool == "" {
delete(namespace.Annotations, util.IPPoolAnnotation)
patch[util.IPPoolAnnotation] = nil
} else {
namespace.Annotations[util.IPPoolAnnotation] = ippool
patch[util.IPPoolAnnotation] = ippool
}

patch, err := util.GenerateStrategicMergePatchPayload(cachedNs, namespace)
if err != nil {
klog.Error(err)
return err
}
if _, err = c.config.KubeClient.CoreV1().Namespaces().Patch(context.Background(), key,
types.StrategicMergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
if err = util.PatchAnnotations(c.config.KubeClient.CoreV1().Namespaces(), key, patch); err != nil {
klog.Errorf("patch namespace %s failed %v", key, err)
}
return err
Expand Down
16 changes: 6 additions & 10 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (c *Controller) handleAddNode(key string) error {
return err
}

annotations := map[string]any{
patch := util.KVPatch{
util.IPAddressAnnotation: ipStr,
util.MacAddressAnnotation: mac,
util.CidrAnnotation: subnet.Spec.CIDRBlock,
Expand All @@ -228,7 +228,7 @@ func (c *Controller) handleAddNode(key string) error {
util.AllocatedAnnotation: "true",
util.PortNameAnnotation: portName,
}
if err = util.UpdateNodeAnnotations(c.config.KubeClient.CoreV1().Nodes(), node.Name, annotations); err != nil {
if err = util.PatchAnnotations(c.config.KubeClient.CoreV1().Nodes(), node.Name, patch); err != nil {
klog.Errorf("failed to update annotations of node %s: %v", node.Name, err)
return err
}
Expand Down Expand Up @@ -329,14 +329,10 @@ func (c *Controller) handleNodeAnnotationsForProviderNetworks(node *v1.Node) err
}

if len(node.Annotations) != 0 {
newNode := node.DeepCopy()
delete(newNode.Annotations, excludeAnno)
delete(newNode.Annotations, interfaceAnno)
if len(newNode.Annotations) != len(node.Annotations) {
if _, err = c.config.KubeClient.CoreV1().Nodes().Update(context.Background(), newNode, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update node %s: %v", node.Name, err)
return err
}
patch := util.KVPatch{excludeAnno: nil, interfaceAnno: nil}
if err = util.PatchAnnotations(c.config.KubeClient.CoreV1().Nodes(), node.Name, patch); err != nil {
klog.Errorf("failed to patch node %s: %v", node.Name, err)
return err
}
}

Expand Down
Loading
Loading