-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtag.go
88 lines (77 loc) · 1.77 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"log"
"os"
"github.com/koudaiii/sltd/aws/elb"
"github.com/koudaiii/sltd/kubernetes"
)
type Client struct {
kubeclient *kubernetes.KubeClient
awsclient *elb.AwsClient
}
func NewClient(inCluster bool) *Client {
return &Client{
kubeclient: kubernetes.NewKubeClient(inCluster),
awsclient: elb.NewELBClient(),
}
}
func (c *Client) process() {
namespaces, err := c.kubeclient.GetAllNamespaces()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
svc, err := c.kubeclient.GetAllServices(namespaces)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
for _, s := range svc {
// log.Println(s)
tags, err := c.awsclient.DescribeTags(s.Name)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
c.attachELBTags(tags, c.kubeclient.UpdateLabelsToDataDogFormat(exchangeTypeFromTagsToLabels(tags), s))
}
}
func (c *Client) attachELBTags(tags []elb.Tag, service kubernetes.Service) error {
for _, s := range service.Labels {
alreadyTag := false
labelToTag := &elb.Tag{
Key: s.Key,
Value: s.Value,
}
for _, t := range tags {
if t.Key == s.Key && t.Value == s.Value {
alreadyTag = true
break
}
if t.Key == s.Key {
log.Println("Replace Tag")
log.Printf("Before: %v\n", t)
log.Printf("After: %v\n", s)
c.awsclient.DeleteTag(service.Name, labelToTag.Key)
c.awsclient.AddTag(service.Name, labelToTag)
}
}
if alreadyTag {
log.Println("skip. ELB Already tagged.")
} else {
log.Println("Add Tag")
c.awsclient.AddTag(service.Name, labelToTag)
}
}
return nil
}
func exchangeTypeFromTagsToLabels(tags []elb.Tag) (labels []kubernetes.Label) {
for _, t := range tags {
labels = append(labels, kubernetes.Label{
Key: t.Key,
Value: t.Value,
})
}
return labels
}