From d1d9340fba0d54e880ddfb1adff9796c1b934451 Mon Sep 17 00:00:00 2001 From: Khurram Baig Date: Mon, 10 Feb 2020 23:48:07 +0530 Subject: [PATCH] Add Liveness Probe for EventListener Deployment Add a Liveness Probe for EventListener Deployment at URL /live --- .errcheck.txt | 1 + cmd/eventlistenersink/main.go | 5 +++++ config/controller.yaml | 4 +++- .../v1alpha1/eventlistener/eventlistener.go | 17 ++++++++++++++ .../eventlistener/eventlistener_test.go | 22 +++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/.errcheck.txt b/.errcheck.txt index 64671e21f..aaadf1279 100644 --- a/.errcheck.txt +++ b/.errcheck.txt @@ -2,6 +2,7 @@ flag.Set os.Setenv logger.Sync +fmt.Fprint fmt.Fprintf fmt.Fprintln (io.Closer).Close diff --git a/cmd/eventlistenersink/main.go b/cmd/eventlistenersink/main.go index 781d4fed0..2c7604ee6 100644 --- a/cmd/eventlistenersink/main.go +++ b/cmd/eventlistenersink/main.go @@ -97,5 +97,10 @@ func main() { // Listen and serve logger.Infof("Listen and serve on port %s", sinkArgs.Port) http.HandleFunc("/", r.HandleEvent) + // For handling Liveness Probe + http.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + fmt.Fprint(w, "ok") + }) logger.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", sinkArgs.Port), nil)) } diff --git a/config/controller.yaml b/config/controller.yaml index ee51a6629..060f76f40 100644 --- a/config/controller.yaml +++ b/config/controller.yaml @@ -45,7 +45,9 @@ spec: "-logtostderr", "-stderrthreshold", "INFO", "-el-image", "github.com/tektoncd/triggers/cmd/eventlistenersink", - "-el-port", "8080" + "-el-port", "8080", + "-period-seconds", "10", + "-failure-threshold", "1" ] volumeMounts: - name: config-logging diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go index 045b5fed4..a8cc5ae33 100644 --- a/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go +++ b/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go @@ -64,6 +64,12 @@ var ( // ElPort defines the port for the EventListener to listen on ElPort = flag.Int("el-port", 8080, "The container port for the EventListener to listen on.") + // PeriodSeconds defines Period Seconds for the EventListener Liveness Probe + PeriodSeconds = flag.Int("period-seconds", 10, + "The Period Seconds for the EventListener Liveness Probe.") + // FailureThreshold defines the Failure Threshold for the EventListener Liveness Probe + FailureThreshold = flag.Int("failure-threshold", 1, + "The Failure Threshold for the EventListener Liveness Probe.") // StaticResourceLabels is a map with all the labels that should be on // all resources generated by the EventListener StaticResourceLabels = map[string]string{ @@ -260,6 +266,17 @@ func (c *Reconciler) reconcileDeployment(el *v1alpha1.EventListener) error { ContainerPort: int32(*ElPort), Protocol: corev1.ProtocolTCP, }}, + LivenessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/live", + Scheme: corev1.URISchemeHTTP, + Port: intstr.FromInt((*ElPort)), + }, + }, + PeriodSeconds: int32(*PeriodSeconds), + FailureThreshold: int32(*FailureThreshold), + }, Args: []string{ "-el-name", el.Name, "-el-namespace", el.Namespace, diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go index 68ac4f315..efb20c64b 100644 --- a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go +++ b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go @@ -284,6 +284,17 @@ func Test_reconcileDeployment(t *testing.T) { Protocol: corev1.ProtocolTCP, }, }, + LivenessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/live", + Scheme: corev1.URISchemeHTTP, + Port: intstr.FromInt((*ElPort)), + }, + }, + PeriodSeconds: int32(*PeriodSeconds), + FailureThreshold: int32(*FailureThreshold), + }, Args: []string{ "-el-name", eventListenerName, "-el-namespace", namespace, @@ -541,6 +552,17 @@ func TestReconcile(t *testing.T) { ContainerPort: int32(*ElPort), Protocol: corev1.ProtocolTCP, }}, + LivenessProbe: &corev1.Probe{ + Handler: corev1.Handler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/live", + Scheme: corev1.URISchemeHTTP, + Port: intstr.FromInt((*ElPort)), + }, + }, + PeriodSeconds: int32(*PeriodSeconds), + FailureThreshold: int32(*FailureThreshold), + }, Args: []string{ "-el-name", eventListenerName, "-el-namespace", namespace,