Skip to content

Commit

Permalink
update implementation of debugpod
Browse files Browse the repository at this point in the history
  • Loading branch information
aabughosh committed Aug 1, 2024
1 parent 55660e1 commit c338734
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

"github.com/openshift-kni/commatrix/debug"

clientutil "github.com/openshift-kni/commatrix/client"

"github.com/openshift-kni/commatrix/commatrix"
Expand Down Expand Up @@ -74,7 +76,8 @@ func main() {
panic(fmt.Sprintf("Error while writing the endpoint slice matrix to file :%v", err))
}
// generate the ss matrix and ss raws
ssMat, ssOutTCP, ssOutUDP, err := commatrix.GenerateSS(cs)
newDebugPod := &debug.NewDebugPod{}
ssMat, ssOutTCP, ssOutUDP, err := commatrix.GenerateSS(cs, newDebugPod)
if err != nil {
panic(fmt.Sprintf("Error while generating the ss matrix and ss raws :%v", err))
}
Expand Down
21 changes: 9 additions & 12 deletions commatrix/commatrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ func TestWriteMatrixToFile(t *testing.T) {
}

func TestGenerateSS(t *testing.T) {
clientset := fake.NewSimpleClientset()
fakeClientset := fake.NewSimpleClientset()
clientset := &clientutil.ClientSet{
CoreV1Interface: fakeClientset.CoreV1(),
}

ctrlTest := gomock.NewController(t)
defer ctrlTest.Finish()

mockDebugPod := debug.NewMockDebugPodInterface(ctrlTest)
NewmockDebugPod := debug.NewMockNewDebugPodInterface(ctrlTest)

tcpOutput := []byte(`LISTEN 0 4096 127.0.0.1:8797 0.0.0.0:* users:(("machine-config-",pid=3534,fd=3))
LISTEN 0 4096 127.0.0.1:8798 0.0.0.0:* users:(("machine-config-",pid=3534,fd=13))
Expand Down Expand Up @@ -98,27 +102,20 @@ UNCONN 0 0 10.46.97.104:500 0.0.0.0:* users:(("pluto",pid=2115,fd=21
).AnyTimes()

mockDebugPod.EXPECT().Clean().Return(nil).AnyTimes()

mockDebugPod.EXPECT().GetNodeName().Return("test-node").AnyTimes()

originalNew := debug.NewDebugPod
debug.NewDebugPod = func(cs *clientutil.ClientSet, node string, namespace string, image string) (debug.DebugPodInterface, error) {
return mockDebugPod, nil
}
defer func() { debug.NewDebugPod = originalNew }()
NewmockDebugPod.EXPECT().New(clientset, "test-node", "openshift-commatrix-debug", "quay.io/openshift-release-dev/ocp-release:4.15.12-multi").Return(mockDebugPod, nil)

cs := &clientutil.ClientSet{
CoreV1Interface: clientset.CoreV1(),
}
testNode := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test-node",
},
}
_, _ = clientset.CoreV1Interface.Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{})

_, _ = clientset.CoreV1().Nodes().Create(context.TODO(), testNode, metav1.CreateOptions{})
ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(clientset, NewmockDebugPod)

ssMat, ssOutTCP, ssOutUDP, err := GenerateSS(cs)
// Pass the expected ClientSet to GenerateSS
expectedSSMat := &types.ComMatrix{
Matrix: []types.ComDetails{
{Direction: "Ingress", Protocol: "UDP", Port: 111, Namespace: "", Service: "rpcbind", Pod: "", Container: "", NodeRole: "", Optional: false},
Expand Down
10 changes: 5 additions & 5 deletions commatrix/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/openshift-kni/commatrix/types"
)

func GenerateSS(cs *clientutil.ClientSet) (ssMat *types.ComMatrix, ssOutTCP, ssOutUDP []byte, err error) {
func GenerateSS(cs *clientutil.ClientSet, debugPod debug.NewDebugPodInterface) (ssMat *types.ComMatrix, ssOutTCP, ssOutUDP []byte, err error) {
nodesList, err := cs.CoreV1Interface.Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, nil, nil, err
Expand All @@ -41,18 +41,18 @@ func GenerateSS(cs *clientutil.ClientSet) (ssMat *types.ComMatrix, ssOutTCP, ssO
for _, n := range nodesList.Items {
node := n
g.Go(func() error {
debugPod, err := debug.NewDebugPod(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage)
newdebugPod, err := debugPod.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage)
if err != nil {
return err
}
defer func() {
err := debugPod.Clean()
err := newdebugPod.Clean()
if err != nil {
fmt.Printf("failed cleaning debug pod %s: %v", debugPod, err)
fmt.Printf("failed cleaning debug pod %s: %v", newdebugPod, err)
}
}()

cds, ssTCP, ssUDP, err := ss.CreateSSOutputFromNode(debugPod, &node)
cds, ssTCP, ssUDP, err := ss.CreateSSOutputFromNode(newdebugPod, &node)
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package debug

//go:generate mockgen -destination=debug_mock.go -package=debug . DebugPodInterface
//go:generate mockgen -destination=new_debug_mock.go -package=debug . NewDebugPodInterface

import (
"context"
Expand All @@ -25,6 +26,12 @@ type DebugPodInterface interface {
GetNodeName() string
}

type NewDebugPodInterface interface {
New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error)
}

type NewDebugPod struct{}

type DebugPod struct {
Name string
Namespace string
Expand All @@ -40,11 +47,9 @@ const (
timeout = 2 * time.Minute
)

var NewDebugPod = New

// New creates debug pod on the given node, puts it in infinite sleep,
// and returns the DebugPod object. Use the Clean() method to delete it.
func New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) {
func (n *NewDebugPod) New(cs *client.ClientSet, node string, namespace string, image string) (DebugPodInterface, error) {
if namespace == "" {
return nil, errors.New("failed creating new debug pod: got empty namespace")
}
Expand Down
55 changes: 55 additions & 0 deletions debug/new_debug_mock.go

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

0 comments on commit c338734

Please sign in to comment.