From c8be59d32bcac5cf1c66cf7e1c17fe4ae5d6ad38 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Fri, 28 Feb 2025 15:53:15 +0800 Subject: [PATCH] [kwokctl] Support:wq for getting default configuration from remote --- cmd/kwokctl/main.go | 9 +++- pkg/config/default_config.go | 85 ++++++++++++++++++++++++++++++++ pkg/consts/consts.go | 3 ++ site/static/config/0.7/kwok.yaml | 4 ++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 pkg/config/default_config.go create mode 100644 site/static/config/0.7/kwok.yaml diff --git a/cmd/kwokctl/main.go b/cmd/kwokctl/main.go index cafaf784a..e4dd67a79 100644 --- a/cmd/kwokctl/main.go +++ b/cmd/kwokctl/main.go @@ -40,7 +40,14 @@ func main() { ctx := signals.SetupSignalContext() ctx, logger := log.InitFlags(ctx, flagset) - ctx, err := config.InitFlags(ctx, flagset) + err := config.FetchDefaultConfig(ctx) + if err != nil { + logger.Warn("Fetch default config", + "err", err, + ) + } + + ctx, err = config.InitFlags(ctx, flagset) if err != nil { _, _ = os.Stderr.Write([]byte(flagset.FlagUsages())) logger.Error("Init config flags", err) diff --git a/pkg/config/default_config.go b/pkg/config/default_config.go new file mode 100644 index 000000000..e1eb5eea7 --- /dev/null +++ b/pkg/config/default_config.go @@ -0,0 +1,85 @@ +/* +Copyright 2025 The Kubernetes 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 ( + "context" + "fmt" + "io" + "net/http" + + "sigs.k8s.io/kwok/pkg/consts" + "sigs.k8s.io/kwok/pkg/log" + "sigs.k8s.io/kwok/pkg/utils/file" + "sigs.k8s.io/kwok/pkg/utils/path" + "sigs.k8s.io/kwok/pkg/utils/version" +) + +// FetchDefaultConfig fetches the default configuration file from the remote URL +// and saves it to the local file system if it doesn't already exist. +func FetchDefaultConfig(ctx context.Context) error { + fullPath := path.Join(WorkDir, consts.ConfigName) + if file.Exists(fullPath) { + return nil + } + + logger := log.FromContext(ctx) + + logger.Info("Fetch default config from remote", + "url", consts.KwokctlDefaultConfigURL, + "path", path.RelFromHome(fullPath), + ) + + err := file.MkdirAll(WorkDir) + if err != nil { + return err + } + + f, err := file.Open(fullPath) + if err != nil { + return err + } + defer func() { + _ = f.Close() + }() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, consts.KwokctlDefaultConfigURL, nil) + if err != nil { + return err + } + req.Header.Set("User-Agent", version.DefaultUserAgent()) + + cli := &http.Client{} + resp, err := cli.Do(req) + if err != nil { + return err + } + + defer func() { + _ = resp.Body.Close() + }() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("status code %d", resp.StatusCode) + } + + _, err = io.Copy(f, resp.Body) + if err != nil { + return err + } + return nil +} diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index b3d36d2c0..5ea47730e 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -28,6 +28,9 @@ var ( BinaryPrefix = "https://github.com/kubernetes-sigs/kwok/releases/download" ImagePrefix = "registry.k8s.io/kwok" + // KwokctlDefaultConfigURL is the URL from which kwokctl will fetch the default configuration when started for the first time + KwokctlDefaultConfigURL = "https://kwok.sigs.k8s.io/config/0.7/kwok.yaml" + // PreRelease is the pre-release version of the project. // It will be overwritten during the `make build` process. PreRelease = "alpha" diff --git a/site/static/config/0.7/kwok.yaml b/site/static/config/0.7/kwok.yaml new file mode 100644 index 000000000..9c5d71d19 --- /dev/null +++ b/site/static/config/0.7/kwok.yaml @@ -0,0 +1,4 @@ +apiVersion: config.kwok.x-k8s.io/v1alpha1 +kind: KwokctlConfiguration +options: + kubeVersion: v1.32.2