enable karmada-scheduler health check endpoint /healthz (#163)

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
Zhen Chang 2021-02-05 10:26:18 +08:00 committed by GitHub
parent a2f81ff3a5
commit 8e47b33b01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -26,6 +26,8 @@ spec:
command:
- /bin/karmada-scheduler
- --kubeconfig=/etc/kubeconfig
- --bind-address=0.0.0.0
- --secure-port=10351
volumeMounts:
- name: kubeconfig
subPath: kubeconfig

View File

@ -9,6 +9,11 @@ import (
componentbaseconfig "k8s.io/component-base/config"
)
const (
defaultBindAddress = "0.0.0.0"
defaultPort = 10351
)
var (
defaultElectionLeaseDuration = metav1.Duration{Duration: 15 * time.Second}
defaultElectionRenewDeadline = metav1.Duration{Duration: 10 * time.Second}
@ -20,6 +25,10 @@ type Options struct {
LeaderElection componentbaseconfig.LeaderElectionConfiguration
KubeConfig string
Master string
// BindAddress is the IP address on which to listen for the --secure-port port.
BindAddress string
// SecurePort is the port that the server serves at.
SecurePort int
}
// NewOptions builds an default scheduler options.
@ -45,4 +54,6 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.LeaderElection.ResourceNamespace, "lock-namespace", "", "Define the namespace of the lock object.")
fs.StringVar(&o.KubeConfig, "kubeconfig", o.KubeConfig, "Path to a KubeConfig. Only required if out-of-cluster.")
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server. Overrides any value in KubeConfig. Only required if out-of-cluster.")
fs.StringVar(&o.BindAddress, "bind-address", defaultBindAddress, "The IP address on which to listen for the --secure-port port.")
fs.IntVar(&o.SecurePort, "secure-port", defaultPort, "The secure port on which to serve HTTPS.")
}

View File

@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"net/http"
"os"
"github.com/google/uuid"
@ -42,6 +43,8 @@ func NewSchedulerCommand(stopChan <-chan struct{}) *cobra.Command {
}
func run(opts *options.Options, stopChan <-chan struct{}) error {
go serveHealthz(fmt.Sprintf("%s:%d", opts.BindAddress, opts.SecurePort))
resetConfig, err := clientcmd.BuildConfigFromFlags(opts.Master, opts.KubeConfig)
if err != nil {
return fmt.Errorf("error building kubeconfig: %s", err.Error())
@ -101,3 +104,12 @@ func run(opts *options.Options, stopChan <-chan struct{}) error {
return nil
}
func serveHealthz(address string) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
klog.Fatal(http.ListenAndServe(address, nil))
}