diff --git a/cmd/machine-healthcheck/main.go b/cmd/machine-healthcheck/main.go index 60a478a60a..cb8ae2be96 100644 --- a/cmd/machine-healthcheck/main.go +++ b/cmd/machine-healthcheck/main.go @@ -12,7 +12,9 @@ import ( "github.com/openshift/machine-api-operator/pkg/controller" sdkVersion "github.com/operator-framework/operator-sdk/version" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + "k8s.io/klog" "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/runtime/signals" ) @@ -24,8 +26,24 @@ func printVersion() { } func main() { - watchNamespace := flag.String("namespace", "", "Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.") - metricsAddress := flag.String("metrics-bind-address", metrics.DefaultHealthCheckMetricsAddress, "Address for hosting metrics") + watchNamespace := flag.String( + "namespace", + "", + "Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.", + ) + + metricsAddress := flag.String( + "metrics-bind-address", + metrics.DefaultHealthCheckMetricsAddress, + "Address for hosting metrics", + ) + + healthAddr := flag.String( + "health-addr", + ":9442", + "The address for health checking.", + ) + flag.Parse() printVersion() @@ -36,7 +54,8 @@ func main() { } opts := manager.Options{ - MetricsBindAddress: *metricsAddress, + MetricsBindAddress: *metricsAddress, + HealthProbeBindAddress: *healthAddr, } if *watchNamespace != "" { opts.Namespace = *watchNamespace @@ -63,6 +82,14 @@ func main() { glog.Fatal(err) } + if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil { + klog.Fatal(err) + } + + if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil { + klog.Fatal(err) + } + glog.Info("Starting the Cmd.") // Start the Cmd diff --git a/cmd/machineset/main.go b/cmd/machineset/main.go index 91d3d57546..0a2b531e4f 100644 --- a/cmd/machineset/main.go +++ b/cmd/machineset/main.go @@ -28,6 +28,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" "k8s.io/klog" "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/runtime/signals" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -54,6 +55,12 @@ func main() { webhookCertdir := flag.String("webhook-cert-dir", defaultWebhookCertdir, "Webhook cert dir, only used when webhook-enabled is true.") + healthAddr := flag.String( + "health-addr", + ":9441", + "The address for health checking.", + ) + flag.Parse() if *watchNamespace != "" { log.Printf("Watching cluster-api objects only in namespace %q for reconciliation.", *watchNamespace) @@ -69,9 +76,10 @@ func main() { // Create a new Cmd to provide shared dependencies and start components syncPeriod := 10 * time.Minute opts := manager.Options{ - MetricsBindAddress: *metricsAddress, - SyncPeriod: &syncPeriod, - Namespace: *watchNamespace, + MetricsBindAddress: *metricsAddress, + SyncPeriod: &syncPeriod, + Namespace: *watchNamespace, + HealthProbeBindAddress: *healthAddr, } mgr, err := manager.New(cfg, opts) @@ -109,6 +117,14 @@ func main() { log.Fatal(err) } + if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil { + klog.Fatal(err) + } + + if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil { + klog.Fatal(err) + } + log.Printf("Starting the Cmd.") // Start the Cmd diff --git a/cmd/vsphere/main.go b/cmd/vsphere/main.go index 9b2b79b04d..1b44c4a6e3 100644 --- a/cmd/vsphere/main.go +++ b/cmd/vsphere/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "time" configv1 "github.com/openshift/api/config/v1" "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" @@ -14,6 +15,7 @@ import ( "github.com/openshift/machine-api-operator/pkg/version" "k8s.io/klog" "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/runtime/signals" ) @@ -26,6 +28,11 @@ func main() { watchNamespace := flag.String("namespace", "", "Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.") metricsAddress := flag.String("metrics-bind-address", metrics.DefaultMachineMetricsAddress, "Address for hosting metrics") flag.Set("logtostderr", "true") + healthAddr := flag.String( + "health-addr", + ":9440", + "The address for health checking.", + ) flag.Parse() if printVersion { @@ -34,9 +41,12 @@ func main() { } cfg := config.GetConfigOrDie() + syncPeriod := 10 * time.Minute opts := manager.Options{ - MetricsBindAddress: *metricsAddress, + MetricsBindAddress: *metricsAddress, + HealthProbeBindAddress: *healthAddr, + SyncPeriod: &syncPeriod, } if *watchNamespace != "" { opts.Namespace = *watchNamespace @@ -70,6 +80,14 @@ func main() { capimachine.AddWithActuator(mgr, machineActuator) + if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil { + klog.Fatal(err) + } + + if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil { + klog.Fatal(err) + } + if err := mgr.Start(signals.SetupSignalHandler()); err != nil { klog.Fatalf("Failed to run manager: %v", err) } diff --git a/pkg/operator/sync.go b/pkg/operator/sync.go index 0e4298b15b..4dc6e67fd7 100644 --- a/pkg/operator/sync.go +++ b/pkg/operator/sync.go @@ -16,6 +16,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/utils/pointer" ) @@ -30,6 +31,9 @@ const ( machineExposeMetricsPort = 8441 machineSetExposeMetricsPort = 8442 machineHealthCheckExposeMetricsPort = 8444 + defaultMachineHealthPort = 9440 + defaultMachineSetHealthPort = 9441 + defaultMachineHealthCheckHealthPort = 9442 kubeRBACConfigName = "config" certStoreName = "machine-api-controllers-tls" ) @@ -379,6 +383,26 @@ func newContainers(config *OperatorConfig, features map[string]bool) []corev1.Co Name: "webhook-server", ContainerPort: 8443, }, + { + Name: "healthz", + ContainerPort: defaultMachineSetHealthPort, + }, + }, + ReadinessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/healthz", + Port: intstr.Parse("healthz"), + }, + }, + }, + LivenessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/readyz", + Port: intstr.Parse("healthz"), + }, + }, }, VolumeMounts: []corev1.VolumeMount{ { @@ -404,6 +428,26 @@ func newContainers(config *OperatorConfig, features map[string]bool) []corev1.Co }, }, }, + Ports: []corev1.ContainerPort{{ + Name: "healthz", + ContainerPort: defaultMachineHealthPort, + }}, + ReadinessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/healthz", + Port: intstr.Parse("healthz"), + }, + }, + }, + LivenessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/readyz", + Port: intstr.Parse("healthz"), + }, + }, + }, }, { Name: "nodelink-controller", @@ -418,6 +462,28 @@ func newContainers(config *OperatorConfig, features map[string]bool) []corev1.Co Command: []string{"/machine-healthcheck"}, Args: args, Resources: resources, + Ports: []corev1.ContainerPort{ + { + Name: "healthz", + ContainerPort: defaultMachineHealthCheckHealthPort, + }, + }, + ReadinessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/healthz", + Port: intstr.Parse("healthz"), + }, + }, + }, + LivenessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/readyz", + Port: intstr.Parse("healthz"), + }, + }, + }, }, } return containers