Debian Codegraph @

ARCHITECTURE.md

raw
1 # Architecture
2
3 * _Updated: December 2021_
4
5 This document describes the repository organization and the kustomize
6 build process. It's meant to lower the barrier to learning and
7 contributing to the code base.
8
9 If not kept up to date, it will just be a historical snapshot.
10
11
12 ## Repository layout
13
14 [human-edited docs]: https://github.com/kubernetes-sigs/cli-experimental/tree/master/site
15 [generated docs]: https://github.com/kubernetes-sigs/cli-experimental/tree/master/docs
16 [rendered docs]: https://kubectl.docs.kubernetes.io
17 [openapi]: https://kubernetes.io/blog/2016/12/kubernetes-supports-openapi
18
19 [`api` module]: https://github.com/kubernetes-sigs/kustomize/blob/master/api/go.mod
20 [`api`]: #the-api-module
21 [`cmd/config` module]: https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/go.mod
22 [`cmd/config`]: #the-cmdconfig-module
23 [`kustomize` module]: https://github.com/kubernetes-sigs/kustomize/blob/master/kustomize/go.mod
24 [`kustomize`]: #the-kustomize-module
25 [`kyaml` module]: https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/go.mod
26 [`kyaml`]: #the-kyaml-module
27 [`kyaml/kio.Filter`]: https://github.com/Kubernetes-sigs/kustomize/blob/master/kyaml/kio/kio.go
28 [`go-yaml`]: https://github.com/go-yaml/yaml/tree/v3
29
30
31 [3922]: https://github.com/kubernetes-sigs/kustomize/issues/3922
32
33
34
35 | directory | purpose |
36 | ---------: | :---------- |
37 | `api` | The [`api`] module, holding high level kustomize code, suitable for import by other programs. |
38 | `cmd` | Various Go programs aiding repo management. See also `hack`. As an outlier, includes the special [`cmd/config`] module. |
39 | `docs` | Old home of documentation; contains pointers to new homes: [human-edited docs], [generated docs] and [rendered docs]. |
40 | `examples` | Full kustomization examples that run as pre-merge tests. |
41 | `functions` | Examples of plugins in KRM function form. TODO([3922]): Move under `plugin`. |
42 | `hack` | Various shell scripts to help with code management. |
43 | `kustomize` | The [`kustomize`] module holds the `main.go` for kustomize. |
44 | `kyaml` | The [`kyaml`] module, holding Kubernetes-specific YAML editing packages used by the [`api`] module. Wraps [`go-yaml`] v3.|
45 | `plugin` | Examples of Kustomize plugins. |
46 | `releasing` | Instructions for releasing the various modules. |
47 | `site` | Old generated documentation, kept to provide redirection links to the new docs. |
48
49
50 ## Modules
51
52 [semantically versioned]: https://semver.org
53 [Go modules]: https://github.com/golang/go/wiki/Modules
54
55 The [Go modules] in the kustomize repository are [semantically versioned].
56
57
58 ### `kustomize`
59
60 > _Depends on [`api`], [`cmd/config`], [`kyaml`]_
61
62 The [`kustomize` module] contains the `main.go` for `kustomize`, buildable with
63
64 ```
65 (cd kustomize; go install .)
66 ```
67
68 [appears in kubectl]: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/kubectl/pkg/cmd/kustomize/kustomize.go
69
70 Below this are packages containing
71 [cobra](http://github.com/spf13/cobra) commands implementing `build`,
72 `edit`, `fix`, etc., packages linked together by `main.go`.
73
74 These command packages are intentionally public, semantically
75 versioned, and can be used in other programs. Specifically, the
76 `kustomize build` command [appears in kubectl] as `kubectl kustomize`.
77
78 The code in the `build` package is dominated by flag validation,
79 with minimal business logic. The critical lines are something
80 like
81
82 ```
83 # Make a kustomizer.
84 k := krusty.MakeKustomizer(
85 HonorKustomizeFlags(krusty.MakeDefaultOptions()),
86 )
87
88 # Run the kustomizer, sending location of kustomization.yaml
89 m := k.Run(fSys, "/path/to/dir")
90
91 # Write the result as YAML.
92 writer.Write(m.AsYaml())
93 ```
94
95 The `krusty` package is in the [`api`] module.
96
97 ### `api`
98
99 > _Depends on [`kyaml`] and code generated from builtin plugin modules_
100
101 The [`api` module] is used by CLI programs like `kustomize` and `kubectl`
102 to read and honor `kustomization.yaml` files and all that implies.
103
104 The main public packages in the [`api` module] are
105
106 | package | |
107 | --------: | :---------- |
108 | `filters` | Implementations of [`kyaml/kio.Filter`] used by kustomize to transform Kubernetes objects. |
109 | `konfig` | Configuration methods and constants in the kustomize API. |
110 | `krusty` | Primary API entry point. Holds the kustomizer and hundreds of tests for it. |
111 | `loader` | Loads kustomization files and the files they refer to, enforcing security rules. |
112 | `resmap` | The primary internal data structure over which the kustomizer and filters work. |
113 | `types` | The `Kustomization` object and ancillary structs. |
114
115 ### `cmd/config`
116
117 > _Depends on [`kyaml`]_
118
119 This module contains cobra commands and kyaml-based functionality to
120 provide unix-like file manipulation commands to kustomize like `grep`
121 and `tree`. These commands may be included in any program that
122 manipulates k8s YAML (e.g. kustomize).
123
124 ### `kyaml`
125
126 > _Has no in-repo dependence_
127
128 The [`kyaml` module] is a kubernetes-focussed enhancement of [go-yaml].
129
130 The YAML manipulation performed by a kustomize is based on these libraries.
131
132 These libraries evolve independently of kustomize, and other programs depend on them.
133
134 The key public packages in the [`kyaml` module] include
135
136 | package | |
137 | --------: | :---------- |
138 | `errors` | Wrapper for the go-errors/errors lib |
139 | `filesys` | A kustomize-specific file system abstraction, to ease writing tests |
140 | `fn/framework` | An SDK for writing KRM Functions in Go |
141 | `fn/runtime` | Implements the runtime for KRM Function extensions |
142 | `kio` | Libraries for reading and writing collections of Kubernetes resources as RNodes |
143 | `openapi` | Loads and accesses openapi schemas for schema-aware resource manipultaion |
144 | `resid` | Representations to aid in unique identification of Kubernetes resources |
145 | `yaml` | A Kubernetes-focused wrapper of [go-yaml], notably including the RNode object |
146
147
148 -------
149
150 ## How _kustomize build_ works
151
152 The command `kustomize build` accepts a single string argument,
153 which must resolve to a directory, possibly in a git repository,
154 called the _kustomization root_.
155
156 This directory must contain a file called `kustomization.yaml`, with
157 YAML that marshals into a single instance of a `Kustomization` object.
158
159 For the remainder of this document, the word _kustomization_ refers to
160 either of these things.
161
162 This kustomization is the access point to a directed, acyclic graph of
163 Kubernetes objects, including other kustomizations, to include in a
164 build.
165
166 Execution of `build` starts and ends in the [`api`] module,
167 frequently dipping into the [`kyaml`] module for lower level
168 YAML manipulation.
169
170 ### The `build` flow
171
172 - Validate command lines arguments and flags.
173
174 - Make a `Kustomizer` as a function of those arguments.
175
176 - Call `Run` on the kustomizer, passing it the path to the
177 kustomization.
178
179 `Run` returns an instance of `ResMap`, the `api` package's
180 representation of a set of kubernetes `Resource` objects.
181
182 This structure offers resource lookup methods (map behavior),
183 but also retains the resources in the order they were
184 specified in kustomization files (list behavior).
185
186 Post-run, the objects are fully hydrated, per the
187 instructions in the kustomization.
188
189 - Marshal the objects as YAML to a file or `stdout`.
190
191
192 ### The `Run` function
193
194 - Create various objects
195
196 - A `ResMap` factory.
197
198 Makes `ResMaps` from byte streams, other `ResMaps`, etc.
199
200 - A file `loader.Loader`.
201
202 It's fed an appropriate set of restrictions, and the path to the kustomization.
203
204 - A plugin loader.
205
206 It finds plugins (transformers, generators or validators)
207 and prepares them for running.
208
209 - A `KustTarget` encapsulating all of the above.
210
211 A KustTarget contains one `Kustomization` and represents
212 everything that kustomization can reach. This will include
213 other `KustTarget` instances, each having a smaller purview than
214 the one referencing it.
215
216 - Call `KustTarget.Load` to load its kustomization.
217
218 This step deals with deprecations and field changes.
219
220 - Load [openapi] data specified by the kustomization.
221
222 This is needed to recognize k8s kinds and their special
223 properties, e.g. which kinds are cluster-scoped, which kinds
224 refer to others, etc.
225
226 - Call `KustTarget.makeCustomizedResmap` to create the `ResMap` result.
227
228 This visits everything referenced by the kustomization,
229 performing all generation, transformation and validation.
230
231 - Finish the `Run` with
232
233 - Optional reordering of objects in `ResMap`, overriding the
234 FIFO rule.
235
236 - Optional addition of _kustomize build annotations_ to the
237 resources. E.g. from which repo and file the resource was
238 read, the fact that kustomize touched the resource, etc.
239 These kustomize-specific annotations are intended for
240 server-side data analytics, file structure traceability and
241 reconstruction, etc.
242
243 ### The `makeCustomizedResmap` function
244
245 This function starts the process of object transformation,
246 as well as accumulation of recursively referenced data.
247
248 - Call `ra := KustTarget.AccumulateTarget`.
249
250 The result, `ra`, is a resource accumulator that contains
251 everything referred to by the current kustomization, now fully
252 hydrated.
253
254 - Uniquify names of generated objects by appending content hashes.
255
256 This cannot be done until the objects are complete.
257
258 - Fix all name references (given that names may have changed).
259
260 E.g. if a ConfigMaps was given a generated name, all objects that
261 refer to that ConfigMap must be given its name.
262
263 - Resolve vars, replacing them with whatever they refer to (a legacy feature).
264
265 ### The `AccumulateTarget` function
266
267 - Call `AccumulateResources` over the `resources` field (this can recurse).
268 - Call `AccumulateComponents` over the `components` field (this can recurse),
269 - Load legacy (pre-plugin) global kustomize configuration,
270 - Load legacy (pre-openapi) _Custom Resource Definition_ data.
271 - In the context of the data loaded above, run the kustomization's
272 - generators,
273 - transformers,
274 - and validators.
275 - Accumulate `vars` (make note of them for later replacement).
276
277 ### `AccumulateResources` and component accumulation
278
279 - If the path is a file:
280 - Accumulate the objects in the file (treating them
281 as opaque kubernetes objects).
282
283 - If the path is a directory:
284 - Create a new `KustTarget` referring to that directory's kustomization.
285 - Call `subRa := KustTarget.AccumulateTarget`.
286 - Call `ra.MergeAccumulator(subRa)`
287 This completes a recursion.
288
289 - If the path is a git URL:
290 - Clone the repository to a temporary directory.
291 - Process the path optionally specified in the URL
292 as a path in the clone.
293 - If no path specified, work from the repository root.
294
295
296 That's as deep as this discussion will go.
297
298 The deeper this document goes into the details, the faster
299 it will get out of date.