@@ -4,4 +4,309 @@ linkTitle: "Kustomizaton File"
44weight : 10
55description : >
66 What is the Kustomizaton file?
7- ---
7+ ---
8+
9+ [ KRM ] : https://github.com/kubernetes/design-proposals-archive/blob/main/architecture/resource-management.md
10+
11+ The kustomization file is a YAML specification of a Kubernetes
12+ Resource Model ([ KRM] ) object called a _ Kustomization_ .
13+ A kustomization describes how to generate or transform
14+ other KRM objects.
15+
16+ Although most practical kustomization files don't actually look this
17+ way, a ` kustomization.yaml ` file is basically four lists:
18+
19+ ```
20+ apiVersion: kustomize.config.k8s.io/v1beta1
21+ kind: Kustomization
22+
23+ resources:
24+ - {pathOrUrl}
25+ - ...
26+
27+ generators:
28+ - {pathOrUrl}
29+ - ...
30+
31+ transformers:
32+ - {pathOrUrl}
33+ - ...
34+
35+ validators:
36+ - {pathOrUrl}
37+ - ...
38+ ```
39+
40+ The order in each of these lists is relevant
41+ and respected.
42+
43+ > There are other fields too, e.g. ` commonLabels ` , ` namePrefixes ` ,
44+ > ` patches ` , etc. These fields are _ convenience_ fields, shorthand for
45+ > longer transformer configuration stanzas, and are discussed later.
46+ > They're what's used most often, but it's useful to first cover
47+ > the fundamentals before discussing the conveniences.
48+
49+ In all cases the ` {pathOrUrl} ` list entry can specify
50+
51+ - a file system path to a YAML _ file_ containing one or
52+ more KRM objects, or
53+ - a _ directory_ (local or in a remote git repo)
54+ that contains a ` kustomization.yaml ` file.
55+
56+ In the latter case, the kustomization is recursively built (aka
57+ _ hydrated_ ) into a flat list of KRM objects that's effectively
58+ injected into the encapsulating list in order. When this happens, the
59+ encapsulating kustomization can be called an _ overlay_ , and what it
60+ refers to can be called a _ base_ .
61+
62+ A typical layout:
63+
64+ ```
65+ app1/
66+ kustomization.yaml
67+ | resources:
68+ | - ../base
69+ | patches:
70+ | - patch1.yaml
71+ patch1.yaml
72+
73+ app2/
74+ kustomization.yaml
75+ | resources:
76+ | - ../base
77+ | patches:
78+ | - patch2.yaml
79+ patch2.yaml
80+
81+ base/
82+ kustomization.yaml
83+ | resources:
84+ | - deployment.yaml
85+ | - configMap.yaml
86+ deployment.yaml
87+ configMap.yaml
88+ ```
89+
90+ [ mainly useful ] : https://github.com/kubernetes-sigs/kustomize/blob/master/api/krusty/inlinetransformer_test.go#L26
91+
92+ Under ` resources ` , the result of reading KRM yaml files or executing
93+ recursive kustomizations becomes the list of _ input objects_ to the
94+ current build stage.
95+
96+ Under ` generators ` , ` transformers ` and ` validators ` , the result of
97+ reading/hydrating is a list of KRM objects that _ configure operations_
98+ that kustomize is expected to perform.
99+
100+ > Some of these fields allow YAML inlining, allowing a KRM object to be
101+ > declared directly in the ` kustomization.yaml ` file (in practice this
102+ > is [ mainly useful] in the ` transformers ` field).
103+
104+ These configurations specify some executable (e.g. a plugin) along
105+ with that executable's _ configuration_ . For example, a replica count
106+ transformer's configuration must specify both an executable capable
107+ of parsing and modifying a _ Deployment_ , and the actual numerical
108+ value (or increment) to use in the Deployment's ` replicas ` field.
109+
110+ ### Ordering
111+
112+ A build stage first processes ` resources ` , then it processes ` generators ` ,
113+ adding to the resource list under consideration, then it processes
114+ ` transformers ` to modify the list, and finally runs ` validators ` to check the
115+ list for whatever error.
116+
117+ ### Conveniences
118+
119+ The ` resources ` field is a convenience. One can omit ` resources `
120+ field and instead use a generator that accepts a file path list,
121+ expanding it as needed. Such a generator would read the file system,
122+ doing the job that kustomize does when processing the ` resources `
123+ field.
124+
125+ All the other fields in a kustomization file (` configMapGenerator ` ,
126+ ` namePrefix ` , ` patches ` , etc.) are conveniences as well, as they are
127+ shorthand for: run a particular generator or transformer with a
128+ particular configuration.
129+
130+ Likewise, a ` validator ` is just a transformer that doesn't transform,
131+ but can (just like a transformer) _ fail the build_ with an error
132+ message. Coding up a validator is identical to coding up a
133+ transformer. The only difference is in how it's used by kustomize;
134+ kustomize attempts to disallow validators from making changes.
135+
136+ The next section explains why the ` generators ` field is also just a
137+ convenience.
138+
139+ ### Generators and Transformers
140+
141+ In the code, the interfaces distinguishing a generator from a
142+ transformer are:
143+
144+ ```
145+ // Generator creates an instance of ResMap.
146+ type Generator interface {
147+ Generate() (ResMap, error)
148+ }
149+
150+ // Transformer can modify an instance of ResMap.
151+ type Transformer interface {
152+ Transform(m ResMap) error
153+ }
154+ ```
155+
156+ In these interfaces, a ` ResMap ` is a list of kubernetes ` Resource ` s
157+ with ancillary map-like lookup and modification methods.
158+
159+ A generator cannot be a transformer, because it doesn't accept an
160+ input other than its own configuration. Configuration for both generators
161+ and transformers are done via a distinct (and common) interface.
162+
163+ A transformer doesn't implement ` Generator ` , but it's capable of
164+ behaving like one.
165+
166+ This is because ` ResMap ` has the methods
167+
168+ ```
169+ Append(*Resource)
170+ Replace(*Resource)
171+ Remove(ResId)
172+ Clear()
173+ ...etc.
174+ ```
175+ i.e. the ResMap interface allows for growing and shrinking
176+ the Resource list, as well as mutating each Resource on it.
177+
178+ A transformer (specifically the author of a transformer)
179+ can call these methods - creating, sorting, destroying, etc.
180+
181+ [ kyaml ] : https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/doc.go
182+
183+ > At the time of writing, the ResMap is being converted to a mutable
184+ > list RNodes, objects that integrate KRM with a new
185+ > kubernetes-specific YAML library called [ kyaml] . As more programs
186+ > speak kyaml, kustomize's role will evolve too.
187+
188+ Transformers have a general generative power.
189+
190+ A kustomization overlay, could, say, fix common oversights made in
191+ cluster configuration.
192+
193+ For example, a transformer could scan all resources, looking for the
194+ _ need_ for a ` PodDisruptionBudget ` , and _ conditionally_ add it
195+ and hook it up as a guard rail for the user.
196+
197+ ### Everything is a transformer
198+
199+ Every field in a kustomization file could be expressed as a
200+ transformer, so any kustomization file can be converted to a
201+ kustomization file with one ` transformers: ` field.
202+
203+ So why keep all these fields?
204+
205+ The fields in kustomization file are useful for ordering and
206+ signalling, e.g. _ these particular things are transformers, and should
207+ run after the generators, but before the validators_ .
208+
209+ Also, they make common use cases easier to express.
210+
211+ E.g. the following two YAML stanzas do the exactly the same thing if
212+ added to a kustomization file:
213+
214+ ```
215+ namePrefix: bob-
216+ ```
217+
218+ ```
219+ transformers:
220+ - |-
221+ apiVersion: builtin
222+ kind: PrefixSuffixTransformer
223+ metadata:
224+ name: myFancyNamePrefixer
225+ prefix: bob-
226+ fieldSpecs:
227+ - path: metadata/name
228+ ```
229+
230+
231+ ### Transformed transformers
232+
233+ The arguments to ` resources ` are usually files containing instances of
234+ _ Deployment_ , _ Service_ , _ PodDisruptionBudget_ , etc., but they could
235+ also be transformer configurations.
236+
237+ In this case the transformer configurations are just grist for the
238+ kustomization mill, and can be modifed and passed up an overlay stack,
239+ and later be used to as input in a ` transformers ` field, whereupon
240+ they'll be applied to any resources at that kustomization stage.
241+
242+ For example, the following file layout has two apps using a common
243+ pair of bases.
244+
245+ One base contains a deployment and a configMap. The other contains
246+ transformer configurations. This is a means to specify a set of
247+ reusable, custom transformer configs.
248+
249+ In between the apps and these bases are intermediate overlays
250+ that transform the base transformer configurations before they are
251+ used in the top level apps.
252+
253+ > ```
254+ > app1/
255+ > kustomization.yaml
256+ > | resources:
257+ > | - ../base/resources
258+ > | transformers:
259+ > | - ../transformers1
260+ > | patches:
261+ > | - patch1.yaml
262+ > patch1.yaml
263+ > | {a patch for resources}
264+ >
265+ > app2/
266+ > kustomization.yaml
267+ > | resources:
268+ > | - ../base/resources
269+ > | transformers:
270+ > | - ../transformers2
271+ > | patches:
272+ > | - patch2.yaml
273+ > patch2.yaml
274+ > | {some other patch for the resources}
275+ >
276+ > transformers1/
277+ > kustomization.yaml
278+ > | resources:
279+ > | - ../base/transformers
280+ > transformerPatch1.yaml
281+ > | {a patch for the base transformer configs}
282+ >
283+ > transformers2/
284+ > kustomization.yaml
285+ > | resources:
286+ > | - ../base/transformers
287+ > transformerPatch1.yaml
288+ > | {some other patch for the base transformer configs}
289+ >
290+ > base/
291+ > transformers/
292+ > kustomization.yaml
293+ > | resources:
294+ > | - transformerConfig1.yaml
295+ > | - transformerConfig2.yaml
296+ > transformerConfig1.yaml
297+ > transformerConfig2.yaml
298+ > resources/
299+ > kustomization.yaml
300+ > | resources:
301+ > | - deployment.yaml
302+ > | - configMap.yaml
303+ > deployment.yaml
304+ > configMap.yaml
305+ > ```
306+
307+ This isn't a recommended or disallowed practice, but something that's
308+ allowed by how kustomization fields are processed.
309+
310+ [References]: /docs/reference/api/kustomization-file/
311+
312+ For a detailed explanation of all available fields in *Kustomization*, check out [References].
0 commit comments