-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/}" |