|
| 1 | +/* |
| 2 | +Copyright 2022 The Koordinator 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 options |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "os" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/klog/v2" |
| 27 | + |
| 28 | + deschedulerconfig "github.com/koordinator-sh/koordinator/pkg/descheduler/apis/config" |
| 29 | + deschedulerconfigscheme "github.com/koordinator-sh/koordinator/pkg/descheduler/apis/config/scheme" |
| 30 | + "github.com/koordinator-sh/koordinator/pkg/descheduler/apis/config/v1alpha2" |
| 31 | +) |
| 32 | + |
| 33 | +func loadConfigFromFile(file string) (*deschedulerconfig.DeschedulerConfiguration, error) { |
| 34 | + data, err := os.ReadFile(file) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + return loadConfig(data) |
| 40 | +} |
| 41 | + |
| 42 | +func loadConfig(data []byte) (*deschedulerconfig.DeschedulerConfiguration, error) { |
| 43 | + // The UniversalDecoder runs defaulting and returns the internal type by default. |
| 44 | + obj, gvk, err := deschedulerconfigscheme.Codecs.UniversalDecoder().Decode(data, nil, nil) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + if cfgObj, ok := obj.(*deschedulerconfig.DeschedulerConfiguration); ok { |
| 49 | + // the field will be cleared later by API machinery during |
| 50 | + // conversion. See DeschedulerConfiguration internal type definition for |
| 51 | + // more details. |
| 52 | + cfgObj.TypeMeta.APIVersion = gvk.GroupVersion().String() |
| 53 | + return cfgObj, nil |
| 54 | + } |
| 55 | + return nil, fmt.Errorf("couldn't decode as DeschedulerConfiguration, got %s: ", gvk) |
| 56 | +} |
| 57 | + |
| 58 | +func encodeConfig(cfg *deschedulerconfig.DeschedulerConfiguration) (*bytes.Buffer, error) { |
| 59 | + buf := new(bytes.Buffer) |
| 60 | + const mediaType = runtime.ContentTypeYAML |
| 61 | + info, ok := runtime.SerializerInfoForMediaType(deschedulerconfigscheme.Codecs.SupportedMediaTypes(), mediaType) |
| 62 | + if !ok { |
| 63 | + return buf, fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType) |
| 64 | + } |
| 65 | + |
| 66 | + var encoder runtime.Encoder |
| 67 | + switch cfg.TypeMeta.APIVersion { |
| 68 | + case v1alpha2.SchemeGroupVersion.String(): |
| 69 | + encoder = deschedulerconfigscheme.Codecs.EncoderForVersion(info.Serializer, v1alpha2.SchemeGroupVersion) |
| 70 | + default: |
| 71 | + encoder = deschedulerconfigscheme.Codecs.EncoderForVersion(info.Serializer, v1alpha2.SchemeGroupVersion) |
| 72 | + } |
| 73 | + if err := encoder.Encode(cfg, buf); err != nil { |
| 74 | + return buf, err |
| 75 | + } |
| 76 | + return buf, nil |
| 77 | +} |
| 78 | + |
| 79 | +// LogOrWriteConfig logs the completed component config and writes it into the given file name as YAML, if either is enabled |
| 80 | +func LogOrWriteConfig(fileName string, cfg *deschedulerconfig.DeschedulerConfiguration, completedProfiles []deschedulerconfig.DeschedulerProfile) error { |
| 81 | + klogV := klog.V(2) |
| 82 | + if !klogV.Enabled() && len(fileName) == 0 { |
| 83 | + return nil |
| 84 | + } |
| 85 | + cfg.Profiles = completedProfiles |
| 86 | + |
| 87 | + buf, err := encodeConfig(cfg) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + if klogV.Enabled() { |
| 93 | + klogV.InfoS("Using component config", "config", buf.String()) |
| 94 | + } |
| 95 | + |
| 96 | + if len(fileName) > 0 { |
| 97 | + configFile, err := os.Create(fileName) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + defer configFile.Close() |
| 102 | + if _, err := io.Copy(configFile, buf); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + klog.InfoS("Wrote configuration", "file", fileName) |
| 106 | + os.Exit(0) |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
0 commit comments