Skip to content

Commit

Permalink
feat: add kustomization
Browse files Browse the repository at this point in the history
  • Loading branch information
Azhovan committed Oct 10, 2023
1 parent b5d08e5 commit ef4401e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
86 changes: 86 additions & 0 deletions apptests/kustomize/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package kustomize

import (
"os"

"github.com/drone/envsubst"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

type Kustomize struct {
directory string // The directory where the kustomization file is located
substitutes map[string]string // The map of environment variables to substitute in the resources
resources resmap.ResMap // The map of resources generated by the kustomization process
}

// New creates a new Kustomize instance with the given directory and substitutes.
func New(dir string, subs map[string]string) *Kustomize {
return &Kustomize{
directory: dir,
substitutes: subs,
resources: resmap.New(),
}
}

// Build runs the kustomization process on the directory and substitutes the
// environment variables in the resources.
func (k *Kustomize) Build() error {
for k, v := range k.substitutes {
os.Setenv(k, v)
}

opts := krusty.MakeDefaultOptions()
opts.Reorder = krusty.ReorderOptionLegacy

kustomizer := krusty.MakeKustomizer(opts)

// Run the kustomizer on the directory and get the resource map
resourceMap, err := kustomizer.Run(filesys.MakeFsOnDisk(), k.directory)
if err != nil {
return err
}

for _, r := range resourceMap.Resources() {
yaml, err := r.AsYAML()
if err != nil {
return err
}

out, err := envsubst.EvalEnv(string(yaml))
if err != nil {
return err
}

// Convert the output string to a resource and append it to the resources map
res, err := newResourceFromString(out)
if err != nil {
return err
}
k.resources.Append(res)
}

return nil
}

// Output returns the YAML representation of the resources map as a string.
func (k *Kustomize) Output() (string, error) {
yml, err := k.resources.AsYaml()
if err != nil {
return "", err
}
return string(yml), nil
}

// newResourceFromString converts a given string to a Kubernetes resource.
func newResourceFromString(str string) (*resource.Resource, error) {
fc := resmap.NewFactory(&resource.Factory{})
resource, err := fc.NewResMapFromBytes([]byte(str))
if err != nil {
return nil, err
}

return resource.Resources()[0], nil
}
41 changes: 41 additions & 0 deletions apptests/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kustomize

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuild(t *testing.T) {
wd, err := os.Getwd()
assert.NoError(t, err)

builder := New(
filepath.Join(wd, "testdata"),
map[string]string{
"name": "test-name",
"namespace": "kommander",
},
)
err = builder.Build()
assert.NoError(t, err)

output, err := builder.Output()
assert.NoError(t, err)

// define the expected output
expected := `apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: HelmRepository
metadata:
name: test-name
namespace: kommander
spec:
interval: 10m
timeout: 1m
url: https://charts.bitnami.com/bitnami/
`

assert.Equal(t, output, expected)
}
4 changes: 4 additions & 0 deletions apptests/kustomize/testdata/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- sample.yaml
10 changes: 10 additions & 0 deletions apptests/kustomize/testdata/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: HelmRepository
metadata:
name: ${name}
namespace: ${namespace}
spec:
interval: 10m
timeout: 1m
url: "${helmMirrorURL:=https://charts.bitnami.com/bitnami/}"

0 comments on commit ef4401e

Please sign in to comment.