Skip to content

Commit

Permalink
Merge pull request #157 from River-sh/add_healthy_check
Browse files Browse the repository at this point in the history
add healthy check and some logs
  • Loading branch information
njucjc authored Jan 8, 2024
2 parents 67cf618 + 44173d7 commit 15df54b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
21 changes: 21 additions & 0 deletions charts/raven-agent/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ spec:
- image: {{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
name: raven-agent
livenessProbe:
failureThreshold: 10
httpGet:
path: /healthz
port: 10275
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 15
readinessProbe:
failureThreshold: 10
httpGet:
path: /readyz
port: 10275
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 15
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
{{- with .Values.containerEnv }}
Expand All @@ -32,6 +52,7 @@ spec:
- --forward-node-ip={{.Values.vpn.forwardNodeIP}}
- --nat-traversal={{.Values.vpn.natTraversal}}
- --metric-bind-addr={{.Values.vpn.metricBindAddr}}
- --health-probe-addr={{.Values.healthyBindAddr}}
- --vpn-bind-port={{.Values.vpn.tunnelAddr}}
- --proxy-metric-bind-addr={{.Values.proxy.metricsBindAddr}}
- --proxy-internal-secure-addr={{.Values.proxy.internalSecureAddr}}
Expand Down
1 change: 1 addition & 0 deletions charts/raven-agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ image:
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
healthyBindAddr: ":10275"

serviceAccount:
# Specifies whether a service account should be created
Expand Down
10 changes: 6 additions & 4 deletions cmd/agent/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ type Config struct {
NodeName string
NodeIP string
MetricsBindAddress string
KubeConfig *rest.Config
Manager manager.Manager
Tunnel *TunnelConfig
Proxy *ProxyConfig
HealthProbeAddr string

KubeConfig *rest.Config
Manager manager.Manager
Tunnel *TunnelConfig
Proxy *ProxyConfig
}

type TunnelConfig struct {
Expand Down
29 changes: 24 additions & 5 deletions cmd/agent/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package options
import (
"errors"
"fmt"

"net"
"os"
"strconv"
Expand All @@ -16,6 +15,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/openyurtio/api/raven/v1beta1"
Expand All @@ -28,6 +28,7 @@ import (
const (
DefaultTunnelMetricsPort = 10265
DefaultProxyMetricsPort = 10266
DefaultHealthyProbeAddr = 10275
)

// AgentOptions has the information that required by the raven agent
Expand All @@ -38,6 +39,7 @@ type AgentOptions struct {
NodeIP string
Kubeconfig string
MetricsBindAddress string
HealthProbeAddr string
}

type TunnelOptions struct {
Expand Down Expand Up @@ -87,6 +89,8 @@ func (o *AgentOptions) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&o.ForwardNodeIP, "forward-node-ip", o.ForwardNodeIP, `Forward node IP or not. (default "false")`)
fs.BoolVar(&o.NATTraversal, "nat-traversal", o.NATTraversal, `Enable NAT Traversal or not. (default "false")`)
fs.StringVar(&o.MetricsBindAddress, "metric-bind-addr", o.MetricsBindAddress, `Binding address of tunnel metrics. (default ":10265")`)
fs.StringVar(&o.HealthProbeAddr, "health-probe-addr", o.HealthProbeAddr, `The address the healthz/readyz endpoint binds to.. (default ":10275")`)

fs.StringVar(&o.VPNPort, "vpn-bind-port", o.VPNPort, `Binding port of vpn. (default ":4500")`)
fs.StringVar(&o.ProxyMetricsAddress, "proxy-metric-bind-addr", o.ProxyMetricsAddress, `Binding address of proxy metrics. (default ":10266")`)
fs.StringVar(&o.InternalSecureAddress, "proxy-internal-secure-addr", o.InternalSecureAddress, `Binding secure address of proxy server. (default ":10263")`)
Expand Down Expand Up @@ -114,7 +118,8 @@ func (o *AgentOptions) Config() (*config.Config, error) {
cfg = restclient.AddUserAgent(cfg, "raven-agent-ds")
c.KubeConfig = cfg
c.MetricsBindAddress = resolveAddress(c.MetricsBindAddress, c.NodeIP, strconv.Itoa(DefaultTunnelMetricsPort))
c.Manager, err = newMgr(cfg, c.MetricsBindAddress)
c.HealthProbeAddr = resolveAddress(c.HealthProbeAddr, c.NodeIP, strconv.Itoa(DefaultHealthyProbeAddr))
c.Manager, err = newMgr(cfg, c.MetricsBindAddress, c.HealthProbeAddr)
if err != nil {
return nil, fmt.Errorf("failed to create manager: %s", err)
}
Expand Down Expand Up @@ -171,25 +176,39 @@ func (o *AgentOptions) Config() (*config.Config, error) {
return c, err
}

func newMgr(cfg *restclient.Config, metricsBindAddress string) (manager.Manager, error) {
func newMgr(cfg *restclient.Config, metricsBindAddress, healthyProbeAddress string) (manager.Manager, error) {
scheme := runtime.NewScheme()
_ = v1.AddToScheme(scheme)
_ = v1beta1.AddToScheme(scheme)

opt := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsBindAddress,
Scheme: scheme,
MetricsBindAddress: metricsBindAddress,
HealthProbeBindAddress: healthyProbeAddress,
}

mgr, err := ctrl.NewManager(cfg, opt)
if err != nil {
klog.ErrorS(err, "failed to new manager for raven agent controller")
return nil, err
}

if err = mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
klog.ErrorS(err, "unable to set up health check")
os.Exit(1)
}
if err = mgr.AddReadyzCheck("check", healthz.Ping); err != nil {
klog.ErrorS(err, "unable to set up ready check")
os.Exit(1)
}

return mgr, nil
}

func resolveAddress(srcAddr, defaultHost, defaultPort string) string {
if srcAddr == "" {
return net.JoinHostPort(defaultHost, defaultPort)
}
host, port, err := net.SplitHostPort(srcAddr)
if err != nil {
return net.JoinHostPort(defaultHost, defaultPort)
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkengine/vpndriver/libreswan/libreswan.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func whackCmdFn(args ...string) error {
time.Sleep(1 * time.Second)
}
if err != nil {
return fmt.Errorf("error whacking with %v: %v", args, err)
return fmt.Errorf("error whacking with %v: %v, error %s", args, err, string(output))
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/proxyengine/proxyclient/proxyclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c *ProxyClient) Start(ctx context.Context) error {
return fmt.Errorf("failed to new cert manager factory for proxy client %s, error %s", c.name, err.Error())
}
clientCertManager.Start()
defer clientCertManager.Stop()
_ = wait.PollUntil(5*time.Second, func() (bool, error) {
if clientCertManager.Current() != nil {
return true, nil
Expand Down
13 changes: 6 additions & 7 deletions pkg/proxyengine/proxyserver/manageheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,16 @@ func (h *headerManger) getAPIServerRequestDestAddress(r *http.Request) (name, ip
}

func (h *headerManger) getNormalRequestDestAddress(r *http.Request) (name, ip, port string, err error) {
var nodeName string
nodeName, port, err = net.SplitHostPort(r.Host)
if err != nil {
return "", "", "", err
}
nodeName := r.Header.Get(utils.RavenProxyHostHeaderKey)
if nodeName == "" {
nodeName = r.Header.Get(utils.RavenProxyHostHeaderKey)
nodeName, port, err = net.SplitHostPort(r.Host)
if err != nil {
return "", "", "", err
}
}
ipAddress := net.ParseIP(nodeName)
if ipAddress != nil {
klog.Warning(utils.FormatProxyServer("raven proxy server not support request.Host is %s", r.Host))
klog.Warning(utils.FormatProxyServer("raven proxy server not support dest address %s and request.URL is %s", ipAddress, r.URL.String()))
return "", "", "", nil
}
var node v1.Node
Expand Down

0 comments on commit 15df54b

Please sign in to comment.