From ad20fc32ed2cbe1319104787a9430b16b9ca6bcc Mon Sep 17 00:00:00 2001 From: airycanon Date: Wed, 6 Nov 2024 10:11:39 +0800 Subject: [PATCH 1/6] Remove the duplicated health check --- cmd/controller/main.go | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 2143775e207..cbbc8bc5c8d 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" @@ -53,36 +47,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, @@ -120,7 +86,3 @@ func main() { sugartrigger.NewController, ) } - -func handler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) -} From 3f759485741891e84a7c53562ea726bd86f4d584 Mon Sep 17 00:00:00 2001 From: airycanon Date: Wed, 6 Nov 2024 10:10:17 +0800 Subject: [PATCH 2/6] Add the missing health check config --- .../deployments/controller.yaml | 19 +++++++++++++++++++ .../deployments/pingsource-mt-adapter.yaml | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/config/brokers/mt-channel-broker/deployments/controller.yaml b/config/brokers/mt-channel-broker/deployments/controller.yaml index 10d35e44185..2235e02dce7 100644 --- a/config/brokers/mt-channel-broker/deployments/controller.yaml +++ b/config/brokers/mt-channel-broker/deployments/controller.yaml @@ -83,8 +83,27 @@ spec: seccompProfile: type: RuntimeDefault + 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 33469bdf2d8..0f62925c305 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 @@ -104,4 +106,21 @@ spec: seccompProfile: type: RuntimeDefault + 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 From b8e58a193584235fca3a8de2baf80ccea7e853c1 Mon Sep 17 00:00:00 2001 From: airycanon Date: Wed, 6 Nov 2024 10:10:17 +0800 Subject: [PATCH 3/6] Add the missing health check --- pkg/adapter/v2/context.go | 11 +++++++++++ pkg/adapter/v2/main.go | 8 ++++++++ 2 files changed, 19 insertions(+) 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)) From e5b94463d24a44067f0ba592799af2074cb8c0e8 Mon Sep 17 00:00:00 2001 From: airycanon Date: Thu, 5 Dec 2024 11:33:29 +0800 Subject: [PATCH 4/6] Remove the duplicated health check --- pkg/adapter/apiserver/adapter.go | 13 ------------- 1 file changed, 13 deletions(-) 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 } From 6ef1eccbc52f536e387f7a754b4ffbb75d8c4ecb Mon Sep 17 00:00:00 2001 From: airycanon Date: Tue, 10 Dec 2024 19:09:30 +0800 Subject: [PATCH 5/6] Fix non-blocking test --- pkg/adapter/v2/main_test.go | 1 + 1 file changed, 1 insertion(+) 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", From 76bc87fe931092789a8d311693e1df3769b46780 Mon Sep 17 00:00:00 2001 From: airycanon Date: Tue, 10 Dec 2024 20:11:02 +0800 Subject: [PATCH 6/6] Fix receive adapter probes path --- .../apiserversource/resources/receive_adapter.go | 13 +++++++++++-- .../resources/receive_adapter_test.go | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter.go b/pkg/reconciler/apiserversource/resources/receive_adapter.go index 4d529c5a337..4bb823647b2 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 48abe550c0e..9b25dbfb669 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", }, }, },