Skip to content

Commit 83f9352

Browse files
committed
Add basic probe checks
1 parent 6983e3f commit 83f9352

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

deployment/nkl-deployment.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,20 @@ spec:
2121
value: "http://192.168.1.109:9000/api"
2222
image: ciroque/nginx-k8s-edge-controller:latest
2323
imagePullPolicy: Always
24+
ports:
25+
- name: http
26+
containerPort: 51031
27+
protocol: TCP
28+
livenessProbe:
29+
httpGet:
30+
path: /livez
31+
port: http
32+
initialDelaySeconds: 5
33+
periodSeconds: 2
34+
readinessProbe:
35+
httpGet:
36+
path: /readyz
37+
port: http
38+
initialDelaySeconds: 5
39+
periodSeconds: 2
2440
serviceAccountName: nginx-k8s-edge-controller

internal/probation/check.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023 f5 Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache
3+
// license that can be found in the LICENSE file.
4+
5+
package probation
6+
7+
type Check interface {
8+
Check() bool
9+
}
10+
11+
type LiveCheck struct {
12+
}
13+
14+
type ReadyCheck struct {
15+
}
16+
17+
type StartupCheck struct {
18+
}
19+
20+
func (l *LiveCheck) Check() bool {
21+
return true
22+
}
23+
24+
func (r *ReadyCheck) Check() bool {
25+
return true
26+
}
27+
28+
func (s *StartupCheck) Check() bool {
29+
return true
30+
}

internal/probation/doc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2023 f5 Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache
3+
// license that can be found in the LICENSE file.
4+
5+
/*
6+
Package probation includes support for Kubernetes health and wellness checks.
7+
*/
8+
9+
package probation

internal/probation/server.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2023 f5 Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache
3+
// license that can be found in the LICENSE file.
4+
5+
package probation
6+
7+
import (
8+
"fmt"
9+
"github.com/sirupsen/logrus"
10+
"net/http"
11+
)
12+
13+
const (
14+
Ok = "OK"
15+
ServiceNotAvailable = "Service Not Available"
16+
)
17+
18+
type HealthServer struct {
19+
httpServer *http.Server
20+
LiveCheck LiveCheck
21+
ReadyCheck ReadyCheck
22+
StartupCheck StartupCheck
23+
}
24+
25+
func (hs *HealthServer) Start() {
26+
mux := http.NewServeMux()
27+
mux.HandleFunc("/livez", hs.HandleLive)
28+
mux.HandleFunc("/readyz", hs.HandleReady)
29+
mux.HandleFunc("/startupz", hs.HandleStartup)
30+
hs.httpServer = &http.Server{Addr: ":51031", Handler: mux}
31+
32+
go func() {
33+
if err := hs.httpServer.ListenAndServe(); err != nil {
34+
logrus.Errorf("unable to start probe listener on %s: %v", hs.httpServer.Addr, err)
35+
}
36+
}()
37+
38+
logrus.Info("Started probe listener on", hs.httpServer.Addr)
39+
}
40+
41+
func (hs *HealthServer) Stop() {
42+
if err := hs.httpServer.Close(); err != nil {
43+
logrus.Errorf("unable to stop probe listener on %s: %v", hs.httpServer.Addr, err)
44+
}
45+
}
46+
47+
func (hs *HealthServer) HandleLive(writer http.ResponseWriter, request *http.Request) {
48+
handleProbe(writer, request, &hs.LiveCheck)
49+
}
50+
51+
func (hs *HealthServer) HandleReady(writer http.ResponseWriter, request *http.Request) {
52+
handleProbe(writer, request, &hs.ReadyCheck)
53+
}
54+
55+
func (hs *HealthServer) HandleStartup(writer http.ResponseWriter, request *http.Request) {
56+
handleProbe(writer, request, &hs.StartupCheck)
57+
}
58+
59+
func handleProbe(writer http.ResponseWriter, _ *http.Request, check Check) {
60+
if check.Check() {
61+
writer.WriteHeader(http.StatusOK)
62+
63+
if _, err := fmt.Fprint(writer, Ok); err != nil {
64+
logrus.Error(err)
65+
}
66+
67+
} else {
68+
writer.WriteHeader(http.StatusServiceUnavailable)
69+
70+
if _, err := fmt.Fprint(writer, ServiceNotAvailable); err != nil {
71+
logrus.Error(err)
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)