From 50e48c260291870c2df8ec06f953854db14c833a Mon Sep 17 00:00:00 2001 From: rohan2794 Date: Tue, 22 Oct 2024 12:49:51 +0530 Subject: [PATCH] feat(rdma): add e2e agent capability to enable and disable network interface Signed-off-by: rohan2794 --- common/e2e_agent/client.go | 52 ++++++++++++++++++++++++++ common/k8stest/util_rdma.go | 36 ++++++++++++++++++ tools/e2e-agent/build.sh | 2 +- tools/e2e-agent/e2e-agent.yaml | 2 +- tools/e2e-agent/server.go | 67 ++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 2 deletions(-) diff --git a/common/e2e_agent/client.go b/common/e2e_agent/client.go index eec3045..2c27242 100644 --- a/common/e2e_agent/client.go +++ b/common/e2e_agent/client.go @@ -105,6 +105,10 @@ type Rdma struct { InterfaceName string `json:"interfaceName"` } +type NetworkInterface struct { + NetworkInterface string `json:"networkInterface"` +} + func sendRequest(reqType, url string, data interface{}) error { _, err := sendRequestGetResponse(reqType, url, data, true) return err @@ -1219,3 +1223,51 @@ func DeleteRdmaDevice(serverAddr string, deeviceName string) (string, error) { logf.Log.Info("DeleteRdmaDevice succeeded", "output", out) return out, err } + +// EnableNetworkInterface enable network interface +func EnableNetworkInterface(serverAddr string, interfaceName string) (string, error) { + data := NetworkInterface{ + NetworkInterface: interfaceName, + } + logf.Log.Info("Executing EnableNetworkInterface", "addr", serverAddr, "data", data) + url := "http://" + getAgentAddress(serverAddr) + "/enablenetworkinterface" + encodedresult, err := sendRequestGetResponse("POST", url, data, false) + if err != nil { + logf.Log.Info("sendRequestGetResponse", "encodedresult", encodedresult, "error", err.Error()) + return encodedresult, err + } + out, e2eagenterrcode, err := UnwrapResult(encodedresult) + if err != nil { + logf.Log.Info("unwrap failed", "encodedresult", encodedresult, "error", err.Error()) + return encodedresult, err + } + if e2eagenterrcode != ErrNone { + return out, fmt.Errorf("failed to enable network interface, errcode %d", e2eagenterrcode) + } + logf.Log.Info("EnableNetworkInterface succeeded", "output", out) + return out, err +} + +// DisableNetworkInterface disable network interface +func DisableNetworkInterface(serverAddr string, interfaceName string) (string, error) { + data := NetworkInterface{ + NetworkInterface: interfaceName, + } + logf.Log.Info("Executing DisableNetworkInterface", "addr", serverAddr, "data", data) + url := "http://" + getAgentAddress(serverAddr) + "/disablenetworkinterface" + encodedresult, err := sendRequestGetResponse("POST", url, data, false) + if err != nil { + logf.Log.Info("sendRequestGetResponse", "encodedresult", encodedresult, "error", err.Error()) + return encodedresult, err + } + out, e2eagenterrcode, err := UnwrapResult(encodedresult) + if err != nil { + logf.Log.Info("unwrap failed", "encodedresult", encodedresult, "error", err.Error()) + return encodedresult, err + } + if e2eagenterrcode != ErrNone { + return out, fmt.Errorf("failed to disable network interface, errcode %d", e2eagenterrcode) + } + logf.Log.Info("DeleteRdmaDevice succeeded", "output", out) + return out, err +} diff --git a/common/k8stest/util_rdma.go b/common/k8stest/util_rdma.go index b7a18ec..ef3cabc 100644 --- a/common/k8stest/util_rdma.go +++ b/common/k8stest/util_rdma.go @@ -132,3 +132,39 @@ func IsVolumeAccessibleOverTcp(volUuid string) (bool, error) { } return false, nil } + +// enable network interface on node +func EnableNetworkInterfaceOnNode(node string) error { + nodeIp, err := GetNodeIPAddress(node) + if err != nil { + return fmt.Errorf("failed to get node %s ip, error: %v", node, err) + } + + // get interface name + iface := e2e_config.GetConfig().NetworkInterface + // enable network interface + out, err := e2e_agent.EnableNetworkInterface(*nodeIp, iface) + if err != nil { + return err + } + logf.Log.Info("Enable network interface", "node", node, "output", out, "interface", iface) + return nil +} + +// disable network interface on node +func DisableNetworkInterfaceOnNode(node string) error { + nodeIp, err := GetNodeIPAddress(node) + if err != nil { + return fmt.Errorf("failed to get node %s ip, error: %v", node, err) + } + + // get interface name + iface := e2e_config.GetConfig().NetworkInterface + // enable network interface + out, err := e2e_agent.DisableNetworkInterface(*nodeIp, iface) + if err != nil { + return err + } + logf.Log.Info("Disable network interface", "node", node, "output", out, "interface", iface) + return nil +} diff --git a/tools/e2e-agent/build.sh b/tools/e2e-agent/build.sh index 56b4659..1e22568 100755 --- a/tools/e2e-agent/build.sh +++ b/tools/e2e-agent/build.sh @@ -9,7 +9,7 @@ # as long as we do not make breaking changes. set -e IMAGE="openebs/e2e-agent" -TAG="v3.0.5" +TAG="v3.0.6" registry="" tag_as_latest="" diff --git a/tools/e2e-agent/e2e-agent.yaml b/tools/e2e-agent/e2e-agent.yaml index 59c2568..7519839 100644 --- a/tools/e2e-agent/e2e-agent.yaml +++ b/tools/e2e-agent/e2e-agent.yaml @@ -68,7 +68,7 @@ spec: securityContext: privileged: true allowPrivilegeEscalation: true - image: openebs/e2e-agent:v3.0.5 + image: openebs/e2e-agent:v3.0.6 imagePullPolicy: Always volumeMounts: - name: host-root diff --git a/tools/e2e-agent/server.go b/tools/e2e-agent/server.go index 2942b33..04d259c 100644 --- a/tools/e2e-agent/server.go +++ b/tools/e2e-agent/server.go @@ -66,6 +66,10 @@ type CmpPaths struct { Path2 string `json:"path2"` } +type NetworkInterface struct { + NetworkInterface string `json:"networkInterface"` +} + func homePage(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Welcome home!\n") } @@ -180,6 +184,8 @@ func handleRequests() { router.HandleFunc("/listrdmadevice", ListRdmaDevice).Methods("POST") router.HandleFunc("/createrdmadevice", CreateRdmaDevice).Methods("POST") router.HandleFunc("/deleterdmadevice", DeleteRdmaDevice).Methods("POST") + router.HandleFunc("/enablenetworkinterface", EnableNetworkInterface).Methods("POST") + router.HandleFunc("/disablenetworkinterface", DisableNetworkInterface).Methods("POST") log.Fatal(http.ListenAndServe(podIP+":"+restPort, router)) } @@ -1188,3 +1194,64 @@ func Cmp(w http.ResponseWriter, r *http.Request) { klog.Info(string(output)) WrapResult(b64out, errCode, w) } + +// EnableNetworkInterface enable network interface +func EnableNetworkInterface(w http.ResponseWriter, r *http.Request) { + var msg string + var iface NetworkInterface + d := json.NewDecoder(r.Body) + if err := d.Decode(&iface); err != nil { + msg = fmt.Sprintf("failed to read JSON encoded data, Error: %s", err.Error()) + klog.Error(msg) + WrapResult(msg, ErrJsonDecode, w) + return + } + + if iface.NetworkInterface == "" { + msg = "no network interface name passed" + klog.Error(msg) + WrapResult(msg, UnprocessableEntityErrorCode, w) + return + } + klog.Info("enable network interface, data: %v", iface) + + iFaceUpCommand := fmt.Sprintf("ifconfig %s up", iface.NetworkInterface) + output, err := bashLocal(iFaceUpCommand) + if err != nil { + msg = fmt.Sprintf("cannot enable network interface, Error %s", err.Error()) + klog.Error(msg) + WrapResult(msg, ErrExecFailed, w) + return + } + WrapResult(string(output), ErrNone, w) +} + +// DisableNetworkInterface disable network interface +func DisableNetworkInterface(w http.ResponseWriter, r *http.Request) { + var msg string + var iface NetworkInterface + d := json.NewDecoder(r.Body) + if err := d.Decode(&iface); err != nil { + msg = fmt.Sprintf("failed to read JSON encoded data, Error: %s", err.Error()) + klog.Error(msg) + WrapResult(msg, ErrJsonDecode, w) + return + } + if iface.NetworkInterface == "" { + msg = "no interface name passed" + klog.Error(msg) + WrapResult(msg, UnprocessableEntityErrorCode, w) + return + } + klog.Info("disable network interface, data: %v", iface) + + iFaceDownCommand := fmt.Sprintf("ifconfig %s down", iface.NetworkInterface) + output, err := bashLocal(iFaceDownCommand) + if err != nil { + msg = fmt.Sprintf("cannot disable network interface, Error %s", err.Error()) + klog.Error(msg) + WrapResult(msg, ErrExecFailed, w) + return + } + WrapResult(string(output), ErrNone, w) +}