Repository: knative/pkg
Pull request #1361 (2020-05-27 21:31:54)

User variadic version and remove nil
Update duck/status to use new version of UnionMaps

/assign @vaikas mattmoor

knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1361#" title="Author self-approved">vagababov</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **vaikas**
You can assign the PR to them by writing `/assign @vaikas` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[apis/duck/OWNERS](https://github.com/knative/pkg/blob/master/apis/duck/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["vaikas"]} -->
knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1361#" title="Author self-approved">vagababov</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **mattmoor**
You can assign the PR to them by writing `/assign @mattmoor` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[apis/duck/OWNERS](https://github.com/knative/pkg/blob/master/apis/duck/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["mattmoor"]} -->
vagababov: ping :)

Patch:
diff --git a/apis/duck/v1/status_types.go b/apis/duck/v1/status_types.go
index 53006708f..aab246b1f 100644
--- a/apis/duck/v1/status_types.go
+++ b/apis/duck/v1/status_types.go
@@ -96,7 +96,7 @@ func (source *Status) ConvertTo(ctx context.Context, sink *Status, predicates ..
 	sink.ObservedGeneration = source.ObservedGeneration
 	if source.Annotations != nil {
 		// This will deep copy the map.
-		sink.Annotations = kmeta.UnionMaps(source.Annotations, nil)
+		sink.Annotations = kmeta.UnionMaps(source.Annotations)
 	}
 
 	conditions := make(apis.Conditions, 0, len(source.Conditions))
diff --git a/apis/duck/v1beta1/status_types.go b/apis/duck/v1beta1/status_types.go
index d90e7dc69..107592e85 100644
--- a/apis/duck/v1beta1/status_types.go
+++ b/apis/duck/v1beta1/status_types.go
@@ -109,7 +109,8 @@ func (s *Status) GetCondition(t apis.ConditionType) *apis.Condition {
 func (source *Status) ConvertTo(ctx context.Context, sink *Status) {
 	sink.ObservedGeneration = source.ObservedGeneration
 	if source.Annotations != nil {
-		sink.Annotations = kmeta.UnionMaps(source.Annotations, nil)
+		// This will deep copy the map.
+		sink.Annotations = kmeta.UnionMaps(source.Annotations)
 	}
 	for _, c := range source.Conditions {
 		switch c.Type {
diff --git a/configmap/parse.go b/configmap/parse.go
index 6479598b7..210d2114e 100644
--- a/configmap/parse.go
+++ b/configmap/parse.go
@@ -22,6 +22,7 @@ import (
 	"strings"
 	"time"
 
+	"k8s.io/apimachinery/pkg/api/resource"
 	"k8s.io/apimachinery/pkg/util/sets"
 )
 
@@ -114,6 +115,21 @@ func AsStringSet(key string, target *sets.String) ParseFunc {
 	}
 }
 
+// AsQuantity parses the value at key as a *resource.Quantity into the target, if it exists
+func AsQuantity(key string, target **resource.Quantity) ParseFunc {
+	return func(data map[string]string) error {
+		if raw, ok := data[key]; ok {
+			val, err := resource.ParseQuantity(raw)
+			if err != nil {
+				return fmt.Errorf("failed to parse %q: %w", key, err)
+			}
+
+			*target = &val
+		}
+		return nil
+	}
+}
+
 // Parse parses the given map using the parser functions passed in.
 func Parse(data map[string]string, parsers ...ParseFunc) error {
 	for _, parse := range parsers {


Test patch:
diff --git a/configmap/parse_test.go b/configmap/parse_test.go
index 14528d351..477dd1488 100644
--- a/configmap/parse_test.go
+++ b/configmap/parse_test.go
@@ -21,6 +21,7 @@ import (
 	"time"
 
 	"github.com/google/go-cmp/cmp"
+	"k8s.io/apimachinery/pkg/api/resource"
 	"k8s.io/apimachinery/pkg/util/sets"
 )
 
@@ -32,9 +33,11 @@ type testConfig struct {
 	f64 float64
 	dur time.Duration
 	set sets.String
+	qua *resource.Quantity
 }
 
 func TestParse(t *testing.T) {
+	fiveHundredM := resource.MustParse("500m")
 	tests := []struct {
 		name      string
 		conf      testConfig
@@ -51,6 +54,7 @@ func TestParse(t *testing.T) {
 			"test-float64":  "1.0",
 			"test-duration": "1m",
 			"test-set":      "a,b,c",
+			"test-quantity": "500m",
 		},
 		want: testConfig{
 			str: "foo.bar",
@@ -60,6 +64,7 @@ func TestParse(t *testing.T) {
 			f64: 1.0,
 			dur: time.Minute,
 			set: sets.NewString("a", "b", "c"),
+			qua: &fiveHundredM,
 		},
 	}, {
 		name: "respect defaults",
@@ -70,6 +75,7 @@ func TestParse(t *testing.T) {
 			i64: 2,
 			f64: 1.0,
 			dur: time.Minute,
+			qua: &fiveHundredM,
 		},
 		want: testConfig{
 			str: "foo.bar",
@@ -78,6 +84,7 @@ func TestParse(t *testing.T) {
 			i64: 2,
 			f64: 1.0,
 			dur: time.Minute,
+			qua: &fiveHundredM,
 		},
 	}, {
 		name: "bool defaults to false",
@@ -111,6 +118,12 @@ func TestParse(t *testing.T) {
 			"test-duration": "foo",
 		},
 		expectErr: true,
+	}, {
+		name: "quantity error",
+		data: map[string]string{
+			"test-quantity": "foo",
+		},
+		expectErr: true,
 	}}
 
 	for _, test := range tests {
@@ -123,6 +136,7 @@ func TestParse(t *testing.T) {
 				AsFloat64("test-float64", &test.conf.f64),
 				AsDuration("test-duration", &test.conf.dur),
 				AsStringSet("test-set", &test.conf.set),
+				AsQuantity("test-quantity", &test.conf.qua),
 			); (err == nil) == test.expectErr {
 				t.Fatal("Failed to parse data:", err)
 			}