Skip to content

Commit

Permalink
Expand List resources.
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
mgoltzsche committed Mar 7, 2021
1 parent f55f33f commit 99a1254
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 21 deletions.
9 changes: 9 additions & 0 deletions cmd/khelm/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ func TestKptFnCommand(t *testing.T) {
}}},
1, "k8sVersion: v1.12.0",
},
{
"expand-list",
kptFnConfig{ChartConfig: &config.ChartConfig{
LoaderConfig: config.LoaderConfig{
Chart: filepath.Join(exampleDir, "expand-list"),
},
}},
3, "\n name: myserviceaccount2\n",
},
{
"namespace",
kptFnConfig{ChartConfig: &config.ChartConfig{
Expand Down
4 changes: 4 additions & 0 deletions example/expand-list/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
description: example chart that outputs resources nested within a List resource
name: list
version: 0.1.0
5 changes: 5 additions & 0 deletions example/expand-list/generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: khelm.mgoltzsche.github.com/v1
kind: ChartRenderer
metadata:
name: expand-list
chart: .
2 changes: 2 additions & 0 deletions example/expand-list/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generators:
- generator.yaml
22 changes: 22 additions & 0 deletions example/expand-list/templates/list.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
name: myserviceaccount1
namespace: ns1
- apiVersion: v1
kind: ServiceAccount
metadata:
name: myserviceaccount2
namespace: ns2
---
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: ns3
1 change: 1 addition & 0 deletions pkg/helm/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestRender(t *testing.T) {
{"rook-ceph-version-range", "example/rook-ceph/operator/generator.yaml", []string{}, "rook-ceph-v0.9.3"},
{"cert-manager", "example/cert-manager/generator.yaml", []string{"cert-manager", "kube-system"}, " name: cert-manager-webhook"},
{"apiversions-condition", "example/apiversions-condition/generator.yaml", []string{}, " config: fancy-config"},
{"expand-list", "example/expand-list/generator.yaml", []string{"ns1", "ns2", "ns3"}, "\n name: myserviceaccount2\n"},
{"namespace", "example/namespace/generator.yaml", []string{"install-namespace", "cluster-role-binding-ns"}, " key: b"},
{"force-namespace", "example/force-namespace/generator.yaml", []string{"forced-namespace"}, " key: b"},
{"kubeVersion", "example/release-name/generator.yaml", []string{}, " k8sVersion: v1.17.0"},
Expand Down
64 changes: 43 additions & 21 deletions pkg/helm/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,10 @@ func (t *manifestTransformer) TransformManifest(manifest io.Reader) (r []*yaml.R
continue
}

meta, err := o.GetMeta()
err = t.addResources(o, &r, &clusterScopedResources)
if err != nil {
break
}

resourceID := meta.GetIdentifier()

// Exclude all not explicitly included resources
if !t.Includes.Match(&resourceID) {
continue
}

// Exclude resources
if t.Excludes.Match(&resourceID) {
continue
}

// Set namespace
err = t.applyNamespace(o, &clusterScopedResources)
if err != nil {
break
}

r = append(r, o)
}
if err == io.EOF {
err = nil
Expand All @@ -75,6 +55,48 @@ func (t *manifestTransformer) TransformManifest(manifest io.Reader) (r []*yaml.R
return
}

func (t *manifestTransformer) addResources(o *yaml.RNode, r *[]*yaml.RNode, clusterScopedResources *[]string) error {
meta, err := o.GetMeta()
if err != nil {
return err
}

if meta.Kind == "List" && meta.APIVersion == "v1" { // Flatten list
if m := o.Field("items"); m != nil {
items, err := m.Value.Elements()
if err != nil {
return errors.Wrap(err, "get List resource items")
}
for _, item := range items {
if err = t.addResources(item, r, clusterScopedResources); err != nil {
return err
}
}
}
return nil
}

resourceID := meta.GetIdentifier()

// Exclude all not explicitly included resources
if !t.Includes.Match(&resourceID) {
return nil
}

// Exclude resources
if t.Excludes.Match(&resourceID) {
return nil
}

// Set namespace
err = t.applyNamespace(o, clusterScopedResources)
if err != nil {
return err
}
*r = append(*r, o)
return nil
}

func (t *manifestTransformer) applyNamespace(o *yaml.RNode, clusterScopedResources *[]string) error {
meta, err := o.GetMeta()
if err != nil {
Expand Down

0 comments on commit 99a1254

Please sign in to comment.