/
planquery.go
187 lines (159 loc) · 4.42 KB
/
planquery.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package terraform
import (
"context"
"fmt"
"github.com/Jeffail/gabs/v2"
"github.com/go-logr/logr"
"github.com/mmlt/environment-operator/pkg/util/exe"
"strings"
)
// GetPlan reads an existing plan and returns a json structure.
func (t *Terraform) GetPlan(ctx context.Context, env []string, dir string) (*gabs.Container, error) {
log := logr.FromContext(ctx).WithName("GetPlan")
o, _, err := exe.Run(log, &exe.Opt{Dir: dir, Env: env}, "", "terraform", "show",
"-json", planName)
if err != nil {
return nil, err
}
return gabs.ParseJSON([]byte(o))
}
// ClustersFromPlan parses a plan and returns AKSClusters that are going to be created, updated or deleted.
// The plan json conforms to https://www.terraform.io/docs/internals/json-format.html
func ClustersFromPlan(plan *gabs.Container) ([]AKSCluster, error) {
var r []AKSCluster
for _, chg := range plan.Path("resource_changes").Children() {
if chg.Path("type").Data().(string) != "azurerm_kubernetes_cluster" {
// not a cluster.
continue
}
act := stringsToAction(chg.Path("change.actions").Children())
if act == 0 {
// no change
continue
}
chgBefore := chg.Path("change.before")
if chgBefore.Data() == nil {
// no change before
continue
}
id := chgBefore.Path("id").Data().(string)
m, err := pathToMap(id)
if err != nil {
return nil, err
}
var kc string
if o := chgBefore.Path("kube_admin_config_raw").Data(); o != nil {
kc = o.(string)
}
r = append(r, AKSCluster{
ResourceGroup: m["resourcegroups"],
Cluster: m["managedclusters"],
Action: act,
KubeconfigRaw: kc,
})
}
return r, nil
}
// AKSCluster represents an AKS Cluster change.
type AKSCluster struct {
ResourceGroup string
Cluster string
Action Action
// KubeconfigRaw contains the kubeconfig of a (to be) deleted cluster.
KubeconfigRaw string
}
// Action is the terraform plan action.
type Action int
const (
ActionCreate Action = 1 << iota
ActionUpdate
ActionDelete
)
// PoolsFromPlan parses a plan and returns AKS Pools that are going to be created, updated or deleted.
// The plan json conforms to https://www.terraform.io/docs/internals/json-format.html
func PoolsFromPlan(plan *gabs.Container) ([]AKSPool, error) {
var r []AKSPool
for _, chg := range plan.Path("resource_changes").Children() {
if chg.Path("type").Data().(string) != "azurerm_kubernetes_cluster_node_pool" {
// not a pool.
continue
}
act := stringsToAction(chg.Path("change.actions").Children())
if act == 0 {
// no change
continue
}
chgBefore := chg.Path("change.before")
if chgBefore.Data() == nil {
// no change before
continue
}
var minCount, maxCount int
if v, ok := chgBefore.Path("min_count").Data().(float64); ok {
minCount = int(v)
}
if v, ok := chgBefore.Path("max_count").Data().(float64); ok {
maxCount = int(v)
}
if minCount == maxCount {
// not an autoscaling pool
continue
}
id := chgBefore.Path("id").Data().(string)
m, err := pathToMap(id)
if err != nil {
return nil, err
}
r = append(r, AKSPool{
ResourceGroup: m["resourcegroups"],
Cluster: m["managedclusters"],
Pool: m["agentpools"],
MinCount: minCount,
MaxCount: maxCount,
Action: act,
})
}
return r, nil
}
// AKSPool represents an AKS Node pool change.
type AKSPool struct {
ResourceGroup string
Cluster string
Pool string
MinCount int
MaxCount int
Action Action
}
// StringsToAction maps a slice of action strings to an Action bitmap.
// https://www.terraform.io/docs/internals/json-format.html#change-representation
func stringsToAction(in []*gabs.Container) (act Action) {
for _, a := range in {
switch a.Data().(string) {
case "create":
act |= ActionCreate
case "update":
act |= ActionUpdate
case "delete":
act |= ActionDelete
//case "no-op", "read": not used.
}
}
return
}
// PathToMap returns a map with key-value pairs from p.
// For example "/key1/value1/key2/value2" results in {"key1":"value1", "key2:"value2"}
func pathToMap(p string) (map[string]string, error) {
ss := strings.Split(p, "/")
ss = ss[1:] // a leading slash results an extra empty element
if len(ss) == 0 {
return nil, fmt.Errorf("empty input")
}
if len(ss)%2 != 0 {
return nil, fmt.Errorf("expected even number of elements")
}
r := make(map[string]string)
for i := 0; i < len(ss)-1; i += 2 {
r[strings.ToLower(ss[i])] = ss[i+1]
}
return r, nil
}