/
create.go
346 lines (295 loc) · 8.15 KB
/
create.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package reproducer
import (
"encoding/json"
"fmt"
"log"
"path/filepath"
"reflect"
"sort"
"strings"
"time"
"github.com/datawire/ambassador/pkg/kates"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var createCmd = &cobra.Command{
Use: "create ( <snapshot> | <extraction> | <archive> )",
Short: "create produces a working set of manifests based on snapshots and/or extractions produce by the extract subcommand",
Long: `The create subcommand is designed for creating high fidelity reproductions of a source cluster using edge-stack. All of the ambassador inputs are recreated exactly as they are in the source cluster. All the services in the source cluster are also recreated, but they are transformed to point to a single set of pods with a "service: reproducer" label. This allows for a high fidelity working reproduction of the source cluster without requiring access to any of the propriety deployments in the source cluster.
The output of the create command can be passed directly to kubectl, e.g.:
reproducer create sanitized.tgz | kubectl apply -f -
You can also save the output and hand edit it should you need to tweak some of the details:
reproducer create sanitized.tgz > repro.yaml
vi repro.yaml
kubectl apply -f repro.yaml
`,
Args: cobra.ExactArgs(1),
RunE: create,
}
func create(cmd *cobra.Command, args []string) error {
filename := args[0]
extensions := map[string]bool{
".yml": true,
".yaml": true,
".json": true,
}
repro := NewRepro()
err := search(filename, func(path, contentType, encoding string, content []byte) error {
base := filepath.Base(path)
ext := filepath.Ext(base)
name := base[:len(base)-len(ext)]
if !extensions[ext] {
log.Printf("skipping %s", path)
return nil
}
if !(name == "snapshot" || name == "manifests") {
log.Printf("skipping %s", path)
return nil
}
log.Printf("found resources from %s", path)
switch name {
case "snapshot":
var snapshot struct {
Kubernetes map[string][]*kates.Unstructured
}
err := json.Unmarshal(content, &snapshot)
if err != nil {
return errors.Wrapf(err, "decoding snapshot at %s", path)
}
for _, values := range snapshot.Kubernetes {
for _, resource := range values {
err := repro.Add(resource)
if err != nil {
return errors.Wrapf(err, "adding resource")
}
}
}
case "manifests":
resources, err := kates.ParseManifests(string(content))
if err != nil {
return errors.Wrapf(err, "decoding manifests at %s", path)
}
for _, resource := range resources {
err := repro.Add(resource)
if err != nil {
return errors.Wrapf(err, "adding resource")
}
}
}
return breakSearch
})
if err == nil {
return errors.Errorf("unable to find suitable snapshot in %s", filename)
}
if err != breakSearch {
return err
}
// Process all the resources we found.
err = repro.Process()
if err != nil {
return err
}
// Marshal all the transformed resources.
marshalled, err := repro.Marshal()
if err != nil {
return err
}
fmt.Print(string(marshalled))
return nil
}
type Repro struct {
Resources map[string][]*kates.Unstructured
Namespaces map[string]bool
Ports map[string]bool
Processed []*kates.Unstructured
}
func NewRepro() *Repro {
return &Repro{
Resources: map[string][]*kates.Unstructured{},
Namespaces: map[string]bool{},
Ports: map[string]bool{},
}
}
// Add an input resource from the source cluster.
func (r *Repro) Add(resource kates.Object) error {
un, err := kates.NewUnstructuredFromObject(resource)
if err != nil {
return err
}
gvk := resource.GetObjectKind().GroupVersionKind()
r.Resources[gvk.Kind] = append(r.Resources[gvk.Kind], un)
return nil
}
func (r *Repro) Process() error {
// Process resources in order.
for _, key := range r.OrderedKinds() {
values, ok := r.Resources[key]
if ok {
delete(r.Resources, key)
for _, resource := range values {
p := r.callProcess(resource)
if p != nil {
r.Processed = append(r.Processed, p)
}
}
}
}
// Remove any included namespaces
for _, p := range r.Processed {
if p.GetObjectKind().GroupVersionKind().Kind == "Namespace" {
delete(r.Namespaces, p.GetName())
}
}
// Auto create any missing namespaces and prepend so they are defined before being used.
ns := []*kates.Unstructured{}
for _, k := range sortedKeys(r.Namespaces) {
un, err := kates.NewUnstructuredFromObject(&kates.Namespace{
TypeMeta: kates.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: kates.ObjectMeta{Name: k},
})
if err != nil {
return errors.Wrapf(err, "error creating namespace %s", k)
}
ns = append(ns, un)
}
r.Processed = append(ns, r.Processed...)
return nil
}
// OrderedKinds returns all the k8s kinds in the proper order to avoid dangling references.
func (r *Repro) OrderedKinds() []string {
return append([]string{
"CustomResourceDefinition",
"Namespace",
"ServiceAccount",
"ClusterRole",
"ClusterRoleBinding",
"Role",
"RoleBinding",
"Secret",
},
sortedKeys(r.Resources)...)
}
func (r *Repro) callProcess(resource *kates.Unstructured) *kates.Unstructured {
if len(resource.GetOwnerReferences()) > 0 {
return nil
}
if resource.GetNamespace() == "kube-system" {
return nil
}
gvk := resource.GetObjectKind().GroupVersionKind()
switch gvk.Kind {
case "APIService":
return nil
case "ComponentStatus":
return nil
case "EndpointSlice":
return nil
case "Endpoints":
return nil
case "Event":
return nil
case "Lease":
return nil
case "Node":
return nil
case "NodeMetrics":
return nil
case "PodMetrics":
return nil
case "StorageClass":
return nil
case "PriorityClass":
return nil
}
obj, err := kates.NewObjectFromUnstructured(resource)
if err != nil {
log.Printf("error processing object: %+v", err)
return nil
}
obj = r.process(obj)
// convert back to unstructured so we serialize prettier, e.g. no creationTimestamp: null
result, err := kates.NewUnstructuredFromObject(obj)
if err != nil {
log.Printf("error making unstructured from object: %+v", err)
return nil
}
return clean(result)
}
func (r *Repro) process(object kates.Object) kates.Object {
r.Namespaces[object.GetNamespace()] = true
rbac := false
switch obj := object.(type) {
case *kates.Service:
obj.Spec.ClusterIP = ""
if !isAmbassadorResource(object) {
obj.Spec.Selector = map[string]string{
"service": "reproducer",
}
for _, port := range obj.Spec.Ports {
r.Ports[port.TargetPort.String()] = true
}
}
case *kates.ClusterRole:
rbac = true
case *kates.ClusterRoleBinding:
rbac = true
case *kates.Role:
rbac = true
case *kates.RoleBinding:
rbac = true
case *kates.ServiceAccount:
rbac = true
if obj.GetName() == "default" {
return nil
}
}
if rbac && strings.Contains(object.GetName(), "system:") {
return nil
}
return object
}
const lastApplied = "kubectl.kubernetes.io/last-applied-configuration"
const bootstrappingLabel = "kubernetes.io/bootstrapping"
// Clean does generic cleanup of resources from the source cluster. Kubectl and/or the API server
// will add a bunch of annotations about last-applied-configurations and managed fields and what
// not, and these annotations will make kubectl and/or the API server barf if present on a resource
// supplied to `kubectl apply`.
func clean(resource *kates.Unstructured) *kates.Unstructured {
if resource == nil {
return nil
}
ann := resource.GetAnnotations()
if ann == nil {
ann = map[string]string{}
}
delete(ann, lastApplied)
labels := resource.GetLabels()
_, ok := labels[bootstrappingLabel]
if ok {
return nil
}
if len(ann) > 0 {
resource.SetAnnotations(ann)
} else {
resource.SetAnnotations(nil)
}
resource.SetManagedFields(nil)
resource.SetCreationTimestamp(kates.Time{Time: time.Time{}})
resource.SetUID("")
resource.SetResourceVersion("")
resource.SetSelfLink("")
resource.SetGeneration(0)
delete(resource.Object, "status")
return resource
}
func (r *Repro) Marshal() ([]byte, error) {
return marshalManifests(r.Processed)
}
func sortedKeys(m interface{}) (result []string) {
mval := reflect.ValueOf(m)
for _, v := range mval.MapKeys() {
result = append(result, v.Interface().(string))
}
sort.Strings(result)
return
}