Skip to content

Commit 52ad644

Browse files
authored
Fix docker socket issue (#595)
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
1 parent 0678514 commit 52ad644

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

deploy/dev.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function(serverVersion='v0.4.2')
2626
insecure: true,
2727
insecureSkipVerify: true,
2828
// debugInfoDisable: true,
29-
socketPath: '/run/docker.sock',
29+
// socketPath: '/run/docker.sock',
3030
// podLabelSelector: 'app.kubernetes.io/name=parca',
3131
});
3232

pkg/containerutils/containerd/containerd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (c *Client) PIDFromContainerID(containerID string) (int, error) {
8484

8585
status, err := c.client.ContainerStatus(context.Background(), request)
8686
if err != nil {
87-
return -1, err
87+
return -1, fmt.Errorf("failed to get container status, request: %v: %w", request, err)
8888
}
8989

9090
info, ok := status.Info["info"]

pkg/containerutils/crio/crio.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (c *Client) PIDFromContainerID(containerID string) (int, error) {
9090

9191
status, err := c.client.ContainerStatus(context.Background(), request)
9292
if err != nil {
93-
return -1, err
93+
return -1, fmt.Errorf("failed to get container status, request: %v: %w", request, err)
9494
}
9595

9696
infoStr, ok := status.Info["info"]

pkg/containerutils/docker/docker.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
const (
27-
DefaultSocketPath = "/var/run/docker.sock"
27+
DefaultSocketPath = "/run/docker.sock"
2828
DefaultTimeout = 2 * time.Second
2929
)
3030

@@ -33,7 +33,13 @@ type Client struct {
3333
}
3434

3535
func NewDockerClient(path string) (*Client, error) {
36+
host := path
37+
if protoAddrParts := strings.SplitN(host, "://", 2); len(protoAddrParts) == 1 {
38+
// The default protocol is unix sockets (tcp and http are also valid).
39+
host = "unix://" + host
40+
}
3641
cli, err := client.NewClientWithOpts(
42+
client.WithHost(host),
3743
client.WithAPIVersionNegotiation(),
3844
client.WithDialContext(func(ctx context.Context, network, addr string) (net.Conn, error) {
3945
return net.DialTimeout("unix", path, DefaultTimeout)
@@ -65,7 +71,7 @@ func (c *Client) PIDFromContainerID(containerID string) (int, error) {
6571

6672
containerJSON, err := c.client.ContainerInspect(context.Background(), containerID)
6773
if err != nil {
68-
return -1, err
74+
return -1, fmt.Errorf("failed to get container status, container id: %s: %w", containerID, err)
6975
}
7076

7177
if containerJSON.State == nil {

pkg/k8s/k8s.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func newCRIClient(logger log.Logger, node *v1.Node, socketPath string) (containe
109109
if socketPath == "" {
110110
socketPath = docker.DefaultSocketPath
111111
}
112+
if _, err := os.Stat(socketPath); err != nil {
113+
return nil, fmt.Errorf("docker socket path is not reachable: %w", err)
114+
}
112115
return docker.NewDockerClient(socketPath)
113116
case "containerd":
114117
if socketPath == "" {
@@ -119,11 +122,17 @@ func newCRIClient(logger log.Logger, node *v1.Node, socketPath string) (containe
119122
socketPath = containerd.DefaultK3SSocketPath
120123
}
121124
}
125+
if _, err := os.Stat(socketPath); err != nil {
126+
return nil, fmt.Errorf("containerd socket path is not reachable: %w", err)
127+
}
122128
return containerd.NewContainerdClient(socketPath)
123129
case "cri-o":
124130
if socketPath == "" {
125131
socketPath = crio.DefaultSocketPath
126132
}
133+
if _, err := os.Stat(socketPath); err != nil {
134+
return nil, fmt.Errorf("CRI-o socket path is not reachable: %w", err)
135+
}
127136
return crio.NewCrioClient(logger, socketPath)
128137
default:
129138
return nil, fmt.Errorf("unknown '%s' cri", criType)

0 commit comments

Comments
 (0)