Skip to content

Commit

Permalink
🚀 support tags annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Apr 2, 2024
1 parent e481035 commit b51dacf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/consts/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
ServiceAnnotationEnableStickySession = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/enable-sticky-session"
ServiceAnnotationEnableTLSEncryption = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/enable-tls-encryption"
ServiceAnnotationSecurityGroups = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/security-groups"
ServiceAnnotationTags = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/tags"
)

const (
Expand Down
29 changes: 29 additions & 0 deletions pkg/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/compute/v2/server"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/certificates"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/listener"
Expand Down Expand Up @@ -492,6 +493,34 @@ func (c *API) ListSecurityGroups() ([]*lObjects.Secgroup, error) {
return resp, err
}

// TAGS
func (c *API) GetTags(resourceID string) ([]*lObjects.ResourceTag, error) {

Check failure on line 497 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: lObjects.ResourceTag

Check failure on line 497 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / lint-codebase

undefined: lObjects.ResourceTag
klog.V(5).Infoln("[API] GetTags: ", "resourceID: ", resourceID)
opt := tag.NewGetOpts(c.ProjectID, resourceID)
resp, err := tag.Get(c.VServerSC, opt)
return resp, err
}

func (c *API) UpdateTags(resourceID string, tags map[string]string) error {
klog.V(5).Infoln("[API] UpdateTags: ", "resourceID: ", resourceID, "tags: ", tags)
optItems := make([]tag.TagRequestItem, 0)

Check failure on line 506 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: tag.TagRequestItem

Check failure on line 506 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / lint-codebase

undefined: tag.TagRequestItem
for key, value := range tags {
optItems = append(optItems, tag.TagRequestItem{

Check failure on line 508 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: tag.TagRequestItem

Check failure on line 508 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / lint-codebase

undefined: tag.TagRequestItem
Key: key,
Value: value,
IsEdited: false,
})
}
opt := tag.NewUpdateOpts(c.ProjectID, resourceID, &tag.UpdateOpts{

Check failure on line 514 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: tag.NewUpdateOpts

Check failure on line 514 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: tag.UpdateOpts

Check failure on line 514 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / lint-codebase

undefined: tag.NewUpdateOpts

Check failure on line 514 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / lint-codebase

undefined: tag.UpdateOpts
ResourceType: "LOAD-BALANCER",
ResourceID: resourceID,
TagRequestList: optItems,
})
_, err := tag.Update(c.VServerSC, opt)

Check failure on line 519 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: tag.Update

Check failure on line 519 in pkg/controller/api.go

View workflow job for this annotation

GitHub Actions / lint-codebase

undefined: tag.Update
klog.V(5).Infoln("[API] UpdateTags: ", "err: ", err)
return err
}

////////////////////////////////////////////////////////////////////////////////////////////////

func (c *API) FindPolicyByName(lbID, listenerID, name string) (*lObjects.Policy, error) {
Expand Down
30 changes: 30 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ func (c *Controller) inspectIngress(ing *nwv1.Ingress) (*IngressInspect, error)
if sgs, ok := ing.Annotations[consts.ServiceAnnotationSecurityGroups]; ok {
ingressInspect.SecurityGroups = ParseStringListAnnotation(sgs, consts.ServiceAnnotationSecurityGroups)
}
if sgs, ok := ing.Annotations[consts.ServiceAnnotationTags]; ok {
ingressInspect.Tags = ParseStringMapAnnotation(sgs, consts.ServiceAnnotationTags)
}
if lbID, ok := ing.Annotations[consts.ServiceAnnotationLoadBalancerID]; ok {
ingressInspect.lbID = lbID
}
Expand Down Expand Up @@ -1171,6 +1174,10 @@ func (c *Controller) actionCompareIngress(lbID string, oldIngExpander, newIngExp
if err != nil {
klog.Errorln("error when ensure security groups", err)
}
err = c.ensureTags(lbID, newIngExpander.Tags)
if err != nil {
klog.Errorln("error when ensure security groups", err)
}

// delete redundant policy and pool if in oldIng
// with id from curLBExpander
Expand Down Expand Up @@ -1476,6 +1483,29 @@ func (c *Controller) ensureSecurityGroups(secgroups, instances []string) error {
return nil
}

func (c *Controller) ensureTags(lbID string, tags map[string]string) error {
if len(tags) < 1 {
return nil
}
// get tags of lb
getTags, err := c.api.GetTags(lbID)
if err != nil {
klog.Errorln("error when get tags", err)
return err
}
// merge tags
tagMap := make(map[string]string)
for _, tag := range getTags {
tagMap[tag.Key] = tag.Value
}
for key, value := range tags {
tagMap[key] = value
}
// update tags
err = c.api.UpdateTags(lbID, tagMap)
return err
}

func (c *Controller) getProjectID() string {
return c.extraInfo.ProjectID
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type IngressInspect struct {
ListenerExpander []*ListenerExpander
CertificateExpander []*CertificateExpander
SecurityGroups []string
InstanceIDs []string
InstanceIDs []string
Tags map[string]string
}

func (ing *IngressInspect) Print() {
Expand Down
18 changes: 18 additions & 0 deletions pkg/controller/vngcloud_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,21 @@ func ParseStringListAnnotation(s, annotation string) []string {
}
return validStr
}

func ParseStringMapAnnotation(s, annotation string) map[string]string {
if s == "" {
return map[string]string{}
}
ss := strings.Split(s, ",")
validStr := map[string]string{}
for _, v := range ss {
str := strings.TrimSpace(v)
if str != "" {
kv := strings.Split(str, "=")
if len(kv) == 2 && kv[0] != "" && kv[1] != "" {
validStr[kv[0]] = kv[1]
}
}
}
return validStr
}

0 comments on commit b51dacf

Please sign in to comment.