From d63c9422683c933d32dc9a55922850ab92f84957 Mon Sep 17 00:00:00 2001 From: Adrian Kostrubiak <72626345+adriankostrubiak-tomtom@users.noreply.github.com> Date: Mon, 6 Dec 2021 06:37:56 -0500 Subject: [PATCH] add healthz, readyz probes to controller manager. (#603) * add healthz, readyz probes to controller manager. fixes #602 Signed-off-by: Adrian Kostrubiak adrian.kostrubiak@tomtom.com * fix manager port * run `make bundle' to update csv * nitty change flag naming to be more consistent with existing flags * fix import ordering --- ...emetry-operator.clusterserviceversion.yaml | 12 ++++++ config/manager/manager.yaml | 12 ++++++ main.go | 37 +++++++++++++------ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml index a022f288e1..9ec503968e 100644 --- a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml @@ -266,11 +266,23 @@ spec: - --metrics-addr=127.0.0.1:8080 - --enable-leader-election image: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:v0.40.0 + livenessProbe: + httpGet: + path: /healthz + port: 8081 + initialDelaySeconds: 15 + periodSeconds: 20 name: manager ports: - containerPort: 9443 name: webhook-server protocol: TCP + readinessProbe: + httpGet: + path: /readyz + port: 8081 + initialDelaySeconds: 5 + periodSeconds: 10 resources: limits: cpu: 200m diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index fbb8654ddc..4057084d49 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -27,6 +27,18 @@ spec: - --enable-leader-election image: controller name: manager + livenessProbe: + httpGet: + path: /healthz + port: 8081 + initialDelaySeconds: 15 + periodSeconds: 20 + readinessProbe: + httpGet: + path: /readyz + port: 8081 + initialDelaySeconds: 5 + periodSeconds: 10 resources: limits: cpu: 200m diff --git a/main.go b/main.go index f70438c099..e09f8928f2 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -68,12 +69,16 @@ func main() { v := version.Get() // add flags related to this operator - var metricsAddr string - var enableLeaderElection bool - var collectorImage string - var targetAllocatorImage string - var autoInstrumentationJava string + var ( + metricsAddr string + probeAddr string + enableLeaderElection bool + collectorImage string + targetAllocatorImage string + autoInstrumentationJava string + ) pflag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") + flag.StringVar(&probeAddr, "health-probe-addr", ":8081", "The address the probe endpoint binds to.") pflag.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") @@ -124,12 +129,13 @@ func main() { } mgrOptions := ctrl.Options{ - Scheme: scheme, - MetricsBindAddress: metricsAddr, - Port: 9443, - LeaderElection: enableLeaderElection, - LeaderElectionID: "9f7554c3.opentelemetry.io", - Namespace: watchNamespace, + Scheme: scheme, + MetricsBindAddress: metricsAddr, + Port: 9443, + HealthProbeBindAddress: probeAddr, + LeaderElection: enableLeaderElection, + LeaderElectionID: "9f7554c3.opentelemetry.io", + Namespace: watchNamespace, } if strings.Contains(watchNamespace, ",") { @@ -206,6 +212,15 @@ func main() { } // +kubebuilder:scaffold:builder + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { + setupLog.Error(err, "unable to set up health check") + os.Exit(1) + } + if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { + setupLog.Error(err, "unable to set up ready check") + os.Exit(1) + } + setupLog.Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager")