Skip to content

Commit 7c1a62d

Browse files
authored
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
1 parent bff7b03 commit 7c1a62d

File tree

9 files changed

+80
-55
lines changed

9 files changed

+80
-55
lines changed

cmd/controller/main.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ import (
2020
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
2121
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2222

23-
"errors"
24-
"log"
25-
"net/http"
26-
"os"
27-
"time"
28-
2923
"knative.dev/pkg/injection/sharedmain"
3024

3125
filteredFactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered"
@@ -53,36 +47,8 @@ import (
5347
)
5448

5549
func main() {
56-
5750
ctx := signals.NewContext()
5851

59-
port := os.Getenv("PROBES_PORT")
60-
if port == "" {
61-
port = "8080"
62-
}
63-
64-
// sets up liveness and readiness probes.
65-
server := http.Server{
66-
ReadTimeout: 5 * time.Second,
67-
Handler: http.HandlerFunc(handler),
68-
Addr: ":" + port,
69-
}
70-
71-
go func() {
72-
73-
go func() {
74-
<-ctx.Done()
75-
_ = server.Shutdown(ctx)
76-
}()
77-
78-
// start the web server on port and accept requests
79-
log.Printf("Readiness and health check server listening on port %s", port)
80-
81-
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
82-
log.Fatal(err)
83-
}
84-
}()
85-
8652
ctx = filteredFactory.WithSelectors(ctx,
8753
auth.OIDCLabelSelector,
8854
eventingtls.TrustBundleLabelSelector,
@@ -120,7 +86,3 @@ func main() {
12086
sugartrigger.NewController,
12187
)
12288
}
123-
124-
func handler(w http.ResponseWriter, r *http.Request) {
125-
w.WriteHeader(http.StatusOK)
126-
}

config/brokers/mt-channel-broker/deployments/controller.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,27 @@ spec:
8383
seccompProfile:
8484
type: RuntimeDefault
8585

86+
livenessProbe:
87+
httpGet:
88+
path: /health
89+
port: probes
90+
scheme: HTTP
91+
initialDelaySeconds: 20
92+
periodSeconds: 10
93+
timeoutSeconds: 5
94+
readinessProbe:
95+
httpGet:
96+
path: /readiness
97+
port: probes
98+
scheme: HTTP
99+
initialDelaySeconds: 20
100+
periodSeconds: 10
101+
timeoutSeconds: 5
102+
86103
ports:
87104
- name: metrics
88105
containerPort: 9090
89106
- name: profiling
90107
containerPort: 8008
108+
- name: probes
109+
containerPort: 8080

config/core/deployments/pingsource-mt-adapter.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ spec:
8787
- containerPort: 9090
8888
name: metrics
8989
protocol: TCP
90+
- name: probes
91+
containerPort: 8080
9092
resources:
9193
requests:
9294
cpu: 125m
@@ -104,4 +106,21 @@ spec:
104106
seccompProfile:
105107
type: RuntimeDefault
106108

109+
livenessProbe:
110+
httpGet:
111+
path: /health
112+
port: probes
113+
scheme: HTTP
114+
initialDelaySeconds: 20
115+
periodSeconds: 10
116+
timeoutSeconds: 5
117+
readinessProbe:
118+
httpGet:
119+
path: /readiness
120+
port: probes
121+
scheme: HTTP
122+
initialDelaySeconds: 20
123+
periodSeconds: 10
124+
timeoutSeconds: 5
125+
107126
serviceAccountName: pingsource-mt-adapter

pkg/adapter/apiserver/adapter.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package apiserver
1919
import (
2020
"context"
2121
"fmt"
22-
"net/http"
2322
"time"
2423

2524
cloudevents "github.com/cloudevents/sdk-go/v2"
@@ -126,20 +125,8 @@ func (a *apiServerAdapter) start(ctx context.Context, stopCh <-chan struct{}) er
126125
}
127126
}
128127

129-
srv := &http.Server{
130-
Addr: ":8080",
131-
// Configure read header timeout to overcome potential Slowloris Attack because ReadHeaderTimeout is not
132-
// configured in the http.Server.
133-
ReadHeaderTimeout: 10 * time.Second,
134-
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
135-
w.WriteHeader(http.StatusOK)
136-
}),
137-
}
138-
go srv.ListenAndServe()
139-
140128
<-stopCh
141129
stop <- struct{}{}
142-
srv.Shutdown(ctx)
143130
return nil
144131
}
145132

pkg/adapter/v2/context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,14 @@ func ConfiguratorOptionsFromContext(ctx context.Context) []ConfiguratorOption {
124124
}
125125
return value.([]ConfiguratorOption)
126126
}
127+
128+
type healthProbesDisabledKey struct{}
129+
130+
// WithHealthProbesDisabled signals to MainWithContext that it should disable default probes (readiness and liveness).
131+
func WithHealthProbesDisabled(ctx context.Context) context.Context {
132+
return context.WithValue(ctx, healthProbesDisabledKey{}, struct{}{})
133+
}
134+
135+
func HealthProbesDisabled(ctx context.Context) bool {
136+
return ctx.Value(healthProbesDisabledKey{}) != nil
137+
}

pkg/adapter/v2/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ func MainWithInformers(ctx context.Context, component string, env EnvConfigAcces
306306
}()
307307
}
308308

309+
if !HealthProbesDisabled(ctx) {
310+
wg.Add(1)
311+
go func() {
312+
defer wg.Done()
313+
injection.ServeHealthProbes(ctx, injection.HealthCheckDefaultPort)
314+
}()
315+
}
316+
309317
// Finally start the adapter (blocking)
310318
if err := adapter.Start(ctx); err != nil {
311319
logger.Fatalw("Start returned an error", zap.Error(err))

pkg/adapter/v2/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestMainWithContext(t *testing.T) {
6767
}()
6868

6969
ctx := context.TODO()
70+
ctx = WithHealthProbesDisabled(ctx)
7071
ctx, _ = fakekubeclient.With(ctx)
7172

7273
MainWithContext(ctx, "mycomponent",

pkg/reconciler/apiserversource/resources/receive_adapter.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,22 @@ func MakeReceiveAdapter(args *ReceiveAdapterArgs) (*appsv1.Deployment, error) {
9696
Name: "metrics",
9797
ContainerPort: 9090,
9898
}, {
99-
Name: "health",
99+
Name: "probes",
100100
ContainerPort: 8080,
101101
}},
102102
ReadinessProbe: &corev1.Probe{
103103
ProbeHandler: corev1.ProbeHandler{
104104
HTTPGet: &corev1.HTTPGetAction{
105-
Port: intstr.FromString("health"),
105+
Path: "readiness",
106+
Port: intstr.FromString("probes"),
107+
},
108+
},
109+
},
110+
LivenessProbe: &corev1.Probe{
111+
ProbeHandler: corev1.ProbeHandler{
112+
HTTPGet: &corev1.HTTPGetAction{
113+
Path: "health",
114+
Port: intstr.FromString("probes"),
106115
},
107116
},
108117
},

pkg/reconciler/apiserversource/resources/receive_adapter_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ O2dgzikq8iSy1BlRsVw=
144144
Name: "metrics",
145145
ContainerPort: 9090,
146146
}, {
147-
Name: "health",
147+
Name: "probes",
148148
ContainerPort: 8080,
149149
}},
150150
Env: []corev1.EnvVar{
@@ -187,7 +187,16 @@ O2dgzikq8iSy1BlRsVw=
187187
ReadinessProbe: &corev1.Probe{
188188
ProbeHandler: corev1.ProbeHandler{
189189
HTTPGet: &corev1.HTTPGetAction{
190-
Port: intstr.FromString("health"),
190+
Port: intstr.FromString("probes"),
191+
Path: "readiness",
192+
},
193+
},
194+
},
195+
LivenessProbe: &corev1.Probe{
196+
ProbeHandler: corev1.ProbeHandler{
197+
HTTPGet: &corev1.HTTPGetAction{
198+
Port: intstr.FromString("probes"),
199+
Path: "health",
191200
},
192201
},
193202
},

0 commit comments

Comments
 (0)