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
29 changes: 27 additions & 2 deletions benchmarks/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,39 @@ func Metrics(experiment *gm.Experiment, suffix string) {
}

func getMetrics(res map[string]float64, url string, controllers ...string) {
pod := addRandomSuffix("curl")
var (
mfs map[string]*dto.MetricFamily
parser = expfmt.NewTextParser(model.LegacyValidation)
)
Eventually(func() error {
pod := addRandomSuffix("curl")
defer func() {
_, _ = k.Run("delete", "pod", "--namespace", "cattle-fleet-system", pod, "--ignore-not-found")
}()

GinkgoWriter.Print("Fetching metrics from " + url + "\n")
out, err := k.Run("run", "--rm", "--attach", "--quiet", "--restart=Never", pod, "--image=curlimages/curl", "--namespace", "cattle-fleet-system", "--command", "--", "curl", "-s", url)

// Create the pod without --attach to avoid kubectl's unreliable
// attach mechanism, which can duplicate output on fallback to logs.
_, _, err := k.RunStdout("run", "--restart=Never", pod, "--image=curlimages/curl", "--namespace", "cattle-fleet-system", "--command", "--", "curl", "-sf", url)
if err != nil {
return fmt.Errorf("kubectl run: %w", err)
}

// Wait for the pod to terminate. Use condition=Ready=false to
// detect both Succeeded and Failed without blocking for the full
// timeout on a failed curl.
_, _, err = k.RunStdout("wait", "--for=condition=Ready=false", "--namespace", "cattle-fleet-system", "pod/"+pod, "--timeout=30s")
if err != nil {
return fmt.Errorf("waiting for pod: %w", err)
}

phase, _, _ := k.RunStdout("get", "pod", pod, "--namespace", "cattle-fleet-system", "-o", "jsonpath={.status.phase}")
if strings.TrimSpace(phase) != "Succeeded" {
return fmt.Errorf("curl pod %s finished with phase %s", pod, phase)
}

out, _, err := k.RunStdout("logs", "--namespace", "cattle-fleet-system", pod)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in case we run into more troubles getting these logs, we could replace creating a new pod with a port forward for fetching metrics, as done in the dump CLI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope it won't be necessary, but yeah, we may have to do that.

if err != nil {
return err
}
Expand Down
41 changes: 30 additions & 11 deletions e2e/testenv/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,49 @@ func (c Command) Run(args ...string) (string, error) {
}

GinkgoWriter.Printf("kubectl %s\n", strings.Join(args, " "))
result, err := c.exec("kubectl", args...)
combined, _, _, err := c.exec("kubectl", args...)
if err != nil {
GinkgoWriter.Printf("result:%s err:%s\n", result, err)
GinkgoWriter.Printf("result:%s err:%s\n", combined, err)
}

return result, err
return combined, err
}

func (c Command) exec(command string, args ...string) (string, error) {
// RunStdout behaves like Run but returns stdout and stderr separately.
func (c Command) RunStdout(args ...string) (stdout, stderr string, err error) {
if c.cnt != "" {
args = append([]string{"--context", c.cnt}, args...)
}

if c.ns != "" {
args = append([]string{"-n", c.ns}, args...)
}

GinkgoWriter.Printf("kubectl %s\n", strings.Join(args, " "))
Comment thread
weyfonk marked this conversation as resolved.
_, stdout, stderr, err = c.exec("kubectl", args...)
if err != nil {
GinkgoWriter.Printf("stdout:%s stderr:%s err:%s\n", stdout, stderr, err)
}

return stdout, stderr, err
}

func (c Command) exec(command string, args ...string) (combined, stdout, stderr string, err error) {
cmd := exec.CommandContext(context.Background(), command, args...)

var b bytes.Buffer
var combinedBuf, outBuf, errBuf bytes.Buffer
if c.stdout {
cmd.Stdout = io.MultiWriter(os.Stdout, &b)
cmd.Stderr = io.MultiWriter(os.Stderr, &b)
cmd.Stdout = io.MultiWriter(os.Stdout, &combinedBuf, &outBuf)
cmd.Stderr = io.MultiWriter(os.Stderr, &combinedBuf, &errBuf)
} else {
cmd.Stdout = &b
cmd.Stderr = &b
cmd.Stdout = io.MultiWriter(&combinedBuf, &outBuf)
cmd.Stderr = io.MultiWriter(&combinedBuf, &errBuf)
}

if c.dir != "" {
cmd.Dir = c.dir
}

err := cmd.Run()
return b.String(), err
err = cmd.Run()
return combinedBuf.String(), outBuf.String(), errBuf.String(), err
}