You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2020/11/10 10:20:23 UTC

[camel-k] 23/25: feat: Enable operator liveness health check

This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit ceee5e3a881bc552fbe5d2877b655be80c1e52a2
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Tue Nov 3 10:38:18 2020 +0100

    feat: Enable operator liveness health check
---
 .../camel-k.v1.3.0-snapshot.clusterserviceversion.yaml  |  6 ++++++
 deploy/operator-deployment.yaml                         | 10 ++++++++--
 helm/camel-k/templates/operator.yaml                    |  6 ++++++
 pkg/cmd/operator.go                                     |  4 +++-
 pkg/cmd/operator/operator.go                            | 17 ++++++++++++-----
 5 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/deploy/olm-catalog/camel-k-dev/1.3.0-snapshot/camel-k.v1.3.0-snapshot.clusterserviceversion.yaml b/deploy/olm-catalog/camel-k-dev/1.3.0-snapshot/camel-k.v1.3.0-snapshot.clusterserviceversion.yaml
index fdf069e..1178c34 100644
--- a/deploy/olm-catalog/camel-k-dev/1.3.0-snapshot/camel-k.v1.3.0-snapshot.clusterserviceversion.yaml
+++ b/deploy/olm-catalog/camel-k-dev/1.3.0-snapshot/camel-k.v1.3.0-snapshot.clusterserviceversion.yaml
@@ -314,6 +314,12 @@ spec:
                       fieldPath: metadata.namespace
                 image: docker.io/apache/camel-k:1.3.0-SNAPSHOT
                 imagePullPolicy: IfNotPresent
+                livenessProbe:
+                  httpGet:
+                    path: /healthz
+                    port: 8081
+                  initialDelaySeconds: 5
+                  periodSeconds: 5
                 name: camel-k-operator
                 ports:
                 - containerPort: 8080
diff --git a/deploy/operator-deployment.yaml b/deploy/operator-deployment.yaml
index ce8e9e1..52e3046 100644
--- a/deploy/operator-deployment.yaml
+++ b/deploy/operator-deployment.yaml
@@ -42,8 +42,8 @@ spec:
           image: docker.io/apache/camel-k:1.3.0-SNAPSHOT
           imagePullPolicy: IfNotPresent
           command:
-          - kamel
-          - operator
+            - kamel
+            - operator
           ports:
             - containerPort: 8080
               name: metrics
@@ -63,3 +63,9 @@ spec:
               valueFrom:
                 fieldRef:
                   fieldPath: metadata.namespace
+          livenessProbe:
+            httpGet:
+              path: /healthz
+              port: 8081
+            initialDelaySeconds: 5
+            periodSeconds: 5
diff --git a/helm/camel-k/templates/operator.yaml b/helm/camel-k/templates/operator.yaml
index 3a870ed..9a68ab0 100644
--- a/helm/camel-k/templates/operator.yaml
+++ b/helm/camel-k/templates/operator.yaml
@@ -58,6 +58,12 @@ spec:
                   fieldPath: metadata.namespace
           image: {{ .Values.operator.image }}
           imagePullPolicy: IfNotPresent
+          livenessProbe:
+            httpGet:
+              path: /healthz
+              port: 8081
+            initialDelaySeconds: 5
+            periodSeconds: 5
           name: camel-k-operator
           ports:
             - containerPort: 8080
diff --git a/pkg/cmd/operator.go b/pkg/cmd/operator.go
index 2d69d01..276a1d4 100644
--- a/pkg/cmd/operator.go
+++ b/pkg/cmd/operator.go
@@ -36,15 +36,17 @@ func newCmdOperator() (*cobra.Command, *operatorCmdOptions) {
 		Run:     options.run,
 	}
 
+	cmd.Flags().Int32("health-port", 8081, "The port of the health endpoint")
 	cmd.Flags().Int32("monitoring-port", 8080, "The port of the metrics endpoint")
 
 	return &cmd, &options
 }
 
 type operatorCmdOptions struct {
+	HealthPort     int32 `mapstructure:"health-port"`
 	MonitoringPort int32 `mapstructure:"monitoring-port"`
 }
 
 func (o *operatorCmdOptions) run(_ *cobra.Command, _ []string) {
-	operator.Run(o.MonitoringPort)
+	operator.Run(o.HealthPort, o.MonitoringPort)
 }
diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go
index ec59ea1..2b1cc54 100644
--- a/pkg/cmd/operator/operator.go
+++ b/pkg/cmd/operator/operator.go
@@ -34,6 +34,7 @@ import (
 
 	ctrl "sigs.k8s.io/controller-runtime"
 	"sigs.k8s.io/controller-runtime/pkg/client/config"
+	"sigs.k8s.io/controller-runtime/pkg/healthz"
 	logf "sigs.k8s.io/controller-runtime/pkg/log"
 	"sigs.k8s.io/controller-runtime/pkg/log/zap"
 	"sigs.k8s.io/controller-runtime/pkg/manager/signals"
@@ -64,8 +65,7 @@ func printVersion() {
 }
 
 // Run starts the Camel K operator
-func Run(monitoringPort int32) {
-	rand.Seed(time.Now().UTC().UnixNano())
+func Run(healthPort, monitoringPort int32) {rand.Seed(time.Now().UTC().UnixNano())
 
 	flag.Parse()
 
@@ -124,15 +124,22 @@ func Run(monitoringPort int32) {
 
 	// Create a new Cmd to provide shared dependencies and start components
 	mgr, err := ctrl.NewManager(cfg, ctrl.Options{
-		Namespace:          namespace,
-		EventBroadcaster:   eventBroadcaster,
-		MetricsBindAddress: ":" + strconv.Itoa(int(monitoringPort)),
+		Namespace:              namespace,
+		EventBroadcaster:       eventBroadcaster,
+		HealthProbeBindAddress: ":" + strconv.Itoa(int(healthPort)),
+		MetricsBindAddress:     ":" + strconv.Itoa(int(monitoringPort)),
 	})
 	if err != nil {
 		log.Error(err, "")
 		os.Exit(1)
 	}
 
+	// Add health check
+	if err := mgr.AddHealthzCheck("health-probe", healthz.Ping); err != nil {
+		log.Error(err, "Unable add liveness check")
+		os.Exit(1)
+	}
+
 	log.Info("Registering Components.")
 
 	// Setup Scheme for all resources