config.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package admission
  14. import (
  15. "io/ioutil"
  16. "net/http"
  17. "time"
  18. "k8s.io/klog"
  19. utilwait "k8s.io/apimachinery/pkg/util/wait"
  20. "k8s.io/apiserver/pkg/admission"
  21. webhookinit "k8s.io/apiserver/pkg/admission/plugin/webhook/initializer"
  22. genericapiserver "k8s.io/apiserver/pkg/server"
  23. "k8s.io/apiserver/pkg/util/webhook"
  24. cacheddiscovery "k8s.io/client-go/discovery/cached/memory"
  25. externalinformers "k8s.io/client-go/informers"
  26. "k8s.io/client-go/kubernetes"
  27. "k8s.io/client-go/rest"
  28. "k8s.io/client-go/restmapper"
  29. quotainstall "k8s.io/kubernetes/pkg/quota/v1/install"
  30. )
  31. // Config holds the configuration needed to for initialize the admission plugins
  32. type Config struct {
  33. CloudConfigFile string
  34. LoopbackClientConfig *rest.Config
  35. ExternalInformers externalinformers.SharedInformerFactory
  36. }
  37. // New sets up the plugins and admission start hooks needed for admission
  38. func (c *Config) New(proxyTransport *http.Transport, serviceResolver webhook.ServiceResolver) ([]admission.PluginInitializer, genericapiserver.PostStartHookFunc, error) {
  39. webhookAuthResolverWrapper := webhook.NewDefaultAuthenticationInfoResolverWrapper(proxyTransport, c.LoopbackClientConfig)
  40. webhookPluginInitializer := webhookinit.NewPluginInitializer(webhookAuthResolverWrapper, serviceResolver)
  41. var cloudConfig []byte
  42. if c.CloudConfigFile != "" {
  43. var err error
  44. cloudConfig, err = ioutil.ReadFile(c.CloudConfigFile)
  45. if err != nil {
  46. klog.Fatalf("Error reading from cloud configuration file %s: %#v", c.CloudConfigFile, err)
  47. }
  48. }
  49. clientset, err := kubernetes.NewForConfig(c.LoopbackClientConfig)
  50. if err != nil {
  51. return nil, nil, err
  52. }
  53. discoveryClient := cacheddiscovery.NewMemCacheClient(clientset.Discovery())
  54. discoveryRESTMapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
  55. kubePluginInitializer := NewPluginInitializer(
  56. cloudConfig,
  57. discoveryRESTMapper,
  58. quotainstall.NewQuotaConfigurationForAdmission(),
  59. )
  60. admissionPostStartHook := func(context genericapiserver.PostStartHookContext) error {
  61. discoveryRESTMapper.Reset()
  62. go utilwait.Until(discoveryRESTMapper.Reset, 30*time.Second, context.StopCh)
  63. return nil
  64. }
  65. return []admission.PluginInitializer{webhookPluginInitializer, kubePluginInitializer}, admissionPostStartHook, nil
  66. }