Skip to content

Commit 3bb9d67

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

File tree

4 files changed

+91
-1
lines changed

4 files changed

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

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

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)