Skip to content

Commit

Permalink
Add config map for custom HC ports
Browse files Browse the repository at this point in the history
  • Loading branch information
dtvalk-ov committed Jul 22, 2022
1 parent b2908fa commit ad578a0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 21 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ RUN VERSION="version=$(git describe --tag --always 2> /dev/null)" \
&& LDFLAGS="-s -w -X '"${BUILDINFO_PACKAGE}$VERSION"' -X '"${BUILDINFO_PACKAGE}$DATETIME"' -X '"${BUILDINFO_PACKAGE}$REPOSITORY"' -X '"${BUILDINFO_PACKAGE}$REVISION"' -X '"${BUILDINFO_PACKAGE}$BUILDER"'" \
&& CGO_ENABLED=0 go build -mod=readonly -a -o /artifacts/${PROJECT} -ldflags="${LDFLAGS}" \
&& cp -R resources /artifacts/resources \
&& cp -R html-templates /artifacts/html-templates

&& cp -R html-templates /artifacts/html-templates \
&& cp -R custom-healthcheck-ports /artifacts/custom-healthcheck-ports

# Multi-stage build - copy only the certs and the binary into the image
FROM scratch
Expand All @@ -27,5 +27,6 @@ COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=0 /artifacts/* /
COPY --from=0 /artifacts/resources /resources
COPY --from=0 /artifacts/html-templates /html-templates
COPY --from=0 /artifacts/custom-healthcheck-ports /custom-healthcheck-ports

CMD [ "/upp-aggregate-healthcheck" ]
4 changes: 2 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type enrichedCheckResult struct {
SystemCode string
}

func initializeController(environment string) *healthCheckController {
service := initializeHealthCheckService()
func initializeController(environment string, hcPorts map[string]int32) *healthCheckController {
service := initializeHealthCheckService(hcPorts)
measuredServices := make(map[string]measuredService)
stickyCategoriesFailedServices := make(map[string]int)

Expand Down
5 changes: 5 additions & 0 deletions custom-healthcheck-ports/delivery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"notifications-push": 8599,
"list-notifications-push": 8599,
"page-notifications-push": 8599
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ service:

env:
PATH_PREFIX: "/__health"
CUSTOM_HEALTHCHECK_PORTS_PATH: "./custom-healthcheck-ports/delivery.json"

categories:
- kind: ConfigMap
Expand Down
2 changes: 2 additions & 0 deletions helm/upp-aggregate-healthcheck/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ spec:
key: cluster.url
- name: PATH_PREFIX
value: {{ .Values.env.PATH_PREFIX }}
- name: CUSTOM_HEALTHCHECK_PORTS_PATH
value: {{ .Values.env.CUSTOM_HEALTHCHECK_PORTS_PATH }}
ports:
- containerPort: 8080
livenessProbe:
Expand Down
43 changes: 40 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"encoding/json"
"fmt"
log "github.com/Financial-Times/go-logger"
"io/ioutil"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -43,6 +45,13 @@ func main() {
EnvVar: "PATH_PREFIX",
})

customHealthcheckPortsPath := app.String(cli.StringOpt{
Name: "customHealthcheckPorts",
Value: "",
Desc: "Path for custom healthcheck ports file",
EnvVar: "CUSTOM_HEALTHCHECK_PORTS_PATH",
})

clusterURL := app.String(cli.StringOpt{
Name: "cluster-url",
Value: "",
Expand All @@ -60,9 +69,19 @@ func main() {
log.InitLogger(*appName, *logLevel)

app.Action = func() {
log.Infof("Starting app with params: [environment: %s], [pathPrefix: %s]", *environment, *pathPrefix)

controller := initializeController(*environment)
log.Infof("Starting app with params: [environment: %s], [pathPrefix: %s], [customHealthcheckPorts: %s]", *environment, *pathPrefix, *customHealthcheckPortsPath)

customPorts := make(map[string]int32)
if *customHealthcheckPortsPath != "" {
var err error
customPorts, err = loadCustomHealthcheckPortsMap(*customHealthcheckPortsPath)
if err != nil {
log.WithError(err).Error("Failed to load custom healthcheck ports map")
} else {
log.WithField("custom-healthcheck-ports", customPorts).Info("Loaded custom healthcheck ports from configuration")
}
}
controller := initializeController(*environment, customPorts)
handler := &httpHandler{
controller: controller,
pathPrefix: *pathPrefix,
Expand Down Expand Up @@ -110,3 +129,21 @@ func listen(httpHandler *httpHandler, pathPrefix string, port int) {
panic(fmt.Sprintf("Cannot set up HTTP listener. Error was: %v", err))
}
}

func loadCustomHealthcheckPortsMap(filePath string) (map[string]int32, error) {
jsonFile, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}
var result map[string]int32
err = json.Unmarshal(byteValue, &result)
if err != nil {
return nil, err
}
return result, nil
}
29 changes: 15 additions & 14 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
)

type k8sHealthcheckService struct {
k8sClient kubernetes.Interface
httpClient *http.Client
services servicesMap
acks map[string]string
k8sClient kubernetes.Interface
httpClient *http.Client
services servicesMap
acks map[string]string
customHCPorts map[string]int32
}

type healthcheckService interface {
Expand Down Expand Up @@ -163,7 +164,7 @@ func getDefaultClient() *http.Client {
}
}

func initializeHealthCheckService() *k8sHealthcheckService {
func initializeHealthCheckService(hcPorts map[string]int32) *k8sHealthcheckService {
httpClient := getDefaultClient()

// creates the in-cluster config
Expand All @@ -181,9 +182,10 @@ func initializeHealthCheckService() *k8sHealthcheckService {
services := make(map[string]service)

k8sService := &k8sHealthcheckService{
httpClient: httpClient,
k8sClient: k8sClient,
services: servicesMap{m: services},
httpClient: httpClient,
k8sClient: k8sClient,
services: servicesMap{m: services},
customHCPorts: hcPorts,
}

go k8sService.watchAcks()
Expand Down Expand Up @@ -441,11 +443,15 @@ func (hs *k8sHealthcheckService) populateService(k8sService *k8score.Service, ac
}
}
appPort, hcPort := getPortsForService(k8sService)
if customPort, exists := hs.customHCPorts[k8sService.Name]; exists {
hcPort = customPort
log.WithField("healthcheck-port", customPort).Infof("Found custom healthcheck port for service %s", k8sService.Name)
}
serviceCode, err := hs.getSystemCodeForService(serviceName, hcPort)
if err != nil {
log.WithError(err).Warnf("Failed to fetch system code for service '%s'", serviceName)
}
log.Debugf("Fetched system code [%s] for service %s", serviceCode, serviceName)
log.Infof("Fetched system code [%s] for service %s", serviceCode, serviceName)

return service{
name: serviceName,
Expand Down Expand Up @@ -486,18 +492,13 @@ func (hs *k8sHealthcheckService) getSystemCodeForService(serviceName string, ser
func getPortsForService(k8sService *k8score.Service) (int32, int32) {
healthcheckPort := defaultAppPort
appPort := defaultAppPort
healthPortSet := false
servicePorts := k8sService.Spec.Ports
for _, port := range servicePorts {
if port.Name == "app" {
appPort = port.TargetPort.IntVal
}
if port.Name == "healthcheck" {
healthcheckPort = port.Port
healthPortSet = true
}
if !healthPortSet {
healthcheckPort = port.Port
}
}
return appPort, healthcheckPort
Expand Down

0 comments on commit ad578a0

Please sign in to comment.