Skip to content

Commit c8be59d

Browse files
committed
[kwokctl] Support:wq for getting default configuration from remote
1 parent 08a2a0a commit c8be59d

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

cmd/kwokctl/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ func main() {
4040
ctx := signals.SetupSignalContext()
4141
ctx, logger := log.InitFlags(ctx, flagset)
4242

43-
ctx, err := config.InitFlags(ctx, flagset)
43+
err := config.FetchDefaultConfig(ctx)
44+
if err != nil {
45+
logger.Warn("Fetch default config",
46+
"err", err,
47+
)
48+
}
49+
50+
ctx, err = config.InitFlags(ctx, flagset)
4451
if err != nil {
4552
_, _ = os.Stderr.Write([]byte(flagset.FlagUsages()))
4653
logger.Error("Init config flags", err)

pkg/config/default_config.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2025 The Kubernetes 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+
"context"
21+
"fmt"
22+
"io"
23+
"net/http"
24+
25+
"sigs.k8s.io/kwok/pkg/consts"
26+
"sigs.k8s.io/kwok/pkg/log"
27+
"sigs.k8s.io/kwok/pkg/utils/file"
28+
"sigs.k8s.io/kwok/pkg/utils/path"
29+
"sigs.k8s.io/kwok/pkg/utils/version"
30+
)
31+
32+
// FetchDefaultConfig fetches the default configuration file from the remote URL
33+
// and saves it to the local file system if it doesn't already exist.
34+
func FetchDefaultConfig(ctx context.Context) error {
35+
fullPath := path.Join(WorkDir, consts.ConfigName)
36+
if file.Exists(fullPath) {
37+
return nil
38+
}
39+
40+
logger := log.FromContext(ctx)
41+
42+
logger.Info("Fetch default config from remote",
43+
"url", consts.KwokctlDefaultConfigURL,
44+
"path", path.RelFromHome(fullPath),
45+
)
46+
47+
err := file.MkdirAll(WorkDir)
48+
if err != nil {
49+
return err
50+
}
51+
52+
f, err := file.Open(fullPath)
53+
if err != nil {
54+
return err
55+
}
56+
defer func() {
57+
_ = f.Close()
58+
}()
59+
60+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, consts.KwokctlDefaultConfigURL, nil)
61+
if err != nil {
62+
return err
63+
}
64+
req.Header.Set("User-Agent", version.DefaultUserAgent())
65+
66+
cli := &http.Client{}
67+
resp, err := cli.Do(req)
68+
if err != nil {
69+
return err
70+
}
71+
72+
defer func() {
73+
_ = resp.Body.Close()
74+
}()
75+
76+
if resp.StatusCode != http.StatusOK {
77+
return fmt.Errorf("status code %d", resp.StatusCode)
78+
}
79+
80+
_, err = io.Copy(f, resp.Body)
81+
if err != nil {
82+
return err
83+
}
84+
return nil
85+
}

pkg/consts/consts.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
BinaryPrefix = "https://github.com/kubernetes-sigs/kwok/releases/download"
2929
ImagePrefix = "registry.k8s.io/kwok"
3030

31+
// KwokctlDefaultConfigURL is the URL from which kwokctl will fetch the default configuration when started for the first time
32+
KwokctlDefaultConfigURL = "https://kwok.sigs.k8s.io/config/0.7/kwok.yaml"
33+
3134
// PreRelease is the pre-release version of the project.
3235
// It will be overwritten during the `make build` process.
3336
PreRelease = "alpha"

site/static/config/0.7/kwok.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: config.kwok.x-k8s.io/v1alpha1
2+
kind: KwokctlConfiguration
3+
options:
4+
kubeVersion: v1.32.2

0 commit comments

Comments
 (0)