Skip to content

Commit

Permalink
Merge pull request #406 from tyrannasaurusbanks/master
Browse files Browse the repository at this point in the history
Add liveness check against /state
  • Loading branch information
bigkraig authored Jul 9, 2018
2 parents 96fdc19 + 29a4f68 commit f924913
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ spec:
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: {{ .Values.controller.readinessProbeInterval }}
timeout: {{ .Values.controller.readinessProbeTimeout }}
livenessProbe:
httpGet:
path: /alive
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 60
{{- if .Values.controller.resources }}
resources:
{{ toYaml .Values.controller.resources | indent 12 }}
Expand Down
3 changes: 3 additions & 0 deletions alb-ingress-controller-helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ controller:
# How often (in seconds) to check controller readiness
readinessProbeInterval: 10

# How long to wait before timeout (in seconds) when checking controller readiness
readinessProbeTimeout: 1

resources: {}
# limits:
# cpu: 100m
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func main() {

http.HandleFunc("/state", ac.StateHandler)
http.HandleFunc("/healthz", ac.StatusHandler)
http.HandleFunc("/alive", ac.AliveHandler)

defer func() {
logger.Infof("Shutting down ingress controller...")
Expand Down
2 changes: 1 addition & 1 deletion pkg/aws/albec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (e *EC2) DescribeSGByPermissionGroup(sg *string) (*string, error) {
}

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

e.cache.Set(key, o.SecurityGroups[0].GroupId, time.Minute*5)
Expand Down
19 changes: 19 additions & 0 deletions pkg/controller/alb-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ type albController struct {

var logger *log.Logger

// Release contains a default value but it's also exported so that it can be overriden with buildFlags
var Release = "1.0.0"

// Build contains a default value but it's also exported so that it can be overriden with buildFlags
var Build = "git-00000000"

func init() {
Expand Down Expand Up @@ -315,6 +318,7 @@ func (ac *albController) StateHandler(w http.ResponseWriter, r *http.Request) {
ac.mutex.RLock()
defer ac.mutex.RUnlock()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(ac.ALBIngresses)
}

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

// AliveHandler validates the bare-minimum internals and only returns a empty response.
// It checks nothing downstream & should only used to ensure the controller is still running.
func (ac *albController) AliveHandler(w http.ResponseWriter, r *http.Request) {
// Take a lock here as a lightweight/minimum way to check the controller is alive.
ac.mutex.RLock()
defer ac.mutex.RUnlock()
w.Header().Set("Content-Type", "application/json")
// Explicitly set a healthy response so that this handler can be used to ascertain liveness.
w.WriteHeader(http.StatusOK)

// Kubernetes only cares about the HTTP status code, so just return an empty body
w.Write([]byte("{}\n"))
return
}

// UpdateIngressStatus returns the hostnames for the ALB.
func (ac *albController) UpdateIngressStatus(ing *extensions.Ingress) []api.LoadBalancerIngress {
id := albingress.GenerateID(ing.ObjectMeta.Namespace, ing.ObjectMeta.Name)
Expand Down
24 changes: 24 additions & 0 deletions pkg/controller/alb-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func TestALBController_StateHandler(t *testing.T) {
ingress := albingress.ALBIngress{}
encodedIngressByteSlice, _ := json.Marshal(ingress)
expectedBody := fmt.Sprintf("[%s]\n", encodedIngressByteSlice)
expectedResponseCode := 200
ac := albController{
ALBIngresses: []*albingress.ALBIngress{&ingress},
}
Expand All @@ -323,6 +324,9 @@ func TestALBController_StateHandler(t *testing.T) {
if rw.Header().Get("Content-Type") != "application/json" {
t.Errorf("Expected header Content-Type: application-json")
}
if rw.Result().StatusCode != expectedResponseCode {
t.Errorf("Expected http status code to be %d, got %d", expectedResponseCode, rw.Result().StatusCode)
}
bodyString := fmt.Sprintf("%s", rw.Body.Bytes())
if expectedBody != bodyString {
t.Errorf("Expected response body to be '%s', found '%s'", expectedBody, bodyString)
Expand Down Expand Up @@ -366,3 +370,23 @@ func TestALBController_StatusHandler(t *testing.T) {
}
}
}
func TestALBController_AliveHandler(t *testing.T) {

expectedResponseCode := 200
expectedResponseBody := "{}\n"
ac := albController{}
w := httptest.NewRecorder()
ac.AliveHandler(w, nil)

if w.Header().Get("Content-Type") != "application/json" {
t.Errorf("Expected header Content-Type: application-json")
}
if w.Result().StatusCode != expectedResponseCode {
t.Errorf("Expected http status code to be %d, got %d", expectedResponseCode, w.Result().StatusCode)
}

responseBody := fmt.Sprintf("%s", string(w.Body.Bytes()[:w.Body.Len()]))
if responseBody != expectedResponseBody {
t.Errorf("Expected response body to be '%s', found '%s'", expectedResponseBody, responseBody)
}
}

0 comments on commit f924913

Please sign in to comment.