k3s/vendor/k8s.io/kubernetes/cmd/kube-scheduler/app/options/configfile.go

89 lines
2.8 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package options
import (
2019-08-30 18:33:25 +00:00
"fmt"
2019-01-12 04:58:27 +00:00
"io/ioutil"
"os"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
2019-01-12 04:58:27 +00:00
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
kubeschedulerscheme "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
2020-08-10 17:43:49 +00:00
kubeschedulerconfigv1beta1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1beta1"
2019-01-12 04:58:27 +00:00
)
func loadConfigFromFile(file string) (*kubeschedulerconfig.KubeSchedulerConfiguration, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return loadConfig(data)
}
func loadConfig(data []byte) (*kubeschedulerconfig.KubeSchedulerConfiguration, error) {
2019-12-12 01:27:03 +00:00
// The UniversalDecoder runs defaulting and returns the internal type by default.
2020-03-26 21:07:15 +00:00
obj, gvk, err := kubeschedulerscheme.Codecs.UniversalDecoder().Decode(data, nil, nil)
2019-12-12 01:27:03 +00:00
if err != nil {
2020-08-10 17:43:49 +00:00
return nil, err
2019-01-12 04:58:27 +00:00
}
2020-03-26 21:07:15 +00:00
if cfgObj, ok := obj.(*kubeschedulerconfig.KubeSchedulerConfiguration); ok {
return cfgObj, nil
}
return nil, fmt.Errorf("couldn't decode as KubeSchedulerConfiguration, got %s: ", gvk)
2019-01-12 04:58:27 +00:00
}
// LogOrWriteConfig logs the completed component config and writes it into the given file name as YAML, if either is enabled
func LogOrWriteConfig(fileName string, cfg *kubeschedulerconfig.KubeSchedulerConfiguration, completedProfiles []kubeschedulerconfig.KubeSchedulerProfile) error {
if !(klog.V(2).Enabled() || len(fileName) > 0) {
return nil
}
cfg.Profiles = completedProfiles
2019-08-30 18:33:25 +00:00
const mediaType = runtime.ContentTypeYAML
info, ok := runtime.SerializerInfoForMediaType(kubeschedulerscheme.Codecs.SupportedMediaTypes(), mediaType)
if !ok {
return fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
2019-01-12 04:58:27 +00:00
}
2019-08-30 18:33:25 +00:00
2020-08-10 17:43:49 +00:00
encoder := kubeschedulerscheme.Codecs.EncoderForVersion(info.Serializer, kubeschedulerconfigv1beta1.SchemeGroupVersion)
if klog.V(2).Enabled() {
bytes, err := runtime.Encode(encoder, cfg)
if err != nil {
return err
}
configString := string(bytes)
klog.Infof("Using component config:\n%+v\n", configString)
2019-01-12 04:58:27 +00:00
}
if len(fileName) > 0 {
configFile, err := os.Create(fileName)
if err != nil {
return err
}
defer configFile.Close()
if err := encoder.Encode(cfg, configFile); err != nil {
return err
}
klog.Infof("Wrote configuration to: %s\n", fileName)
os.Exit(0)
}
2019-01-12 04:58:27 +00:00
return nil
}