From ea198fddccf266f345c13b33847e5def663ae6aa Mon Sep 17 00:00:00 2001 From: Gabriela Cervantes Date: Tue, 25 Jul 2023 17:34:29 +0000 Subject: [PATCH] metrics: Add FIO runner k8s Add program to execute FIO workloads using k8s. Signed-off-by: Gabriela Cervantes --- .../metrics/storage/fio-k8s/pkg/k8s/Makefile | 8 +++ tests/metrics/storage/fio-k8s/pkg/k8s/exec.go | 35 ++++++++++ tests/metrics/storage/fio-k8s/pkg/k8s/k8s.go | 69 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 tests/metrics/storage/fio-k8s/pkg/k8s/Makefile create mode 100644 tests/metrics/storage/fio-k8s/pkg/k8s/exec.go create mode 100644 tests/metrics/storage/fio-k8s/pkg/k8s/k8s.go diff --git a/tests/metrics/storage/fio-k8s/pkg/k8s/Makefile b/tests/metrics/storage/fio-k8s/pkg/k8s/Makefile new file mode 100644 index 0000000000..c596cec7cb --- /dev/null +++ b/tests/metrics/storage/fio-k8s/pkg/k8s/Makefile @@ -0,0 +1,8 @@ +# +# Copyright (c) 2021 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# +gomod: + GO111MODULE=on go mod edit -replace=github.com/kata-containers/tests/metrics/exec=../exec + GO111MODULE=on go mod tidy diff --git a/tests/metrics/storage/fio-k8s/pkg/k8s/exec.go b/tests/metrics/storage/fio-k8s/pkg/k8s/exec.go new file mode 100644 index 0000000000..1d37a1b875 --- /dev/null +++ b/tests/metrics/storage/fio-k8s/pkg/k8s/exec.go @@ -0,0 +1,35 @@ +// Copyright (c) 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// +package k8s + +import ( + "fmt" + + exec "github.com/kata-containers/tests/metrics/exec" +) + +type execOpt struct { + showInStdOut bool +} + +type ExecOption func(e *execOpt) + +func ExecOptShowStdOut() ExecOption { + return func(e *execOpt) { + e.showInStdOut = true + } + +} + +func (p *Pod) Exec(cmd string, opts ...ExecOption) (output string, err error) { + log.Debugf("Exec %q in %s", cmd, p.YamlPath) + o := &execOpt{showInStdOut: false} + for _, opt := range opts { + opt(o) + + } + execCmd := fmt.Sprintf("kubectl exec -f %s -- /bin/bash -c %q", p.YamlPath, cmd) + return exec.ExecCmd(execCmd, Debug || o.showInStdOut) +} diff --git a/tests/metrics/storage/fio-k8s/pkg/k8s/k8s.go b/tests/metrics/storage/fio-k8s/pkg/k8s/k8s.go new file mode 100644 index 0000000000..6e1e7a0482 --- /dev/null +++ b/tests/metrics/storage/fio-k8s/pkg/k8s/k8s.go @@ -0,0 +1,69 @@ +// Copyright (c) 2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// +package k8s + +import ( + "fmt" + + exec "github.com/kata-containers/tests/metrics/exec" + "github.com/pkg/errors" +) + +//logger interface for pkg +var log logger +var Debug bool = false + +type logger interface { + Infof(string, ...interface{}) + Debugf(string, ...interface{}) + Errorf(string, ...interface{}) +} + +func SetLogger(l logger) { + log = l +} + +type Pod struct { + YamlPath string +} + +func (p *Pod) waitForReady() (err error) { + log.Debugf("Wait for pod %s", p.YamlPath) + _, err = exec.ExecCmd("kubectl wait --for=condition=ready -f "+p.YamlPath, Debug) + return err +} + +func (p *Pod) Run() (err error) { + + log.Debugf("Creating K8s Pod %s", p.YamlPath) + _, err = exec.ExecCmd("kubectl apply -f "+p.YamlPath, Debug) + if err != nil { + return errors.Wrapf(err, "Failed to run pod %s", p.YamlPath) + } + + err = p.waitForReady() + if err != nil { + return errors.Wrapf(err, "Failed to wait for pod %s", p.YamlPath) + } + return err +} + +func (p *Pod) Delete() (err error) { + log.Debugf("Delete pod %s", p.YamlPath) + _, err = exec.ExecCmd("kubectl delete --ignore-not-found -f "+p.YamlPath, Debug) + return errors.Wrapf(err, "Failed to delete pod %s", p.YamlPath) +} + +func (p *Pod) CopyFromHost(src, dst string) (err error) { + podName, err := exec.ExecCmd("kubectl get -f "+p.YamlPath+" -o jsonpath={.metadata.name}", Debug) + if err != nil { + return nil + } + + log.Debugf("Copy from host %q->%q in pod %s", src, dst, p.YamlPath) + execCmd := fmt.Sprintf("kubectl cp %s %s:%s", src, podName, dst) + _, err = exec.ExecCmd(execCmd, Debug) + return err +}