Skip to content

Commit c9ae9c3

Browse files
committed
Introduce kustomize build options
1 parent a722bdc commit c9ae9c3

File tree

8 files changed

+275
-12
lines changed

8 files changed

+275
-12
lines changed

docs/gitrepo-structure.md

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ kustomize:
5151
# Use a custom folder for kustomize resources. This folder must contain
5252
# a kustomization.yaml file.
5353
dir: ./kustomize
54+
# Specify build options for kustomize build.
55+
# Default: ""
56+
buildOptions: --load-restrictor LoadRestrictionsNone
5457

5558
helm:
5659
# Use a custom location for the Helm chart. This can refer to any go-getter URL.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
rsc.io/letsencrypt v0.0.3 // indirect
6464
sigs.k8s.io/cli-utils v0.23.1
6565
sigs.k8s.io/kustomize/api v0.8.8
66+
sigs.k8s.io/kustomize/kustomize/v4 v4.1.2
6667
sigs.k8s.io/kustomize/kyaml v0.10.17
6768
sigs.k8s.io/yaml v1.2.0
6869
)

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,7 @@ sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5
20632063
sigs.k8s.io/kustomize/api v0.8.8 h1:G2z6JPSSjtWWgMeWSoHdXqyftJNmMmyxXpwENGoOtGE=
20642064
sigs.k8s.io/kustomize/api v0.8.8/go.mod h1:He1zoK0nk43Pc6NlV085xDXDXTNprtcyKZVm3swsdNY=
20652065
sigs.k8s.io/kustomize/cmd/config v0.9.10/go.mod h1:Mrby0WnRH7hA6OwOYnYpfpiY0WJIMgYrEDfwOeFdMK0=
2066+
sigs.k8s.io/kustomize/kustomize/v4 v4.1.2 h1:iP3ckqMIftwsIKnMqtztReSkkPJvhqNc5QiOpMoFpbY=
20662067
sigs.k8s.io/kustomize/kustomize/v4 v4.1.2/go.mod h1:PxBvo4WGYlCLeRPL+ziT64wBXqbgfcalOS/SXa/tcyo=
20672068
sigs.k8s.io/kustomize/kyaml v0.4.0/go.mod h1:XJL84E6sOFeNrQ7CADiemc1B0EjIxHo3OhW4o1aJYNw=
20682069
sigs.k8s.io/kustomize/kyaml v0.10.7/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=

pkg/apis/fleet.cattle.io/v1alpha1/bundle.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ type YAMLOptions struct {
218218
}
219219

220220
type KustomizeOptions struct {
221-
Dir string `json:"dir,omitempty"`
221+
Dir string `json:"dir,omitempty"`
222+
BuildOptions string `json:"buildOptions,omitempty"`
222223
}
223224

224225
type HelmOptions struct {

pkg/helmdeployer/deployer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *by
9999
data = nil
100100
}
101101

102-
newObjs, processed, err := kustomize.Process(p.manifest, data, p.opts.Kustomize.Dir)
102+
newObjs, processed, err := kustomize.Process(p.manifest, data, *p.opts.Kustomize)
103103
if err != nil {
104104
return nil, err
105105
}

pkg/kustomize/kustomize.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package kustomize
22

33
import (
4+
"bytes"
45
"path/filepath"
6+
"strings"
7+
8+
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
59

610
"github.com/rancher/fleet/pkg/content"
711
"github.com/rancher/fleet/pkg/manifest"
@@ -11,7 +15,7 @@ import (
1115
"k8s.io/apimachinery/pkg/runtime"
1216
"sigs.k8s.io/kustomize/api/filesys"
1317
"sigs.k8s.io/kustomize/api/krusty"
14-
"sigs.k8s.io/kustomize/api/types"
18+
"sigs.k8s.io/kustomize/kustomize/v4/commands/build"
1519
"sigs.k8s.io/yaml"
1620
)
1721

@@ -20,7 +24,8 @@ const (
2024
ManifestsYAML = "fleet-manifests.yaml"
2125
)
2226

23-
func Process(m *manifest.Manifest, content []byte, dir string) ([]runtime.Object, bool, error) {
27+
func Process(m *manifest.Manifest, content []byte, options fleet.KustomizeOptions) ([]runtime.Object, bool, error) {
28+
dir := options.Dir
2429
if dir == "" {
2530
dir = "."
2631
}
@@ -41,7 +46,7 @@ func Process(m *manifest.Manifest, content []byte, dir string) ([]runtime.Object
4146
}
4247
}
4348

44-
objs, err := kustomize(fs, dir)
49+
objs, err := kustomize(fs, dir, options.BuildOptions)
4550
return objs, true, err
4651
}
4752

@@ -90,13 +95,17 @@ func toFilesystem(m *manifest.Manifest, dir string, manifestsContent []byte) (fi
9095
return f, err
9196
}
9297

93-
func kustomize(fs filesys.FileSystem, dir string) (result []runtime.Object, err error) {
94-
pcfg := types.DisabledPluginConfig()
95-
kust := krusty.MakeKustomizer(&krusty.Options{
96-
LoadRestrictions: types.LoadRestrictionsRootOnly,
97-
PluginConfig: pcfg,
98-
})
99-
resMap, err := kust.Run(fs, dir)
98+
func kustomize(fs filesys.FileSystem, dir string, buildOptions string) (result []runtime.Object, err error) {
99+
buildOpts := strings.Split(buildOptions, " ")
100+
cmd := build.NewCmdBuild(fs, build.MakeHelp("kustomize", "build"), new(bytes.Buffer))
101+
cmd.Flags().Parse(buildOpts)
102+
if err := build.Validate([]string{dir}); err != nil {
103+
return nil, err
104+
}
105+
k := krusty.MakeKustomizer(
106+
build.HonorKustomizeFlags(krusty.MakeDefaultOptions()),
107+
)
108+
resMap, err := k.Run(fs, dir)
100109
if err != nil {
101110
return nil, err
102111
}

pkg/kustomize/kustomize_test.go

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
package kustomize
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/rancher/wrangler/pkg/yaml"
8+
"github.com/stretchr/testify/assert"
9+
10+
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
11+
"sigs.k8s.io/kustomize/api/types"
12+
)
13+
14+
func assertActualTrimSpaceEqualsExpected(t *testing.T, actual string, expected string) {
15+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(actual))
16+
}
17+
18+
func writeSmallBase(th kusttest_test.Harness) {
19+
th.WriteK("/app/base", `
20+
namePrefix: a-
21+
commonLabels:
22+
app: myApp
23+
resources:
24+
- deployment.yaml
25+
- service.yaml
26+
`)
27+
th.WriteF("/app/base/service.yaml", `
28+
apiVersion: v1
29+
kind: Service
30+
metadata:
31+
name: myService
32+
spec:
33+
selector:
34+
backend: bungie
35+
ports:
36+
- port: 7002
37+
`)
38+
th.WriteF("/app/base/deployment.yaml", `
39+
apiVersion: apps/v1
40+
kind: Deployment
41+
metadata:
42+
name: myDeployment
43+
spec:
44+
template:
45+
metadata:
46+
labels:
47+
backend: awesome
48+
spec:
49+
containers:
50+
- name: whatever
51+
image: whatever
52+
`)
53+
}
54+
55+
func TestSmallOverlay(t *testing.T) {
56+
th := kusttest_test.MakeHarness(t)
57+
writeSmallBase(th)
58+
th.WriteK("/app/overlay", `
59+
namePrefix: b-
60+
commonLabels:
61+
env: prod
62+
quotedFruit: "peach"
63+
quotedBoolean: "true"
64+
resources:
65+
- ../base
66+
patchesStrategicMerge:
67+
- deployment/deployment.yaml
68+
images:
69+
- name: whatever
70+
newTag: 1.8.0
71+
`)
72+
73+
th.WriteF("/app/overlay/configmap/app.env", `
74+
DB_USERNAME=admin
75+
DB_PASSWORD=somepw
76+
`)
77+
th.WriteF("/app/overlay/configmap/app-init.ini", `
78+
FOO=bar
79+
BAR=baz
80+
`)
81+
th.WriteF("/app/overlay/deployment/deployment.yaml", `
82+
apiVersion: apps/v1
83+
kind: Deployment
84+
metadata:
85+
name: myDeployment
86+
spec:
87+
replicas: 1000
88+
`)
89+
result, err := kustomize(th.GetFSys(), "/app/overlay", "")
90+
if err != nil {
91+
t.Fatalf("kustomize failed: %v", err)
92+
}
93+
data, err := yaml.ToBytes(result)
94+
if err != nil {
95+
t.Fatalf("yaml.ToBytes failed: %v", err)
96+
}
97+
assertActualTrimSpaceEqualsExpected(t, string(data), `
98+
apiVersion: v1
99+
kind: Service
100+
metadata:
101+
labels:
102+
app: myApp
103+
env: prod
104+
quotedBoolean: "true"
105+
quotedFruit: peach
106+
name: b-a-myService
107+
spec:
108+
ports:
109+
- port: 7002
110+
selector:
111+
app: myApp
112+
backend: bungie
113+
env: prod
114+
quotedBoolean: "true"
115+
quotedFruit: peach
116+
117+
---
118+
apiVersion: apps/v1
119+
kind: Deployment
120+
metadata:
121+
labels:
122+
app: myApp
123+
env: prod
124+
quotedBoolean: "true"
125+
quotedFruit: peach
126+
name: b-a-myDeployment
127+
spec:
128+
replicas: 1000
129+
selector:
130+
matchLabels:
131+
app: myApp
132+
env: prod
133+
quotedBoolean: "true"
134+
quotedFruit: peach
135+
template:
136+
metadata:
137+
labels:
138+
app: myApp
139+
backend: awesome
140+
env: prod
141+
quotedBoolean: "true"
142+
quotedFruit: peach
143+
spec:
144+
containers:
145+
- image: whatever:1.8.0
146+
name: whatever
147+
`)
148+
}
149+
150+
func TestSharedPatchDisAllowed(t *testing.T) {
151+
th := kusttest_test.MakeHarness(t)
152+
writeSmallBase(th)
153+
th.WriteK("/app/overlay", `
154+
commonLabels:
155+
env: prod
156+
resources:
157+
- ../base
158+
patchesStrategicMerge:
159+
- ../shared/deployment-patch.yaml
160+
`)
161+
th.WriteF("/app/shared/deployment-patch.yaml", `
162+
apiVersion: apps/v1
163+
kind: Deployment
164+
metadata:
165+
name: myDeployment
166+
spec:
167+
replicas: 1000
168+
`)
169+
_, err := kustomize(th.GetFSys(), "/app/overlay", "")
170+
if !strings.Contains(
171+
err.Error(),
172+
"security; file '/app/shared/deployment-patch.yaml' is not in or below '/app/overlay'") {
173+
t.Fatalf("unexpected error: %s", err)
174+
}
175+
}
176+
177+
func TestSharedPatchAllowed(t *testing.T) {
178+
th := kusttest_test.MakeHarness(t)
179+
writeSmallBase(th)
180+
th.WriteK("/app/overlay", `
181+
commonLabels:
182+
env: prod
183+
resources:
184+
- ../base
185+
patchesStrategicMerge:
186+
- ../shared/deployment-patch.yaml
187+
`)
188+
th.WriteF("/app/shared/deployment-patch.yaml", `
189+
apiVersion: apps/v1
190+
kind: Deployment
191+
metadata:
192+
name: myDeployment
193+
spec:
194+
replicas: 1000
195+
`)
196+
result, err := kustomize(th.GetFSys(), "/app/overlay", "--load-restrictor "+types.LoadRestrictionsNone.String())
197+
if err != nil {
198+
t.Fatalf("kustomize failed: %v", err)
199+
}
200+
data, err := yaml.ToBytes(result)
201+
if err != nil {
202+
t.Fatalf("yaml.ToBytes failed: %v", err)
203+
}
204+
assertActualTrimSpaceEqualsExpected(t, string(data), `
205+
apiVersion: v1
206+
kind: Service
207+
metadata:
208+
labels:
209+
app: myApp
210+
env: prod
211+
name: a-myService
212+
spec:
213+
ports:
214+
- port: 7002
215+
selector:
216+
app: myApp
217+
backend: bungie
218+
env: prod
219+
220+
---
221+
apiVersion: apps/v1
222+
kind: Deployment
223+
metadata:
224+
labels:
225+
app: myApp
226+
env: prod
227+
name: a-myDeployment
228+
spec:
229+
replicas: 1000
230+
selector:
231+
matchLabels:
232+
app: myApp
233+
env: prod
234+
template:
235+
metadata:
236+
labels:
237+
app: myApp
238+
backend: awesome
239+
env: prod
240+
spec:
241+
containers:
242+
- image: whatever
243+
name: whatever
244+
`)
245+
}

pkg/options/calculate.go

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func merge(base, next fleet.BundleDeploymentOptions) fleet.BundleDeploymentOptio
8383
if next.Kustomize.Dir != "" {
8484
result.Kustomize.Dir = next.Kustomize.Dir
8585
}
86+
if next.Kustomize.BuildOptions != "" {
87+
result.Kustomize.BuildOptions = next.Kustomize.BuildOptions
88+
}
8689
}
8790
if next.Diff != nil {
8891
if result.Diff == nil {

0 commit comments

Comments
 (0)