Merge pull request #15767 from hakman/set_env_vars

Allow setting env vars from the command line
This commit is contained in:
Kubernetes Prow Robot 2023-08-11 08:13:27 -07:00 committed by GitHub
commit f8465fbb48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import (
"strings"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kops/pkg/apis/kops"
)
func SetString(target interface{}, targetPath string, newValue string) error {
@ -188,6 +189,16 @@ func setType(v reflect.Value, newValue string) error {
case "intstr.IntOrString":
newV = reflect.ValueOf(intstr.Parse(newValue))
case "kops.EnvVar":
name, value, found := strings.Cut(newValue, "=")
envVar := kops.EnvVar{
Name: name,
}
if found {
envVar.Value = value
}
newV = reflect.ValueOf(envVar)
default:
// This handles enums and other simple conversions
newV = reflect.ValueOf(newValue)

View File

@ -22,6 +22,8 @@ import (
"reflect"
"strings"
"testing"
"k8s.io/kops/pkg/apis/kops"
)
type fakeEnum string
@ -56,6 +58,8 @@ type fakeObjectContainers struct {
Int32 *int32 `json:"int32"`
Int64 *int64 `json:"int64"`
Env []kops.EnvVar `json:"env"`
Enum fakeEnum `json:"enum"`
EnumSlice []fakeEnum `json:"enumSlice"`
}
@ -169,6 +173,20 @@ func TestSet(t *testing.T) {
Path: "spec.containers[0].enumSlice",
Value: "GHI,JKL",
},
{
Name: "set env var",
Input: "{ 'spec': { 'containers': [ {} ] } }",
Expected: "{ 'spec': { 'containers': [ { 'env': [ { 'name': 'ABC' } ] } ] } }",
Path: "spec.containers[0].env",
Value: "ABC",
},
{
Name: "set env var with val",
Input: "{ 'spec': { 'containers': [ {} ] } }",
Expected: "{ 'spec': { 'containers': [ { 'env': [ { 'name': 'ABC', 'value': 'DEF' } ] } ] } }",
Path: "spec.containers[0].env",
Value: "ABC=DEF",
},
// Not sure if we should do this...
// {
// Name: "creating missing array elements",