/
reconcile.go
237 lines (205 loc) · 6.23 KB
/
reconcile.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package kubernetes
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/stretchr/objx"
funk "github.com/thoas/go-funk"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)
// Order in which install different kinds of Kubernetes objects.
// Inspired by https://github.com/helm/helm/blob/8c84a0bc0376650bc3d7334eef0c46356c22fa36/pkg/releaseutil/kind_sorter.go
var kindOrder = []string{
"Namespace",
"NetworkPolicy",
"ResourceQuota",
"LimitRange",
"PodSecurityPolicy",
"PodDisruptionBudget",
"ServiceAccount",
"Secret",
"ConfigMap",
"StorageClass",
"PersistentVolume",
"PersistentVolumeClaim",
"CustomResourceDefinition",
"ClusterRole",
"ClusterRoleList",
"ClusterRoleBinding",
"ClusterRoleBindingList",
"Role",
"RoleList",
"RoleBinding",
"RoleBindingList",
"Service",
"DaemonSet",
"Pod",
"ReplicationController",
"ReplicaSet",
"Deployment",
"HorizontalPodAutoscaler",
"StatefulSet",
"Job",
"CronJob",
"Ingress",
"APIService",
}
// Reconcile extracts kubernetes Manifests from raw evaluated jsonnet <kind>/<name>,
// provided the manifests match the given regular expressions. It finds each manifest by
// recursively walking the jsonnet structure.
//
// In addition, we sort the manifests to ensure the order is consistent in each
// show/diff/apply cycle. This isn't necessary, but it does help users by producing
// consistent diffs.
func Reconcile(raw map[string]interface{}, spec v1alpha1.Spec, targets []*regexp.Regexp) (state manifest.List, err error) {
extracted, err := extract(raw)
if err != nil {
return nil, errors.Wrap(err, "flattening manifests")
}
out := make(manifest.List, 0, len(extracted))
for _, m := range extracted {
out = append(out, m)
}
// If we have any kind-name matchers, we should filter all the manifests by matching
// against their <kind>/<name> identifier.
if len(targets) > 0 {
tmp := funk.Filter(out, func(i interface{}) bool {
p := objectspec(i.(manifest.Manifest))
for _, t := range targets {
if t.MatchString(p) {
return true
}
}
return false
}).([]manifest.Manifest)
out = manifest.List(tmp)
}
// Stable output order
sort.SliceStable(out, func(i int, j int) bool {
var io, jo int
// anything that is not in kindOrder will get to the end of the install list.
for io = 0; io < len(kindOrder); io++ {
if out[i].Kind() == kindOrder[io] {
break
}
}
for jo = 0; jo < len(kindOrder); jo++ {
if out[j].Kind() == kindOrder[jo] {
break
}
}
// If Kind of both objects are at different indexes of kindOrder, sort by them
if io != jo {
return io < jo
}
// If the Kinds themselves are different (e.g. both of the Kinds are not in
// the kindOrder), order alphabetically.
if out[i].Kind() != out[j].Kind() {
return out[i].Kind() < out[j].Kind()
}
// Otherwise, order the objects by name.
return out[i].Metadata().Name() < out[j].Metadata().Name()
})
return out, nil
}
func extract(deep interface{}) (map[string]manifest.Manifest, error) {
extracted := make(map[string]manifest.Manifest)
if err := walkJSON(deep, extracted, nil); err != nil {
return nil, err
}
return extracted, nil
}
// walkJSON recurses into either a map or list, returning a list of all objects that look
// like kubernetes resources. We support resources at an arbitrary level of nesting, and
// return an error if a node is not walkable.
//
// Handling the different types is quite gross, so we split this method into a generic
// walkJSON, and then walkObj/walkList to handle the two different types of collection we
// support.
func walkJSON(ptr interface{}, extracted map[string]manifest.Manifest, path trace) error {
// check for known types
switch v := ptr.(type) {
case map[string]interface{}:
return walkObj(v, extracted, path)
case []interface{}:
return walkList(v, extracted, path)
}
return ErrorPrimitiveReached{
path: path.Base(),
key: path.Name(),
primitive: ptr,
}
}
func walkList(list []interface{}, extracted map[string]manifest.Manifest, path trace) error {
for idx, value := range list {
err := walkJSON(value, extracted, append(path, fmt.Sprintf("[%d]", idx)))
if err != nil {
return err
}
}
return nil
}
func walkObj(obj objx.Map, extracted map[string]manifest.Manifest, path trace) error {
obj = obj.Exclude([]string{"__ksonnet"}) // remove our private ksonnet field
// This looks like a kubernetes manifest, so make one and return it
if isKubernetesManifest(obj) {
m, err := manifest.NewFromObj(obj)
if err != nil {
return err.(*manifest.SchemaError).WithName(path.Full())
}
extracted[path.Full()] = m
return nil
}
for key, value := range obj {
path := append(path, key)
if value == nil { // result from false if condition in Jsonnet
continue
}
err := walkJSON(value, extracted, path)
if err != nil {
return err
}
}
return nil
}
type trace []string
func (t trace) Full() string {
return "." + strings.Join(t, ".")
}
func (t trace) Base() string {
if len(t) > 0 {
t = t[:len(t)-1]
}
return "." + strings.Join(t, ".")
}
func (t trace) Name() string {
if len(t) > 0 {
return t[len(t)-1]
}
return ""
}
// ErrorPrimitiveReached occurs when walkJSON reaches the end of nested dicts without finding a valid Kubernetes manifest
type ErrorPrimitiveReached struct {
path, key string
primitive interface{}
}
func (e ErrorPrimitiveReached) Error() string {
return fmt.Sprintf("recursion did not resolve in a valid Kubernetes object. "+
" In path `%s` found key `%s` of type `%T` instead.",
e.path, e.key, e.primitive)
}
// isKubernetesManifest attempts to infer whether the given object is a valid kubernetes
// resource by verifying the presence of apiVersion and kind. These two
// fields are required for kubernetes to accept any resource.
//
// In future, it might be a good idea to allow users to opt their object out of being
// interpreted as a kubernetes resource, perhaps with a field like `exclude: true`. For
// now, any object within the jsonnet output that quacks like a kubernetes resource will
// be provided to the kubernetes API.
func isKubernetesManifest(obj objx.Map) bool {
return obj.Get("apiVersion").IsStr() && obj.Get("apiVersion").Str() != "" &&
obj.Get("kind").IsStr() && obj.Get("kind").Str() != ""
}