-
Notifications
You must be signed in to change notification settings - Fork 122
/
containerd_utils.go
154 lines (120 loc) · 3.72 KB
/
containerd_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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package helpers
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
)
// ContainerdSocket is the default socket for containerd.
const (
UnixContainerdSocket = "/run/containerd/containerd.sock"
)
// ErrNoContainerd is returned when containerd is not running.
var ErrNoContainerd = errors.New("no active containerd instance found")
type ContainerdMetadata struct {
Container containerd.Container
Namespace string
}
type ContainerInfo struct {
ID string
Namespace string
ImageName string
ImageID string
Labels map[string]string
}
type namespace = string
// ContainerdInterface is the interface for containerd operations.
type ContainerdInterface interface {
Initialize() error
Containers(...namespace) (map[namespace][]containerd.Container, error)
Namespaces() ([]namespace, error)
}
// ContainerdClient is the client for containerd.
type ContainerdClient struct {
client *containerd.Client
}
// containerdError wraps the error returned by containerd.
func containerdError(err error) error {
return fmt.Errorf("containerd error: %w", err)
}
// Initialize initializes the containerd client.
func (cc *ContainerdClient) Initialize() error {
if !IsContainerdRunning() {
return containerdError(ErrNoContainerd)
}
client, err := containerd.New(UnixContainerdSocket)
if err != nil {
return containerdError(err)
}
cc.client = client
return nil
}
// Namespaces returns the list of namespaces.
func (cc *ContainerdClient) Namespaces() ([]string, error) {
namespaces, err := cc.client.NamespaceService().List(context.Background())
if err != nil {
return nil, containerdError(err)
}
return namespaces, nil
}
// Containers returns the list of containers per namespace. A list of namespaces can be provided
// as arguments. If no namespace is provided, all containers are returned.
func (cc *ContainerdClient) Containers(nss ...string) (map[namespace][]containerd.Container, error) {
if len(nss) == 0 {
allNamespaces, err := cc.Namespaces()
if err != nil {
return nil, containerdError(err)
}
nss = allNamespaces
}
return cc.containersFromNamespaces(nss)
}
func (cc *ContainerdClient) containersFromNamespaces(nss []string) (map[namespace][]containerd.Container, error) {
containersPerNamespace := map[namespace][]containerd.Container{}
for _, ns := range nss {
ctx := namespaces.WithNamespace(context.Background(), ns)
containers, err := cc.client.Containers(ctx)
if err != nil {
return nil, containerdError(err)
}
containersPerNamespace[ns] = containers
}
return containersPerNamespace, nil
}
func IsContainerdRunning() bool {
if runtime.GOOS == "windows" {
return false
}
sock, err := os.Stat(UnixContainerdSocket)
if err != nil {
return false
}
return (sock.Mode() & os.ModeSocket) != 0
}
// GetContainerdInfo returns detailed information about a container.
func GetContainerdInfo(containerMeta ContainerdMetadata) (ContainerInfo, error) {
containerID := containerMeta.Container.ID()
ctx := namespaces.WithNamespace(context.Background(), containerMeta.Namespace)
containerLabels, err := containerMeta.Container.Labels(ctx)
if err != nil {
return ContainerInfo{}, containerdError(err)
}
containerImage, err := containerMeta.Container.Image(ctx)
if err != nil {
return ContainerInfo{}, containerdError(err)
}
containerImageName := containerImage.Name()
containerImageID := containerImage.Target().Digest.String()
return ContainerInfo{
ID: containerID,
Namespace: containerMeta.Namespace,
ImageName: containerImageName,
ImageID: containerImageID,
Labels: containerLabels,
}, nil
}