Skip to content

Commit

Permalink
feat: add readiness and liveness probes (#52)
Browse files Browse the repository at this point in the history
* feat: add readiness and liveness probes

* fix: remove initalDelaySeconds

* docs: chart/readme updates
  • Loading branch information
alec-pinson authored Jan 11, 2022
1 parent a1023ae commit ef15ed3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion helm/ip-whitelister/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.4.0
version: 0.5.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
2 changes: 1 addition & 1 deletion helm/ip-whitelister/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ resource_configs:
4. Deploy to your Kubernetes cluster
```
helm upgrade ip-whitelister https://github.com/alec-pinson/ip-whitelister/releases/download/v1.0.8/helm-chart-ip-whitelister-0.4.0.tgz --install --wait -f values.yaml
helm upgrade ip-whitelister https://github.com/alec-pinson/ip-whitelister/releases/download/v1.1.0/helm-chart-ip-whitelister-0.5.0.tgz --install --wait -f values.yaml
```

8 changes: 4 additions & 4 deletions helm/ip-whitelister/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ spec:
mountPath: /app/config/resources
livenessProbe:
httpGet:
path: /
port: http
path: /live
port: 8090
readinessProbe:
httpGet:
path: /
port: http
path: /ready
port: 8090
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumes:
Expand Down
34 changes: 34 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (

type handle func(w http.ResponseWriter, req *http.Request) error

var httpLive bool = true
var httpReady bool = false

type Error struct {
Code int
Message string
Expand Down Expand Up @@ -150,8 +153,15 @@ func (a *Authentication) initAzure() {
Scopes: []string{"profile"},
}

http.Handle("/live", handle(livenessHandler))
http.Handle("/ready", handle(readinessHandler))
http.Handle("/callback", handle(callbackHandler))
http.Handle("/", handle(IndexHandler))
log.Fatal(http.ListenAndServe(":8090", nil))
}

func (a *Authentication) start() {
httpReady = true
log.Print("http.initAzure(): ip whitelister started")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Expand Down Expand Up @@ -221,3 +231,27 @@ func IndexHandler(w http.ResponseWriter, req *http.Request) error {

return indexTempl.Execute(w, &data)
}

func livenessHandler(w http.ResponseWriter, req *http.Request) error {
var err error
if httpLive {
w.WriteHeader(200)
_, err = w.Write([]byte("ok"))
} else {
w.WriteHeader(500)
_, err = w.Write([]byte("not ok"))
}
return err
}

func readinessHandler(w http.ResponseWriter, req *http.Request) error {
var err error
if httpReady {
w.WriteHeader(200)
_, err = w.Write([]byte("ok"))
} else {
w.WriteHeader(500)
_, err = w.Write([]byte("not ok"))
}
return err
}
7 changes: 5 additions & 2 deletions whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ func (*Whitelist) init() {
// enable ttl check on whitelisted ips
go w.ttl()

// initialize authentication
go h.init(c.Auth)

// update resources on startup
w.updateResources()

// initialize http/authentication
h.init(c.Auth)
// initialize http
h.start()
}

func (w *Whitelist) add(u *User) bool {
Expand Down

0 comments on commit ef15ed3

Please sign in to comment.