Repository: knative/pkg (go)

Issue:
Allow override QPS and Burst via flag
<!--
Pro-tip: You can leave this block commented, and it still works!

Select the appropriate areas for your issue:

/area API

Classify what kind of issue this is:

/kind feature
-->

The `sharedmain` package doesn't allow overriding client QPS and Burst via a flag and this forces doing convoluted logic to override it:

```golang
	clientQPS := flag.Float64("clientQPS", float64(rest.DefaultQPS), "Overrides rest.Config.DefaultQPS.")
	clientBurst := flag.Int("clientBurst", rest.DefaultBurst, "Overrides rest.Config.Burst.")

	cfg := sharedmain.ParseAndGetConfigOrDie()

	cfg.QPS = float32(*clientQPS)
	cfg.Burst = *clientBurst
```

Also, this allows having a standardized way of configuring QPS and burst for all controllers.

## Proposal

- `--kube-api-qps <float32>` flag sets `Config.QPS`, default value `rest.DefaultQPS`
- `--kube-api-burst <int>` flag sets `Config.Burst`, default value `rest.DefaultBurst`


Pull request:
Allow override QPS and Burst via flag
Fixes #1700 

Add `--kube-api-qps` and `--kube-api-burst` command-line flags 
to control client QPS and Burst respectively.

The default value is set to 0 to keep backward compatibility.

Patch:
diff --git a/.codecov.yaml b/.codecov.yaml
index 293b2a951..da447d16a 100644
--- a/.codecov.yaml
+++ b/.codecov.yaml
@@ -4,7 +4,9 @@ coverage:
   status:
     project:
       default:
-        target: 80
+        # https://github.com/knative/pkg/issues/1785
+        # Real goal is 80, but prow sees this as a blocker if not met for merging.
+        target: 68
         threshold: 1%
     patch:
       # Disable the coverage threshold of the patch, so that PRs are
diff --git a/OWNERS_ALIASES b/OWNERS_ALIASES
index 08d75e775..5eed75f37 100644
--- a/OWNERS_ALIASES
+++ b/OWNERS_ALIASES
@@ -8,6 +8,7 @@ aliases:
   - tcnghia
   - vagababov
   - vaikas
+  - n3wscott
 
   apis-approvers:
   - mattmoor
diff --git a/README.md b/README.md
index b0098ca9c..7304a49c8 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,7 @@
 
 [![GoDoc](https://godoc.org/github.com/knative/pkg?status.svg)](https://godoc.org/github.com/knative/pkg)
 [![Go Report Card](https://goreportcard.com/badge/knative/pkg)](https://goreportcard.com/report/knative/pkg)
+[![codecov](https://codecov.io/gh/knative/pkg/branch/master/graph/badge.svg)](https://codecov.io/gh/knative/pkg)
 
 Knative `pkg` provides a place for sharing common Knative packages across the
 Knative repos.
diff --git a/apis/duck/v1/fuzzer.go b/apis/duck/v1/fuzzer.go
new file mode 100644
index 000000000..4ef66e589
--- /dev/null
+++ b/apis/duck/v1/fuzzer.go
@@ -0,0 +1,44 @@
+/*
+Copyright 2020 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1
+
+import (
+	fuzz "github.com/google/gofuzz"
+	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
+	"k8s.io/apimachinery/pkg/runtime/serializer"
+	"knative.dev/pkg/apis"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+)
+
+var testConditions = apis.Conditions{{Type: apis.ConditionReady}, {Type: apis.ConditionSucceeded}}
+
+// FuzzerFuncs includes fuzzing funcs for knative.dev/duck v1 types
+// In particular it makes sure that Delivery has only valid BackoffPolicyType in it.
+//
+// For other examples see
+// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
+var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
+	func(codecs serializer.CodecFactory) []interface{} {
+		return []interface{}{
+			func(status *Status, c fuzz.Continue) {
+				c.FuzzNoCustom(status) // fuzz the Status
+				status.SetConditions(testConditions)
+				pkgfuzzer.FuzzConditions(status, c)
+			},
+		}
+	},
+)
diff --git a/apis/duck/v1beta1/fuzzer.go b/apis/duck/v1beta1/fuzzer.go
new file mode 100644
index 000000000..efc8be973
--- /dev/null
+++ b/apis/duck/v1beta1/fuzzer.go
@@ -0,0 +1,43 @@
+/*
+Copyright 2020 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1beta1
+
+import (
+	fuzz "github.com/google/gofuzz"
+	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
+	"k8s.io/apimachinery/pkg/runtime/serializer"
+	"knative.dev/pkg/apis"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+)
+
+var testConditions = apis.Conditions{{Type: apis.ConditionReady}, {Type: apis.ConditionSucceeded}}
+
+// FuzzerFuncs includes fuzzing funcs for knative.dev/duck v1beta1 Status types
+//
+// For other examples see
+// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/fuzzer/fuzzer.go
+var FuzzerFuncs = fuzzer.MergeFuzzerFuncs(
+	func(codecs serializer.CodecFactory) []interface{} {
+		return []interface{}{
+			func(status *Status, c fuzz.Continue) {
+				c.FuzzNoCustom(status) // fuzz the Status
+				status.SetConditions(testConditions)
+				pkgfuzzer.FuzzConditions(status, c)
+			},
+		}
+	},
+)
diff --git a/injection/config.go b/injection/config.go
index 01a5962b7..bae2b2e25 100644
--- a/injection/config.go
+++ b/injection/config.go
@@ -20,6 +20,7 @@ import (
 	"errors"
 	"flag"
 	"log"
+	"math"
 	"os"
 	"os/user"
 	"path/filepath"
@@ -37,6 +38,9 @@ func ParseAndGetRESTConfigOrDie() *rest.Config {
 			"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
 		kubeconfig = flag.String("kubeconfig", "",
 			"Path to a kubeconfig. Only required if out-of-cluster.")
+
+		burst = flag.Int("kube-api-burst", 0, "Maximum burst for throttle.")
+		qps   = flag.Float64("kube-api-qps", 0, "Maximum QPS to the server from the client.")
 	)
 	klog.InitFlags(flag.CommandLine)
 	flag.Parse()
@@ -46,6 +50,16 @@ func ParseAndGetRESTConfigOrDie() *rest.Config {
 		log.Fatal("Error building kubeconfig: ", err)
 	}
 
+	if *burst < 0 {
+		log.Fatal("Invalid burst value", *burst)
+	}
+	if *qps < 0 || *qps > math.MaxFloat32 {
+		log.Fatal("Invalid QPS value", *qps)
+	}
+
+	cfg.Burst = *burst
+	cfg.QPS = float32(*qps)
+
 	return cfg
 }
 


Test patch:
diff --git a/apis/duck/v1/roundtrip_test.go b/apis/duck/v1/roundtrip_test.go
new file mode 100644
index 000000000..25bca151b
--- /dev/null
+++ b/apis/duck/v1/roundtrip_test.go
@@ -0,0 +1,38 @@
+/*
+Copyright 2020 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1
+
+import (
+	"testing"
+
+	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
+	"k8s.io/apimachinery/pkg/runtime"
+	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+	"knative.dev/pkg/apis/testing/roundtrip"
+)
+
+func TestEventingRoundTripTypesToJSON(t *testing.T) {
+	scheme := runtime.NewScheme()
+	utilruntime.Must(AddToScheme(scheme))
+
+	fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
+		pkgfuzzer.Funcs,
+		FuzzerFuncs,
+	)
+	roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
+}
diff --git a/apis/duck/v1alpha1/roundtrip_test.go b/apis/duck/v1alpha1/roundtrip_test.go
new file mode 100644
index 000000000..c40197e50
--- /dev/null
+++ b/apis/duck/v1alpha1/roundtrip_test.go
@@ -0,0 +1,37 @@
+/*
+Copyright 2020 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1alpha1
+
+import (
+	"testing"
+
+	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
+	"k8s.io/apimachinery/pkg/runtime"
+	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+	"knative.dev/pkg/apis/testing/roundtrip"
+)
+
+func TestEventingRoundTripTypesToJSON(t *testing.T) {
+	scheme := runtime.NewScheme()
+	utilruntime.Must(AddToScheme(scheme))
+
+	fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
+		pkgfuzzer.Funcs,
+	)
+	roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
+}
diff --git a/apis/duck/v1beta1/roundtrip_test.go b/apis/duck/v1beta1/roundtrip_test.go
new file mode 100644
index 000000000..f727740ae
--- /dev/null
+++ b/apis/duck/v1beta1/roundtrip_test.go
@@ -0,0 +1,38 @@
+/*
+Copyright 2020 The Knative Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package v1beta1
+
+import (
+	"testing"
+
+	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
+	"k8s.io/apimachinery/pkg/runtime"
+	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
+	pkgfuzzer "knative.dev/pkg/apis/testing/fuzzer"
+	"knative.dev/pkg/apis/testing/roundtrip"
+)
+
+func TestEventingRoundTripTypesToJSON(t *testing.T) {
+	scheme := runtime.NewScheme()
+	utilruntime.Must(AddToScheme(scheme))
+
+	fuzzerFuncs := fuzzer.MergeFuzzerFuncs(
+		pkgfuzzer.Funcs,
+		FuzzerFuncs,
+	)
+	roundtrip.ExternalTypesViaJSON(t, scheme, fuzzerFuncs)
+}