Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: karmadactl init supports deployment through configuration files #5357

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: run karmadactl init test
run: |
export CLUSTER_VERSION=kindest/node:${{ matrix.k8s }}
Expand All @@ -48,7 +47,7 @@ jobs:
export KUBECONFIG=${HOME}/karmada/karmada-apiserver.config
GO111MODULE=on go install github.com/onsi/ginkgo/v2/ginkgo
ginkgo -v --race --trace -p --focus="[BasicPropagation] propagation testing deployment propagation testing" ./test/e2e/
- name: export logs
- name: export logs
if: always()
run: |
export ARTIFACTS_PATH=${{ github.workspace }}/karmadactl-test-logs/${{ matrix.k8s }}/
Expand Down
1 change: 1 addition & 0 deletions hack/verify-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ missing_license_header_files="$($ADDLICENSE_BIN \
-ignore "**/*.yml" \
-ignore "**/*.json" \
-ignore ".idea/**" \
-ignore ".git/**"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiansuo114 Can you help to recall why need to ignore .git here?

PS: I'm exploring the reason why the license check becomes inactive, and seems this line breaks the functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my detailed response under #5984 (comment), the idea is that files under the .git/ directory, similar to those under .github/, .idea/, and other paths, should not be subject to license checks. When I ran the related check scripts locally, I encountered issues related to this, so I added path ignore rules to the script.

.)" || true

if [[ "$missing_license_header_files" ]]; then
Expand Down
7 changes: 6 additions & 1 deletion pkg/karmadactl/cmdinit/cmdinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ var (
%[1]s init --karmada-apiserver-replicas 3 --etcd-replicas 3 --etcd-storage-mode PVC --storage-classes-name {StorageClassesName}

# Specify external IPs(load balancer or HA IP) which used to sign the certificate
%[1]s init --cert-external-ip 10.235.1.2 --cert-external-dns www.karmada.io`)
%[1]s init --cert-external-ip 10.235.1.2 --cert-external-dns www.karmada.io

# Install Karmada using a configuration file
%[1]s init --config /path/to/your/config/file.yaml`)
)

// NewCmdInit install Karmada on Kubernetes
Expand Down Expand Up @@ -149,6 +152,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")
RainbowMango marked this conversation as resolved.
Show resolved Hide resolved
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 +170,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
106 changes: 106 additions & 0 deletions pkg/karmadactl/cmdinit/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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"

"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) (*KarmadaInitConfig, error) {
var config *KarmadaInitConfig
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) (*KarmadaInitConfig, 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)
}
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) {
tiansuo114 marked this conversation as resolved.
Show resolved Hide resolved
gvkmap := make(map[schema.GroupVersionKind][]byte)

gvk, err := yamlserializer.DefaultMetaFactory.Interpret(yamlBytes)
if err != nil {
return nil, fmt.Errorf("failed to interpret YAML document: %w", err)
}
if len(gvk.Group) == 0 || len(gvk.Version) == 0 || len(gvk.Kind) == 0 {
return nil, fmt.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, checking for the "InitConfiguration" kind, group, and version, and unmarshals its content into an InitConfiguration object.
func documentMapToInitConfiguration(gvkmap map[schema.GroupVersionKind][]byte) (*KarmadaInitConfig, error) {
var initcfg *KarmadaInitConfig

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 == "KarmadaInitConfig" {
if gvk.Group != GroupName || gvk.Version != SchemeGroupVersion.Version {
return nil, fmt.Errorf("invalid Group or Version: expected group %q and version %q, but got group %q and version %q", GroupName, SchemeGroupVersion.Version, gvk.Group, gvk.Version)
}
initcfg = &KarmadaInitConfig{}
if err := yaml.Unmarshal(fileContent, initcfg); err != nil {
return nil, err
}
}
}

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

return initcfg, nil
}
Loading