mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
refactor: inlineAnyEmbeddedLists
This commit is contained in:
@@ -61,8 +61,6 @@ metadata:
|
||||
apiVersion: example.com/v1alpha1
|
||||
kind: MyResourceList
|
||||
metadata:
|
||||
annotations:
|
||||
kustomize.config.k8s.io/convertToInlineList: "false"
|
||||
name: test
|
||||
rules: []
|
||||
`)
|
||||
@@ -77,8 +75,6 @@ metadata:
|
||||
apiVersion: example.com/v1alpha1
|
||||
kind: MyResourceList
|
||||
metadata:
|
||||
annotations:
|
||||
kustomize.config.k8s.io/convertToInlineList: "false"
|
||||
name: test
|
||||
rules: []
|
||||
`)
|
||||
@@ -95,8 +91,6 @@ resources:
|
||||
apiVersion: infra.protonbase.io/v1alpha1
|
||||
kind: AccessWhiteList
|
||||
metadata:
|
||||
annotations:
|
||||
kustomize.config.k8s.io/convertToInlineList: "false"
|
||||
name: wlmls5769f
|
||||
namespace: dc7i4hyxzw
|
||||
spec:
|
||||
@@ -110,8 +104,6 @@ spec:
|
||||
apiVersion: infra.protonbase.io/v1alpha1
|
||||
kind: AccessWhiteList
|
||||
metadata:
|
||||
annotations:
|
||||
kustomize.config.k8s.io/convertToInlineList: "false"
|
||||
name: wlmls5769f
|
||||
namespace: dc7i4hyxzw
|
||||
spec:
|
||||
@@ -120,3 +112,103 @@ spec:
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
func TestListToIndividualResources(t *testing.T) {
|
||||
th := kusttest_test.MakeHarness(t)
|
||||
th.WriteK(".", `
|
||||
resources:
|
||||
- list.yaml
|
||||
`)
|
||||
|
||||
th.WriteF("list.yaml", `
|
||||
apiVersion: v1
|
||||
kind: PodList
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: my-pod-1
|
||||
namespace: default
|
||||
labels:
|
||||
app: my-app
|
||||
spec:
|
||||
containers:
|
||||
- name: my-container
|
||||
image: nginx:1.19.10
|
||||
ports:
|
||||
- containerPort: 80
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: my-pod-2
|
||||
namespace: default
|
||||
labels:
|
||||
app: my-app
|
||||
spec:
|
||||
containers:
|
||||
- name: my-container
|
||||
image: nginx:1.19.10
|
||||
ports:
|
||||
- containerPort: 80
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: my-pod-3
|
||||
namespace: default
|
||||
labels:
|
||||
app: my-app
|
||||
spec:
|
||||
containers:
|
||||
- name: my-container
|
||||
image: nginx:1.19.10
|
||||
ports:
|
||||
- containerPort: 80
|
||||
`)
|
||||
|
||||
m := th.Run(".", th.MakeDefaultOptions())
|
||||
|
||||
|
||||
th.AssertActualEqualsExpected(m, `
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: my-app
|
||||
name: my-pod-1
|
||||
namespace: default
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx:1.19.10
|
||||
name: my-container
|
||||
ports:
|
||||
- containerPort: 80
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: my-app
|
||||
name: my-pod-2
|
||||
namespace: default
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx:1.19.10
|
||||
name: my-container
|
||||
ports:
|
||||
- containerPort: 80
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
app: my-app
|
||||
name: my-pod-3
|
||||
namespace: default
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx:1.19.10
|
||||
name: my-container
|
||||
ports:
|
||||
- containerPort: 80
|
||||
`)
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
@@ -190,16 +189,6 @@ func (rf *Factory) inlineAnyEmbeddedLists(
|
||||
var n0 *yaml.RNode
|
||||
for len(nodes) > 0 {
|
||||
n0, nodes = nodes[0], nodes[1:]
|
||||
annotations := n0.GetAnnotations()
|
||||
convertToInlineList, err := strconv.ParseBool(annotations["kustomize.config.k8s.io/convertToInlineList"])
|
||||
if err != nil {
|
||||
convertToInlineList = true
|
||||
}
|
||||
if !convertToInlineList {
|
||||
// if the annotation is set to true, then we should not convert the list to inline
|
||||
result = append(result, n0)
|
||||
continue
|
||||
}
|
||||
kind := n0.GetKind()
|
||||
if !strings.HasSuffix(kind, "List") {
|
||||
result = append(result, n0)
|
||||
@@ -213,7 +202,10 @@ func (rf *Factory) inlineAnyEmbeddedLists(
|
||||
}
|
||||
items, ok := m["items"]
|
||||
if !ok {
|
||||
// treat as an empty list
|
||||
// Items field is not present.
|
||||
// This is not a collections resource.
|
||||
// read more https://kubernetes.io/docs/reference/using-api/api-concepts/#collections
|
||||
result = append(result, n0)
|
||||
continue
|
||||
}
|
||||
slice, ok := items.([]interface{})
|
||||
|
||||
Reference in New Issue
Block a user