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 791f848
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.4
github.com/vngcloud/vngcloud-go-sdk v0.0.0-20240319100753-40d72148ed5d
github.com/vngcloud/vngcloud-go-sdk v0.0.1-0.20240402083045-717486a6beaa
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/vngcloud/vngcloud-go-sdk v0.0.0-20240319100753-40d72148ed5d h1:3LE7xzWmPe4feQ/kbl48G3VMpWc7ccugiq1YpHePHmE=
github.com/vngcloud/vngcloud-go-sdk v0.0.0-20240319100753-40d72148ed5d/go.mod h1:ooZo+kYuaDbn1HzS8KUEORKGj68yEtDx1XLJU06Xw4k=
github.com/vngcloud/vngcloud-go-sdk v0.0.1-0.20240401092435-a4b235c1bd91 h1:c6iyv/fMj38HWOv7rJNSlOBpyANTBIRofPkjQ8Zla9Q=
github.com/vngcloud/vngcloud-go-sdk v0.0.1-0.20240401092435-a4b235c1bd91/go.mod h1:ooZo+kYuaDbn1HzS8KUEORKGj68yEtDx1XLJU06Xw4k=
github.com/vngcloud/vngcloud-go-sdk v0.0.1-0.20240402083045-717486a6beaa h1:GBygkRokxP6847ucAFBZM2LqWnpJZgVNt6b0xcO4swg=
github.com/vngcloud/vngcloud-go-sdk v0.0.1-0.20240402083045-717486a6beaa/go.mod h1:ooZo+kYuaDbn1HzS8KUEORKGj68yEtDx1XLJU06Xw4k=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
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) {
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)
for key, value := range tags {
optItems = append(optItems, tag.TagRequestItem{
Key: key,
Value: value,
IsEdited: false,
})
}
opt := tag.NewUpdateOpts(c.ProjectID, resourceID, &tag.UpdateOpts{
ResourceType: "LOAD-BALANCER",
ResourceID: resourceID,
TagRequestList: optItems,
})
_, err := tag.Update(c.VServerSC, opt)
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 791f848

Please sign in to comment.