Skip to content

Commit 706554a

Browse files
committed
[kwokctl] Support for getting default configuration from remote
1 parent 08a2a0a commit 706554a

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-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

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
)
30+
31+
func FetchDefaultConfig(ctx context.Context) error {
32+
fullPath := path.Join(WorkDir, consts.ConfigName)
33+
if file.Exists(fullPath) {
34+
return nil
35+
}
36+
37+
logger := log.FromContext(ctx)
38+
39+
logger.Info("Fetch default config from remote",
40+
"url", consts.KwokctlDefaultConfigURL,
41+
"path", path.RelFromHome(fullPath),
42+
)
43+
44+
err := file.MkdirAll(WorkDir)
45+
if err != nil {
46+
return err
47+
}
48+
49+
f, err := file.Open(fullPath)
50+
if err != nil {
51+
return err
52+
}
53+
defer func() {
54+
_ = f.Close()
55+
}()
56+
57+
resp, err := http.Get(consts.KwokctlDefaultConfigURL)
58+
if err != nil {
59+
return err
60+
}
61+
defer func() {
62+
_ = resp.Body.Close()
63+
}()
64+
65+
if resp.StatusCode != http.StatusOK {
66+
return fmt.Errorf("status code %d", resp.StatusCode)
67+
}
68+
69+
_, err = io.Copy(f, resp.Body)
70+
if err != nil {
71+
return err
72+
}
73+
return nil
74+
}

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)