From e230f1925719cc452ace0ff8f7e4eb8fc8c83087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Thu, 6 Feb 2025 01:07:12 +0100 Subject: [PATCH] [release-v1.16] Add health check config and clean up duplicated code (#8308) (#1059) * Add health check config and clean up duplicated code (#8308) * Remove the duplicated health check * Add the missing health check config * Add the missing health check * Remove the duplicated health check * Fix non-blocking test * Fix receive adapter probes path * run `make generate-release` --------- Co-authored-by: Yukun Wang --- cmd/controller/main.go | 38 ------------------- .../deployments/controller.yaml | 19 ++++++++++ .../deployments/pingsource-mt-adapter.yaml | 19 ++++++++++ .../release/artifacts/eventing-core.yaml | 19 ++++++++++ .../release/artifacts/mt-channel-broker.yaml | 19 ++++++++++ pkg/adapter/apiserver/adapter.go | 13 ------- pkg/adapter/v2/context.go | 11 ++++++ pkg/adapter/v2/main.go | 8 ++++ pkg/adapter/v2/main_test.go | 1 + .../resources/receive_adapter.go | 13 ++++++- .../resources/receive_adapter_test.go | 13 ++++++- 11 files changed, 118 insertions(+), 55 deletions(-) diff --git a/cmd/controller/main.go b/cmd/controller/main.go index a5ce470eba0..2e77674acfd 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -20,12 +20,6 @@ import ( // Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters). // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" - "errors" - "log" - "net/http" - "os" - "time" - "knative.dev/pkg/injection/sharedmain" filteredFactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered" @@ -51,36 +45,8 @@ import ( ) func main() { - ctx := signals.NewContext() - port := os.Getenv("PROBES_PORT") - if port == "" { - port = "8080" - } - - // sets up liveness and readiness probes. - server := http.Server{ - ReadTimeout: 5 * time.Second, - Handler: http.HandlerFunc(handler), - Addr: ":" + port, - } - - go func() { - - go func() { - <-ctx.Done() - _ = server.Shutdown(ctx) - }() - - // start the web server on port and accept requests - log.Printf("Readiness and health check server listening on port %s", port) - - if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { - log.Fatal(err) - } - }() - ctx = filteredFactory.WithSelectors(ctx, auth.OIDCLabelSelector, eventingtls.TrustBundleLabelSelector, @@ -115,7 +81,3 @@ func main() { sugartrigger.NewController, ) } - -func handler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) -} diff --git a/config/brokers/mt-channel-broker/deployments/controller.yaml b/config/brokers/mt-channel-broker/deployments/controller.yaml index 0a4e4d3bc1c..06ca7df6d9b 100644 --- a/config/brokers/mt-channel-broker/deployments/controller.yaml +++ b/config/brokers/mt-channel-broker/deployments/controller.yaml @@ -81,8 +81,27 @@ spec: drop: - ALL + livenessProbe: + httpGet: + path: /health + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + readinessProbe: + httpGet: + path: /readiness + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + ports: - name: metrics containerPort: 9090 - name: profiling containerPort: 8008 + - name: probes + containerPort: 8080 diff --git a/config/core/deployments/pingsource-mt-adapter.yaml b/config/core/deployments/pingsource-mt-adapter.yaml index adcf734af3a..3ebab545c8e 100644 --- a/config/core/deployments/pingsource-mt-adapter.yaml +++ b/config/core/deployments/pingsource-mt-adapter.yaml @@ -87,6 +87,8 @@ spec: - containerPort: 9090 name: metrics protocol: TCP + - name: probes + containerPort: 8080 resources: requests: cpu: 125m @@ -102,4 +104,21 @@ spec: drop: - ALL + livenessProbe: + httpGet: + path: /health + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + readinessProbe: + httpGet: + path: /readiness + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + serviceAccountName: pingsource-mt-adapter diff --git a/openshift/release/artifacts/eventing-core.yaml b/openshift/release/artifacts/eventing-core.yaml index b5c2c06a15c..f66095a979f 100644 --- a/openshift/release/artifacts/eventing-core.yaml +++ b/openshift/release/artifacts/eventing-core.yaml @@ -6506,6 +6506,8 @@ spec: - containerPort: 9090 name: metrics protocol: TCP + - name: probes + containerPort: 8080 resources: requests: cpu: 125m @@ -6521,6 +6523,23 @@ spec: drop: - ALL + livenessProbe: + httpGet: + path: /health + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + readinessProbe: + httpGet: + path: /readiness + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + serviceAccountName: pingsource-mt-adapter --- # Copyright 2018 The Knative Authors diff --git a/openshift/release/artifacts/mt-channel-broker.yaml b/openshift/release/artifacts/mt-channel-broker.yaml index 3ae62ec3a04..9bf44f3284e 100644 --- a/openshift/release/artifacts/mt-channel-broker.yaml +++ b/openshift/release/artifacts/mt-channel-broker.yaml @@ -799,11 +799,30 @@ spec: drop: - ALL + livenessProbe: + httpGet: + path: /health + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + readinessProbe: + httpGet: + path: /readiness + port: probes + scheme: HTTP + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + ports: - name: metrics containerPort: 9090 - name: profiling containerPort: 8008 + - name: probes + containerPort: 8080 --- # Copyright 2020 The Knative Authors # diff --git a/pkg/adapter/apiserver/adapter.go b/pkg/adapter/apiserver/adapter.go index cda3e617e44..5a26d501cdf 100644 --- a/pkg/adapter/apiserver/adapter.go +++ b/pkg/adapter/apiserver/adapter.go @@ -19,7 +19,6 @@ package apiserver import ( "context" "fmt" - "net/http" "time" cloudevents "github.com/cloudevents/sdk-go/v2" @@ -126,20 +125,8 @@ func (a *apiServerAdapter) start(ctx context.Context, stopCh <-chan struct{}) er } } - srv := &http.Server{ - Addr: ":8080", - // Configure read header timeout to overcome potential Slowloris Attack because ReadHeaderTimeout is not - // configured in the http.Server. - ReadHeaderTimeout: 10 * time.Second, - Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - }), - } - go srv.ListenAndServe() - <-stopCh stop <- struct{}{} - srv.Shutdown(ctx) return nil } diff --git a/pkg/adapter/v2/context.go b/pkg/adapter/v2/context.go index f36f95daa7c..db03f4feab9 100644 --- a/pkg/adapter/v2/context.go +++ b/pkg/adapter/v2/context.go @@ -124,3 +124,14 @@ func ConfiguratorOptionsFromContext(ctx context.Context) []ConfiguratorOption { } return value.([]ConfiguratorOption) } + +type healthProbesDisabledKey struct{} + +// WithHealthProbesDisabled signals to MainWithContext that it should disable default probes (readiness and liveness). +func WithHealthProbesDisabled(ctx context.Context) context.Context { + return context.WithValue(ctx, healthProbesDisabledKey{}, struct{}{}) +} + +func HealthProbesDisabled(ctx context.Context) bool { + return ctx.Value(healthProbesDisabledKey{}) != nil +} diff --git a/pkg/adapter/v2/main.go b/pkg/adapter/v2/main.go index 1eb274364f4..af6598d5ce1 100644 --- a/pkg/adapter/v2/main.go +++ b/pkg/adapter/v2/main.go @@ -306,6 +306,14 @@ func MainWithInformers(ctx context.Context, component string, env EnvConfigAcces }() } + if !HealthProbesDisabled(ctx) { + wg.Add(1) + go func() { + defer wg.Done() + injection.ServeHealthProbes(ctx, injection.HealthCheckDefaultPort) + }() + } + // Finally start the adapter (blocking) if err := adapter.Start(ctx); err != nil { logger.Fatalw("Start returned an error", zap.Error(err)) diff --git a/pkg/adapter/v2/main_test.go b/pkg/adapter/v2/main_test.go index bf0d1d0e827..9d8f0bebfa0 100644 --- a/pkg/adapter/v2/main_test.go +++ b/pkg/adapter/v2/main_test.go @@ -67,6 +67,7 @@ func TestMainWithContext(t *testing.T) { }() ctx := context.TODO() + ctx = WithHealthProbesDisabled(ctx) ctx, _ = fakekubeclient.With(ctx) MainWithContext(ctx, "mycomponent", diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter.go b/pkg/reconciler/apiserversource/resources/receive_adapter.go index 670eacd672a..23d37305a0c 100644 --- a/pkg/reconciler/apiserversource/resources/receive_adapter.go +++ b/pkg/reconciler/apiserversource/resources/receive_adapter.go @@ -96,13 +96,22 @@ func MakeReceiveAdapter(args *ReceiveAdapterArgs) (*appsv1.Deployment, error) { Name: "metrics", ContainerPort: 9090, }, { - Name: "health", + Name: "probes", ContainerPort: 8080, }}, ReadinessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ - Port: intstr.FromString("health"), + Path: "readiness", + Port: intstr.FromString("probes"), + }, + }, + }, + LivenessProbe: &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "health", + Port: intstr.FromString("probes"), }, }, }, diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter_test.go b/pkg/reconciler/apiserversource/resources/receive_adapter_test.go index b16187ef8a2..1db66692a81 100644 --- a/pkg/reconciler/apiserversource/resources/receive_adapter_test.go +++ b/pkg/reconciler/apiserversource/resources/receive_adapter_test.go @@ -144,7 +144,7 @@ O2dgzikq8iSy1BlRsVw= Name: "metrics", ContainerPort: 9090, }, { - Name: "health", + Name: "probes", ContainerPort: 8080, }}, Env: []corev1.EnvVar{ @@ -187,7 +187,16 @@ O2dgzikq8iSy1BlRsVw= ReadinessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ - Port: intstr.FromString("health"), + Port: intstr.FromString("probes"), + Path: "readiness", + }, + }, + }, + LivenessProbe: &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Port: intstr.FromString("probes"), + Path: "health", }, }, },