Skip to content

Commit 20ed505

Browse files
committed
feat: CNS checks apiserver in healthz
1 parent 281770b commit 20ed505

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

cns/healthserver/healthz.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

cns/service/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ func main() {
642642
return nil
643643
}),
644644
}
645-
go healthserver.Start(z, cnsconfig.MetricsBindAddress, &healthz.Handler{}, readyChecker)
645+
healthzHandler := healthserver.NewHealthzHandlerWithChecks()
646+
go healthserver.Start(z, cnsconfig.MetricsBindAddress, healthzHandler, readyChecker)
646647

647648
nmaConfig, err := nmagent.NewConfig(cnsconfig.WireserverIP)
648649
if err != nil {

0 commit comments

Comments
 (0)