enable karmada-scheduler health check endpoint /healthz (#163)
Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
parent
a2f81ff3a5
commit
8e47b33b01
|
@ -26,6 +26,8 @@ spec:
|
||||||
command:
|
command:
|
||||||
- /bin/karmada-scheduler
|
- /bin/karmada-scheduler
|
||||||
- --kubeconfig=/etc/kubeconfig
|
- --kubeconfig=/etc/kubeconfig
|
||||||
|
- --bind-address=0.0.0.0
|
||||||
|
- --secure-port=10351
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- name: kubeconfig
|
- name: kubeconfig
|
||||||
subPath: kubeconfig
|
subPath: kubeconfig
|
||||||
|
|
|
@ -9,6 +9,11 @@ import (
|
||||||
componentbaseconfig "k8s.io/component-base/config"
|
componentbaseconfig "k8s.io/component-base/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultBindAddress = "0.0.0.0"
|
||||||
|
defaultPort = 10351
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultElectionLeaseDuration = metav1.Duration{Duration: 15 * time.Second}
|
defaultElectionLeaseDuration = metav1.Duration{Duration: 15 * time.Second}
|
||||||
defaultElectionRenewDeadline = metav1.Duration{Duration: 10 * time.Second}
|
defaultElectionRenewDeadline = metav1.Duration{Duration: 10 * time.Second}
|
||||||
|
@ -20,6 +25,10 @@ type Options struct {
|
||||||
LeaderElection componentbaseconfig.LeaderElectionConfiguration
|
LeaderElection componentbaseconfig.LeaderElectionConfiguration
|
||||||
KubeConfig string
|
KubeConfig string
|
||||||
Master 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.
|
// 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.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.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.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.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -42,6 +43,8 @@ func NewSchedulerCommand(stopChan <-chan struct{}) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(opts *options.Options, stopChan <-chan struct{}) error {
|
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)
|
resetConfig, err := clientcmd.BuildConfigFromFlags(opts.Master, opts.KubeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error building kubeconfig: %s", err.Error())
|
return fmt.Errorf("error building kubeconfig: %s", err.Error())
|
||||||
|
@ -101,3 +104,12 @@ func run(opts *options.Options, stopChan <-chan struct{}) error {
|
||||||
|
|
||||||
return nil
|
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))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue