| 
 | 1 | +package healthserver  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +"net/http"  | 
 | 5 | + | 
 | 6 | +"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"  | 
 | 7 | +"github.com/pkg/errors"  | 
 | 8 | +metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"  | 
 | 9 | +"k8s.io/apimachinery/pkg/runtime"  | 
 | 10 | +utilruntime "k8s.io/apimachinery/pkg/util/runtime"  | 
 | 11 | +ctrl "sigs.k8s.io/controller-runtime"  | 
 | 12 | +"sigs.k8s.io/controller-runtime/pkg/client"  | 
 | 13 | +"sigs.k8s.io/controller-runtime/pkg/healthz"  | 
 | 14 | +)  | 
 | 15 | + | 
 | 16 | +var schema = runtime.NewScheme()  | 
 | 17 | + | 
 | 18 | +func init() {  | 
 | 19 | +utilruntime.Must(v1alpha.AddToScheme(schema))  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +func NewHealthzHandlerWithChecks() http.Handler {  | 
 | 23 | +cfg, err := ctrl.GetConfig()  | 
 | 24 | +if err != nil {  | 
 | 25 | +panic(err)  | 
 | 26 | +}  | 
 | 27 | +cli, err := client.New(cfg, client.Options{  | 
 | 28 | +Scheme: schema,  | 
 | 29 | +})  | 
 | 30 | +if err != nil {  | 
 | 31 | +panic(err)  | 
 | 32 | +}  | 
 | 33 | + | 
 | 34 | +checks := map[string]healthz.Checker{  | 
 | 35 | +"nnc": func(req *http.Request) error {  | 
 | 36 | +ctx := req.Context()  | 
 | 37 | +// we just care that we're allowed to List NNCs so set limit to 1 to minimize  | 
 | 38 | +// additional load on apiserver  | 
 | 39 | +if err := cli.List(ctx, &v1alpha.NodeNetworkConfigList{}, &client.ListOptions{  | 
 | 40 | +Namespace: metav1.NamespaceSystem,  | 
 | 41 | +Limit: int64(1),  | 
 | 42 | +}); err != nil {  | 
 | 43 | +return errors.Wrap(err, "failed to list NodeNetworkConfig")  | 
 | 44 | +}  | 
 | 45 | +return nil  | 
 | 46 | +},  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +// strip prefix so that it runs through all checks registered on the handler.  | 
 | 50 | +// otherwise it will look for a check named "healthz" and return a 404 if not there.  | 
 | 51 | +return http.StripPrefix("/healthz", &healthz.Handler{  | 
 | 52 | +Checks: checks,  | 
 | 53 | +})  | 
 | 54 | +}  | 
0 commit comments