Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add kustomization #1663

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions apptests/kustomize/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package kustomize

import (
"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 {
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
}

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

out, err := envsubst.Eval(string(yaml), func(s string) string {
return k.substitutes[s]
})
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)
Azhovan marked this conversation as resolved.
Show resolved Hide resolved
}

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
}
37 changes: 37 additions & 0 deletions apptests/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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",
"namespace": "test",
},
)
err = builder.Build()
assert.NoError(t, err)

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

// define the expected output
expected := `apiVersion: apps/v1
kind: Deployment
metadata:
name: test
namespace: test
`

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
6 changes: 6 additions & 0 deletions apptests/kustomize/testdata/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ${name}
namespace: ${namespace}
Loading