Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

Expand Down
12 changes: 12 additions & 0 deletions contrib/internal/mountns/ns_other.go
Original file line number Diff line number Diff line change
@@ -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")
}
42 changes: 0 additions & 42 deletions contrib/utils/kubectl_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 == "" {
Expand Down
54 changes: 54 additions & 0 deletions contrib/utils/kubectl_cmd_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions contrib/utils/kubectl_cmd_other.go
Original file line number Diff line number Diff line change
@@ -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")
}
8 changes: 2 additions & 6 deletions contrib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"fmt"
"net"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"syscall"
"time"

"github.com/Azure/kperf/api/types"
Expand Down Expand Up @@ -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()
Expand All @@ -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())
Expand Down
18 changes: 18 additions & 0 deletions contrib/utils/utils_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions contrib/utils/utils_other.go
Original file line number Diff line number Diff line change
@@ -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...)
}