Skip to content

Commit a1fd6ef

Browse files
committed
feat: annotation to disable list conversion
1 parent cc9dd34 commit a1fd6ef

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

api/krusty/no_list_items_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
annotations:
65+
kustomize.config.k8s.io/convertToInlineList: "false"
66+
name: test
67+
rules: []
68+
`)
69+
m := th.Run(".", th.MakeDefaultOptions())
70+
71+
th.AssertActualEqualsExpected(m, `
72+
apiVersion: example.com/v1alpha1
73+
kind: MyResource
74+
metadata:
75+
name: service
76+
---
77+
apiVersion: example.com/v1alpha1
78+
kind: MyResourceList
79+
metadata:
80+
annotations:
81+
kustomize.config.k8s.io/convertToInlineList: "false"
82+
name: test
83+
rules: []
84+
`)
85+
}
86+
87+
func TestListSuffix5485(t *testing.T) {
88+
th := kusttest_test.MakeHarness(t)
89+
th.WriteK(".", `
90+
resources:
91+
- resource.yaml
92+
`)
93+
94+
th.WriteF("resource.yaml", `
95+
apiVersion: infra.protonbase.io/v1alpha1
96+
kind: AccessWhiteList
97+
metadata:
98+
annotations:
99+
kustomize.config.k8s.io/convertToInlineList: "false"
100+
name: wlmls5769f
101+
namespace: dc7i4hyxzw
102+
spec:
103+
rules:
104+
- sourceIps: 0.0.0.0/16
105+
`)
106+
107+
m := th.Run(".", th.MakeDefaultOptions())
108+
109+
th.AssertActualEqualsExpected(m, `
110+
apiVersion: infra.protonbase.io/v1alpha1
111+
kind: AccessWhiteList
112+
metadata:
113+
annotations:
114+
kustomize.config.k8s.io/convertToInlineList: "false"
115+
name: wlmls5769f
116+
namespace: dc7i4hyxzw
117+
spec:
118+
rules:
119+
- sourceIps: 0.0.0.0/16
120+
`)
121+
122+
}

api/resource/factory.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"log"
10+
"strconv"
1011
"strings"
1112

1213
"sigs.k8s.io/kustomize/api/ifc"
@@ -189,6 +190,16 @@ func (rf *Factory) inlineAnyEmbeddedLists(
189190
var n0 *yaml.RNode
190191
for len(nodes) > 0 {
191192
n0, nodes = nodes[0], nodes[1:]
193+
annotations := n0.GetAnnotations()
194+
convertToInlineList, err := strconv.ParseBool(annotations["kustomize.config.k8s.io/convertToInlineList"])
195+
if err != nil {
196+
convertToInlineList = true
197+
}
198+
if !convertToInlineList {
199+
// if the annotation is set to true, then we should not convert the list to inline
200+
result = append(result, n0)
201+
continue
202+
}
192203
kind := n0.GetKind()
193204
if !strings.HasSuffix(kind, "List") {
194205
result = append(result, n0)

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)