...

Source file src/k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/config.go

Documentation: k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction

     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 podtolerationrestriction
    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  	internalapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction"
    27  	"k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction/install"
    28  	versionedapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction/v1alpha1"
    29  	"k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction/validation"
    30  )
    31  
    32  var (
    33  	scheme = runtime.NewScheme()
    34  	codecs = serializer.NewCodecFactory(scheme)
    35  )
    36  
    37  func init() {
    38  	install.Install(scheme)
    39  }
    40  
    41  // LoadConfiguration loads the provided configuration.
    42  func loadConfiguration(config io.Reader) (*internalapi.Configuration, error) {
    43  	// if no config is provided, return a default configuration
    44  	if config == nil {
    45  		externalConfig := &versionedapi.Configuration{}
    46  		scheme.Default(externalConfig)
    47  		internalConfig := &internalapi.Configuration{}
    48  		if err := scheme.Convert(externalConfig, internalConfig, nil); err != nil {
    49  			return nil, err
    50  		}
    51  		return internalConfig, nil
    52  	}
    53  	// we have a config so parse it.
    54  	data, err := ioutil.ReadAll(config)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	decoder := codecs.UniversalDecoder()
    59  	decodedObj, err := runtime.Decode(decoder, data)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	externalConfig, ok := decodedObj.(*internalapi.Configuration)
    64  	if !ok {
    65  		return nil, fmt.Errorf("unexpected type: %T", decodedObj)
    66  	}
    67  
    68  	if err := validation.ValidateConfiguration(externalConfig); err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	return externalConfig, nil
    73  }
    74  

View as plain text