enable karmada-controller-manager health check endpoint /healthz (#154)

Signed-off-by: RainbowMango <renhongcai@huawei.com>
This commit is contained in:
Hongcai Ren 2021-01-30 15:15:48 +08:00 committed by GitHub
parent bca72a58fb
commit d13875b2c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 3 deletions

View File

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

View File

@ -11,6 +11,7 @@ import (
"k8s.io/component-base/logs"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"github.com/karmada-io/karmada/cmd/controller-manager/app/options"
"github.com/karmada-io/karmada/pkg/controllers/binding"
@ -42,6 +43,7 @@ func NewControllerManagerCommand(stopChan <-chan struct{}) *cobra.Command {
}
cmd.Flags().AddGoFlagSet(flag.CommandLine)
opts.AddFlags(cmd.Flags())
return cmd
}
@ -55,15 +57,22 @@ func Run(opts *options.Options, stopChan <-chan struct{}) error {
panic(err)
}
controllerManager, err := controllerruntime.NewManager(config, controllerruntime.Options{
Scheme: gclient.NewSchema(),
LeaderElection: true, // TODO(RainbowMango): Add a flag '--enable-leader-election' for this option.
LeaderElectionID: "41db11fa.karmada.io",
Scheme: gclient.NewSchema(),
LeaderElection: true, // TODO(RainbowMango): Add a flag '--enable-leader-election' for this option.
LeaderElectionID: "41db11fa.karmada.io",
HealthProbeBindAddress: fmt.Sprintf("%s:%d", opts.BindAddress, opts.SecurePort),
LivenessEndpointName: "/healthz",
})
if err != nil {
klog.Errorf("failed to build controller manager: %v", err)
return err
}
if err = controllerManager.AddHealthzCheck("ping", healthz.Ping); err != nil {
klog.Errorf("failed to add health check endpoint: %v", err)
return err
}
setupControllers(controllerManager, stopChan)
// blocks until the stop channel is closed.

View File

@ -3,6 +3,7 @@ package options
import (
"time"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/leaderelection/resourcelock"
componentbaseconfig "k8s.io/component-base/config"
@ -15,10 +16,20 @@ var (
defaultElectionRetryPeriod = metav1.Duration{Duration: 2 * time.Second}
)
const (
defaultBindAddress = "0.0.0.0"
defaultPort = 10357
)
// Options contains everything necessary to create and run controller-manager.
type Options struct {
HostNamespace string
LeaderElection componentbaseconfig.LeaderElectionConfiguration
// BindAddress is the IP address on which to listen for the --secure-port port.
BindAddress string
// SecurePort is the port that the the server serves at.
// Note: We hope support https in the future once controller-runtime provides the functionality.
SecurePort int
}
// NewOptions builds an empty options.
@ -53,3 +64,11 @@ func (o *Options) Complete() {
klog.Infof("Set default value: Options.LeaderElection.RetryPeriod = %s", defaultElectionRetryPeriod.Duration.String())
}
}
// AddFlags adds flags to the specified FlagSet.
func (o *Options) AddFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.BindAddress, "bind-address", defaultBindAddress,
"The IP address on which to listen for the --secure-port port.")
flags.IntVar(&o.SecurePort, "secure-port", defaultPort,
"The secure port on which to serve HTTPS.")
}