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 (
|
2021-07-02 08:43:15 +00:00
|
|
|
"bytes"
|
2019-08-30 18:33:25 +00:00
|
|
|
"fmt"
|
2021-07-02 08:43:15 +00:00
|
|
|
"io"
|
2019-01-12 04:58:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2021-03-18 22:40:29 +00:00
|
|
|
"k8s.io/klog/v2"
|
2021-07-02 08:43:15 +00:00
|
|
|
"k8s.io/kubernetes/pkg/scheduler/apis/config"
|
|
|
|
"k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
|
|
|
|
configv1beta1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1beta1"
|
|
|
|
configv1beta2 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1beta2"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func loadConfigFromFile(file string) (*config.KubeSchedulerConfiguration, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
data, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadConfig(data)
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func loadConfig(data []byte) (*config.KubeSchedulerConfiguration, error) {
|
2019-12-12 01:27:03 +00:00
|
|
|
// The UniversalDecoder runs defaulting and returns the internal type by default.
|
2021-07-02 08:43:15 +00:00
|
|
|
obj, gvk, err := scheme.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
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
if cfgObj, ok := obj.(*config.KubeSchedulerConfiguration); ok {
|
|
|
|
// We don't set this field in pkg/scheduler/apis/config/{version}/conversion.go
|
|
|
|
// because the field will be cleared later by API machinery during
|
|
|
|
// conversion. See KubeSchedulerConfiguration internal type definition for
|
|
|
|
// more details.
|
|
|
|
cfgObj.TypeMeta.APIVersion = gvk.GroupVersion().String()
|
2020-03-26 21:07:15 +00:00
|
|
|
return cfgObj, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("couldn't decode as KubeSchedulerConfiguration, got %s: ", gvk)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func encodeConfig(cfg *config.KubeSchedulerConfiguration) (*bytes.Buffer, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
const mediaType = runtime.ContentTypeYAML
|
|
|
|
info, ok := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), mediaType)
|
|
|
|
if !ok {
|
|
|
|
return buf, fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
var encoder runtime.Encoder
|
|
|
|
switch cfg.TypeMeta.APIVersion {
|
|
|
|
case configv1beta1.SchemeGroupVersion.String():
|
|
|
|
encoder = scheme.Codecs.EncoderForVersion(info.Serializer, configv1beta1.SchemeGroupVersion)
|
|
|
|
case configv1beta2.SchemeGroupVersion.String():
|
|
|
|
encoder = scheme.Codecs.EncoderForVersion(info.Serializer, configv1beta2.SchemeGroupVersion)
|
|
|
|
default:
|
|
|
|
encoder = scheme.Codecs.EncoderForVersion(info.Serializer, configv1beta2.SchemeGroupVersion)
|
|
|
|
}
|
|
|
|
if err := encoder.Encode(cfg, buf); err != nil {
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
// LogOrWriteConfig logs the completed component config and writes it into the given file name as YAML, if either is enabled
|
2021-07-02 08:43:15 +00:00
|
|
|
func LogOrWriteConfig(fileName string, cfg *config.KubeSchedulerConfiguration, completedProfiles []config.KubeSchedulerProfile) error {
|
2020-12-01 01:06:26 +00:00
|
|
|
if !(klog.V(2).Enabled() || len(fileName) > 0) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cfg.Profiles = completedProfiles
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
buf, err := encodeConfig(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
if klog.V(2).Enabled() {
|
2021-07-02 08:43:15 +00:00
|
|
|
klog.InfoS("Using component config", "config", buf.String())
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
if len(fileName) > 0 {
|
|
|
|
configFile, err := os.Create(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
2021-07-02 08:43:15 +00:00
|
|
|
if _, err := io.Copy(configFile, buf); err != nil {
|
2020-12-01 01:06:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
klog.InfoS("Wrote configuration", "file", fileName)
|
2020-12-01 01:06:26 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|