Skip to content

Commit 1504d8f

Browse files
committed
Change defaulting, add tests
1 parent 0d05be8 commit 1504d8f

File tree

4 files changed

+379
-39
lines changed

4 files changed

+379
-39
lines changed

pkg/manager/internal.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import (
3636
"k8s.io/apiserver/pkg/authorization/authorizer"
3737
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
3838
"k8s.io/apiserver/pkg/server/options"
39-
"k8s.io/client-go/kubernetes"
39+
authenticationv1 "k8s.io/client-go/kubernetes/typed/authentication/v1"
40+
authorizationv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
4041
"k8s.io/client-go/rest"
4142
"k8s.io/client-go/tools/leaderelection"
4243
"k8s.io/client-go/tools/leaderelection/resourcelock"
@@ -92,15 +93,18 @@ type controllerManager struct {
9293
// metricsListener is used to serve prometheus metrics
9394
metricsListener net.Listener
9495

95-
// metricsInsecureServing enables insecure metrics serving.
96-
// This means metrics will be served via http and without authentication and authorization.
97-
metricsInsecureServing bool
96+
// metricsSecureServing enables secure metrics serving.
97+
// This means metrics will be served via https and with authentication and authorization.
98+
metricsSecureServing bool
9899

99100
// metricsExtraHandlers contains extra handlers to register on http server that serves metrics.
100101
metricsExtraHandlers map[string]http.Handler
101102

102-
// metricsKubeClient is the client used to authenticate and authorize requests to the metrics endpoint.
103-
metricsKubeClient *kubernetes.Clientset
103+
// metricsAuthenticationClient is the client used to authenticate requests to the metrics endpoint.
104+
metricsAuthenticationClient authenticationv1.AuthenticationV1Interface
105+
106+
// metricsAuthorizationClient is the client used to authorize requests to the metrics endpoint.
107+
metricsAuthorizationClient authorizationv1.AuthorizationV1Interface
104108

105109
// healthProbeListener is used to serve liveness probe
106110
healthProbeListener net.Listener
@@ -318,9 +322,9 @@ func (cm *controllerManager) addMetricsServer() error {
318322

319323
log := cm.logger.WithValues("path", defaultMetricsEndpoint)
320324

321-
if !cm.metricsInsecureServing {
325+
if cm.metricsSecureServing {
322326
var err error
323-
handler, err = withAuthenticationAndAuthorization(log, cm.metricsKubeClient, handler)
327+
handler, err = withAuthenticationAndAuthorization(log, cm.metricsAuthenticationClient, cm.metricsAuthorizationClient, handler)
324328
if err != nil {
325329
return fmt.Errorf("failed to add metrics server: %w", err)
326330
}
@@ -340,11 +344,11 @@ func (cm *controllerManager) addMetricsServer() error {
340344
})
341345
}
342346

343-
func withAuthenticationAndAuthorization(log logr.Logger, metricsKubeClient *kubernetes.Clientset, handler http.Handler) (http.Handler, error) {
347+
func withAuthenticationAndAuthorization(log logr.Logger, authenticationClient authenticationv1.AuthenticationV1Interface, authorizationClient authorizationv1.AuthorizationV1Interface, handler http.Handler) (http.Handler, error) {
344348
authenticatorConfig := authenticatorfactory.DelegatingAuthenticatorConfig{
345349
Anonymous: false, // Require authentication.
346350
CacheTTL: 1 * time.Minute,
347-
TokenAccessReviewClient: metricsKubeClient.AuthenticationV1(),
351+
TokenAccessReviewClient: authenticationClient,
348352
TokenAccessReviewTimeout: 10 * time.Second,
349353
WebhookRetryBackoff: options.DefaultAuthWebhookRetryBackoff(),
350354
}
@@ -354,7 +358,7 @@ func withAuthenticationAndAuthorization(log logr.Logger, metricsKubeClient *kube
354358
}
355359

356360
authorizerConfig := authorizerfactory.DelegatingAuthorizerConfig{
357-
SubjectAccessReviewClient: metricsKubeClient.AuthorizationV1(),
361+
SubjectAccessReviewClient: authorizationClient,
358362
AllowCacheTTL: 5 * time.Minute,
359363
DenyCacheTTL: 30 * time.Second,
360364
WebhookRetryBackoff: options.DefaultAuthWebhookRetryBackoff(),
@@ -392,13 +396,13 @@ func withAuthenticationAndAuthorization(log logr.Logger, metricsKubeClient *kube
392396

393397
authorized, reason, err := delegatingAuthorizer.Authorize(ctx, attributes)
394398
if err != nil {
395-
msg := fmt.Sprintf("Authorization for user %s failed", attributes.User)
399+
msg := fmt.Sprintf("Authorization for user %s failed", attributes.User.GetName())
396400
log.Error(err, fmt.Sprintf("%s: %s", msg, err))
397401
http.Error(w, msg, http.StatusInternalServerError)
398402
return
399403
}
400404
if authorized != authorizer.DecisionAllow {
401-
msg := fmt.Sprintf("Authorization denied for user %s", attributes.User)
405+
msg := fmt.Sprintf("Authorization denied for user %s", attributes.User.GetName())
402406
log.V(4).Info(fmt.Sprintf("%s: %s", msg, reason))
403407
http.Error(w, msg, http.StatusForbidden)
404408
return

pkg/manager/manager.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/client-go/kubernetes"
33+
authenticationv1 "k8s.io/client-go/kubernetes/typed/authentication/v1"
34+
authorizationv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
3335
"k8s.io/client-go/rest"
3436
"k8s.io/client-go/tools/leaderelection/resourcelock"
3537
"k8s.io/client-go/tools/record"
@@ -260,7 +262,14 @@ type Options struct {
260262
// MetricsBindAddress is the TCP address that the controller should bind to
261263
// for serving prometheus metrics.
262264
// It can be set to "0" to disable the metrics serving.
263-
// Per default metrics will be served via https and authenticated (via TokenReviews)
265+
//
266+
// Per default metrics will be served via http and without authentication and authorization.
267+
// If MetricsSecureServing is enabled metrics will be served via https and authenticated (via TokenReviews)
268+
// and authorized (via SubjectAccessReviews) with the kube-apiserver.
269+
MetricsBindAddress string
270+
271+
// MetricsSecureServing enables secure metrics serving.
272+
// This means metrics will be served via https and authenticated (via TokenReviews)
264273
// and authorized (via SubjectAccessReviews) with the kube-apiserver.
265274
//
266275
// For the authentication and authorization the controller needs a ClusterRole
@@ -283,13 +292,7 @@ type Options struct {
283292
// - "/metrics"
284293
// verbs:
285294
// - get
286-
// Alternatively MetricsInsecureServing can be set to true, then metrics will
287-
// be served via http and authentication and authorization is skipped.
288-
MetricsBindAddress string
289-
290-
// MetricsInsecureServing enables insecure metrics serving.
291-
// This means metrics will be served via http and authentication and authorization is skipped.
292-
MetricsInsecureServing bool
295+
MetricsSecureServing bool
293296

294297
// HealthProbeBindAddress is the TCP address that the controller should bind to
295298
// for serving health probes
@@ -383,7 +386,7 @@ type Options struct {
383386
// Dependency injection for testing
384387
newRecorderProvider func(config *rest.Config, httpClient *http.Client, scheme *runtime.Scheme, logger logr.Logger, makeBroadcaster intrec.EventBroadcasterProducer) (*intrec.Provider, error)
385388
newResourceLock func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error)
386-
newMetricsListener func(addr string, insecureServing bool) (net.Listener, error)
389+
newMetricsListener func(addr string, secureServing bool) (net.Listener, error)
387390
newHealthProbeListener func(addr string) (net.Listener, error)
388391
newPprofListener func(addr string) (net.Listener, error)
389392
}
@@ -480,18 +483,20 @@ func New(config *rest.Config, options Options) (Manager, error) {
480483
}
481484
}
482485

483-
var metricsKubeClient *kubernetes.Clientset
484-
if !options.MetricsInsecureServing {
485-
var err error
486-
metricsKubeClient, err = kubernetes.NewForConfigAndClient(config, cluster.GetHTTPClient())
486+
var metricsAuthenticationClient authenticationv1.AuthenticationV1Interface
487+
var metricsAuthorizationClient authorizationv1.AuthorizationV1Interface
488+
if options.MetricsSecureServing {
489+
metricsKubeClient, err := kubernetes.NewForConfigAndClient(config, cluster.GetHTTPClient())
487490
if err != nil {
488491
return nil, err
489492
}
493+
metricsAuthenticationClient = metricsKubeClient.AuthenticationV1()
494+
metricsAuthorizationClient = metricsKubeClient.AuthorizationV1()
490495
}
491496

492497
// Create the metrics listener. This will throw an error if the metrics bind
493498
// address is invalid or already in use.
494-
metricsListener, err := options.newMetricsListener(options.MetricsBindAddress, options.MetricsInsecureServing)
499+
metricsListener, err := options.newMetricsListener(options.MetricsBindAddress, options.MetricsSecureServing)
495500
if err != nil {
496501
return nil, err
497502
}
@@ -524,9 +529,10 @@ func New(config *rest.Config, options Options) (Manager, error) {
524529
recorderProvider: recorderProvider,
525530
resourceLock: resourceLock,
526531
metricsListener: metricsListener,
527-
metricsInsecureServing: options.MetricsInsecureServing,
532+
metricsSecureServing: options.MetricsSecureServing,
528533
metricsExtraHandlers: metricsExtraHandlers,
529-
metricsKubeClient: metricsKubeClient,
534+
metricsAuthenticationClient: metricsAuthenticationClient,
535+
metricsAuthorizationClient: metricsAuthorizationClient,
530536
controllerConfig: options.Controller,
531537
logger: options.Logger,
532538
elected: make(chan struct{}),

0 commit comments

Comments
 (0)