Skip to content

Commit

Permalink
feature: add 'yurthub-healthcheck-timeout' flag for 'yurtctl convert'…
Browse files Browse the repository at this point in the history
… command (#290)

Signed-off-by: SataQiu <1527062125@qq.com>
  • Loading branch information
SataQiu authored May 7, 2021
1 parent 84d3fe8 commit d61c30c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 44 deletions.
30 changes: 23 additions & 7 deletions pkg/yurtctl/cmd/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package convert
import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -51,12 +52,18 @@ const (
ProviderKind Provider = "kind"
)

const (
// defaultYurthubHealthCheckTimeout defines the default timeout for yurthub health check phase
defaultYurthubHealthCheckTimeout = 2 * time.Minute
)

// ConvertOptions has the information that required by convert operation
type ConvertOptions struct {
clientSet *kubernetes.Clientset
CloudNodes []string
Provider Provider
YurhubImage string
YurthubHealthCheckTimeout time.Duration
YurtControllerManagerImage string
YurctlServantImage string
YurttunnelServerImage string
Expand Down Expand Up @@ -102,6 +109,8 @@ func NewConvertCmd() *cobra.Command {
cmd.Flags().String("yurthub-image",
"openyurt/yurthub:latest",
"The yurthub image.")
cmd.Flags().Duration("yurthub-healthcheck-timeout", defaultYurthubHealthCheckTimeout,
"The timeout for yurthub health check.")
cmd.Flags().String("yurt-controller-manager-image",
"openyurt/yurt-controller-manager:latest",
"The yurt-controller-manager image.")
Expand Down Expand Up @@ -154,6 +163,12 @@ func (co *ConvertOptions) Complete(flags *pflag.FlagSet) error {
}
co.YurhubImage = yhi

yurthubHealthCheckTimeout, err := flags.GetDuration("yurthub-healthcheck-timeout")
if err != nil {
return err
}
co.YurthubHealthCheckTimeout = yurthubHealthCheckTimeout

ycmi, err := flags.GetString("yurt-controller-manager-image")
if err != nil {
return err
Expand Down Expand Up @@ -335,13 +350,14 @@ func (co *ConvertOptions) RunConvert() (err error) {
return err
}
if err = kubeutil.RunServantJobs(co.clientSet, map[string]string{
"provider": string(co.Provider),
"action": "convert",
"yurtctl_servant_image": co.YurctlServantImage,
"yurthub_image": co.YurhubImage,
"joinToken": joinToken,
"pod_manifest_path": co.PodMainfestPath,
"kubeadm_conf_path": co.KubeadmConfPath,
"provider": string(co.Provider),
"action": "convert",
"yurtctl_servant_image": co.YurctlServantImage,
"yurthub_image": co.YurhubImage,
"yurthub_healthcheck_timeout": co.YurthubHealthCheckTimeout.String(),
"joinToken": joinToken,
"pod_manifest_path": co.PodMainfestPath,
"kubeadm_conf_path": co.KubeadmConfPath,
}, edgeNodeNames, true); err != nil {
klog.Errorf("fail to run ServantJobs: %s", err)
return
Expand Down
72 changes: 36 additions & 36 deletions pkg/yurtctl/cmd/convert/edgenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
nodeutil "k8s.io/kubernetes/pkg/controller/util/node"
Expand All @@ -45,21 +46,21 @@ const (
kubeletConfigRegularExpression = "\\-\\-kubeconfig=.*kubelet.conf"
apiserverAddrRegularExpression = "server: (http(s)?:\\/\\/)?[\\w][-\\w]{0,62}(\\.[\\w][-\\w]{0,62})*(:[\\d]{1,5})?"
hubHealthzCheckFrequency = 10 * time.Second
failedRetry = 5
filemode = 0666
dirmode = 0755
)

// ConvertEdgeNodeOptions has the information required by sub command convert edgenode
type ConvertEdgeNodeOptions struct {
clientSet *kubernetes.Clientset
EdgeNodes []string
YurthubImage string
YurctlServantImage string
PodMainfestPath string
JoinToken string
KubeadmConfPath string
openyurtDir string
clientSet *kubernetes.Clientset
EdgeNodes []string
YurthubImage string
YurthubHealthCheckTimeout time.Duration
YurctlServantImage string
PodMainfestPath string
JoinToken string
KubeadmConfPath string
openyurtDir string
}

// NewConvertEdgeNodeOptions creates a new ConvertEdgeNodeOptions
Expand Down Expand Up @@ -87,6 +88,8 @@ func NewConvertEdgeNodeCmd() *cobra.Command {
"The list of edge nodes wanted to be convert.(e.g. -e edgenode1,edgenode2)")
cmd.Flags().String("yurthub-image", "openyurt/yurthub:latest",
"The yurthub image.")
cmd.Flags().Duration("yurthub-healthcheck-timeout", defaultYurthubHealthCheckTimeout,
"The timeout for yurthub health check.")
cmd.Flags().String("yurtctl-servant-image", "openyurt/yurtctl-servant:latest",
"The yurtctl-servant image.")
cmd.Flags().String("pod-manifest-path", "",
Expand Down Expand Up @@ -114,6 +117,12 @@ func (c *ConvertEdgeNodeOptions) Complete(flags *pflag.FlagSet) error {
}
c.YurthubImage = yurthubImage

yurthubHealthCheckTimeout, err := flags.GetDuration("yurthub-healthcheck-timeout")
if err != nil {
return err
}
c.YurthubHealthCheckTimeout = yurthubHealthCheckTimeout

ycsi, err := flags.GetString("yurtctl-servant-image")
if err != nil {
return err
Expand Down Expand Up @@ -215,12 +224,13 @@ func (c *ConvertEdgeNodeOptions) RunConvertEdgeNode() (err error) {
return err
}
if err = kubeutil.RunServantJobs(c.clientSet, map[string]string{
"action": "convert",
"yurtctl_servant_image": c.YurctlServantImage,
"yurthub_image": c.YurthubImage,
"joinToken": joinToken,
"pod_manifest_path": c.PodMainfestPath,
"kubeadm_conf_path": c.KubeadmConfPath,
"action": "convert",
"yurtctl_servant_image": c.YurctlServantImage,
"yurthub_image": c.YurthubImage,
"yurthub_healthcheck_timeout": c.YurthubHealthCheckTimeout.String(),
"joinToken": joinToken,
"pod_manifest_path": c.PodMainfestPath,
"kubeadm_conf_path": c.KubeadmConfPath,
}, c.EdgeNodes, true); err != nil {
klog.Errorf("fail to run ServantJobs: %s", err)
return err
Expand Down Expand Up @@ -310,7 +320,7 @@ func (c *ConvertEdgeNodeOptions) SetupYurthub() error {
klog.Infof("create the %s/yurt-hub.yaml", c.PodMainfestPath)

// 2. wait yurthub pod to be ready
err = hubHealthcheck()
err = hubHealthcheck(c.YurthubHealthCheckTimeout)
return err
}

Expand Down Expand Up @@ -380,33 +390,23 @@ func (c *ConvertEdgeNodeOptions) getKubeletSvcBackup() string {
}

// hubHealthcheck will check the status of yurthub pod
func hubHealthcheck() error {
func hubHealthcheck(timeout time.Duration) error {
serverHealthzURL, err := url.Parse(fmt.Sprintf("http://%s", enutil.ServerHealthzServer))
if err != nil {
return err
}
serverHealthzURL.Path = enutil.ServerHealthzURLPath

intervalTicker := time.NewTicker(hubHealthzCheckFrequency)
defer intervalTicker.Stop()
retry := failedRetry
for {
select {
case <-intervalTicker.C:
_, err := pingClusterHealthz(http.DefaultClient, serverHealthzURL.String())
retry--
if err != nil {
if retry > 0 {
klog.Infof("yurt-hub is not ready, ping cluster healthz with result: %v, will retry %d times", err, retry)
} else {
return fmt.Errorf("yurt-hub failed after retry 5 times, ping cluster healthz with result: %v", err)
}
} else {
klog.Infof("yurt-hub healthz is OK")
return nil
}
start := time.Now()
return wait.PollImmediate(hubHealthzCheckFrequency, timeout, func() (bool, error) {
_, err := pingClusterHealthz(http.DefaultClient, serverHealthzURL.String())
if err != nil {
klog.Infof("yurt-hub is not ready, ping cluster healthz with result: %v", err)
return false, nil
}
}
klog.Infof("yurt-hub healthz is OK after %f seconds", time.Since(start).Seconds())
return true, nil
})
}

func pingClusterHealthz(client *http.Client, addr string) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yurtctl/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ spec:
- /bin/sh
- -c
args:
- "cp /usr/local/bin/yurtctl /tmp && nsenter -t 1 -m -u -n -i -- /var/tmp/yurtctl convert edgenode --yurthub-image {{.yurthub_image}} --join-token {{.joinToken}} && rm /tmp/yurtctl"
- "cp /usr/local/bin/yurtctl /tmp && nsenter -t 1 -m -u -n -i -- /var/tmp/yurtctl convert edgenode --yurthub-image {{.yurthub_image}} --yurthub-healthcheck-timeout {{.yurthub_healthcheck_timeout}} --join-token {{.joinToken}} && rm /tmp/yurtctl"
securityContext:
privileged: true
volumeMounts:
Expand Down

0 comments on commit d61c30c

Please sign in to comment.