k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/testing/authentication_info_resolver.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testing
    18  
    19  import (
    20  	"sync/atomic"
    21  
    22  	"k8s.io/apiserver/pkg/admission/plugin/webhook/testcerts"
    23  	"k8s.io/apiserver/pkg/util/webhook"
    24  	"k8s.io/client-go/rest"
    25  )
    26  
    27  // Wrapper turns an AuthenticationInfoResolver into a AuthenticationInfoResolverWrapper that unconditionally
    28  // returns the given AuthenticationInfoResolver.
    29  func Wrapper(r webhook.AuthenticationInfoResolver) func(webhook.AuthenticationInfoResolver) webhook.AuthenticationInfoResolver {
    30  	return func(webhook.AuthenticationInfoResolver) webhook.AuthenticationInfoResolver {
    31  		return r
    32  	}
    33  }
    34  
    35  // NewAuthenticationInfoResolver creates a fake AuthenticationInfoResolver that counts cache misses on
    36  // every call to its methods.
    37  func NewAuthenticationInfoResolver(cacheMisses *int32) webhook.AuthenticationInfoResolver {
    38  	return &authenticationInfoResolver{
    39  		restConfig: &rest.Config{
    40  			TLSClientConfig: rest.TLSClientConfig{
    41  				CAData:   testcerts.CACert,
    42  				CertData: testcerts.ClientCert,
    43  				KeyData:  testcerts.ClientKey,
    44  			},
    45  		},
    46  		cacheMisses: cacheMisses,
    47  	}
    48  }
    49  
    50  type authenticationInfoResolver struct {
    51  	restConfig  *rest.Config
    52  	cacheMisses *int32
    53  }
    54  
    55  func (a *authenticationInfoResolver) ClientConfigFor(hostPort string) (*rest.Config, error) {
    56  	atomic.AddInt32(a.cacheMisses, 1)
    57  	return a.restConfig, nil
    58  }
    59  
    60  func (a *authenticationInfoResolver) ClientConfigForService(serviceName, serviceNamespace string, servicePort int) (*rest.Config, error) {
    61  	atomic.AddInt32(a.cacheMisses, 1)
    62  	return a.restConfig, nil
    63  }
    64  
    65  // NewPanickingAuthenticationInfoResolver creates a fake AuthenticationInfoResolver that panics
    66  func NewPanickingAuthenticationInfoResolver(panicMessage string) webhook.AuthenticationInfoResolver {
    67  	return &panickingAuthenticationInfoResolver{
    68  		panicMessage: panicMessage,
    69  	}
    70  }
    71  
    72  type panickingAuthenticationInfoResolver struct {
    73  	panicMessage string
    74  }
    75  
    76  func (a *panickingAuthenticationInfoResolver) ClientConfigFor(hostPort string) (*rest.Config, error) {
    77  	panic(a.panicMessage)
    78  }
    79  
    80  func (a *panickingAuthenticationInfoResolver) ClientConfigForService(serviceName, serviceNamespace string, servicePort int) (*rest.Config, error) {
    81  	panic(a.panicMessage)
    82  }