config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2017 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 podtolerationrestriction
  14. import (
  15. "fmt"
  16. "io"
  17. "io/ioutil"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/runtime/serializer"
  20. internalapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction"
  21. "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction/install"
  22. versionedapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction/v1alpha1"
  23. "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction/validation"
  24. )
  25. var (
  26. scheme = runtime.NewScheme()
  27. codecs = serializer.NewCodecFactory(scheme)
  28. )
  29. func init() {
  30. install.Install(scheme)
  31. }
  32. // LoadConfiguration loads the provided configuration.
  33. func loadConfiguration(config io.Reader) (*internalapi.Configuration, error) {
  34. // if no config is provided, return a default configuration
  35. if config == nil {
  36. externalConfig := &versionedapi.Configuration{}
  37. scheme.Default(externalConfig)
  38. internalConfig := &internalapi.Configuration{}
  39. if err := scheme.Convert(externalConfig, internalConfig, nil); err != nil {
  40. return nil, err
  41. }
  42. return internalConfig, nil
  43. }
  44. // we have a config so parse it.
  45. data, err := ioutil.ReadAll(config)
  46. if err != nil {
  47. return nil, err
  48. }
  49. decoder := codecs.UniversalDecoder()
  50. decodedObj, err := runtime.Decode(decoder, data)
  51. if err != nil {
  52. return nil, err
  53. }
  54. externalConfig, ok := decodedObj.(*internalapi.Configuration)
  55. if !ok {
  56. return nil, fmt.Errorf("unexpected type: %T", decodedObj)
  57. }
  58. if err := validation.ValidateConfiguration(externalConfig); err != nil {
  59. return nil, err
  60. }
  61. return externalConfig, nil
  62. }