Skip to content

Commit

Permalink
ss: support concurrent execution
Browse files Browse the repository at this point in the history
When using goroutines, the behavior of CreateComDetailsFromNode resulted
in the deletion of the debug namespace, causing failure.

Here, we relocate the creation and deletion of the debug namespace
outside of the function to support running this function in parallel threads.
  • Loading branch information
liornoy committed May 9, 2024
1 parent 0e7c540 commit d7076cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
34 changes: 31 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
"path/filepath"
"sync"

"golang.org/x/sync/errgroup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

clientutil "github.com/openshift-kni/commatrix/client"
"github.com/openshift-kni/commatrix/commatrix"
"github.com/openshift-kni/commatrix/consts"
"github.com/openshift-kni/commatrix/debug"
"github.com/openshift-kni/commatrix/ss"
"github.com/openshift-kni/commatrix/types"
"golang.org/x/sync/errgroup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
Expand Down Expand Up @@ -110,12 +113,37 @@ func main() {
}

nodesComDetails := []types.ComDetails{}

err = debug.CreateNamespace(cs, consts.DefaultDebugNamespace)
if err != nil {
panic(err)
}
defer func() {
err := debug.DeleteNamespace(cs, consts.DefaultDebugNamespace)
if err != nil {
panic(err)
}
}()

nLock := &sync.Mutex{}
g := new(errgroup.Group)
for _, n := range nodesList.Items {
node := n
g.Go(func() error {
cds, err := ss.CreateComDetailsFromNode(cs, &node, tcpFile, udpFile)
nLock.Lock()
debugPod, err := debug.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage)
if err != nil {
return err
}
nLock.Unlock()
defer func() {
err := debugPod.Clean()
if err != nil {
fmt.Printf("failed cleaning debug pod %s: %v", debugPod, err)
}
}()

cds, err := ss.CreateComDetailsFromNode(debugPod, &node, tcpFile, udpFile)
if err != nil {
return err
}
Expand Down
21 changes: 10 additions & 11 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ func New(cs *client.ClientSet, node string, namespace string, image string) (*De
return nil, errors.New("failed creating new debug pod: got empty namespace")
}

err := createNamespace(cs, namespace)
if err != nil {
return nil, err
}

pod, err := createPodAndWait(cs, interval, timeout, node, namespace, image)
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,11 +81,6 @@ func (dp *DebugPod) Clean() error {
return fmt.Errorf("failed deleting debug pod %s/%s: %v\n%s", dp.Namespace, dp.Name, err, string(output))
}

output, err = exec.Command("oc", "delete", "ns", dp.Namespace).CombinedOutput()
if err != nil {
return fmt.Errorf("failed deleting debug namespace %s: %v\n%s", dp.Namespace, err, string(output))
}

return nil
}

Expand Down Expand Up @@ -290,7 +280,7 @@ func getPodDefinition(node string, namespace string, image string) *corev1.Pod {
}
}

func createNamespace(cs *client.ClientSet, namespace string) error {
func CreateNamespace(cs *client.ClientSet, namespace string) error {
ns := getNamespaceDefinition(namespace)
_, err := cs.Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
if err != nil && !k8serrors.IsAlreadyExists(err) {
Expand All @@ -300,6 +290,15 @@ func createNamespace(cs *client.ClientSet, namespace string) error {
return nil
}

func DeleteNamespace(cs *client.ClientSet, namespace string) error {
err := cs.Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed deleting namespace %s: %v", namespace, err)
}

return nil
}

func getNamespaceDefinition(namespace string) *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Expand Down
14 changes: 1 addition & 13 deletions ss/ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"

"github.com/openshift-kni/commatrix/client"
"github.com/openshift-kni/commatrix/consts"
"github.com/openshift-kni/commatrix/debug"
"github.com/openshift-kni/commatrix/nodes"
Expand All @@ -24,18 +23,7 @@ const (
duration = time.Second * 5
)

func CreateComDetailsFromNode(cs *client.ClientSet, node *corev1.Node, tcpFile, udpFile *os.File) ([]types.ComDetails, error) {
debugPod, err := debug.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage)
if err != nil {
return nil, err
}
defer func() {
err := debugPod.Clean()
if err != nil {
fmt.Printf("failed cleaning debug pod %s: %v", debugPod, err)
}
}()

func CreateComDetailsFromNode(debugPod *debug.DebugPod, node *corev1.Node, tcpFile, udpFile *os.File) ([]types.ComDetails, error) {
ssOutTCP, err := debugPod.ExecWithRetry("ss -anpltH", interval, duration)
if err != nil {
return nil, err
Expand Down

0 comments on commit d7076cc

Please sign in to comment.