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