Debian Codegraph @

api/krusty/customconfigreusable_test.go

raw
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package krusty_test
5
6 import (
7 "testing"
8
9 kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
10 "sigs.k8s.io/kustomize/api/types"
11 )
12
13 // Demo custom configuration as a base.
14 // This test shows usage of three custom configurations sitting
15 // in a base, allowing them to be reused in any number of
16 // kustomizations.
17 func TestReusableCustomTransformers(t *testing.T) {
18 th := kusttest_test.MakeEnhancedHarness(t).
19 PrepBuiltin("AnnotationsTransformer").
20 PrepBuiltin("LabelTransformer")
21 th.GetPluginConfig().BpLoadingOptions = types.BploUseStaticallyLinked
22 defer th.Reset()
23
24 // First write three custom configurations for builtin plugins.
25
26 // A custom name prefixer that only touches Deployments and Services.
27 th.WriteF("mytransformers/deploymentServicePrefixer.yaml", `
28 apiVersion: builtin
29 kind: PrefixSuffixTransformer
30 metadata:
31 name: myPrefixer
32 prefix: bob-
33 fieldSpecs:
34 - kind: Deployment
35 path: metadata/name
36 - kind: Service
37 path: metadata/name
38 `)
39
40 // A custom annotator exclusively annotating Roles.
41 th.WriteF("mytransformers/roleAnnotator.yaml", `
42 apiVersion: builtin
43 kind: AnnotationsTransformer
44 metadata:
45 name: myAnnotator
46 annotations:
47 # Imagine that SRE has not approved this role yet.
48 status: probationary
49 fieldSpecs:
50 - path: metadata/annotations
51 create: true
52 kind: Role
53 `)
54
55 // A custom labeller that only labels Deployments,
56 // and only labels them at their top metadata level
57 // exclusively. It does not modify selectors or
58 // add labels to pods in the template.
59 th.WriteF("mytransformers/deploymentLabeller.yaml", `
60 apiVersion: builtin
61 kind: LabelTransformer
62 metadata:
63 name: myLabeller
64 labels:
65 pager: 867-5301
66 fieldSpecs:
67 - path: metadata/labels
68 create: true
69 kind: Deployment
70 `)
71
72 // Combine these custom transformers in one kustomization file.
73 // This kustomization file contains only resources that
74 // all happen to be plugin configurations. This makes
75 // these plugins re-usable as a group in any number of other
76 // kustomizations.
77 th.WriteK("mytransformers", `
78 resources:
79 - deploymentServicePrefixer.yaml
80 - roleAnnotator.yaml
81 - deploymentLabeller.yaml
82 `)
83
84 // Finally, define the kustomization for the (arbitrarily named)
85 // staging environment.
86 th.WriteK("staging", `
87
88 # Bring in the custom transformers.
89 transformers:
90 - ../mytransformers
91
92 # Also use the "classic" labeller, which behind the scenes uses
93 # the LabelTransformer, but with a broad configuration targeting
94 # many resources and fields (including selector fields).
95 # It's a big hammer - probably too big; the output shows all the
96 # places 'acmeCorp' now appears as a result. To avoid this,
97 # define your own config for your own LabelTransformer instance
98 # as shown above.
99 commonLabels:
100 company: acmeCorp
101
102 # Specify the resources to modify.
103 resources:
104 - deployment.yaml
105 - role.yaml
106 - service.yaml
107 `)
108 th.WriteF("staging/deployment.yaml", `
109 apiVersion: apps/v1
110 kind: Deployment
111 metadata:
112 name: myDeployment
113 spec:
114 template:
115 metadata:
116 labels:
117 backend: flawless
118 spec:
119 containers:
120 - name: whatever
121 image: whatever
122 `)
123 th.WriteF("staging/role.yaml", `
124 apiVersion: v1
125 kind: Role
126 metadata:
127 name: myRole
128 `)
129 th.WriteF("staging/service.yaml", `
130 apiVersion: v1
131 kind: Service
132 metadata:
133 name: myService
134 `)
135
136 m := th.Run("staging", th.MakeDefaultOptions())
137 th.AssertActualEqualsExpected(m, `
138 apiVersion: apps/v1
139 kind: Deployment
140 metadata:
141 labels:
142 company: acmeCorp
143 pager: 867-5301
144 name: bob-myDeployment
145 spec:
146 selector:
147 matchLabels:
148 company: acmeCorp
149 template:
150 metadata:
151 labels:
152 backend: flawless
153 company: acmeCorp
154 spec:
155 containers:
156 - image: whatever
157 name: whatever
158 ---
159 apiVersion: v1
160 kind: Role
161 metadata:
162 annotations:
163 status: probationary
164 labels:
165 company: acmeCorp
166 name: myRole
167 ---
168 apiVersion: v1
169 kind: Service
170 metadata:
171 labels:
172 company: acmeCorp
173 name: bob-myService
174 spec:
175 selector:
176 company: acmeCorp
177 `)
178 }