From 3401b36268cf7fec3815ed7e0c527f138a0061a3 Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Tue, 25 Feb 2025 11:44:14 -0500 Subject: [PATCH] contrib: buildable on other platforms (darwin/wins) Just make it buildable on other platforms. Signed-off-by: Wei Fu --- .../internal/mountns/{ns.go => ns_linux.go} | 2 + contrib/internal/mountns/ns_other.go | 12 +++++ contrib/utils/kubectl_cmd.go | 42 --------------- contrib/utils/kubectl_cmd_linux.go | 54 +++++++++++++++++++ contrib/utils/kubectl_cmd_other.go | 17 ++++++ contrib/utils/utils.go | 8 +-- contrib/utils/utils_linux.go | 18 +++++++ contrib/utils/utils_other.go | 15 ++++++ 8 files changed, 120 insertions(+), 48 deletions(-) rename contrib/internal/mountns/{ns.go => ns_linux.go} (97%) create mode 100644 contrib/internal/mountns/ns_other.go create mode 100644 contrib/utils/kubectl_cmd_linux.go create mode 100644 contrib/utils/kubectl_cmd_other.go create mode 100644 contrib/utils/utils_linux.go create mode 100644 contrib/utils/utils_other.go diff --git a/contrib/internal/mountns/ns.go b/contrib/internal/mountns/ns_linux.go similarity index 97% rename from contrib/internal/mountns/ns.go rename to contrib/internal/mountns/ns_linux.go index 0f2311eb..b0c98543 100644 --- a/contrib/internal/mountns/ns.go +++ b/contrib/internal/mountns/ns_linux.go @@ -1,3 +1,5 @@ +//go:build linux + // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. diff --git a/contrib/internal/mountns/ns_other.go b/contrib/internal/mountns/ns_other.go new file mode 100644 index 00000000..c8bac3b8 --- /dev/null +++ b/contrib/internal/mountns/ns_other.go @@ -0,0 +1,12 @@ +//go:build !linux + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package mountns + +import "fmt" + +func Executes(run func() error) error { + return fmt.Errorf("not supported") +} diff --git a/contrib/utils/kubectl_cmd.go b/contrib/utils/kubectl_cmd.go index a69be58b..23d5fc6e 100644 --- a/contrib/utils/kubectl_cmd.go +++ b/contrib/utils/kubectl_cmd.go @@ -9,11 +9,6 @@ import ( "net/url" "strings" "time" - - "github.com/Azure/kperf/contrib/internal/mountns" - "golang.org/x/sys/unix" - - "k8s.io/klog/v2" ) // KubectlRunner is the wrapper of exec.Command to execute kubectl command. @@ -57,43 +52,6 @@ func (kr *KubectlRunner) FQDN(ctx context.Context, timeout time.Duration) (strin return strings.ToLower(host), nil } -// Metrics returns the metrics for a specific kube-apiserver. -func (kr *KubectlRunner) Metrics(ctx context.Context, timeout time.Duration, fqdn, ip string) ([]byte, error) { - args := []string{} - if kr.kubeCfgPath != "" { - args = append(args, "--kubeconfig", kr.kubeCfgPath) - } - args = append(args, "get", "--raw", "/metrics") - - var result []byte - - merr := mountns.Executes(func() error { - newETCHostFile, cleanup, err := CreateTempFileWithContent([]byte(fmt.Sprintf("%s %s\n", ip, fqdn))) - if err != nil { - return err - } - defer func() { _ = cleanup() }() - - target := "/etc/hosts" - - err = unix.Mount(newETCHostFile, target, "none", unix.MS_BIND, "") - if err != nil { - return fmt.Errorf("failed to mount %s on %s: %w", - newETCHostFile, target, err) - } - defer func() { - derr := unix.Unmount(target, 0) - if derr != nil { - klog.Warningf("failed umount %s", target) - } - }() - - result, err = runCommand(ctx, timeout, "kubectl", args) - return err - }) - return result, merr -} - // Wait runs wait subcommand. func (kr *KubectlRunner) Wait(ctx context.Context, timeout time.Duration, condition, waitTimeout, target string) error { if condition == "" { diff --git a/contrib/utils/kubectl_cmd_linux.go b/contrib/utils/kubectl_cmd_linux.go new file mode 100644 index 00000000..e6c65724 --- /dev/null +++ b/contrib/utils/kubectl_cmd_linux.go @@ -0,0 +1,54 @@ +//go:build linux + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package utils + +import ( + "context" + "fmt" + "time" + + "github.com/Azure/kperf/contrib/internal/mountns" + + "golang.org/x/sys/unix" + "k8s.io/klog/v2" +) + +// Metrics returns the metrics for a specific kube-apiserver. +func (kr *KubectlRunner) Metrics(ctx context.Context, timeout time.Duration, fqdn, ip string) ([]byte, error) { + args := []string{} + if kr.kubeCfgPath != "" { + args = append(args, "--kubeconfig", kr.kubeCfgPath) + } + args = append(args, "get", "--raw", "/metrics") + + var result []byte + + merr := mountns.Executes(func() error { + newETCHostFile, cleanup, err := CreateTempFileWithContent([]byte(fmt.Sprintf("%s %s\n", ip, fqdn))) + if err != nil { + return err + } + defer func() { _ = cleanup() }() + + target := "/etc/hosts" + + err = unix.Mount(newETCHostFile, target, "none", unix.MS_BIND, "") + if err != nil { + return fmt.Errorf("failed to mount %s on %s: %w", + newETCHostFile, target, err) + } + defer func() { + derr := unix.Unmount(target, 0) + if derr != nil { + klog.Warningf("failed umount %s", target) + } + }() + + result, err = runCommand(ctx, timeout, "kubectl", args) + return err + }) + return result, merr +} diff --git a/contrib/utils/kubectl_cmd_other.go b/contrib/utils/kubectl_cmd_other.go new file mode 100644 index 00000000..5e4c219e --- /dev/null +++ b/contrib/utils/kubectl_cmd_other.go @@ -0,0 +1,17 @@ +//go:build !linux + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package utils + +import ( + "context" + "fmt" + "time" +) + +// Metrics returns the metrics for a specific kube-apiserver. +func (kr *KubectlRunner) Metrics(ctx context.Context, timeout time.Duration, fqdn, ip string) ([]byte, error) { + return nil, fmt.Errorf("not supported") +} diff --git a/contrib/utils/utils.go b/contrib/utils/utils.go index b16f556a..e4275b25 100644 --- a/contrib/utils/utils.go +++ b/contrib/utils/utils.go @@ -10,11 +10,9 @@ import ( "fmt" "net" "os" - "os/exec" "sort" "strconv" "strings" - "syscall" "time" "github.com/Azure/kperf/api/types" @@ -426,8 +424,7 @@ func runCommand(ctx context.Context, timeout time.Duration, cmd string, args []s defer cancel() } - c := exec.CommandContext(ctx, cmd, args...) - c.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} + c := newExecCommand(ctx, cmd, args...) logger.WithKeyValues("level", "info").LogKV("msg", "start command", "cmd", c.String()) output, err := c.CombinedOutput() @@ -448,8 +445,7 @@ func runCommandWithInput(ctx context.Context, timeout time.Duration, cmd string, defer cancel() } - c := exec.CommandContext(ctx, cmd, args...) - c.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} + c := newExecCommand(ctx, cmd, args...) c.Stdin = strings.NewReader(input) logger.WithKeyValues("level", "info").LogKV("msg", "start command", "cmd", c.String()) diff --git a/contrib/utils/utils_linux.go b/contrib/utils/utils_linux.go new file mode 100644 index 00000000..5039dfbd --- /dev/null +++ b/contrib/utils/utils_linux.go @@ -0,0 +1,18 @@ +//go:build linux + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package utils + +import ( + "context" + "os/exec" + "syscall" +) + +func newExecCommand(ctx context.Context, name string, args ...string) *exec.Cmd { + c := exec.CommandContext(ctx, name, args...) + c.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} + return c +} diff --git a/contrib/utils/utils_other.go b/contrib/utils/utils_other.go new file mode 100644 index 00000000..cf652baf --- /dev/null +++ b/contrib/utils/utils_other.go @@ -0,0 +1,15 @@ +//go:build !linux + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package utils + +import ( + "context" + "os/exec" +) + +func newExecCommand(ctx context.Context, name string, args ...string) *exec.Cmd { + return exec.CommandContext(ctx, name, args...) +}