Skip to content

Commit

Permalink
Test available app ports for services
Browse files Browse the repository at this point in the history
  • Loading branch information
dtvalk-ov committed May 26, 2022
1 parent 09c66cf commit d5cbee9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
27 changes: 19 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
module github.com/Financial-Times/upp-aggregate-healthcheck

go 1.15
go 1.17

require (
github.com/Financial-Times/go-fthealth v0.0.0-20171204124831-1b007e2b37b7
github.com/Financial-Times/go-logger v0.0.0-20180323124113-febee6537e90
github.com/gorilla/mux v1.6.2
github.com/jawher/mow.cli v1.0.4
github.com/prometheus/client_golang v0.9.0-pre1.0.20180907102542-7858729281ec
github.com/stretchr/testify v1.2.2
k8s.io/api v0.0.0-20180628040859-072894a440bd
k8s.io/apimachinery v0.0.0-20180621070125-103fd098999d
k8s.io/client-go v8.0.0+incompatible
)

require (
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/jawher/mow.cli v1.0.4
github.com/json-iterator/go v1.1.5 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/onsi/ginkgo v1.11.0 // indirect
github.com/onsi/gomega v1.8.1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/prometheus/client_golang v0.9.0-pre1.0.20180907102542-7858729281ec
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 // indirect
github.com/sirupsen/logrus v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // indirect
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect
golang.org/x/text v0.3.0 // indirect
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/api v0.0.0-20180628040859-072894a440bd
k8s.io/apimachinery v0.0.0-20180621070125-103fd098999d
k8s.io/client-go v8.0.0+incompatible
gopkg.in/yaml.v2 v2.2.4 // indirect
k8s.io/kube-openapi v0.0.0-20180620173706-91cfa479c814 // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLM
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down
36 changes: 8 additions & 28 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package main
import (
"encoding/json"
"fmt"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
log "github.com/Financial-Times/go-logger"
"html/template"
"net/http"
"net/url"
"strings"
"time"

fthealth "github.com/Financial-Times/go-fthealth/v1_1"
log "github.com/Financial-Times/go-logger"
)

type httpHandler struct {
Expand Down Expand Up @@ -323,18 +321,9 @@ func useCache(theURL *url.URL) bool {

func buildHealthcheckJSONResponse(w http.ResponseWriter, healthResult enrichedHealthResult) {
type CheckResultWithHeimdalAck struct {
ID string `json:"id"`
Name string `json:"name"`
SystemCode string `json:"systemCode"`
Ok bool `json:"ok"`
Severity uint8 `json:"severity"`
BusinessImpact string `json:"businessImpact"`
TechnicalSummary string `json:"technicalSummary"`
PanicGuide string `json:"panicGuide"`
CheckOutput string `json:"checkOutput"`
LastUpdated time.Time `json:"lastUpdated"`
Ack string `json:"ack,omitempty"`
HeimdalAck string `json:"_acknowledged,omitempty"`
fthealth.CheckResult
SystemCode string `json:"systemCode"`
HeimdalAck string `json:"_acknowledged,omitempty"`
}

type HealthResult struct {
Expand All @@ -351,18 +340,9 @@ func buildHealthcheckJSONResponse(w http.ResponseWriter, healthResult enrichedHe
for _, check := range healthResult.Checks {
originalCheck := check.CheckResult
newCheck := CheckResultWithHeimdalAck{
SystemCode: check.SystemCode,
ID: originalCheck.ID,
Name: originalCheck.Name,
Ok: originalCheck.Ok,
Severity: originalCheck.Severity,
BusinessImpact: originalCheck.BusinessImpact,
TechnicalSummary: originalCheck.TechnicalSummary,
PanicGuide: originalCheck.PanicGuide,
CheckOutput: originalCheck.CheckOutput,
LastUpdated: originalCheck.LastUpdated,
Ack: originalCheck.Ack,
HeimdalAck: originalCheck.Ack,
CheckResult: originalCheck,
SystemCode: check.SystemCode,
HeimdalAck: originalCheck.Ack,
}
newChecks = append(newChecks, newCheck)
}
Expand Down
25 changes: 14 additions & 11 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,11 @@ func (hs *k8sHealthcheckService) populateSystemCodeForService(serviceName string
defer func(g chan struct{}) {
<-g
}(guard)
log.Infof("Fetching system code for service %s", serviceName)

req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d/__health", serviceName, servicePort), nil)
reqURL := fmt.Sprintf("http://%s:%d/__health", serviceName, servicePort)
log.Infof("Fetching system code for service %s by calling %s", serviceName, reqURL)

req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
log.WithError(err).Errorf("Failed to prepare system code request for service %s", serviceName)
return
Expand All @@ -472,16 +474,16 @@ func (hs *k8sHealthcheckService) populateSystemCodeForService(serviceName string
log.WithError(err).Errorf("Failed to execute system code request for service %s", serviceName)
return
}
if resp.StatusCode != http.StatusOK {
log.WithError(err).Errorf("System code request returned non-200 code [%d] for service %s", resp.StatusCode, serviceName)
return
}
defer func() {
err := resp.Body.Close()
if err != nil {
log.WithError(err).Errorf("Failed to close close response body reader for service %s.", serviceName)
log.WithError(err).Errorf("Failed to close the response body reader for service %s.", serviceName)
}
}()
if resp.StatusCode != http.StatusOK {
log.WithError(err).Errorf("System code request returned non-200 code [%d] for service %s", resp.StatusCode, serviceName)
return
}
response := &healthcheckEndpointResponse{}
err = json.NewDecoder(resp.Body).Decode(response)
if err != nil {
Expand All @@ -499,14 +501,15 @@ func (hs *k8sHealthcheckService) populateSystemCodeForService(serviceName string
}

func getAppPortForService(k8sService *k8score.Service) int32 {
healthcheckPort := defaultAppPort
servicePorts := k8sService.Spec.Ports
for _, port := range servicePorts {
if port.Name == "app" {
return port.TargetPort.IntVal
if port.Name == "healthcheck" {
return port.Port
}
healthcheckPort = port.Port
}

return defaultAppPort
return healthcheckPort
}

func getAcksConfigMap(k8sClient kubernetes.Interface) (k8score.ConfigMap, error) {
Expand Down

0 comments on commit d5cbee9

Please sign in to comment.