Skip to content

Commit

Permalink
[kwokctl] Support for getting default configuration from remote
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Feb 28, 2025
1 parent 08a2a0a commit 706554a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/kwokctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions pkg/config/default_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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"
)

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()
}()

resp, err := http.Get(consts.KwokctlDefaultConfigURL)
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
}
3 changes: 3 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions site/static/config/0.7/kwok.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: config.kwok.x-k8s.io/v1alpha1
kind: KwokctlConfiguration
options:
kubeVersion: v1.32.2

0 comments on commit 706554a

Please sign in to comment.