Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR]: refactor execute pod cmd with client-go function #2467

Merged
merged 4 commits into from
Oct 26, 2024
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
3 changes: 3 additions & 0 deletions ray-operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions ray-operator/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ray-operator/test/support/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package support
import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -16,12 +17,14 @@ type Client interface {
Core() kubernetes.Interface
Ray() rayclient.Interface
Dynamic() dynamic.Interface
Config() rest.Config
}

type testClient struct {
core kubernetes.Interface
ray rayclient.Interface
dynamic dynamic.Interface
config rest.Config
}

var _ Client = (*testClient)(nil)
Expand All @@ -38,6 +41,10 @@ func (t *testClient) Dynamic() dynamic.Interface {
return t.dynamic
}

func (t *testClient) Config() rest.Config {
return t.config
}

func newTestClient() (Client, error) {
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
Expand Down Expand Up @@ -66,5 +73,6 @@ func newTestClient() (Client, error) {
core: kubeClient,
ray: rayClient,
dynamic: dynamicClient,
config: *cfg,
}, nil
}
40 changes: 33 additions & 7 deletions ray-operator/test/support/core.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package support

import (
"bytes"
"io"
"os/exec"

"github.com/stretchr/testify/assert"

"github.com/onsi/gomega"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -63,11 +65,35 @@ func storeContainerLog(t Test, namespace *corev1.Namespace, podName, containerNa
}

func ExecPodCmd(t Test, pod *corev1.Pod, containerName string, cmd []string) {
kubectlCmd := []string{"exec", pod.Name, "-n", pod.Namespace, "-c", containerName, "--"}
kubectlCmd = append(kubectlCmd, cmd...)

t.T().Logf("Executing command: kubectl %s", kubectlCmd)
output, err := exec.Command("kubectl", kubectlCmd...).CombinedOutput()
t.T().Logf("Command output: %s", output)
req := t.Client().Core().CoreV1().RESTClient().
Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Command: cmd,
Container: containerName,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, clientgoscheme.ParameterCodec)

t.T().Logf("Executing command: %s", cmd)
cfg := t.Client().Config()
exec, err := remotecommand.NewSPDYExecutor(&cfg, "POST", req.URL())
assert.NoError(t.T(), err)
// Capture the output streams
var stdout, stderr bytes.Buffer
// Execute the command in the pod
err = exec.StreamWithContext(t.Ctx(), remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Tty: false,
})
t.T().Logf("Command stdout: %s", stdout.String())
t.T().Logf("Command stderr: %s", stderr.String())
assert.NoError(t.T(), err)
}
Loading