Merge pull request #15799 from hakman/set_duration

Allow setting metav1.Duration from the command line
This commit is contained in:
Kubernetes Prow Robot 2023-08-18 10:24:28 -07:00 committed by GitHub
commit 6088e36922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -21,7 +21,9 @@ import (
"reflect"
"strconv"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kops/pkg/apis/kops"
)
@ -199,6 +201,13 @@ func setType(v reflect.Value, newValue string) error {
}
newV = reflect.ValueOf(envVar)
case "v1.Duration":
duration, err := time.ParseDuration(newValue)
if err != nil {
return fmt.Errorf("cannot interpret %q value as v1.Duration", newValue)
}
newV = reflect.ValueOf(metav1.Duration{Duration: duration})
default:
// This handles enums and other simple conversions
newV = reflect.ValueOf(newValue)

View File

@ -23,6 +23,7 @@ import (
"strings"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/pkg/apis/kops"
)
@ -60,6 +61,8 @@ type fakeObjectContainers struct {
Env []kops.EnvVar `json:"env"`
Duration *metav1.Duration `json:"duration"`
Enum fakeEnum `json:"enum"`
EnumSlice []fakeEnum `json:"enumSlice"`
}
@ -187,6 +190,13 @@ func TestSet(t *testing.T) {
Path: "spec.containers[0].env",
Value: "ABC=DEF",
},
{
Name: "set duration",
Input: "{ 'spec': { 'containers': [ {} ] } }",
Expected: "{ 'spec': { 'containers': [ { 'duration': '500ms' } ] } }",
Path: "spec.containers[0].duration",
Value: "500ms",
},
// Not sure if we should do this...
// {
// Name: "creating missing array elements",