Skip to content

Commit

Permalink
Add configuration for k8s client config timeout (#296)
Browse files Browse the repository at this point in the history
Previously, value of timeout can not be configured and
is 10s as default value.

Now add the falg kube-api-timeout that can be configured by
falg.

Signed-off-by: yuzhipeng <[email protected]>

Signed-off-by: yuzhipeng <[email protected]>
  • Loading branch information
yuzp1996 committed Oct 10, 2022
1 parent 8078a70 commit bef814f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
3 changes: 2 additions & 1 deletion check/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package check

import (
"context"
"testing"

. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
)

func TestInstalledTekton(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions client/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2022 The Katanomi 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 client

import (
"os"
"testing"
"time"

. "github.com/onsi/gomega"
)

func TestNewDefaultClientWithTimeOut(t *testing.T) {
g := NewGomegaWithT(t)

os.Setenv("HTTP_CLIENT_TIMEOUT", "20")
client := NewHTTPClient()

g.Expect(client.Timeout).To(Equal(20 * time.Second))
}

func TestNewDefaultClientWithDefaultTimeOut(t *testing.T) {
g := NewGomegaWithT(t)

os.Unsetenv("HTTP_CLIENT_TIMEOUT")
client := NewHTTPClient()

g.Expect(client.Timeout).To(Equal(30 * time.Second))
}
9 changes: 6 additions & 3 deletions sharedmain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var (
DefaultTimeout = kclient.DefaultTimeout
DefaultQPS = kclient.DefaultQPS
DefaultBurst = kclient.DefaultBurst
Timeout time.Duration
Burst int
QPS float64
ConfigFile string
Expand Down Expand Up @@ -129,6 +130,9 @@ type AppBuilder struct {

// ParseFlag parse flag needed for App
func ParseFlag() {
flag.DurationVar(&Timeout, "kube-api-timeout", DefaultTimeout,
"The maximum length of time to wait before giving up on a server request."+
"A value of zero means no timeout. DefaultTimeOut: 10s")
flag.Float64Var(&QPS, "kube-api-qps", float64(DefaultQPS),
"qps indicates the maximum QPS to the master from this client."+
"If it's zero, the created RESTClient will use DefaultQPS: 50")
Expand All @@ -154,9 +158,8 @@ func (a *AppBuilder) init() {
ParseFlag()
a.Context = ctrl.SetupSignalHandler()
a.Context, a.Config = GetConfigOrDie(a.Context)
if a.Config.Timeout == 0 {
a.Config.Timeout = DefaultTimeout
}
a.Config.Timeout = Timeout

if a.Config.QPS < float32(QPS) {
a.Config.QPS = float32(QPS)
}
Expand Down
2 changes: 1 addition & 1 deletion sharedmain/sharedmain_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package sharedmain_test
package sharedmain

import (
"testing"
Expand Down
42 changes: 33 additions & 9 deletions sharedmain/sharedmain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,46 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package sharedmain_test
package sharedmain

import (
"github.com/katanomi/pkg/sharedmain"
"flag"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Sharedmain", func() {
Context("ParseFlag should work as except", func() {
It("If flag is not provided,should return the default value", func() {
sharedmain.ParseFlag()
Expect(sharedmain.QPS).To(Equal(float64(sharedmain.DefaultQPS)))
Expect(sharedmain.Burst).To(Equal(sharedmain.DefaultBurst))
Expect(sharedmain.ConfigFile).To(Equal(""))
var _ = BeforeSuite(func() {
ParseFlag()
})

var _ = Describe("ParseFlag", func() {

When("flag not provided", func() {
It("return default values", func() {
Expect(QPS).To(Equal(float64(DefaultQPS)))
Expect(Burst).To(Equal(DefaultBurst))
Expect(Timeout).To(Equal(DefaultTimeout))
Expect(ConfigFile).To(Equal(""))
})
})

When("flag proviede", func() {
BeforeEach(func() {
flag.CommandLine.Parse([]string{
"--kube-api-timeout", "20s",
"--kube-api-qps", "80",
"--kube-api-burst", "90",
"--config", "config",
})
})
It("return configured values", func() {
Expect(QPS).To(Equal(float64(80)))
Expect(Burst).To(Equal(90))
Expect(Timeout).To(Equal(20 * time.Second))
Expect(ConfigFile).To(Equal("config"))
})
})

})

0 comments on commit bef814f

Please sign in to comment.