Skip to content

Commit

Permalink
🌱 add tags vks-cluster-ids
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Apr 17, 2024
1 parent f305386 commit d3ba4dc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions pkg/utils/vngcloud/tag.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}
9 changes: 6 additions & 3 deletions pkg/vngcloud/vlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit d3ba4dc

Please sign in to comment.