Skip to content

Commit 5ec6a3f

Browse files
rbtrpaulgmiller
andauthored
move metrics to service port, add healthz, pprof (#1318)
* combine metrics and healthz * use root mux and bind pprof routes on debug config opt Signed-off-by: Evan Baker <[email protected]> Co-authored-by: Paul Miller <[email protected]>
1 parent 739c1a2 commit 5ec6a3f

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

cns/configuration/configuration.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type CNSConfig struct {
2222
ChannelMode string
2323
InitializeFromCNI bool
2424
ManagedSettings ManagedSettings
25-
MetricsBindAddress string
25+
Debug bool
2626
SyncHostNCTimeoutMs int
2727
SyncHostNCVersionIntervalMs int
2828
TLSCertificatePath string
@@ -150,9 +150,6 @@ func SetCNSConfigDefaults(config *CNSConfig) {
150150
if config.ChannelMode == "" {
151151
config.ChannelMode = cns.Direct
152152
}
153-
if config.MetricsBindAddress == "" {
154-
config.MetricsBindAddress = ":9090"
155-
}
156153
if config.SyncHostNCVersionIntervalMs == 0 {
157154
config.SyncHostNCVersionIntervalMs = 1000 //nolint:gomnd // default times
158155
}

cns/configuration/configuration_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func TestReadConfigFromFile(t *testing.T) {
6161
NodeID: "abc",
6262
NodeSyncIntervalInSeconds: 30,
6363
},
64-
MetricsBindAddress: ":9091",
6564
SyncHostNCTimeoutMs: 5,
6665
SyncHostNCVersionIntervalMs: 5,
6766
TLSCertificatePath: "/test",
@@ -191,7 +190,7 @@ func TestSetCNSConfigDefaults(t *testing.T) {
191190
ManagedSettings: ManagedSettings{
192191
NodeSyncIntervalInSeconds: 30,
193192
},
194-
MetricsBindAddress: ":9090",
193+
Debug: false,
195194
SyncHostNCTimeoutMs: 500,
196195
SyncHostNCVersionIntervalMs: 1000,
197196
TelemetrySettings: TelemetrySettings{
@@ -210,7 +209,6 @@ func TestSetCNSConfigDefaults(t *testing.T) {
210209
ManagedSettings: ManagedSettings{
211210
NodeSyncIntervalInSeconds: 1,
212211
},
213-
MetricsBindAddress: ":9091",
214212
SyncHostNCTimeoutMs: 5,
215213
SyncHostNCVersionIntervalMs: 1,
216214
TelemetrySettings: TelemetrySettings{
@@ -226,7 +224,6 @@ func TestSetCNSConfigDefaults(t *testing.T) {
226224
ManagedSettings: ManagedSettings{
227225
NodeSyncIntervalInSeconds: 1,
228226
},
229-
MetricsBindAddress: ":9091",
230227
SyncHostNCTimeoutMs: 5,
231228
SyncHostNCVersionIntervalMs: 1,
232229
TelemetrySettings: TelemetrySettings{

cns/service/main.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
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

5862
const (
@@ -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

Comments
 (0)