Skip to content

Commit

Permalink
expand environmentt variables in the yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
so-jelly committed Jan 23, 2023
1 parent 68ecec1 commit ba9ac47
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 167 deletions.
5 changes: 3 additions & 2 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,16 @@ 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))

if err = decoder.Decode(unstructuredObj); err != nil {
return nil, fmt.Errorf("error decoding yaml %s: %w", path, err)
}

unstructuredObj.Object = Translate(unstructuredObj.Object).(map[string]interface{})

obj, err := ConvertUnstructured(unstructuredObj)
if err != nil {
return nil, fmt.Errorf("error converting unstructured object %s (%s): %w", ResourceID(unstructuredObj), path, err)
Expand Down
22 changes: 22 additions & 0 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 @@ -182,6 +183,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 @@ -198,6 +204,13 @@ spec:
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)))

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

Expand All @@ -216,6 +229,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 Down
103 changes: 0 additions & 103 deletions pkg/test/utils/translate.go

This file was deleted.

62 changes: 0 additions & 62 deletions pkg/test/utils/translate_test.go

This file was deleted.

0 comments on commit ba9ac47

Please sign in to comment.