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

manifest envsubst #329

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ func (t *Case) determineNamespace() *namespace {
ns.Name = fmt.Sprintf("kuttl-test-%s", petname.Generate(2, "-"))
ns.AutoCreated = true
}

if _, ok := os.LookupEnv("NAMESPACE"); ok { // user can supply this environment variable
so-jelly marked this conversation as resolved.
Show resolved Hide resolved
os.Setenv("NAMESPACE", t.PreferredNamespace) // set this environment variable so it can be templated by other 'steps'
}
// if we have a preferred namespace, we do NOT auto-create
return ns
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ func LoadYAML(path string, r io.Reader) ([]client.Object, error) {
return nil, fmt.Errorf("error reading yaml %s: %w", path, err)
}

// replace all variables from the environment
data = []byte(os.ExpandEnv(string(data)))

unstructuredObj := &unstructured.Unstructured{}
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))

Expand Down
61 changes: 59 additions & 2 deletions pkg/test/utils/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -168,7 +169,8 @@ func TestRetryWithTimeout(t *testing.T) {
}

func TestLoadYAML(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test.yaml")
tmpDir := t.TempDir()
tmpfile, err := os.CreateTemp(tmpDir, "test.yaml")
assert.Nil(t, err)
defer tmpfile.Close()

Expand All @@ -182,6 +184,11 @@ spec:
containers:
- name: nginx
image: nginx:1.7.9
- name: app
image: app:$APP_VERSION
resources:
requests:
cpu: ${CPU_COUNT}
---
apiVersion: v1
kind: Pod
Expand All @@ -193,11 +200,36 @@ spec:
containers:
- name: nginx
image: nginx:1.7.9
---
apiVersion: $GROUP_VERSION
kind: $KIND
metadata:
name: hello
spec:
config:
$KEY_VAR: bar
`), 0600)
if err != nil {
t.Fatal(err)
}

appVersion := "0.0.1-alpha"
t.Setenv("APP_VERSION", appVersion)
// test replacing a different type (int64 in this case)
// and nested values
cpuCount := int64(2)
t.Setenv("CPU_COUNT", strconv.Itoa(int(cpuCount)))

// test GVK
groupVersion := "example.com/v1"
t.Setenv("GROUP_VERSION", groupVersion)
kind := "custom"
t.Setenv("KIND", kind)

// key update
keyVar := "baz"
t.Setenv("KEY_VAR", keyVar)

objs, err := LoadYAMLFromFile(tmpfile.Name())
assert.Nil(t, err)

Expand All @@ -216,6 +248,15 @@ spec:
"image": "nginx:1.7.9",
"name": "nginx",
},
map[string]interface{}{
"name": "app",
"image": "app:" + appVersion,
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"cpu": cpuCount,
},
},
},
},
},
},
Expand All @@ -241,10 +282,26 @@ spec:
},
},
}, objs[1])

assert.Equal(t, &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": groupVersion,
"kind": kind,
"metadata": map[string]interface{}{
"name": "hello",
},
"spec": map[string]interface{}{
"config": map[string]interface{}{
keyVar: "bar",
},
},
},
}, objs[2])
}

func TestMatchesKind(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test.yaml")
tmpDir := t.TempDir()
tmpfile, err := os.CreateTemp(tmpDir, "test.yaml")
assert.Nil(t, err)
defer tmpfile.Close()

Expand Down
1 change: 1 addition & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build tools
// +build tools

// Package tools is used to import go modules that we use for tooling as dependencies.
Expand Down