-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
245 lines (209 loc) · 7.84 KB
/
renderer.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
package plumber
import (
"context"
"embed"
"encoding/json"
"fmt"
"path"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
k8syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/filesys"
"gopkg.in/yaml.v2"
)
// BaseKustomizationPath is the location for the base kustomization.yaml file. This controller
// expects to find this file among the read files from the embed reference. This is the file
// that, after parse, is send over to all registered KMutators for further transformations.
const BaseKustomizationPath = "/kustomize/base/kustomization.yaml"
// KustomizeMutator is a function that is intended to mutate a Kustomization struct.
type KustomizeMutator func(context.Context, *types.Kustomization) error
// ObjectMutator is a function that is intended to mutate a Kubernetes object.
type ObjectMutator func(context.Context, client.Object) error
// FSMutator is a function that is intended to mutate a embed files prior to rendering them as
// a kustomize graph.
type FSMutator func(context.Context, filesys.FileSystem) error
// Renderer is a base controller to provide some tooling around rendering and creating resources
// based in a kustomize directory struct. Files are expected to be injected into this controller
// by means of an embed.FS struct. The filesystem struct, inside the embed.FS struct, is expected
// to comply with the following layout:
//
// /kustomize
// /kustomize/base/kustomization.yaml
// /kustomize/base/object_a.yaml
// /kustomize/base/object_a.yaml
// /kustomize/overlay0/kustomization.yaml
// /kustomize/overlay0/object_c.yaml
// /kustomize/overlay1/kustomization.yaml
// /kustomize/overlay1/object_d.yaml
//
// In other words, we have a base kustomization under base/ directory and each other directory is
// treated as an overlay to be applied on top of base.
type Renderer struct {
cli client.Client
from embed.FS
fowner string
unstructured bool
kmutators []KustomizeMutator
omutators []ObjectMutator
fsmutators []FSMutator
}
// NewRenderer returns a kustomize renderer reading and applying files provided by the embed.FS
// reference. Files are read from 'emb' into a filesys.FileSystem representation and then used
// as argument to Kustomize when generating objects.
func NewRenderer(cli client.Client, emb embed.FS, opts ...Option) *Renderer {
ctrl := &Renderer{
cli: cli,
from: emb,
fowner: "plumber",
}
for _, opt := range opts {
opt(ctrl)
}
return ctrl
}
// Render applies provided overlay and creates objects in the kubernetes API using internal client.
// In case of failures there is no rollback so it is possible that this ends up partially creating
// the objects (returns at the first failure). Prior to object creation this function feeds all
// registered OMutators with the objects allowing for last time adjusts. Mutations in the default
// kustomization.yaml are also executed here.
func (r *Renderer) Render(ctx context.Context, overlay string) error {
objs, err := r.parse(ctx, overlay)
if err != nil {
return fmt.Errorf("error parsing kustomize files: %w", err)
}
for _, obj := range objs {
for _, mut := range r.omutators {
if err := mut(ctx, obj); err != nil {
return fmt.Errorf("error mutating object: %w", err)
}
}
opts := []client.PatchOption{client.ForceOwnership, client.FieldOwner(r.fowner)}
err := r.cli.Patch(ctx, obj, client.Apply, opts...)
if err == nil {
continue
}
if !errors.IsNotFound(err) {
return fmt.Errorf("error patching object: %w", err)
}
if err := r.cli.Create(ctx, obj); err != nil {
return fmt.Errorf("error creating object: %w", err)
}
}
return nil
}
// parse reads kustomize files and returns them all parsed as valid client.Object structs. Loads
// everything from the embed.FS into a filesys.FileSystem instance, mutates the base kustomization
// and returns the objects as a slice of client.Object.
func (r *Renderer) parse(ctx context.Context, overlay string) ([]client.Object, error) {
virtfs, err := LoadFS(r.from)
if err != nil {
return nil, fmt.Errorf("unable to load overlay: %w", err)
}
for _, mut := range r.fsmutators {
if err := mut(ctx, virtfs); err != nil {
return nil, fmt.Errorf("error mutating filesystem: %w", err)
}
}
if err := r.mutateKustomization(ctx, virtfs); err != nil {
return nil, fmt.Errorf("error setting object name prefix: %w", err)
}
res, err := krusty.MakeKustomizer(krusty.MakeDefaultOptions()).Run(
virtfs, path.Join("kustomize", overlay),
)
if err != nil {
return nil, fmt.Errorf("error running kustomize: %w", err)
}
var objs []client.Object
for _, rsc := range res.Resources() {
if r.unstructured {
clientobj, err := r.unstructuredObject(rsc)
if err != nil {
return nil, fmt.Errorf("error converting type to unstructure: %w", err)
}
objs = append(objs, clientobj)
continue
}
clientobj, err := r.typedObject(rsc)
if err != nil {
return nil, err
}
objs = append(objs, clientobj)
}
return objs, nil
}
// unstructuredObject converts a kustomize resource into a client.Object. This is useful when
// the object is not registered in the scheme.
func (r *Renderer) unstructuredObject(rsc *resource.Resource) (client.Object, error) {
data, err := rsc.AsYAML()
if err != nil {
return nil, fmt.Errorf("error converting resource to yaml: %w", err)
}
obj := &unstructured.Unstructured{}
dec := k8syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
if _, _, err := dec.Decode(data, nil, obj); err != nil {
return nil, fmt.Errorf("error decoding unstructured object: %w", err)
}
return obj, nil
}
// typedObject converts a kustomize resource to a typed client.Object. This is done by
// marshaling the resource to yaml and then unmarshaling it to the correct type.
func (r *Renderer) typedObject(rsc *resource.Resource) (client.Object, error) {
runtimeobj, err := r.cli.Scheme().New(
schema.GroupVersionKind{
Group: rsc.GetGvk().Group,
Version: rsc.GetGvk().Version,
Kind: rsc.GetGvk().Kind,
},
)
if err != nil {
return nil, fmt.Errorf("unable to instantiate runtime object: %w", err)
}
rawjson, err := rsc.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("error marshaling resource: %w", err)
}
if err := json.Unmarshal(rawjson, runtimeobj); err != nil {
return nil, fmt.Errorf("error unmarshaling into object: %w", err)
}
clientobj, ok := runtimeobj.(client.Object)
if !ok {
// this should not happen as all runtime.Object also implement
// client.Object. keeping this as a safeguard.
gvkstr := runtimeobj.GetObjectKind().GroupVersionKind().String()
return nil, fmt.Errorf("%s is not client.Object", gvkstr)
}
return clientobj, nil
}
// mutateKustomization feeds all registered KMutators with the parsed BaseKustomizationPath.
// After feeding KMutators the output is marshaled and written back to the filesys.FileSystem.
func (r *Renderer) mutateKustomization(ctx context.Context, fs filesys.FileSystem) error {
if len(r.kmutators) == 0 {
return nil
}
olddt, err := fs.ReadFile(BaseKustomizationPath)
if err != nil {
return fmt.Errorf("error reading base kustomization: %w", err)
}
var kust types.Kustomization
if err := yaml.Unmarshal(olddt, &kust); err != nil {
return fmt.Errorf("error parsing base kustomization: %w", err)
}
for _, mut := range r.kmutators {
if err := mut(ctx, &kust); err != nil {
return fmt.Errorf("error mutating kustomization: %w", err)
}
}
newdt, err := yaml.Marshal(kust)
if err != nil {
return fmt.Errorf("error marshaling base kustomization: %w", err)
}
if err := fs.WriteFile(BaseKustomizationPath, newdt); err != nil {
return fmt.Errorf("error writing base kustomization: %w", err)
}
return nil
}