From cdefccd51803bec8a1ccf3e753002cdcf53ba511 Mon Sep 17 00:00:00 2001 From: chymy Date: Mon, 12 Apr 2021 15:45:41 +0800 Subject: [PATCH] Add healthprobe for bootstrap and controlplane Signed-off-by: chymy --- bootstrap/kubeadm/config/manager/manager.yaml | 12 ++++++++++ bootstrap/kubeadm/main.go | 23 +++++++++++++++++-- .../kubeadm/config/manager/manager.yaml | 12 ++++++++++ controlplane/kubeadm/main.go | 23 +++++++++++++++++-- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/bootstrap/kubeadm/config/manager/manager.yaml b/bootstrap/kubeadm/config/manager/manager.yaml index e9a8b6886318..e5313ed4d2d2 100644 --- a/bootstrap/kubeadm/config/manager/manager.yaml +++ b/bootstrap/kubeadm/config/manager/manager.yaml @@ -24,6 +24,18 @@ spec: - "--feature-gates=MachinePool=${EXP_MACHINE_POOL:=false}" image: controller:latest name: manager + ports: + - containerPort: 9440 + name: healthz + protocol: TCP + readinessProbe: + httpGet: + path: /readyz + port: healthz + livenessProbe: + httpGet: + path: /healthz + port: healthz terminationGracePeriodSeconds: 10 serviceAccountName: manager tolerations: diff --git a/bootstrap/kubeadm/main.go b/bootstrap/kubeadm/main.go index 1bbcb4b6b28c..214ce67f4a6d 100644 --- a/bootstrap/kubeadm/main.go +++ b/bootstrap/kubeadm/main.go @@ -41,6 +41,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/healthz" // +kubebuilder:scaffold:imports ) @@ -71,6 +72,7 @@ var ( syncPeriod time.Duration webhookPort int webhookCertDir string + healthAddr string ) func InitFlags(fs *pflag.FlagSet) { @@ -110,6 +112,9 @@ func InitFlags(fs *pflag.FlagSet) { fs.StringVar(&webhookCertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/", "Webhook cert dir, only used when webhook-port is specified.") + fs.StringVar(&healthAddr, "health-addr", ":9440", + "The address the health endpoint binds to.") + feature.MutableGates.AddFlag(fs) } @@ -144,8 +149,9 @@ func main() { &corev1.ConfigMap{}, &corev1.Secret{}, }, - Port: webhookPort, - CertDir: webhookCertDir, + Port: webhookPort, + HealthProbeBindAddress: healthAddr, + CertDir: webhookCertDir, }) if err != nil { setupLog.Error(err, "unable to start manager") @@ -155,6 +161,7 @@ func main() { // Setup the context that's going to be used in controllers and for the manager. ctx := ctrl.SetupSignalHandler() + setupChecks(mgr) setupWebhooks(mgr) setupReconcilers(ctx, mgr) @@ -166,6 +173,18 @@ func main() { } } +func setupChecks(mgr ctrl.Manager) { + if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil { + setupLog.Error(err, "unable to create ready check") + os.Exit(1) + } + + if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil { + setupLog.Error(err, "unable to create health check") + os.Exit(1) + } +} + func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { if err := (&kubeadmbootstrapcontrollers.KubeadmConfigReconciler{ Client: mgr.GetClient(), diff --git a/controlplane/kubeadm/config/manager/manager.yaml b/controlplane/kubeadm/config/manager/manager.yaml index f178a92a1b80..42276200fa9f 100644 --- a/controlplane/kubeadm/config/manager/manager.yaml +++ b/controlplane/kubeadm/config/manager/manager.yaml @@ -23,6 +23,18 @@ spec: - "--metrics-bind-addr=127.0.0.1:8080" image: controller:latest name: manager + ports: + - containerPort: 9440 + name: healthz + protocol: TCP + readinessProbe: + httpGet: + path: /readyz + port: healthz + livenessProbe: + httpGet: + path: /healthz + port: healthz terminationGracePeriodSeconds: 10 serviceAccountName: manager tolerations: diff --git a/controlplane/kubeadm/main.go b/controlplane/kubeadm/main.go index 48fbe1f24111..9f5991e588cb 100644 --- a/controlplane/kubeadm/main.go +++ b/controlplane/kubeadm/main.go @@ -41,6 +41,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/healthz" // +kubebuilder:scaffold:imports ) @@ -71,6 +72,7 @@ var ( syncPeriod time.Duration webhookPort int webhookCertDir string + healthAddr string ) // InitFlags initializes the flags. @@ -107,6 +109,9 @@ func InitFlags(fs *pflag.FlagSet) { fs.StringVar(&webhookCertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/", "Webhook cert dir, only used when webhook-port is specified.") + + fs.StringVar(&healthAddr, "health-addr", ":9440", + "The address the health endpoint binds to.") } func main() { rand.Seed(time.Now().UnixNano()) @@ -139,8 +144,9 @@ func main() { &corev1.ConfigMap{}, &corev1.Secret{}, }, - Port: webhookPort, - CertDir: webhookCertDir, + Port: webhookPort, + HealthProbeBindAddress: healthAddr, + CertDir: webhookCertDir, }) if err != nil { setupLog.Error(err, "unable to start manager") @@ -150,6 +156,7 @@ func main() { // Setup the context that's going to be used in controllers and for the manager. ctx := ctrl.SetupSignalHandler() + setupChecks(mgr) setupReconcilers(ctx, mgr) setupWebhooks(mgr) @@ -161,6 +168,18 @@ func main() { } } +func setupChecks(mgr ctrl.Manager) { + if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil { + setupLog.Error(err, "unable to create ready check") + os.Exit(1) + } + + if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil { + setupLog.Error(err, "unable to create health check") + os.Exit(1) + } +} + func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { // Set up a ClusterCacheTracker to provide to controllers // requiring a connection to a remote cluster