Skip to content

Commit 77daec8

Browse files
committed
refactor: inlineAnyEmbeddedLists
1 parent a1fd6ef commit 77daec8

File tree

2 files changed

+104
-20
lines changed

2 files changed

+104
-20
lines changed

api/krusty/no_list_items_test.go

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ metadata:
6161
apiVersion: example.com/v1alpha1
6262
kind: MyResourceList
6363
metadata:
64-
annotations:
65-
kustomize.config.k8s.io/convertToInlineList: "false"
6664
name: test
6765
rules: []
6866
`)
@@ -77,8 +75,6 @@ metadata:
7775
apiVersion: example.com/v1alpha1
7876
kind: MyResourceList
7977
metadata:
80-
annotations:
81-
kustomize.config.k8s.io/convertToInlineList: "false"
8278
name: test
8379
rules: []
8480
`)
@@ -95,8 +91,6 @@ resources:
9591
apiVersion: infra.protonbase.io/v1alpha1
9692
kind: AccessWhiteList
9793
metadata:
98-
annotations:
99-
kustomize.config.k8s.io/convertToInlineList: "false"
10094
name: wlmls5769f
10195
namespace: dc7i4hyxzw
10296
spec:
@@ -110,13 +104,111 @@ spec:
110104
apiVersion: infra.protonbase.io/v1alpha1
111105
kind: AccessWhiteList
112106
metadata:
113-
annotations:
114-
kustomize.config.k8s.io/convertToInlineList: "false"
115107
name: wlmls5769f
116108
namespace: dc7i4hyxzw
117109
spec:
118110
rules:
119111
- sourceIps: 0.0.0.0/16
120112
`)
121113

114+
}
115+
116+
func TestListToIndividualResources(t *testing.T) {
117+
th := kusttest_test.MakeHarness(t)
118+
th.WriteK(".", `
119+
resources:
120+
- list.yaml
121+
`)
122+
123+
th.WriteF("list.yaml", `
124+
apiVersion: v1
125+
kind: PodList
126+
items:
127+
- apiVersion: v1
128+
kind: Pod
129+
metadata:
130+
name: my-pod-1
131+
namespace: default
132+
labels:
133+
app: my-app
134+
spec:
135+
containers:
136+
- name: my-container
137+
image: nginx:1.19.10
138+
ports:
139+
- containerPort: 80
140+
- apiVersion: v1
141+
kind: Pod
142+
metadata:
143+
name: my-pod-2
144+
namespace: default
145+
labels:
146+
app: my-app
147+
spec:
148+
containers:
149+
- name: my-container
150+
image: nginx:1.19.10
151+
ports:
152+
- containerPort: 80
153+
- apiVersion: v1
154+
kind: Pod
155+
metadata:
156+
name: my-pod-3
157+
namespace: default
158+
labels:
159+
app: my-app
160+
spec:
161+
containers:
162+
- name: my-container
163+
image: nginx:1.19.10
164+
ports:
165+
- containerPort: 80
166+
`)
167+
168+
m := th.Run(".", th.MakeDefaultOptions())
169+
170+
171+
th.AssertActualEqualsExpected(m, `
172+
apiVersion: v1
173+
kind: Pod
174+
metadata:
175+
labels:
176+
app: my-app
177+
name: my-pod-1
178+
namespace: default
179+
spec:
180+
containers:
181+
- image: nginx:1.19.10
182+
name: my-container
183+
ports:
184+
- containerPort: 80
185+
---
186+
apiVersion: v1
187+
kind: Pod
188+
metadata:
189+
labels:
190+
app: my-app
191+
name: my-pod-2
192+
namespace: default
193+
spec:
194+
containers:
195+
- image: nginx:1.19.10
196+
name: my-container
197+
ports:
198+
- containerPort: 80
199+
---
200+
apiVersion: v1
201+
kind: Pod
202+
metadata:
203+
labels:
204+
app: my-app
205+
name: my-pod-3
206+
namespace: default
207+
spec:
208+
containers:
209+
- image: nginx:1.19.10
210+
name: my-container
211+
ports:
212+
- containerPort: 80
213+
`)
122214
}

api/resource/factory.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"fmt"
99
"log"
10-
"strconv"
1110
"strings"
1211

1312
"sigs.k8s.io/kustomize/api/ifc"
@@ -190,16 +189,6 @@ func (rf *Factory) inlineAnyEmbeddedLists(
190189
var n0 *yaml.RNode
191190
for len(nodes) > 0 {
192191
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-
}
203192
kind := n0.GetKind()
204193
if !strings.HasSuffix(kind, "List") {
205194
result = append(result, n0)
@@ -213,7 +202,10 @@ func (rf *Factory) inlineAnyEmbeddedLists(
213202
}
214203
items, ok := m["items"]
215204
if !ok {
216-
// 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)
217209
continue
218210
}
219211
slice, ok := items.([]interface{})

0 commit comments

Comments
 (0)