Skip to content

Commit cfc7bd0

Browse files
committed
Support multiple label selection ability in EtcdNodeSelectorLabels
Signed-off-by: tiansuo114 <[email protected]>
1 parent 21467f8 commit cfc7bd0

File tree

11 files changed

+1339
-3
lines changed

11 files changed

+1339
-3
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/onsi/ginkgo/v2 v2.17.2
1919
github.com/onsi/gomega v1.33.1
2020
github.com/opensearch-project/opensearch-go v1.1.0
21+
github.com/pkg/errors v0.9.1
2122
github.com/prometheus/client_golang v1.18.0
2223
github.com/spf13/cobra v1.8.0
2324
github.com/spf13/pflag v1.0.5
@@ -132,7 +133,6 @@ require (
132133
github.com/pelletier/go-toml v1.9.5 // indirect
133134
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
134135
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
135-
github.com/pkg/errors v0.9.1 // indirect
136136
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
137137
github.com/prometheus/client_model v0.5.0 // indirect
138138
github.com/prometheus/common v0.46.0 // indirect

hack/verify-license.sh

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ missing_license_header_files="$($ADDLICENSE_BIN \
4040
-ignore "**/*.yml" \
4141
-ignore "**/*.json" \
4242
-ignore ".idea/**" \
43+
-ignore ".git/**"
4344
.)" || true
4445

4546
if [[ "$missing_license_header_files" ]]; then

pkg/karmadactl/cmdinit/cmdinit.go

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
149149
flags.StringVar(&opts.ExternalEtcdKeyPrefix, "external-etcd-key-prefix", "", "The key prefix to be configured to kube-apiserver through --etcd-prefix.")
150150
// karmada
151151
flags.StringVar(&opts.CRDs, "crds", kubernetes.DefaultCrdURL, "Karmada crds resource.(local file e.g. --crds /root/crds.tar.gz)")
152+
flags.StringVar(&opts.KarmadaInitFilePath, "config", "", "Karmada init file path")
152153
flags.StringVarP(&opts.KarmadaAPIServerAdvertiseAddress, "karmada-apiserver-advertise-address", "", "", "The IP address the Karmada API Server will advertise it's listening on. If not set, the address on the master node will be used.")
153154
flags.Int32VarP(&opts.KarmadaAPIServerNodePort, "port", "p", 32443, "Karmada apiserver service node port")
154155
flags.StringVarP(&opts.KarmadaDataPath, "karmada-data", "d", "/etc/karmada", "Karmada data path. kubeconfig cert and crds files")
@@ -166,6 +167,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
166167
flags.StringVarP(&opts.KarmadaAggregatedAPIServerImage, "karmada-aggregated-apiserver-image", "", kubernetes.DefaultKarmadaAggregatedAPIServerImage, "Karmada aggregated apiserver image")
167168
flags.Int32VarP(&opts.KarmadaAggregatedAPIServerReplicas, "karmada-aggregated-apiserver-replicas", "", 1, "Karmada aggregated apiserver replica set")
168169
flags.IntVarP(&opts.WaitComponentReadyTimeout, "wait-component-ready-timeout", "", cmdinitoptions.WaitComponentReadyTimeout, "Wait for karmada component ready timeout. 0 means wait forever")
170+
169171
return cmd
170172
}
171173

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)