@@ -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
2225type 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 {
3034func 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" )
0 commit comments