github.com/Azure/aad-pod-identity@v1.8.17/pkg/probes/probes.go (about)

     1  package probes
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"k8s.io/klog/v2"
     7  )
     8  
     9  // InitHealthProbe - sets up a health probe which responds with success (200 - OK) once its initialized.
    10  // The contents of the healthz endpoint will be the string "Active" if the condition is satisfied.
    11  // The condition is set to true when the sync cycle has become active in case of MIC and the iptables
    12  // rules set in case of NMI.
    13  func InitHealthProbe(condition *bool) {
    14  	http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
    15  		w.WriteHeader(200)
    16  		if *condition {
    17  			_, _ = w.Write([]byte("Active"))
    18  		} else {
    19  			_, _ = w.Write([]byte("Not Active"))
    20  		}
    21  	})
    22  }
    23  
    24  func startAsync(port string) {
    25  	err := http.ListenAndServe(":"+port, nil)
    26  	if err != nil {
    27  		klog.Fatalf("http listen and serve error: %+v", err)
    28  	}
    29  
    30  	klog.Info("http listen and serve started !")
    31  }
    32  
    33  // Start starts the required http server to start the probe to respond.
    34  func Start(port string) {
    35  	go startAsync(port)
    36  }
    37  
    38  // InitAndStart initializes the default probes and starts the http listening port.
    39  func InitAndStart(port string, condition *bool) {
    40  	InitHealthProbe(condition)
    41  	klog.Infof("initialized health probe on port %s", port)
    42  	// start the probe.
    43  	Start(port)
    44  	klog.Info("started health probe")
    45  }