Skip to content

Commit c3872ce

Browse files
authored
Merge pull request #5745 from isarns/master
Add Annotation to Control Inline List Conversion in Kustomize Resources"
2 parents d35d21c + a5f43ec commit c3872ce

File tree

4 files changed

+249
-2
lines changed

4 files changed

+249
-2
lines changed

api/krusty/no_list_items_test.go

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
)
11+
12+
// test for https://github.com/kubernetes-sigs/kustomize/issues/4240
13+
func TestSuffix5042(t *testing.T) {
14+
th := kusttest_test.MakeHarness(t)
15+
th.WriteK(".", `
16+
resources:
17+
- resource.yaml
18+
`)
19+
20+
th.WriteF("resource.yaml", `
21+
apiVersion: example.com/v1alpha1
22+
kind: MyResource
23+
metadata:
24+
name: service
25+
---
26+
apiVersion: example.com/v1alpha1
27+
kind: MyResourceTwo
28+
metadata:
29+
name: test
30+
rules: []
31+
`)
32+
m := th.Run(".", th.MakeDefaultOptions())
33+
34+
th.AssertActualEqualsExpected(m, `
35+
apiVersion: example.com/v1alpha1
36+
kind: MyResource
37+
metadata:
38+
name: service
39+
---
40+
apiVersion: example.com/v1alpha1
41+
kind: MyResourceTwo
42+
metadata:
43+
name: test
44+
rules: []
45+
`)
46+
}
47+
48+
func TestListSuffix5042(t *testing.T) {
49+
th := kusttest_test.MakeHarness(t)
50+
th.WriteK(".", `
51+
resources:
52+
- resource.yaml
53+
`)
54+
55+
th.WriteF("resource.yaml", `
56+
apiVersion: example.com/v1alpha1
57+
kind: MyResource
58+
metadata:
59+
name: service
60+
---
61+
apiVersion: example.com/v1alpha1
62+
kind: MyResourceList
63+
metadata:
64+
name: test
65+
rules: []
66+
`)
67+
m := th.Run(".", th.MakeDefaultOptions())
68+
69+
th.AssertActualEqualsExpected(m, `
70+
apiVersion: example.com/v1alpha1
71+
kind: MyResource
72+
metadata:
73+
name: service
74+
---
75+
apiVersion: example.com/v1alpha1
76+
kind: MyResourceList
77+
metadata:
78+
name: test
79+
rules: []
80+
`)
81+
}
82+
83+
func TestListSuffix5485(t *testing.T) {
84+
th := kusttest_test.MakeHarness(t)
85+
th.WriteK(".", `
86+
resources:
87+
- resource.yaml
88+
`)
89+
90+
th.WriteF("resource.yaml", `
91+
apiVersion: infra.protonbase.io/v1alpha1
92+
kind: AccessWhiteList
93+
metadata:
94+
name: wlmls5769f
95+
namespace: dc7i4hyxzw
96+
spec:
97+
rules:
98+
- sourceIps: 0.0.0.0/16
99+
`)
100+
101+
m := th.Run(".", th.MakeDefaultOptions())
102+
103+
th.AssertActualEqualsExpected(m, `
104+
apiVersion: infra.protonbase.io/v1alpha1
105+
kind: AccessWhiteList
106+
metadata:
107+
name: wlmls5769f
108+
namespace: dc7i4hyxzw
109+
spec:
110+
rules:
111+
- sourceIps: 0.0.0.0/16
112+
`)
113+
}
114+
115+
func TestListToIndividualResources(t *testing.T) {
116+
th := kusttest_test.MakeHarness(t)
117+
th.WriteK(".", `
118+
resources:
119+
- list.yaml
120+
`)
121+
122+
th.WriteF("list.yaml", `
123+
apiVersion: v1
124+
kind: PodList
125+
items:
126+
- apiVersion: v1
127+
kind: Pod
128+
metadata:
129+
name: my-pod-1
130+
namespace: default
131+
labels:
132+
app: my-app
133+
spec:
134+
containers:
135+
- name: my-container
136+
image: nginx:1.19.10
137+
ports:
138+
- containerPort: 80
139+
- apiVersion: v1
140+
kind: Pod
141+
metadata:
142+
name: my-pod-2
143+
namespace: default
144+
labels:
145+
app: my-app
146+
spec:
147+
containers:
148+
- name: my-container
149+
image: nginx:1.19.10
150+
ports:
151+
- containerPort: 80
152+
- apiVersion: v1
153+
kind: Pod
154+
metadata:
155+
name: my-pod-3
156+
namespace: default
157+
labels:
158+
app: my-app
159+
spec:
160+
containers:
161+
- name: my-container
162+
image: nginx:1.19.10
163+
ports:
164+
- containerPort: 80
165+
`)
166+
167+
m := th.Run(".", th.MakeDefaultOptions())
168+
169+
th.AssertActualEqualsExpected(m, `
170+
apiVersion: v1
171+
kind: Pod
172+
metadata:
173+
labels:
174+
app: my-app
175+
name: my-pod-1
176+
namespace: default
177+
spec:
178+
containers:
179+
- image: nginx:1.19.10
180+
name: my-container
181+
ports:
182+
- containerPort: 80
183+
---
184+
apiVersion: v1
185+
kind: Pod
186+
metadata:
187+
labels:
188+
app: my-app
189+
name: my-pod-2
190+
namespace: default
191+
spec:
192+
containers:
193+
- image: nginx:1.19.10
194+
name: my-container
195+
ports:
196+
- containerPort: 80
197+
---
198+
apiVersion: v1
199+
kind: Pod
200+
metadata:
201+
labels:
202+
app: my-app
203+
name: my-pod-3
204+
namespace: default
205+
spec:
206+
containers:
207+
- image: nginx:1.19.10
208+
name: my-container
209+
ports:
210+
- containerPort: 80
211+
`)
212+
}
213+
214+
// Empty list should result in no resources
215+
func TestEmptyList(t *testing.T) {
216+
th := kusttest_test.MakeHarness(t)
217+
th.WriteK(".", `
218+
resources:
219+
- emptyList.yaml
220+
`)
221+
th.WriteF("emptyList.yaml", `
222+
apiVersion: v1
223+
kind: PodList
224+
items: []
225+
`)
226+
m := th.Run(".", th.MakeDefaultOptions())
227+
th.AssertActualEqualsExpected(m, "")
228+
}

api/resource/factory.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ func (rf *Factory) inlineAnyEmbeddedLists(
202202
}
203203
items, ok := m["items"]
204204
if !ok {
205-
// treat as an empty list
205+
// Items field is not present.
206+
// This is not a collections resource.
207+
// read more https://kubernetes.io/docs/reference/using-api/api-concepts/#collections
208+
result = append(result, n0)
206209
continue
207210
}
208211
slice, ok := items.([]interface{})

api/resource/factory_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ kind: List
301301
t.Fatalf("failed to create new instance with %v: %v", deploymentB, errB)
302302
}
303303

304+
deploymentNoItems := "deployment-no-items"
305+
testDeploymentNoItems, errNoItems := factory.FromMap(
306+
map[string]interface{}{
307+
"apiVersion": "v1",
308+
"kind": "List",
309+
},
310+
)
311+
if errNoItems != nil {
312+
t.Fatalf("failed to create new instance with %v: %v", deploymentNoItems, testDeploymentNoItems)
313+
}
314+
304315
fSys := filesys.MakeFsInMemory()
305316
fSys.WriteFile(string(patchGood1), []byte(patch1))
306317
fSys.WriteFile(string(patchGood2), []byte(patch2))
@@ -363,7 +374,7 @@ kind: List
363374
},
364375
"listWithNoItems": {
365376
input: []types.PatchStrategicMerge{patchList4},
366-
expectedOut: []*Resource{},
377+
expectedOut: []*Resource{testDeploymentNoItems},
367378
expectedErr: false,
368379
},
369380
}

go.work.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
232232
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954 h1:RMLoZVzv4GliuWafOuPuQDKSm1SJph7uCRnnS61JAn4=
233233
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
234234
github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE=
235+
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
235236
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0 h1:w3NnFcKR5241cfmQU5ZZAsf0xcpId6mWOupTvJlUX2U=
236237
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
237238
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 h1:cenwrSVm+Z7QLSV/BsnenAOcDXdX4cMv4wP0B/5QbPg=
@@ -275,6 +276,7 @@ github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
275276
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
276277
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
277278
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
279+
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
278280
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
279281
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
280282
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
@@ -528,6 +530,7 @@ golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
528530
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
529531
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
530532
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
533+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
531534
golang.org/x/oauth2 v0.5.0/go.mod h1:9/XBHVqLaWO3/BRHs5jbpYCnOZVjj5V0ndyaAM7KB4I=
532535
golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE=
533536
golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI=
@@ -548,7 +551,9 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
548551
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
549552
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
550553
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
554+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
551555
golang.org/x/telemetry v0.0.0-20240208230135-b75ee8823808/go.mod h1:KG1lNk5ZFNssSZLrpVb4sMXKMpGwGXOxSG3rnu2gZQQ=
556+
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
552557
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
553558
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
554559
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=

0 commit comments

Comments
 (0)