Skip to content

Commit 29a4f68

Browse files
Add aliveHandler to simply verify controller livesness.
Add liveness check into deployment.yaml. Add explicit responseCode to stateHandler. Also fix some compiler warnings in alb-controller and correct log statement in ec2.
1 parent 39cde7f commit 29a4f68

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

alb-ingress-controller-helm/templates/controller-deployment.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ spec:
6565
scheme: HTTP
6666
initialDelaySeconds: 30
6767
periodSeconds: {{ .Values.controller.readinessProbeInterval }}
68+
timeout: {{ .Values.controller.readinessProbeTimeout }}
69+
livenessProbe:
70+
httpGet:
71+
path: /alive
72+
port: 8080
73+
scheme: HTTP
74+
initialDelaySeconds: 30
75+
periodSeconds: 60
6876
{{- if .Values.controller.resources }}
6977
resources:
7078
{{ toYaml .Values.controller.resources | indent 12 }}

alb-ingress-controller-helm/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ controller:
4343
# How often (in seconds) to check controller readiness
4444
readinessProbeInterval: 10
4545

46+
# How long to wait before timeout (in seconds) when checking controller readiness
47+
readinessProbeTimeout: 1
48+
4649
resources: {}
4750
# limits:
4851
# cpu: 100m

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func main() {
5656

5757
http.HandleFunc("/state", ac.StateHandler)
5858
http.HandleFunc("/healthz", ac.StatusHandler)
59+
http.HandleFunc("/alive", ac.AliveHandler)
5960

6061
defer func() {
6162
logger.Infof("Shutting down ingress controller...")

pkg/aws/albec2/ec2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (e *EC2) DescribeSGByPermissionGroup(sg *string) (*string, error) {
8484
}
8585

8686
if len(o.SecurityGroups) != 1 {
87-
return nil, fmt.Errorf("Found more than 1 matching (managed) instance SGs. Found %d", len(o.SecurityGroups))
87+
return nil, fmt.Errorf("Didn't find exactly 1 matching (managed) instance SG. Found %d", len(o.SecurityGroups))
8888
}
8989

9090
e.cache.Set(key, o.SecurityGroups[0].GroupId, time.Minute*5)

pkg/controller/alb-controller.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ type albController struct {
6363

6464
var logger *log.Logger
6565

66+
// Release contains a default value but it's also exported so that it can be overriden with buildFlags
6667
var Release = "1.0.0"
68+
69+
// Build contains a default value but it's also exported so that it can be overriden with buildFlags
6770
var Build = "git-00000000"
6871

6972
func init() {
@@ -315,6 +318,7 @@ func (ac *albController) StateHandler(w http.ResponseWriter, r *http.Request) {
315318
ac.mutex.RLock()
316319
defer ac.mutex.RUnlock()
317320
w.Header().Set("Content-Type", "application/json")
321+
w.WriteHeader(http.StatusOK)
318322
_ = json.NewEncoder(w).Encode(ac.ALBIngresses)
319323
}
320324

@@ -356,6 +360,21 @@ func (ac *albController) StatusHandler(w http.ResponseWriter, r *http.Request) {
356360
encoder.Encode(checkResults)
357361
}
358362

363+
// AliveHandler validates the bare-minimum internals and only returns a empty response.
364+
// It checks nothing downstream & should only used to ensure the controller is still running.
365+
func (ac *albController) AliveHandler(w http.ResponseWriter, r *http.Request) {
366+
// Take a lock here as a lightweight/minimum way to check the controller is alive.
367+
ac.mutex.RLock()
368+
defer ac.mutex.RUnlock()
369+
w.Header().Set("Content-Type", "application/json")
370+
// Explicitly set a healthy response so that this handler can be used to ascertain liveness.
371+
w.WriteHeader(http.StatusOK)
372+
373+
// Kubernetes only cares about the HTTP status code, so just return an empty body
374+
w.Write([]byte("{}\n"))
375+
return
376+
}
377+
359378
// UpdateIngressStatus returns the hostnames for the ALB.
360379
func (ac *albController) UpdateIngressStatus(ing *extensions.Ingress) []api.LoadBalancerIngress {
361380
id := albingress.GenerateID(ing.ObjectMeta.Namespace, ing.ObjectMeta.Name)

pkg/controller/alb-controller_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ func TestALBController_StateHandler(t *testing.T) {
313313
ingress := albingress.ALBIngress{}
314314
encodedIngressByteSlice, _ := json.Marshal(ingress)
315315
expectedBody := fmt.Sprintf("[%s]\n", encodedIngressByteSlice)
316+
expectedResponseCode := 200
316317
ac := albController{
317318
ALBIngresses: []*albingress.ALBIngress{&ingress},
318319
}
@@ -323,6 +324,9 @@ func TestALBController_StateHandler(t *testing.T) {
323324
if rw.Header().Get("Content-Type") != "application/json" {
324325
t.Errorf("Expected header Content-Type: application-json")
325326
}
327+
if rw.Result().StatusCode != expectedResponseCode {
328+
t.Errorf("Expected http status code to be %d, got %d", expectedResponseCode, rw.Result().StatusCode)
329+
}
326330
bodyString := fmt.Sprintf("%s", rw.Body.Bytes())
327331
if expectedBody != bodyString {
328332
t.Errorf("Expected response body to be '%s', found '%s'", expectedBody, bodyString)
@@ -366,3 +370,23 @@ func TestALBController_StatusHandler(t *testing.T) {
366370
}
367371
}
368372
}
373+
func TestALBController_AliveHandler(t *testing.T) {
374+
375+
expectedResponseCode := 200
376+
expectedResponseBody := "{}\n"
377+
ac := albController{}
378+
w := httptest.NewRecorder()
379+
ac.AliveHandler(w, nil)
380+
381+
if w.Header().Get("Content-Type") != "application/json" {
382+
t.Errorf("Expected header Content-Type: application-json")
383+
}
384+
if w.Result().StatusCode != expectedResponseCode {
385+
t.Errorf("Expected http status code to be %d, got %d", expectedResponseCode, w.Result().StatusCode)
386+
}
387+
388+
responseBody := fmt.Sprintf("%s", string(w.Body.Bytes()[:w.Body.Len()]))
389+
if responseBody != expectedResponseBody {
390+
t.Errorf("Expected response body to be '%s', found '%s'", expectedResponseBody, responseBody)
391+
}
392+
}

0 commit comments

Comments
 (0)