Newer
Older
Learning / Webhook / webhook-server / main.go
package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"

	"k8s.io/klog/v2"
)

func main() {
	flag.Parse()
	// // handle our core application
	// http.HandleFunc("/validate-pods", ServeValidatePods)
	// http.HandleFunc("/mutate-pods", ServeMutatePods)
	http.HandleFunc("/health", ServeHealth)

	// start the server
	// listens to clear text http on port 8080 unless TLS env var is set to "true"
	if os.Getenv("TLS") == "true" {
		cert := "/etc/admission-webhook/tls/tls.crt"
		key := "/etc/admission-webhook/tls/tls.key"
		klog.Info("Listening on port 443...")
		klog.Info(http.ListenAndServeTLS(":443", cert, key, nil))
	} else {
		klog.Info("Listening on port 8080...")
		klog.Info(http.ListenAndServe(":8080", nil))
	}
}

// ServeHealth returns 200 when things are good
func ServeHealth(w http.ResponseWriter, r *http.Request) {
	klog.Infof("uri %s healthy", r.RequestURI)
	fmt.Fprint(w, "OK")
}