Skip to content

Commit 4dbfc94

Browse files
committed
fix healthz issues
1 parent 5ff95c3 commit 4dbfc94

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

cns/healthserver/healthz.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55

66
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
77
"github.com/pkg/errors"
8+
meta "k8s.io/apimachinery/pkg/api/meta"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/client-go/rest"
1012
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1113
ctrl "sigs.k8s.io/controller-runtime"
1214
"sigs.k8s.io/controller-runtime/pkg/client"
15+
apiutil "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1316
"sigs.k8s.io/controller-runtime/pkg/healthz"
1417
)
1518

@@ -21,6 +24,7 @@ func init() {
2124

2225
type Config struct {
2326
PingAPIServer bool
27+
Mapper meta.RESTMapper
2428
}
2529

2630
// NewHealthzHandlerWithChecks will return a [http.Handler] for CNS's /healthz endpoint.
@@ -30,12 +34,26 @@ type Config struct {
3034
func NewHealthzHandlerWithChecks(cfg *Config) (http.Handler, error) {
3135
checks := make(map[string]healthz.Checker)
3236
if cfg.PingAPIServer {
33-
cfg, err := ctrl.GetConfig()
37+
restCfg, err := ctrl.GetConfig()
3438
if err != nil {
3539
return nil, errors.Wrap(err, "failed to get kubeconfig")
3640
}
37-
cli, err := client.New(cfg, client.Options{
41+
// Use the provided (test) RESTMapper when present; otherwise fall back to
42+
// a dynamic, discovery-based mapper for production.
43+
mapper := cfg.Mapper
44+
if mapper == nil {
45+
httpClient, err := rest.HTTPClientFor(restCfg)
46+
if err != nil {
47+
return nil, errors.Wrap(err, "build http client for REST mapper")
48+
}
49+
mapper, err = apiutil.NewDynamicRESTMapper(restCfg, httpClient)
50+
if err != nil {
51+
return nil, errors.Wrap(err, "build rest mapper")
52+
}
53+
}
54+
cli, err := client.New(restCfg, client.Options{
3855
Scheme: scheme,
56+
Mapper: mapper,
3957
})
4058
if err != nil {
4159
return nil, errors.Wrap(err, "failed to build client")

cns/healthserver/healthz_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/require"
11+
meta "k8s.io/apimachinery/pkg/api/meta"
12+
"k8s.io/apimachinery/pkg/runtime/schema"
1113
)
1214

1315
const nncCRD = `{
@@ -158,6 +160,13 @@ const nncResult = `{
158160
}
159161
}`
160162

163+
func staticNNCMapper() meta.RESTMapper {
164+
gv := schema.GroupVersion{Group: "acn.azure.com", Version: "v1alpha"}
165+
m := meta.NewDefaultRESTMapper([]schema.GroupVersion{gv})
166+
m.Add(schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: "NodeNetworkConfig"}, meta.RESTScopeNamespace)
167+
return m
168+
}
169+
161170
func TestNewHealthzHandlerWithChecks(t *testing.T) {
162171
tests := []struct {
163172
name string
@@ -169,6 +178,7 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
169178
name: "list NNC gives 200 should indicate healthy",
170179
config: &Config{
171180
PingAPIServer: true,
181+
Mapper: staticNNCMapper(),
172182
},
173183
apiStatusCode: http.StatusOK,
174184
expectedHealthy: true,
@@ -177,6 +187,7 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
177187
name: "unauthorized (401) from apiserver should be unhealthy",
178188
config: &Config{
179189
PingAPIServer: true,
190+
Mapper: staticNNCMapper(),
180191
},
181192
apiStatusCode: http.StatusUnauthorized,
182193
expectedHealthy: false,
@@ -185,6 +196,7 @@ func TestNewHealthzHandlerWithChecks(t *testing.T) {
185196
name: "channel nodesubnet should not call apiserver so it doesn't matter if the status code is a 401",
186197
config: &Config{
187198
PingAPIServer: false,
199+
Mapper: staticNNCMapper(),
188200
},
189201
apiStatusCode: http.StatusUnauthorized,
190202
expectedHealthy: true,

0 commit comments

Comments
 (0)