Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dialing https server #4324

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions charts/kube-ovn/templates/controller-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ spec:
exec:
command:
- /kube-ovn/kube-ovn-controller-healthcheck
- --tls={{- .Values.func.SECURE_SERVING }}
periodSeconds: 3
timeoutSeconds: 45
livenessProbe:
exec:
command:
- /kube-ovn/kube-ovn-controller-healthcheck
- --tls={{- .Values.func.SECURE_SERVING }}
initialDelaySeconds: 300
periodSeconds: 7
failureThreshold: 5
Expand Down
38 changes: 31 additions & 7 deletions cmd/controller_health_check/controller_health_check.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
package controller_health_check

import (
"net"
"flag"
"os"
"time"

"github.com/spf13/pflag"
"k8s.io/klog/v2"

"github.com/kubeovn/kube-ovn/pkg/util"
)

func CmdMain() {
tls := pflag.Bool("tls", false, "Whether kube-ovn-controller uses TLS")

klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)

// sync the glog and klog flags.
pflag.CommandLine.VisitAll(func(f1 *pflag.Flag) {
f2 := klogFlags.Lookup(f1.Name)
if f2 != nil {
value := f1.Value.String()
if err := f2.Value.Set(value); err != nil {
util.LogFatalAndExit(err, "failed to set pflag")
}
}
})

pflag.CommandLine.AddGoFlagSet(klogFlags)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

addr := "127.0.0.1:10660"
if os.Getenv("ENABLE_BIND_LOCAL_IP") == "true" {
addr = util.JoinHostPort(os.Getenv("POD_IP"), 10660)
}

conn, err := net.DialTimeout("tcp", addr, 3*time.Second)
if err != nil {
util.LogFatalAndExit(err, "failed to probe the socket")
if *tls {
addr = "tls://" + addr
} else {
addr = "tcp://" + addr
}
err = conn.Close()
if err != nil {
util.LogFatalAndExit(err, "failed to close connection")

if err := util.DialTCP(addr, time.Second, false); err != nil {
util.LogFatalAndExit(err, "failed to probe the socket")
}
}
2 changes: 2 additions & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4209,12 +4209,14 @@ spec:
exec:
command:
- /kube-ovn/kube-ovn-controller-healthcheck
- --tls=${SECURE_SERVING}
periodSeconds: 3
timeoutSeconds: 45
livenessProbe:
exec:
command:
- /kube-ovn/kube-ovn-controller-healthcheck
- --tls=${SECURE_SERVING}
initialDelaySeconds: 300
periodSeconds: 7
failureThreshold: 5
Expand Down
37 changes: 30 additions & 7 deletions pkg/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
Expand All @@ -18,24 +19,46 @@
"k8s.io/klog/v2"
)

func DialAPIServer(host string) error {
func DialTCP(host string, timeout time.Duration, verbose bool) error {
u, err := url.Parse(host)
if err != nil {
return fmt.Errorf("failed to parse host %q: %v", host, err)
}

var conn net.Conn
address := net.JoinHostPort(u.Hostname(), u.Port())
timer := time.NewTimer(3 * time.Second)
switch u.Scheme {
case "tcp", "http":
conn, err = net.DialTimeout("tcp", address, timeout)
case "tls", "https":
config := &tls.Config{InsecureSkipVerify: true} // #nosec G402
Dismissed Show dismissed Hide dismissed
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", address, config)
default:
return fmt.Errorf("unsupported scheme %q", u.Scheme)
}

if err == nil {
if verbose {
klog.Infof("succeeded to dial host %q", host)
}
_ = conn.Close()
return nil
}

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

func DialAPIServer(host string) error {
interval := 3 * time.Second
timer := time.NewTimer(interval)
for i := 0; i < 10; i++ {
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
err := DialTCP(host, interval, true)
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)
klog.Warningf("failed to dial apiserver %q: %v", host, err)
<-timer.C
timer.Reset(3 * time.Second)
timer.Reset(interval)
}

return fmt.Errorf("timed out dialing apiserver %q", host)
Expand Down
Loading