Skip to content

Commit

Permalink
controller: fix apiserver connection timeout on startup (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Mar 24, 2023
1 parent 4b8654d commit 8f67a32
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/controller/config.go
Expand Up @@ -12,10 +12,10 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"kubevirt.io/client-go/kubecli"

clientset "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned"
"github.com/kubeovn/kube-ovn/pkg/util"
"kubevirt.io/client-go/kubecli"
)

// Configuration is the controller conf
Expand Down Expand Up @@ -290,6 +290,13 @@ func (config *Configuration) initKubeClient() error {
klog.Errorf("failed to build kubeconfig %v", err)
return err
}

// try to connect to apiserver's tcp port
if err = util.DialApiServer(cfg.Host); err != nil {
klog.Errorf("failed to dial apiserver: %v", err)
return err
}

cfg.QPS = 1000
cfg.Burst = 2000
// use cmd arg to modify timeout later
Expand Down
7 changes: 7 additions & 0 deletions pkg/daemon/config.go
Expand Up @@ -322,6 +322,13 @@ func (config *Configuration) initKubeClient() error {
return err
}
}

// try to connect to apiserver's tcp port
if err = util.DialApiServer(cfg.Host); err != nil {
klog.Errorf("failed to dial apiserver: %v", err)
return err
}

cfg.QPS = 1000
cfg.Burst = 2000

Expand Down
28 changes: 28 additions & 0 deletions pkg/util/k8s.go
@@ -1,11 +1,39 @@
package util

import (
"fmt"
"net"
"net/url"
"strings"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)

func DialApiServer(host string) error {
u, err := url.Parse(host)
if err != nil {
return fmt.Errorf("failed to parse host %q: %v", host, err)
}

address := net.JoinHostPort(u.Hostname(), u.Port())
timer := time.NewTimer(3 * time.Second)
for i := 0; i < 10; i++ {
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
if err == nil {
klog.Infof("succeeded to dial apiserver %q", address)
_ = conn.Close()
return nil
}
klog.Warningf("failed to dial apiserver %q: %v", address, err)
<-timer.C
timer.Reset(3 * time.Second)
}

return fmt.Errorf("timed out dialing apiserver %q", host)
}

func GetNodeInternalIP(node v1.Node) (ipv4, ipv6 string) {
var ips []string
for _, addr := range node.Status.Addresses {
Expand Down

0 comments on commit 8f67a32

Please sign in to comment.