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