|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada 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 | + "os" |
| 22 | + "sort" |
| 23 | + |
| 24 | + "github.com/pkg/errors" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + yamlserializer "k8s.io/apimachinery/pkg/runtime/serializer/yaml" |
| 27 | + "k8s.io/apimachinery/pkg/util/yaml" |
| 28 | + "k8s.io/klog/v2" |
| 29 | +) |
| 30 | + |
| 31 | +// LoadInitConfiguration loads the InitConfiguration from the specified file path. |
| 32 | +// It delegates the actual loading to the loadInitConfigurationFromFile function. |
| 33 | +func LoadInitConfiguration(cfgPath string) (*KarmadaInitConfig, error) { |
| 34 | + var config *KarmadaInitConfig |
| 35 | + var err error |
| 36 | + |
| 37 | + config, err = loadInitConfigurationFromFile(cfgPath) |
| 38 | + |
| 39 | + return config, err |
| 40 | +} |
| 41 | + |
| 42 | +// loadInitConfigurationFromFile reads the file at the specified path and converts it into an InitConfiguration. |
| 43 | +// It reads the file contents and then converts the bytes to an InitConfiguration. |
| 44 | +func loadInitConfigurationFromFile(cfgPath string) (*KarmadaInitConfig, error) { |
| 45 | + klog.V(1).Infof("loading configuration from %q", cfgPath) |
| 46 | + |
| 47 | + b, err := os.ReadFile(cfgPath) |
| 48 | + if err != nil { |
| 49 | + return nil, fmt.Errorf("unable to read config from %q: %v", cfgPath, err) |
| 50 | + } |
| 51 | + |
| 52 | + return BytesToInitConfiguration(b) |
| 53 | +} |
| 54 | + |
| 55 | +// BytesToInitConfiguration parses the given byte slice into an InitConfiguration. |
| 56 | +// It first converts the bytes to a map of GroupVersionKind to bytes, then processes this map to extract the InitConfiguration. |
| 57 | +func BytesToInitConfiguration(b []byte) (*KarmadaInitConfig, error) { |
| 58 | + gvkmap, err := ParseGVKYamlMap(b) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + return documentMapToInitConfiguration(gvkmap) |
| 64 | +} |
| 65 | + |
| 66 | +// ParseGVKYamlMap parses a single YAML document into a map of GroupVersionKind to byte slices. |
| 67 | +// This function is a simplified version that handles only a single YAML document. |
| 68 | +func ParseGVKYamlMap(yamlBytes []byte) (map[schema.GroupVersionKind][]byte, error) { |
| 69 | + gvkmap := make(map[schema.GroupVersionKind][]byte) |
| 70 | + |
| 71 | + gvk, err := yamlserializer.DefaultMetaFactory.Interpret(yamlBytes) |
| 72 | + if err != nil { |
| 73 | + return nil, errors.Wrap(err, "failed to interpret YAML document") |
| 74 | + } |
| 75 | + if len(gvk.Group) == 0 || len(gvk.Version) == 0 || len(gvk.Kind) == 0 { |
| 76 | + return nil, errors.Errorf("invalid configuration for GroupVersionKind %+v: kind and apiVersion is mandatory information that must be specified", gvk) |
| 77 | + } |
| 78 | + gvkmap[*gvk] = yamlBytes |
| 79 | + |
| 80 | + return gvkmap, nil |
| 81 | +} |
| 82 | + |
| 83 | +// documentMapToInitConfiguration processes a map of GroupVersionKind to byte slices to extract the InitConfiguration. |
| 84 | +// It iterates over the map, looking for the "InitConfiguration" kind and unmarshals its content into an InitConfiguration object. |
| 85 | +func documentMapToInitConfiguration(gvkmap map[schema.GroupVersionKind][]byte) (*KarmadaInitConfig, error) { |
| 86 | + var initcfg *KarmadaInitConfig |
| 87 | + |
| 88 | + gvks := make([]schema.GroupVersionKind, 0, len(gvkmap)) |
| 89 | + for gvk := range gvkmap { |
| 90 | + gvks = append(gvks, gvk) |
| 91 | + } |
| 92 | + sort.Slice(gvks, func(i, j int) bool { |
| 93 | + return gvks[i].String() < gvks[j].String() |
| 94 | + }) |
| 95 | + |
| 96 | + for _, gvk := range gvks { |
| 97 | + fileContent := gvkmap[gvk] |
| 98 | + if gvk.Kind == "KarmadaInitConfig" { |
| 99 | + initcfg = &KarmadaInitConfig{} |
| 100 | + if err := yaml.Unmarshal(fileContent, initcfg); err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if initcfg == nil { |
| 107 | + return nil, fmt.Errorf("no KarmadaInitConfig kind was found in the YAML file") |
| 108 | + } |
| 109 | + |
| 110 | + return initcfg, nil |
| 111 | +} |
0 commit comments