From d3ba4dcfdbc7bb82f64563cddeace7b6f2163af4 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 17 Apr 2024 14:09:59 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20add=20tags=20vks-cluster-ids?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/consts/consts.go | 4 +++ pkg/ingress/controller/controller.go | 9 ++++-- pkg/utils/vngcloud/tag.go | 41 ++++++++++++++++++++++++++++ pkg/vngcloud/vlb.go | 9 ++++-- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index a59b1d5..ea3423c 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -18,6 +18,10 @@ const ( DEFAULT_L4_PACKAGE_ID = "lbp-96b6b072-aadb-4b58-9d5f-c16ad69d36aa" // ............................... DEFAULT_HTTPS_LISTENER_NAME = "vks_https_listener" DEFAULT_HTTP_LISTENER_NAME = "vks_http_listener" + VKS_TAG_KEY = "vks-cluster-ids" + VKS_TAGS_SEPARATOR = "_" + VKS_CLUSTER_ID_PREFIX = "k8s-" + VKS_CLUSTER_ID_LENGTH = 40 // DeprecatedLabelNodeRoleMaster specifies that a node is a master // It's copied over to kubeadm until it's merged in core: https://github.com/kubernetes/kubernetes/pull/39112 diff --git a/pkg/ingress/controller/controller.go b/pkg/ingress/controller/controller.go index 47aaf2f..5ea7cba 100644 --- a/pkg/ingress/controller/controller.go +++ b/pkg/ingress/controller/controller.go @@ -1506,9 +1506,6 @@ func (c *Controller) ensureSecurityGroups(oldInspect, inspect *Expander) error { } func (c *Controller) ensureTags(lbID string, tags map[string]string) error { - if len(tags) < 1 { - return nil - } // get tags of lb getTags, err := vngcloudutil.GetTags(c.vServerSC, c.getProjectID(), lbID) if err != nil { @@ -1527,6 +1524,12 @@ func (c *Controller) ensureTags(lbID string, tags map[string]string) error { tagMap[key] = value } } + vksClusterTags := tagMap[consts.VKS_TAG_KEY] + newTags := vngcloudutil.JoinVKSTag(vksClusterTags, c.getClusterID()) + if newTags != vksClusterTags { + isNeedUpdate = true + tagMap[consts.VKS_TAG_KEY] = newTags + } if !isNeedUpdate { klog.Infof("No need to update tags for lb: %v", lbID) return nil diff --git a/pkg/utils/vngcloud/tag.go b/pkg/utils/vngcloud/tag.go index c812e6f..8637e35 100644 --- a/pkg/utils/vngcloud/tag.go +++ b/pkg/utils/vngcloud/tag.go @@ -1,6 +1,9 @@ package vngcloud import ( + "strings" + + "github.com/vngcloud/cloud-provider-vngcloud/pkg/consts" "github.com/vngcloud/vngcloud-go-sdk/client" lObjects "github.com/vngcloud/vngcloud-go-sdk/vngcloud/objects" "github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/compute/v2/extensions/tag" @@ -33,3 +36,41 @@ func UpdateTags(client *client.ServiceClient, projectID string, resourceID strin klog.V(5).Infoln("[API] UpdateTags: ", "err: ", err) return err } + +func isValidVKSID(id string) bool { + return len(id) == consts.VKS_CLUSTER_ID_LENGTH && strings.HasPrefix(id, consts.VKS_CLUSTER_ID_PREFIX) +} + +func JoinVKSTag(current, id string) string { + tags := strings.Split(current, consts.VKS_TAGS_SEPARATOR) + tagsValid := make(map[string]bool) + for _, tag := range tags { + if isValidVKSID(tag) { + tagsValid[tag] = true + } + } + if isValidVKSID(id) { + tagsValid[id] = true + } + newTags := make([]string, 0) + for tag := range tagsValid { + newTags = append(newTags, tag) + } + return strings.Join(newTags, consts.VKS_TAGS_SEPARATOR) +} + +func RemoveVKSTag(current, id string) string { + tags := strings.Split(current, consts.VKS_TAGS_SEPARATOR) + tagsValid := make(map[string]bool) + for _, tag := range tags { + if isValidVKSID(tag) { + tagsValid[tag] = true + } + } + delete(tagsValid, id) + newTags := make([]string, 0) + for tag := range tagsValid { + newTags = append(newTags, tag) + } + return strings.Join(newTags, consts.VKS_TAGS_SEPARATOR) +} diff --git a/pkg/vngcloud/vlb.go b/pkg/vngcloud/vlb.go index afca49a..d51e0b8 100644 --- a/pkg/vngcloud/vlb.go +++ b/pkg/vngcloud/vlb.go @@ -944,9 +944,6 @@ func (c *vLB) ensureSecurityGroups(oldInspect, inspect *Expander) error { } func (c *vLB) ensureTags(lbID string, tags map[string]string) error { - if len(tags) < 1 { - return nil - } // get tags of lb getTags, err := vngcloudutil.GetTags(c.vServerSC, c.getProjectID(), lbID) if err != nil { @@ -965,6 +962,12 @@ func (c *vLB) ensureTags(lbID string, tags map[string]string) error { tagMap[key] = value } } + vksClusterTags := tagMap[consts.VKS_TAG_KEY] + newTags := vngcloudutil.JoinVKSTag(vksClusterTags, c.getClusterID()) + if newTags != vksClusterTags { + isNeedUpdate = true + tagMap[consts.VKS_TAG_KEY] = newTags + } if !isNeedUpdate { klog.Infof("No need to update tags for lb: %v", lbID) return nil