Skip to content

Commit d7ebedb

Browse files
committed
refactor: return error from NewHealthzHandlerWithChecks instead of panicking
1 parent e0ea5b0 commit d7ebedb

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

cns/healthserver/healthz.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package healthserver
22

33
import (
4-
"fmt"
54
"net/http"
65

76
"github.com/Azure/azure-container-networking/cns"
@@ -22,16 +21,20 @@ func init() {
2221
utilruntime.Must(v1alpha.AddToScheme(scheme))
2322
}
2423

25-
func NewHealthzHandlerWithChecks(cnsConfig *configuration.CNSConfig) http.Handler {
24+
// NewHealthzHandlerWithChecks will return a [http.Handler] for CNS's /healthz endpoint.
25+
// Depending on what we expect CNS to be able to read (based on the [configuration.CNSConfig])
26+
// then the checks registered to the handler will test for those expectations. For example, in
27+
// ChannelMode: CRD, the health check will ensure that CNS is able to list NNCs successfully.
28+
func NewHealthzHandlerWithChecks(cnsConfig *configuration.CNSConfig) (http.Handler, error) {
2629
cfg, err := ctrl.GetConfig()
2730
if err != nil {
28-
panic(fmt.Errorf("unable to get config: %w", err))
31+
return nil, errors.Wrap(err, "unable to get a kubeconfig")
2932
}
3033
cli, err := client.New(cfg, client.Options{
3134
Scheme: scheme,
3235
})
3336
if err != nil {
34-
panic(fmt.Errorf("unable to create client: %w", err))
37+
return nil, errors.Wrap(err, "unable to create a client")
3538
}
3639

3740
checks := make(map[string]healthz.Checker)
@@ -54,5 +57,5 @@ func NewHealthzHandlerWithChecks(cnsConfig *configuration.CNSConfig) http.Handle
5457
// otherwise it will look for a check named "healthz" and return a 404 if not there.
5558
return http.StripPrefix("/healthz", &healthz.Handler{
5659
Checks: checks,
57-
})
60+
}), nil
5861
}

cns/healthserver/healthz_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
197197
configureLocalAPIServer(t, tt.apiStatusCode)
198198

199199
responseRecorder := httptest.NewRecorder()
200-
healthHandler := NewHealthzHandlerWithChecks(tt.cnsConfig)
200+
healthHandler, err := NewHealthzHandlerWithChecks(tt.cnsConfig)
201+
require.NoError(t, err)
202+
201203
healthHandler.ServeHTTP(responseRecorder, httptest.NewRequest("GET", "/healthz", nil))
202204

203205
require.Equal(t, tt.expectedHealthy, responseRecorder.Code == http.StatusOK)

cns/service/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,11 @@ func main() {
643643
}),
644644
}
645645

646-
healthzHandler := healthserver.NewHealthzHandlerWithChecks(cnsconfig)
646+
healthzHandler, err := healthserver.NewHealthzHandlerWithChecks(cnsconfig)
647+
if err != nil {
648+
logger.Errorf("unable to initialize a healthz handler: %v", err)
649+
return
650+
}
647651
go healthserver.Start(z, cnsconfig.MetricsBindAddress, healthzHandler, readyChecker)
648652

649653
nmaConfig, err := nmagent.NewConfig(cnsconfig.WireserverIP)

0 commit comments

Comments
 (0)