Skip to content

Commit

Permalink
basic healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
x0ddf committed Feb 10, 2025
1 parent 76944cb commit 8c9d340
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion controllers/admission_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -33,6 +34,8 @@ func (ac *AdmissionController) Handle(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("could not parse admission review: %v", err), http.StatusBadRequest)
return
}
requestId := admissionReview.Request.UID
log.Printf("new admission request:%v", requestId)

// Get the requesting user
userName := ExtractUserMeta(admissionReview.Request)
Expand All @@ -59,6 +62,7 @@ func (ac *AdmissionController) Handle(w http.ResponseWriter, r *http.Request) {
// Create the patch bytes
patchBytes, err := json.Marshal(patches)
if err != nil {
log.Printf("fail to process request with id:%v", requestId)
http.Error(w, fmt.Sprintf("could not marshal patch: %v", err), http.StatusInternalServerError)
return
}
Expand All @@ -78,13 +82,15 @@ func (ac *AdmissionController) Handle(w http.ResponseWriter, r *http.Request) {
admissionReview.Response = admissionResponse
resp, err := json.Marshal(admissionReview)
if err != nil {
log.Printf("fail to process request with id:%v", requestId)
http.Error(w, fmt.Sprintf("could not marshal response: %v", err), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(resp); err != nil {
http.Error(w, fmt.Sprintf("fail to write response:% v", err), http.StatusInternalServerError)
log.Printf("fail to process request with id:%v", requestId)
http.Error(w, fmt.Sprintf("fail to write response:%v", err), http.StatusInternalServerError)
}
}

Expand Down
17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"

"github.com/x0ddf/whodidthat-controller/controllers"
)
Expand All @@ -24,15 +26,26 @@ func main() {
ac := controllers.NewAdmissionController()

http.HandleFunc("/mutate", ac.Handle)
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(map[string]string{
"status": "ok",
"time": time.Now().Format(time.RFC3339),
})
if err != nil {
return
}
})

server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{certs},
},
}

if err := server.ListenAndServe(); err != nil {
log.Printf("starting server on %d", port)
if err := server.ListenAndServeTLS(tlsCert, tlsKey); err != nil {
log.Panic(err)
}
}

0 comments on commit 8c9d340

Please sign in to comment.