Skip to content

Commit

Permalink
refactor: inlineAnyEmbeddedLists
Browse files Browse the repository at this point in the history
  • Loading branch information
isarns committed Aug 13, 2024
1 parent a1fd6ef commit 77daec8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 20 deletions.
108 changes: 100 additions & 8 deletions api/krusty/no_list_items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ metadata:
apiVersion: example.com/v1alpha1
kind: MyResourceList
metadata:
annotations:
kustomize.config.k8s.io/convertToInlineList: "false"
name: test
rules: []
`)
Expand All @@ -77,8 +75,6 @@ metadata:
apiVersion: example.com/v1alpha1
kind: MyResourceList
metadata:
annotations:
kustomize.config.k8s.io/convertToInlineList: "false"
name: test
rules: []
`)
Expand All @@ -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:
Expand All @@ -110,13 +104,111 @@ spec:
apiVersion: infra.protonbase.io/v1alpha1
kind: AccessWhiteList
metadata:
annotations:
kustomize.config.k8s.io/convertToInlineList: "false"
name: wlmls5769f
namespace: dc7i4hyxzw
spec:
rules:
- sourceIps: 0.0.0.0/16
`)

}

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
`)
}
16 changes: 4 additions & 12 deletions api/resource/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"

"sigs.k8s.io/kustomize/api/ifc"
Expand Down Expand Up @@ -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)
Expand All @@ -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{})
Expand Down

0 comments on commit 77daec8

Please sign in to comment.