k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/config/kubeconfig.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 config 18 19 import ( 20 "fmt" 21 "io" 22 "path" 23 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/apimachinery/pkg/runtime/serializer" 26 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 27 "k8s.io/apimachinery/pkg/util/validation/field" 28 "k8s.io/apiserver/pkg/admission/plugin/webhook/config/apis/webhookadmission" 29 "k8s.io/apiserver/pkg/admission/plugin/webhook/config/apis/webhookadmission/v1" 30 "k8s.io/apiserver/pkg/admission/plugin/webhook/config/apis/webhookadmission/v1alpha1" 31 ) 32 33 var ( 34 scheme = runtime.NewScheme() 35 codecs = serializer.NewCodecFactory(scheme) 36 ) 37 38 func init() { 39 utilruntime.Must(webhookadmission.AddToScheme(scheme)) 40 utilruntime.Must(v1.AddToScheme(scheme)) 41 utilruntime.Must(v1alpha1.AddToScheme(scheme)) 42 } 43 44 // LoadConfig extract the KubeConfigFile from configFile 45 func LoadConfig(configFile io.Reader) (string, error) { 46 var kubeconfigFile string 47 if configFile != nil { 48 // we have a config so parse it. 49 data, err := io.ReadAll(configFile) 50 if err != nil { 51 return "", err 52 } 53 decoder := codecs.UniversalDecoder() 54 decodedObj, err := runtime.Decode(decoder, data) 55 if err != nil { 56 return "", err 57 } 58 config, ok := decodedObj.(*webhookadmission.WebhookAdmission) 59 if !ok { 60 return "", fmt.Errorf("unexpected type: %T", decodedObj) 61 } 62 63 if !path.IsAbs(config.KubeConfigFile) { 64 return "", field.Invalid(field.NewPath("kubeConfigFile"), config.KubeConfigFile, "must be an absolute file path") 65 } 66 67 kubeconfigFile = config.KubeConfigFile 68 } 69 return kubeconfigFile, nil 70 }