/
helper.go
72 lines (61 loc) · 1.54 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package helper
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"os/exec"
"strconv"
"sync"
"time"
)
func GetAllContainers() []types.Container {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
return containers
}
func IsDockerInstalled() bool {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
_, err = cli.ServerVersion(ctx)
if err != nil {
return false
}
return true
}
func CreateTestContainer(count int) {
var wg sync.WaitGroup
containerNumber := len(GetAllContainers())
for i := 1; i <= count; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
containerName := "busybox-ebpfmon" + strconv.Itoa(containerNumber+i)
_, err := exec.Command("sh", "-c", "docker run -itd --rm --name "+containerName+" busybox").Output()
if err != nil {
fmt.Printf("Failed to create container: %v\n", err)
} else {
fmt.Printf("Successfully created container: %s\n", containerName)
}
}(i)
}
wg.Wait()
fmt.Println("Wait for containers are initialized...")
time.Sleep(5 * time.Second)
}
func InputCount() int {
fmt.Println("Enter the number of docker containers(busybox) to create: ")
var containerCount int
_, err := fmt.Scan(&containerCount)
if err != nil {
fmt.Println(err)
}
return containerCount
}