Skip to content

Commit f9b5b7a

Browse files
authored
contrib: buildable on other platforms (darwin/wins) (#155)
Just make it buildable on other platforms. Signed-off-by: Wei Fu <weifu@microsoft.com>
1 parent 2def6bc commit f9b5b7a

File tree

8 files changed

+120
-48
lines changed

8 files changed

+120
-48
lines changed

contrib/internal/mountns/ns.go renamed to contrib/internal/mountns/ns_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build linux
2+
13
// Copyright (c) Microsoft Corporation.
24
// Licensed under the MIT License.
35

contrib/internal/mountns/ns_other.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !linux
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the MIT License.
5+
6+
package mountns
7+
8+
import "fmt"
9+
10+
func Executes(run func() error) error {
11+
return fmt.Errorf("not supported")
12+
}

contrib/utils/kubectl_cmd.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import (
99
"net/url"
1010
"strings"
1111
"time"
12-
13-
"github.com/Azure/kperf/contrib/internal/mountns"
14-
"golang.org/x/sys/unix"
15-
16-
"k8s.io/klog/v2"
1712
)
1813

1914
// 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
5752
return strings.ToLower(host), nil
5853
}
5954

60-
// Metrics returns the metrics for a specific kube-apiserver.
61-
func (kr *KubectlRunner) Metrics(ctx context.Context, timeout time.Duration, fqdn, ip string) ([]byte, error) {
62-
args := []string{}
63-
if kr.kubeCfgPath != "" {
64-
args = append(args, "--kubeconfig", kr.kubeCfgPath)
65-
}
66-
args = append(args, "get", "--raw", "/metrics")
67-
68-
var result []byte
69-
70-
merr := mountns.Executes(func() error {
71-
newETCHostFile, cleanup, err := CreateTempFileWithContent([]byte(fmt.Sprintf("%s %s\n", ip, fqdn)))
72-
if err != nil {
73-
return err
74-
}
75-
defer func() { _ = cleanup() }()
76-
77-
target := "/etc/hosts"
78-
79-
err = unix.Mount(newETCHostFile, target, "none", unix.MS_BIND, "")
80-
if err != nil {
81-
return fmt.Errorf("failed to mount %s on %s: %w",
82-
newETCHostFile, target, err)
83-
}
84-
defer func() {
85-
derr := unix.Unmount(target, 0)
86-
if derr != nil {
87-
klog.Warningf("failed umount %s", target)
88-
}
89-
}()
90-
91-
result, err = runCommand(ctx, timeout, "kubectl", args)
92-
return err
93-
})
94-
return result, merr
95-
}
96-
9755
// Wait runs wait subcommand.
9856
func (kr *KubectlRunner) Wait(ctx context.Context, timeout time.Duration, condition, waitTimeout, target string) error {
9957
if condition == "" {

contrib/utils/kubectl_cmd_linux.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//go:build linux
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the MIT License.
5+
6+
package utils
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"time"
12+
13+
"github.com/Azure/kperf/contrib/internal/mountns"
14+
15+
"golang.org/x/sys/unix"
16+
"k8s.io/klog/v2"
17+
)
18+
19+
// Metrics returns the metrics for a specific kube-apiserver.
20+
func (kr *KubectlRunner) Metrics(ctx context.Context, timeout time.Duration, fqdn, ip string) ([]byte, error) {
21+
args := []string{}
22+
if kr.kubeCfgPath != "" {
23+
args = append(args, "--kubeconfig", kr.kubeCfgPath)
24+
}
25+
args = append(args, "get", "--raw", "/metrics")
26+
27+
var result []byte
28+
29+
merr := mountns.Executes(func() error {
30+
newETCHostFile, cleanup, err := CreateTempFileWithContent([]byte(fmt.Sprintf("%s %s\n", ip, fqdn)))
31+
if err != nil {
32+
return err
33+
}
34+
defer func() { _ = cleanup() }()
35+
36+
target := "/etc/hosts"
37+
38+
err = unix.Mount(newETCHostFile, target, "none", unix.MS_BIND, "")
39+
if err != nil {
40+
return fmt.Errorf("failed to mount %s on %s: %w",
41+
newETCHostFile, target, err)
42+
}
43+
defer func() {
44+
derr := unix.Unmount(target, 0)
45+
if derr != nil {
46+
klog.Warningf("failed umount %s", target)
47+
}
48+
}()
49+
50+
result, err = runCommand(ctx, timeout, "kubectl", args)
51+
return err
52+
})
53+
return result, merr
54+
}

contrib/utils/kubectl_cmd_other.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build !linux
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the MIT License.
5+
6+
package utils
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"time"
12+
)
13+
14+
// Metrics returns the metrics for a specific kube-apiserver.
15+
func (kr *KubectlRunner) Metrics(ctx context.Context, timeout time.Duration, fqdn, ip string) ([]byte, error) {
16+
return nil, fmt.Errorf("not supported")
17+
}

contrib/utils/utils.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import (
1010
"fmt"
1111
"net"
1212
"os"
13-
"os/exec"
1413
"sort"
1514
"strconv"
1615
"strings"
17-
"syscall"
1816
"time"
1917

2018
"github.com/Azure/kperf/api/types"
@@ -426,8 +424,7 @@ func runCommand(ctx context.Context, timeout time.Duration, cmd string, args []s
426424
defer cancel()
427425
}
428426

429-
c := exec.CommandContext(ctx, cmd, args...)
430-
c.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
427+
c := newExecCommand(ctx, cmd, args...)
431428

432429
logger.WithKeyValues("level", "info").LogKV("msg", "start command", "cmd", c.String())
433430
output, err := c.CombinedOutput()
@@ -448,8 +445,7 @@ func runCommandWithInput(ctx context.Context, timeout time.Duration, cmd string,
448445
defer cancel()
449446
}
450447

451-
c := exec.CommandContext(ctx, cmd, args...)
452-
c.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
448+
c := newExecCommand(ctx, cmd, args...)
453449
c.Stdin = strings.NewReader(input)
454450

455451
logger.WithKeyValues("level", "info").LogKV("msg", "start command", "cmd", c.String())

contrib/utils/utils_linux.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build linux
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the MIT License.
5+
6+
package utils
7+
8+
import (
9+
"context"
10+
"os/exec"
11+
"syscall"
12+
)
13+
14+
func newExecCommand(ctx context.Context, name string, args ...string) *exec.Cmd {
15+
c := exec.CommandContext(ctx, name, args...)
16+
c.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
17+
return c
18+
}

contrib/utils/utils_other.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !linux
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the MIT License.
5+
6+
package utils
7+
8+
import (
9+
"context"
10+
"os/exec"
11+
)
12+
13+
func newExecCommand(ctx context.Context, name string, args ...string) *exec.Cmd {
14+
return exec.CommandContext(ctx, name, args...)
15+
}

0 commit comments

Comments
 (0)