Skip to content

Commit

Permalink
refactor: update the mux server to expose both healthz and metrics, a…
Browse files Browse the repository at this point in the history
…dd graceful shutdown
  • Loading branch information
dimakis committed Aug 1, 2023
1 parent 82a4e8c commit 4bcf9e1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 14 deletions.
85 changes: 74 additions & 11 deletions cmd/kar-controllers/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ limitations under the License.
package app

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"net/http"

"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/options"
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/queuejob"
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/health"
"github.com/prometheus/client_golang/prometheus/promhttp"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
Expand Down Expand Up @@ -67,7 +75,7 @@ func Run(opt *options.ServerOption) error {
jobctrl.Run(neverStop)

// This call is blocking (unless an error occurs) which equates to <-neverStop
err = listenHealthProbe(opt)
err = startHealthAndMetricsServers(opt)
if err != nil {
return err
}
Expand All @@ -76,14 +84,69 @@ func Run(opt *options.ServerOption) error {
}

// Starts the health probe listener
func listenHealthProbe(opt *options.ServerOption) error {
handler := http.NewServeMux()
handler.Handle("/healthz", &health.Handler{})
err := http.ListenAndServe(opt.HealthProbeListenAddr, handler)
if err != nil {
return err
}

return nil
func startHealthAndMetricsServers(opt *options.ServerOption) error {
// Start the metrics server
RecordMetrics()

metricsHandler := http.NewServeMux()
metricsHandler.Handle("/metrics", promhttp.Handler())
healthHandler := http.NewServeMux()
healthHandler.Handle("/healthz", &health.Handler{})

metricsServer := &http.Server{
Addr: opt.MetricsListenAddr,
Handler: metricsHandler,
}

healthServer := &http.Server{
Addr: opt.HealthProbeListenAddr,
Handler: healthHandler,
}

// make a channel for errors for each server
metricsServerErrChan := make(chan error)
healthServerErrChan := make(chan error)

// start servers in their own goroutines
go func() {
err := metricsServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
metricsServerErrChan <- err
}
}()

go func() {
err := healthServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
healthServerErrChan <- err
}
}()

// make a channel to listen for OS shutdown signal
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

// use select to wait for either a shutdown signal or an error
select {
case <-stop:
// received an OS shutdown signal, shut down servers gracefully
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

errM := metricsServer.Shutdown(ctx)
if errM != nil {
return fmt.Errorf("metrics server shutdown error: %v", errM)
}
errH := healthServer.Shutdown(ctx)
if errH != nil {
return fmt.Errorf("health server shutdown error: %v", errH)
}
case err := <-metricsServerErrChan:
return fmt.Errorf("metrics server error: %v", err)
case err := <-healthServerErrChan:
return fmt.Errorf("health server error: %v", err)
}

return nil
}

7 changes: 5 additions & 2 deletions deployment/mcad-controller/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ spec:
- name: http
port: 80
targetPort: 8080
- name: metrics
port: 8083
targetPort: 8083
selector:
app: custom-metrics-apiserver
---
Expand Down Expand Up @@ -353,7 +356,7 @@ spec:
- containerPort: 8080
name: http
- containerPort: 8083
name: metrics
name: metrics-server
volumeMounts:
- mountPath: /tmp
name: temp-vol
Expand All @@ -373,7 +376,7 @@ spec:
port: 8081
periodSeconds: 5
timeoutSeconds: 5
metrics:
metrics-server:
httpGet:
path: /metrics
port: 8083
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ spec:
selector:
app: custom-metrics-apiserver
---
apiVersion: v1
kind: Service
metadata:
name: metrics-server
spec:
ports:
- name: https
port: 443
targetPort: 6443
- name: http
port: 80
targetPort: 8080
- name: metrics-server
port: 8083
targetPort: 8083
selector:
app: metrics-server
---
#{{ if .Values.configMap.multiCluster }}
apiVersion: apiregistration.k8s.io/v1beta1
kind: APIService
Expand Down Expand Up @@ -8025,7 +8043,6 @@ spec:
#{{ if .Values.volumes.hostPath }}
- name: agent-config-vol
mountPath: /root/kubernetes
#{{ end }}
#{{ end }}
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
Expand All @@ -8038,6 +8055,8 @@ spec:
name: https
- containerPort: 8080
name: http
- containerPort: 8083
name: metrics-server
volumeMounts:
- mountPath: /tmp
name: temp-vol
Expand Down

0 comments on commit 4bcf9e1

Please sign in to comment.