99 "encoding/json"
1010 "fmt"
1111 "net/http"
12+ "net/http/pprof"
1213 "os"
1314 "os/signal"
1415 "runtime"
@@ -46,13 +47,16 @@ import (
4647 "github.com/Azure/azure-container-networking/store"
4748 "github.com/avast/retry-go/v3"
4849 "github.com/pkg/errors"
50+ "github.com/prometheus/client_golang/prometheus/promhttp"
4951 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5052 "k8s.io/apimachinery/pkg/fields"
5153 "k8s.io/apimachinery/pkg/types"
5254 "k8s.io/client-go/kubernetes"
5355 ctrl "sigs.k8s.io/controller-runtime"
5456 "sigs.k8s.io/controller-runtime/pkg/cache"
5557 "sigs.k8s.io/controller-runtime/pkg/client"
58+ "sigs.k8s.io/controller-runtime/pkg/healthz"
59+ "sigs.k8s.io/controller-runtime/pkg/metrics"
5660)
5761
5862const (
@@ -334,8 +338,8 @@ func sendRegisterNodeRequest(
334338 httpc * http.Client ,
335339 httpRestService cns.HTTPService ,
336340 nodeRegisterRequest cns.NodeRegisterRequest ,
337- registerURL string ) error {
338-
341+ registerURL string ,
342+ ) error {
339343 var body bytes.Buffer
340344 err := json .NewEncoder (& body ).Encode (nodeRegisterRequest )
341345 if err != nil {
@@ -991,10 +995,9 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
991995 })
992996
993997 manager , err := ctrl .NewManager (kubeConfig , ctrl.Options {
994- Scheme : nodenetworkconfig .Scheme ,
995- MetricsBindAddress : cnsconfig .MetricsBindAddress ,
996- Namespace : "kube-system" , // TODO(rbtr): namespace should be in the cns config
997- NewCache : nodeScopedCache ,
998+ Scheme : nodenetworkconfig .Scheme ,
999+ Namespace : "kube-system" , // TODO(rbtr): namespace should be in the cns config
1000+ NewCache : nodeScopedCache ,
9981001 })
9991002 if err != nil {
10001003 return errors .Wrap (err , "failed to create manager" )
@@ -1013,6 +1016,27 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
10131016 return errors .Wrapf (err , "failed to setup reconciler with manager" )
10141017 }
10151018
1019+ // adding some routes to the root serve mux
1020+ mux := httpRestServiceImplementation .Listener .GetMux ()
1021+
1022+ if cnsconfig .Debug {
1023+ // add pprof endpoints
1024+ mux .HandleFunc ("/debug/pprof/" , pprof .Index )
1025+ mux .HandleFunc ("/debug/pprof/cmdline" , pprof .Cmdline )
1026+ mux .HandleFunc ("/debug/pprof/profile" , pprof .Profile )
1027+ mux .HandleFunc ("/debug/pprof/symbol" , pprof .Symbol )
1028+ mux .HandleFunc ("/debug/pprof/trace" , pprof .Trace )
1029+ }
1030+
1031+ // add metrics endpoints
1032+ mux .Handle ("/metrics" , promhttp .HandlerFor (metrics .Registry , promhttp.HandlerOpts {
1033+ ErrorHandling : promhttp .HTTPErrorOnError ,
1034+ }))
1035+
1036+ // add healthz endpoints
1037+ healthzhandler := healthz.Handler {}
1038+ mux .Handle ("/healthz" , http .StripPrefix ("/healthz" , & healthzhandler ))
1039+
10161040 // Start the Manager which starts the reconcile loop.
10171041 // The Reconciler will send an initial NodeNetworkConfig update to the PoolMonitor, starting the
10181042 // Monitor's internal loop.
@@ -1057,6 +1081,7 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
10571081 }
10581082 }
10591083 }()
1084+
10601085 logger .Printf ("initialized and started SyncHostNCVersion loop" )
10611086
10621087 return nil
0 commit comments