/
utils.go
40 lines (31 loc) · 1006 Bytes
/
utils.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
package container
import (
"bytes"
"context"
"io"
"github.com/docker/docker/api/types"
"github.com/sirupsen/logrus"
)
func (k *KeepalivedContainerCollectorHost) dockerExecCmd(cmd []string) (*bytes.Buffer, error) {
rst, err := k.dockerCli.ContainerExecCreate(context.Background(), k.containerName, types.ExecConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: cmd,
})
if err != nil {
logrus.WithError(err).WithField("CMD", cmd).Error("Error creating exec container")
return nil, err
}
response, err := k.dockerCli.ContainerExecAttach(context.Background(), rst.ID, types.ExecStartCheck{})
if err != nil {
logrus.WithError(err).WithField("CMD", cmd).Error("Error attaching a connection to an exec process")
return nil, err
}
defer response.Close()
data, err := io.ReadAll(response.Reader)
if err != nil {
logrus.WithError(err).WithField("CMD", cmd).Error("Error reading response from docker command")
return nil, err
}
return bytes.NewBuffer(data), nil
}