Skip to content

Commit

Permalink
Support multiple label selection ability in EtcdNodeSelectorLabels
Browse files Browse the repository at this point in the history
Signed-off-by: tiansuo114 <1729765480@qq.com>

fix ci

Signed-off-by: tiansuo114 <1729765480@qq.com>

add unit test

Signed-off-by: tiansuo114 <1729765480@qq.com>

change unit test

Signed-off-by: tiansuo114 <1729765480@qq.com>

rm mapsEqual

Signed-off-by: tiansuo114 <1729765480@qq.com>

Added the ability to read configurations from configuration

Signed-off-by: tiansuo114 <1729765480@qq.com>

Add comment

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix

Signed-off-by: tiansuo114 <1729765480@qq.com>

add vendor

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix ci

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix ci

Signed-off-by: tiansuo114 <1729765480@qq.com>

add license

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix

Signed-off-by: tiansuo114 <1729765480@qq.com>

fix

Signed-off-by: tiansuo114 <1729765480@qq.com>
  • Loading branch information
tiansuo114 committed Sep 21, 2024
1 parent 21c18eb commit f4e06e6
Show file tree
Hide file tree
Showing 10 changed files with 986 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.1
github.com/opensearch-project/opensearch-go v1.1.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -132,7 +133,6 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/karmadactl/cmdinit/cmdinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
flags.StringVar(&opts.ExternalEtcdKeyPrefix, "external-etcd-key-prefix", "", "The key prefix to be configured to kube-apiserver through --etcd-prefix.")
// karmada
flags.StringVar(&opts.CRDs, "crds", kubernetes.DefaultCrdURL, "Karmada crds resource.(local file e.g. --crds /root/crds.tar.gz)")
flags.StringVar(&opts.KarmadaInitFilePath, "config", "", "Karmada init file path")
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.")
flags.Int32VarP(&opts.KarmadaAPIServerNodePort, "port", "p", 32443, "Karmada apiserver service node port")
flags.StringVarP(&opts.KarmadaDataPath, "karmada-data", "d", "/etc/karmada", "Karmada data path. kubeconfig cert and crds files")
Expand All @@ -166,6 +167,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
flags.StringVarP(&opts.KarmadaAggregatedAPIServerImage, "karmada-aggregated-apiserver-image", "", kubernetes.DefaultKarmadaAggregatedAPIServerImage, "Karmada aggregated apiserver image")
flags.Int32VarP(&opts.KarmadaAggregatedAPIServerReplicas, "karmada-aggregated-apiserver-replicas", "", 1, "Karmada aggregated apiserver replica set")
flags.IntVarP(&opts.WaitComponentReadyTimeout, "wait-component-ready-timeout", "", cmdinitoptions.WaitComponentReadyTimeout, "Wait for karmada component ready timeout. 0 means wait forever")

return cmd
}

Expand Down
111 changes: 111 additions & 0 deletions pkg/karmadactl/cmdinit/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2024 The Karmada 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 config

import (
"fmt"
"os"
"sort"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
yamlserializer "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/klog/v2"
)

// LoadInitConfiguration loads the InitConfiguration from the specified file path.
// It delegates the actual loading to the loadInitConfigurationFromFile function.
func LoadInitConfiguration(cfgPath string) (*InitConfiguration, error) {
var config *InitConfiguration
var err error

config, err = loadInitConfigurationFromFile(cfgPath)

return config, err
}

// loadInitConfigurationFromFile reads the file at the specified path and converts it into an InitConfiguration.
// It reads the file contents and then converts the bytes to an InitConfiguration.
func loadInitConfigurationFromFile(cfgPath string) (*InitConfiguration, error) {
klog.V(1).Infof("loading configuration from %q", cfgPath)

b, err := os.ReadFile(cfgPath)
if err != nil {
return nil, fmt.Errorf("unable to read config from %q: %v", cfgPath, err)
}

return BytesToInitConfiguration(b)
}

// BytesToInitConfiguration parses the given byte slice into an InitConfiguration.
// It first converts the bytes to a map of GroupVersionKind to bytes, then processes this map to extract the InitConfiguration.
func BytesToInitConfiguration(b []byte) (*InitConfiguration, error) {
gvkmap, err := ParseGVKYamlMap(b)
if err != nil {
return nil, err
}

return documentMapToInitConfiguration(gvkmap)
}

// ParseGVKYamlMap parses a single YAML document into a map of GroupVersionKind to byte slices.
// This function is a simplified version that handles only a single YAML document.
func ParseGVKYamlMap(yamlBytes []byte) (map[schema.GroupVersionKind][]byte, error) {
gvkmap := make(map[schema.GroupVersionKind][]byte)

gvk, err := yamlserializer.DefaultMetaFactory.Interpret(yamlBytes)
if err != nil {
return nil, errors.Wrap(err, "failed to interpret YAML document")
}
if len(gvk.Group) == 0 || len(gvk.Version) == 0 || len(gvk.Kind) == 0 {
return nil, errors.Errorf("invalid configuration for GroupVersionKind %+v: kind and apiVersion is mandatory information that must be specified", gvk)
}
gvkmap[*gvk] = yamlBytes

return gvkmap, nil
}

// documentMapToInitConfiguration processes a map of GroupVersionKind to byte slices to extract the InitConfiguration.
// It iterates over the map, looking for the "InitConfiguration" kind and unmarshals its content into an InitConfiguration object.
func documentMapToInitConfiguration(gvkmap map[schema.GroupVersionKind][]byte) (*InitConfiguration, error) {
var initcfg *InitConfiguration

gvks := make([]schema.GroupVersionKind, 0, len(gvkmap))
for gvk := range gvkmap {
gvks = append(gvks, gvk)
}
sort.Slice(gvks, func(i, j int) bool {
return gvks[i].String() < gvks[j].String()
})

for _, gvk := range gvks {
fileContent := gvkmap[gvk]
if gvk.Kind == "InitConfiguration" {
initcfg = &InitConfiguration{}
if err := yaml.Unmarshal(fileContent, initcfg); err != nil {
return nil, err
}
}
}

if initcfg == nil {
return nil, fmt.Errorf("no InitConfiguration kind was found in the YAML file")
}

return initcfg, nil
}
Loading

0 comments on commit f4e06e6

Please sign in to comment.