-
Notifications
You must be signed in to change notification settings - Fork 2
/
label.go
172 lines (137 loc) · 4.74 KB
/
label.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package label
import (
"autolabel/logging"
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"sync"
"google.golang.org/api/compute/v1"
"google.golang.org/api/pubsub/v1"
"gopkg.in/yaml.v2"
)
type Config struct {
Projects []string
Labels map[string]map[string]string
}
func GKEInstanceAutoLabeler(ctx context.Context, _ pubsub.PubsubMessage) error {
filename, _ := filepath.Abs("./configuration.yaml")
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
var config Config
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
panic(err)
}
err = setLabelsOnInstances(ctx, config)
if err != nil {
logging.Logger.Error(err.Error())
return err
}
return nil
}
func setLabelsOnInstances(ctx context.Context, config Config) error {
computeService, err := compute.NewService(ctx)
if err != nil {
fmt.Println("Error", err)
logging.Logger.Error(fmt.Sprintf("Could not create compute service. Error: %s", err))
return err
}
var wg sync.WaitGroup
for _, project := range config.Projects {
wg.Add(1)
go evaluateProject(ctx, config, project, *computeService, &wg)
}
wg.Wait()
return nil
}
func evaluateProject(ctx context.Context, config Config, project string, computeService compute.Service, wg *sync.WaitGroup) error {
defer wg.Done()
// Only check/set labels on running instances
filters := [...]string{
"status = RUNNING",
}
zoneListCall := computeService.Zones.List(project)
zoneList, err := zoneListCall.Do()
if err != nil {
logging.Logger.Debug(fmt.Sprintf("Could not list zones in project %s Skipping...", project))
return err
}
for _, zone := range zoneList.Items {
instanceListCall := computeService.Instances.List(project, zone.Name)
instanceListCall.Filter(strings.Join(filters[:], " "))
instanceList, err := instanceListCall.Do()
if err != nil {
logging.Logger.Error(fmt.Sprintf("Could not get a list of instances in project %s zone %s", project, zone.Name))
return err
}
for _, instance := range instanceList.Items {
reconcileInstanceLabels(ctx, computeService, *instance, config, project)
}
}
return nil
}
// Check that map is a subset of another map and values are equal
func mapIsSubsetOfMap(superset map[string]string, subset map[string]string) bool {
for key, value := range subset {
if value != superset[key] {
logging.Logger.Info(fmt.Sprintf("Map %s is not a subset %s", subset, superset))
return false
}
}
return true
}
// When instance labels have different values to provided labels, use provided values
func reconcileInstanceLabels(ctx context.Context, computeService compute.Service, instance compute.Instance, config Config, project string) error {
for key, labels := range config.Labels { // Order not specified
if strings.Contains(instance.Name, key) {
if mapIsSubsetOfMap(instance.Labels, labels) {
logging.Logger.Debug(fmt.Sprintf("Correct labels are already set on %s Skipping...", instance.Name))
continue
}
zone := string([]rune(instance.Zone)[strings.LastIndex(instance.Zone, "/")+1:])
for labelKey, labelValue := range labels {
instance.Labels[labelKey] = labelValue
}
rb := &compute.InstancesSetLabelsRequest{
LabelFingerprint: instance.LabelFingerprint,
Labels: instance.Labels,
}
_, err := computeService.Instances.SetLabels(project, zone, instance.Name, rb).Context(ctx).Do()
if err != nil {
logging.Logger.Error(fmt.Sprintf("Error setting labels on %s", instance.Name))
fmt.Println(err)
return err
}
logging.Logger.Debug(fmt.Sprintf("Set labels %s on instance %s in project %s", labels, instance.Name, project))
// Assume that disk labels are replica of instance labels, so always change both together
reconcileDiskLabels(ctx, computeService, instance, labels, project)
}
}
return nil
}
// Only labels disks that match instance name. GKE does not allow to create multiple peristent disks per instance.
func reconcileDiskLabels(ctx context.Context, computeService compute.Service, instance compute.Instance, labels map[string]string, project string) error {
zone := string([]rune(instance.Zone)[strings.LastIndex(instance.Zone, "/")+1:])
resp, err := computeService.Disks.Get(project, zone, instance.Name).Context(ctx).Do()
if err != nil {
logging.Logger.Error(fmt.Sprintf("Error getting Disk labels for %s", instance.Name))
return err
}
for labelKey, labelValue := range labels {
resp.Labels[labelKey] = labelValue
}
rb := &compute.ZoneSetLabelsRequest{
LabelFingerprint: resp.LabelFingerprint,
Labels: resp.Labels,
}
_, err = computeService.Disks.SetLabels(project, zone, instance.Name, rb).Context(ctx).Do()
if err != nil {
logging.Logger.Error(fmt.Sprintf("Error setting labels Disk on %s", instance.Name))
return err
}
return nil
}