Skip to content

Commit

Permalink
Support windows binary
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jul 5, 2023
1 parent cc9c0ad commit e7e038f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 30 deletions.
57 changes: 38 additions & 19 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,32 @@ jobs:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
include:
# Windows
# Linux containers are not supported on Windows Runner
# https://github.com/orgs/community/discussions/25491#discussioncomment-3248089
# - windows-latest
kwokctl-runtime:
- binary
- docker
- podman
- kind
- kind-podman
exclude:
- os: windows-latest
kwokctl-runtime: binary

# MacOS
- os: macos-latest
kwokctl-runtime: podman
kwokctl-runtime: binary
- os: macos-latest
kwokctl-runtime: docker
- os: macos-latest
kwokctl-runtime: kind

# Linux
- os: ubuntu-latest
kwokctl-runtime: binary
- os: ubuntu-latest
kwokctl-runtime: docker
- os: ubuntu-latest
kwokctl-runtime: podman
- os: ubuntu-latest
kwokctl-runtime: kind
- os: ubuntu-latest
kwokctl-runtime: kind-podman
include:
# nerdctl is still very early and has many bugs and differences in docker behavior,
# so we need to verify that it works on every release.
- os: ubuntu-latest
Expand Down Expand Up @@ -158,22 +166,33 @@ jobs:
# https://www.downloadkubernetes.com
- name: Download Kubernetes Source Code
uses: actions/checkout@v3
if: ${{ matrix.kwokctl-runtime == 'binary' && matrix.os == 'macos-latest' }}
if: ${{ matrix.kwokctl-runtime == 'binary' && ( matrix.os == 'macos-latest' || matrix.os == 'windows-latest' ) }}
with:
repository: kubernetes/kubernetes
path: kubernetes
ref: v1.27.3
- name: Build Kubernetes Binary
- name: Build Kubernetes Binary for Windows
if: ${{ matrix.kwokctl-runtime == 'binary' && matrix.os == 'windows-latest' }}
shell: bash
run: |
export GOBIN="$(go env GOPATH)/bin"
cd "${GITHUB_WORKSPACE}/kubernetes" && go install ./cmd/{kube-apiserver,kube-controller-manager,kube-scheduler}
mkdir -p ~/.kwok && cat > ~/.kwok/kwok.yaml << EOF
kind: KwokctlConfiguration
apiVersion: config.kwok.x-k8s.io/v1alpha1
options:
kubeBinaryPrefix: '${GOBIN//\\//}'
EOF
- name: Build Kubernetes Binary for MacOS
if: ${{ matrix.kwokctl-runtime == 'binary' && matrix.os == 'macos-latest' }}
shell: bash
run: |
cd "${GITHUB_WORKSPACE}/kubernetes" && make WHAT="cmd/kube-apiserver cmd/kube-controller-manager cmd/kube-scheduler"
mkdir -p ~/.kwok
cat > ~/.kwok/kwok.yaml << EOF
mkdir -p ~/.kwok && cat > ~/.kwok/kwok.yaml << EOF
kind: KwokctlConfiguration
apiVersion: config.kwok.x-k8s.io/v1alpha1
options:
kubeBinaryPrefix: "${GITHUB_WORKSPACE}/kubernetes/_output/bin"
kubeBinaryPrefix: '${GITHUB_WORKSPACE}/kubernetes/_output/bin'
EOF
# TODO: workaround for https://github.com/actions/runner-images/issues/7753 (caused by https://bugs.launchpad.net/ubuntu/+source/libpod/+bug/2024394).
Expand Down Expand Up @@ -228,7 +247,7 @@ jobs:
sudo systemctl daemon-reload
- name: Make pki directory
if: ${{ matrix.kwokctl-runtime == 'binary' }}
if: ${{ matrix.kwokctl-runtime == 'binary' && matrix.os != 'windows-latest' }}
shell: bash
run: |
sudo mkdir -p /var/run/kubernetes
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func loadRaw(r io.Reader) ([]json.RawMessage, error) {
if errors.Is(err, io.EOF) {
break
}
return nil, err
return nil, fmt.Errorf("failed to decode %q: %w", raw, err)
}
if len(raw) == 0 {
// Ignore empty documents
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwokctl/runtime/binary/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func (c *Cluster) Logs(ctx context.Context, name string, out io.Writer) error {

f, err := os.OpenFile(logs, os.O_RDONLY, 0640)
if err != nil {
return err
return fmt.Errorf("failed to open %s: %w", logs, err)
}
defer func() {
err = f.Close()
Expand Down
11 changes: 5 additions & 6 deletions pkg/kwokctl/runtime/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

Expand All @@ -36,7 +35,7 @@ import (
// ForkExec forks a new process and execs the given command.
// The process will be terminated when the context is canceled.
func (c *Cluster) ForkExec(ctx context.Context, dir string, name string, args ...string) error {
pidPath := path.Join(dir, "pids", filepath.Base(name)+".pid")
pidPath := path.Join(dir, "pids", path.OnlyName(name)+".pid")
if file.Exists(pidPath) {
pidData, err := os.ReadFile(pidPath)
if err == nil {
Expand All @@ -50,7 +49,7 @@ func (c *Cluster) ForkExec(ctx context.Context, dir string, name string, args ..
}
ctx = exec.WithDir(ctx, dir)
ctx = exec.WithFork(ctx, true)
logPath := path.Join(dir, "logs", filepath.Base(name)+".log")
logPath := path.Join(dir, "logs", path.OnlyName(name)+".log")
logFile, err := c.OpenFile(logPath)
if err != nil {
return fmt.Errorf("open log file %s: %w", logPath, err)
Expand Down Expand Up @@ -80,7 +79,7 @@ func (c *Cluster) ForkExec(ctx context.Context, dir string, name string, args ..

// ForkExecKill kills the process if it is running.
func (c *Cluster) ForkExecKill(ctx context.Context, dir string, name string) error {
pidPath := path.Join(dir, "pids", filepath.Base(name)+".pid")
pidPath := path.Join(dir, "pids", path.OnlyName(name)+".pid")
if !file.Exists(pidPath) {
// No pid file exists, which means the process has been terminated
logger := log.FromContext(ctx)
Expand Down Expand Up @@ -116,7 +115,7 @@ func (c *Cluster) ForkExecKill(ctx context.Context, dir string, name string) err

// ForkExecIsRunning checks if the process is running.
func (c *Cluster) ForkExecIsRunning(ctx context.Context, dir string, name string) bool {
pidPath := path.Join(dir, "pids", filepath.Base(name)+".pid")
pidPath := path.Join(dir, "pids", path.OnlyName(name)+".pid")
if !file.Exists(pidPath) {
logger := log.FromContext(ctx)
logger.Debug("Stat file not exists",
Expand Down Expand Up @@ -206,7 +205,7 @@ func FormatExec(ctx context.Context, name string, args ...string) string {
_, _ = fmt.Fprintf(out, "%s ", strings.Join(opt.Env, " "))
}

_, _ = fmt.Fprintf(out, "%s", path.Base(name))
_, _ = fmt.Fprintf(out, "%s", path.OnlyName(name))

for _, arg := range args {
_, _ = fmt.Fprintf(out, " %s", arg)
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/file/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func DownloadWithCacheAndExtract(ctx context.Context, cacheDir, src, dest string
return err
}
err = untar(ctx, cacheTar, func(file string) (string, bool) {
if path.Base(file) == match {
if path.OnlyName(file) == match {
return cache, true
}
return "", false
Expand Down
18 changes: 16 additions & 2 deletions pkg/utils/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func Expand(path string) (string, error) {
}
}

return filepath.Abs(path)
p, err := filepath.Abs(path)
if err != nil {
return "", err
}
return Clean(p), nil
}

// RelFromHome returns a path relative to the home directory.
Expand All @@ -89,10 +93,20 @@ func Join(elem ...string) string {

// Dir is a wrapper around filepath.Dir.
func Dir(path string) string {
return filepath.Dir(path)
return Clean(filepath.Dir(path))
}

// Base is a wrapper around filepath.Base.
func Base(path string) string {
return filepath.Base(path)
}

// Ext is a wrapper around filepath.Ext.
func Ext(path string) string {
return filepath.Ext(path)
}

// OnlyName returns the file name without extension.
func OnlyName(path string) string {
return strings.TrimSuffix(Base(path), Ext(path))
}
6 changes: 6 additions & 0 deletions pkg/utils/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func TestParseFromOutput(t *testing.T) {
},
want: semver.MustParse("1.26.0"),
},
{
args: args{
s: "Kubernetes v0.0.0-master+$Format:%H$",
},
want: semver.MustParse("255.0.0"),
},
{
args: args{
s: "prometheus, version 2.35.0 (branch: HEAD)",
Expand Down
6 changes: 6 additions & 0 deletions test/kwokctl/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ LOCAL_PATH="${ROOT_DIR}/bin/${GOOS}/${GOARCH}"

export KWOK_CONTROLLER_BINARY="${LOCAL_PATH}/kwok"
export KWOKCTL_CONTROLLER_BINARY="${LOCAL_PATH}/kwokctl"

if [[ "${GOOS}" == "windows" ]]; then
KWOK_CONTROLLER_BINARY="${KWOK_CONTROLLER_BINARY}.exe"
KWOKCTL_CONTROLLER_BINARY="${KWOKCTL_CONTROLLER_BINARY}.exe"
fi

export KWOK_CONTROLLER_IMAGE="localhost/kwok:${VERSION}"
export PATH="${LOCAL_PATH}:${PATH}"

Expand Down

0 comments on commit e7e038f

Please sign in to comment.