-
Notifications
You must be signed in to change notification settings - Fork 67
/
kubernetes.go
259 lines (233 loc) · 7.89 KB
/
kubernetes.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2022-2024 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
"context"
"fmt"
"os"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
profilestorepb "github.com/parca-dev/parca/gen/proto/go/parca/profilestore/v1alpha1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/parca-dev/parca-agent/pkg/cgroup"
"github.com/parca-dev/parca-agent/pkg/discovery/kubernetes/containerruntimes"
"github.com/parca-dev/parca-agent/pkg/discovery/kubernetes/containerruntimes/containerd"
"github.com/parca-dev/parca-agent/pkg/discovery/kubernetes/containerruntimes/crio"
"github.com/parca-dev/parca-agent/pkg/discovery/kubernetes/containerruntimes/docker"
"github.com/parca-dev/parca-agent/pkg/namespace"
)
const KubeConfigEnv = "KUBECONFIG"
type Client struct {
logger log.Logger
clientset *kubernetes.Clientset
nodeName string
fieldSelector string
criClient containerruntimes.CRIClient
}
func NewKubernetesClient(logger log.Logger, nodeName, socketPath string) (*Client, error) {
var (
config *rest.Config
err error
)
kubeconfigFile := os.Getenv(KubeConfigEnv)
if kubeconfigFile != "" {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfigFile)
if err != nil {
return nil, fmt.Errorf("create config from %s: %w", kubeconfigFile, err)
}
} else {
config, err = rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("create in-cluster config: %w", err)
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("create clientset: %w", err)
}
fieldSelector := fields.OneTermEqualSelector("spec.nodeName", nodeName).String()
node, err := clientset.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("get node %v: %w", nodeName, err)
}
// get a CRI client to talk to the CRI handling pods in this node
criClient, err := newCRIClient(logger, node, socketPath)
if err != nil {
return nil, fmt.Errorf("create CRI client: %w", err)
}
return &Client{
logger: logger,
clientset: clientset,
nodeName: nodeName,
fieldSelector: fieldSelector,
criClient: criClient,
}, nil
}
func (c *Client) Clientset() kubernetes.Interface {
return c.clientset
}
func newCRIClient(logger log.Logger, node *v1.Node, socketPath string) (containerruntimes.CRIClient, error) {
criVersion := node.Status.NodeInfo.ContainerRuntimeVersion
list := strings.Split(criVersion, "://")
if len(list) < 1 {
return nil, fmt.Errorf("impossible to get CRI type from %s", criVersion)
}
criType := list[0]
switch criType {
case "docker":
if socketPath == "" {
level.Debug(logger).Log("msg", "no docker socket path provided, using default", "path", docker.DefaultSocketPath)
socketPath = docker.DefaultSocketPath
}
if _, err := os.Stat(socketPath); err != nil {
return nil, fmt.Errorf("docker socket path is not reachable: %w", err)
}
return docker.NewDockerClient(socketPath)
case "containerd":
if socketPath == "" {
if _, err := os.Stat(containerd.DefaultSocketPath); err == nil {
level.Debug(logger).Log("msg", "no containerd socket path provided, using default", "path", containerd.DefaultSocketPath)
socketPath = containerd.DefaultSocketPath
}
if _, err := os.Stat(containerd.DefaultK3SSocketPath); err == nil {
if socketPath != "" {
level.Warn(logger).Log("msg", "multiple containerd socket paths found, using k3s", "path", containerd.DefaultK3SSocketPath)
} else {
level.Debug(logger).Log("msg", "no k3s containerd socket path provided, using default", "path", containerd.DefaultK3SSocketPath)
}
socketPath = containerd.DefaultK3SSocketPath
}
}
if _, err := os.Stat(socketPath); err != nil {
return nil, fmt.Errorf("containerd socket path is not reachable: %w", err)
}
return containerd.NewContainerdClient(socketPath)
case "cri-o":
if socketPath == "" {
level.Debug(logger).Log("msg", "no cri-o socket path provided, using default", "path", crio.DefaultSocketPath)
socketPath = crio.DefaultSocketPath
}
if _, err := os.Stat(socketPath); err != nil {
return nil, fmt.Errorf("CRI-o socket path is not reachable: %w", err)
}
return crio.NewCrioClient(logger, socketPath)
default:
return nil, fmt.Errorf("unknown '%s' cri", criType)
}
}
func (c *Client) Close() error {
return c.criClient.Close()
}
type ContainerDefinition struct {
NodeName string
ContainerID string
CgroupPath string
CgroupID uint64
Mntns uint64
Namespace string
PodName string
ContainerName string
PodLabels map[string]string
CgroupV1 string
CgroupV2 string
MountSources []string
PID int
}
func (c *ContainerDefinition) Labels() []*profilestorepb.Label {
return []*profilestorepb.Label{{
Name: "node",
Value: c.NodeName,
}, {
Name: "namespace",
Value: c.Namespace,
}, {
Name: "pod",
Value: c.PodName,
}, {
Name: "container",
Value: c.ContainerName,
}, {
Name: "containerid",
Value: c.ContainerID,
}}
}
// PodToContainers return a list of the containers of a given Pod.
// Containers that are not running or don't have an ID are not considered.
func (c *Client) PodToContainers(pod *v1.Pod) []*ContainerDefinition {
containers := []*ContainerDefinition{}
for _, s := range pod.Status.ContainerStatuses {
if s.ContainerID == "" {
continue
}
if s.State.Running == nil {
continue
}
pid, err := c.criClient.PIDFromContainerID(s.ContainerID)
if err != nil {
level.Debug(c.logger).Log("msg", "skipping pod, cannot find pid", "namespace", pod.GetNamespace(), "pod", pod.GetName(), "err", err)
continue
}
cgroupPathV1, cgroupPathV2, err := cgroup.Paths(pid)
if err != nil {
level.Debug(c.logger).Log("msg", "skipping pod, cannot find cgroup path", "namespace", pod.GetNamespace(), "pod", pod.GetName(), "err", err)
continue
}
cgroupPathV2WithMountpoint, _ := cgroup.PathV2AddMountpoint(cgroupPathV2)
cgroupID, _ := cgroup.ID(cgroupPathV2WithMountpoint)
mntns, err := namespace.MountNamespaceInode(pid) // linux namespace.
if err != nil {
level.Debug(c.logger).Log("msg", "skipping pod, cannot find mnt namespace", "namespace", pod.GetNamespace(), "pod", pod.GetName(), "err", err)
continue
}
containerDef := &ContainerDefinition{
NodeName: c.nodeName,
ContainerID: s.ContainerID,
CgroupPath: cgroupPathV2WithMountpoint,
CgroupID: cgroupID,
Mntns: mntns,
Namespace: pod.GetNamespace(), // kubernetes namespace.
PodName: pod.GetName(),
ContainerName: s.Name,
PodLabels: pod.ObjectMeta.Labels,
PID: pid,
CgroupV1: cgroupPathV1,
CgroupV2: cgroupPathV2,
}
containers = append(containers, containerDef)
}
return containers
}
// ListContainers return a list of the current containers that are
// running in the node.
func (c *Client) ListContainers() ([]*ContainerDefinition, error) {
// List pods
pods, err := c.clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{
FieldSelector: c.fieldSelector,
})
if err != nil {
return nil, err
}
var arr []*ContainerDefinition
for _, p := range pods.Items {
pod := p
containers := c.PodToContainers(&pod)
arr = append(arr, containers...)
}
return arr, nil
}