forked from kubewharf/katalyst-core
/
pod.go
350 lines (292 loc) · 10.8 KB
/
pod.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
Copyright 2022 The Katalyst 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 pod
import (
"context"
"fmt"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"golang.org/x/time/rate"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"github.com/kubewharf/katalyst-core/pkg/config"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)
const (
metricsNamePodCacheSync = "pod_cache_sync"
metricsNamePodCacheTotalCount = "pod_cache_total_count"
metricsNamePodCacheNotFound = "pod_cache_not_found"
metricsNamePodFetcherHealth = "pod_fetcher_health"
)
type ContextKey string
const (
BypassCacheKey ContextKey = "bypass_cache"
BypassCacheTrue ContextKey = "true"
)
type PodFetcher interface {
KubeletPodFetcher
// Run starts the preparing logic to collect pod metadata.
Run(ctx context.Context)
// GetContainerID & GetContainerSpec are used to parse running container info
GetContainerID(podUID, containerName string) (string, error)
GetContainerSpec(podUID, containerName string) (*v1.Container, error)
// GetPod returns Pod by UID
GetPod(ctx context.Context, podUID string) (*v1.Pod, error)
}
type podFetcherImpl struct {
kubeletPodFetcher KubeletPodFetcher
runtimePodFetcher RuntimePodFetcher
kubeletPodsCache map[string]*v1.Pod
kubeletPodsCacheLock sync.RWMutex
runtimePodsCache map[string]*RuntimePod
runtimePodsCacheLock sync.RWMutex
emitter metrics.MetricEmitter
conf *config.Configuration
cgroupRootPaths []string
}
func NewPodFetcher(conf *config.Configuration, emitter metrics.MetricEmitter) (PodFetcher, error) {
runtimePodFetcher, err := NewRuntimePodFetcher(conf)
if err != nil {
klog.Errorf("init runtime pod fetcher failed: %v", err)
runtimePodFetcher = nil
}
return &podFetcherImpl{
kubeletPodFetcher: NewKubeletPodFetcher(conf),
runtimePodFetcher: runtimePodFetcher,
emitter: emitter,
conf: conf,
cgroupRootPaths: common.GetKubernetesCgroupRootPathWithSubSys(common.DefaultSelectedSubsys),
}, nil
}
func (w *podFetcherImpl) GetContainerSpec(podUID, containerName string) (*v1.Container, error) {
if w == nil {
return nil, fmt.Errorf("get container spec from nil pod fetcher")
}
kubeletPodsCache, err := w.getKubeletPodsCache(context.Background())
if err != nil {
return nil, fmt.Errorf("getKubeletPodsCache failed with error: %v", err)
}
if kubeletPodsCache[podUID] == nil {
return nil, fmt.Errorf("pod of uid: %s isn't found", podUID)
}
for i := range kubeletPodsCache[podUID].Spec.Containers {
if kubeletPodsCache[podUID].Spec.Containers[i].Name == containerName {
return kubeletPodsCache[podUID].Spec.Containers[i].DeepCopy(), nil
}
}
return nil, fmt.Errorf("container: %s isn't found in pod: %s spec", containerName, podUID)
}
func (w *podFetcherImpl) GetContainerID(podUID, containerName string) (string, error) {
if w == nil {
return "", fmt.Errorf("get container id from nil pod fetcher")
}
kubeletPodsCache, err := w.getKubeletPodsCache(context.Background())
if err != nil {
return "", fmt.Errorf("getKubeletPodsCache failed with error: %v", err)
}
pod := kubeletPodsCache[podUID]
if pod == nil {
return "", fmt.Errorf("pod of uid: %s isn't found", podUID)
}
return native.GetContainerID(pod, containerName)
}
func (w *podFetcherImpl) Run(ctx context.Context) {
watcherInfo := general.FileWatcherInfo{
Path: w.cgroupRootPaths,
Filename: "",
Op: fsnotify.Create,
}
watcherCh, err := general.RegisterFileEventWatcher(ctx.Done(), watcherInfo)
if err != nil {
klog.Fatalf("register file event watcher failed: %s", err)
}
timer := time.NewTimer(w.conf.KubeletPodCacheSyncPeriod)
rateLimiter := rate.NewLimiter(w.conf.KubeletPodCacheSyncMaxRate, w.conf.KubeletPodCacheSyncBurstBulk)
go func() {
for {
select {
case <-watcherCh:
if rateLimiter.Allow() {
w.syncKubeletPod(ctx)
timer.Reset(w.conf.KubeletPodCacheSyncPeriod)
}
case <-timer.C:
w.syncKubeletPod(ctx)
timer.Reset(w.conf.KubeletPodCacheSyncPeriod)
case <-ctx.Done():
klog.Infof("file event watcher stopped")
klog.Infof("stop timer channel when ctx.Done() has been received")
timer.Stop()
return
}
}
}()
go wait.UntilWithContext(ctx, w.syncRuntimePod, w.conf.RuntimePodCacheSyncPeriod)
go wait.Until(w.checkPodCache, 30*time.Second, ctx.Done())
<-ctx.Done()
}
func (w *podFetcherImpl) GetPodList(ctx context.Context, podFilter func(*v1.Pod) bool) ([]*v1.Pod, error) {
kubeletPodsCache, err := w.getKubeletPodsCache(ctx)
if err != nil {
return nil, fmt.Errorf("getKubeletPodsCache failed with error: %v", err)
}
res := make([]*v1.Pod, 0, len(kubeletPodsCache))
for _, p := range kubeletPodsCache {
if podFilter != nil && !podFilter(p) {
continue
}
res = append(res, p.DeepCopy())
}
return res, nil
}
func (w *podFetcherImpl) GetPod(ctx context.Context, podUID string) (*v1.Pod, error) {
kubeletPodsCache, err := w.getKubeletPodsCache(ctx)
if err != nil {
return nil, fmt.Errorf("getKubeletPodsCache failed with error: %v", err)
}
if pod, ok := kubeletPodsCache[podUID]; ok {
return pod, nil
}
return nil, fmt.Errorf("failed to find pod by uid %v", podUID)
}
func (w *podFetcherImpl) getKubeletPodsCache(ctx context.Context) (map[string]*v1.Pod, error) {
// if current kubelet pod cache is nil or enforce bypass, we sync cache first
w.kubeletPodsCacheLock.RLock()
if w.kubeletPodsCache == nil || len(w.kubeletPodsCache) == 0 || ctx.Value(BypassCacheKey) == BypassCacheTrue {
w.kubeletPodsCacheLock.RUnlock()
w.syncKubeletPod(ctx)
} else {
w.kubeletPodsCacheLock.RUnlock()
}
// the second time checks if the kubelet pod cache is nil, if it is,
// it means the first sync of the kubelet pod failed and returns an error
w.kubeletPodsCacheLock.RLock()
defer w.kubeletPodsCacheLock.RUnlock()
if w.kubeletPodsCache == nil || len(w.kubeletPodsCache) == 0 {
return nil, fmt.Errorf("first sync kubelet pod cache failed")
}
return w.kubeletPodsCache, nil
}
// syncRuntimePod sync local runtime pod cache from runtime pod fetcher.
func (w *podFetcherImpl) syncRuntimePod(_ context.Context) {
if w.runtimePodFetcher == nil {
klog.Error("runtime pod fetcher init not success")
_ = w.emitter.StoreInt64("pod_cache_runtime_init_failed", 1, metrics.MetricTypeNameRaw)
return
}
runtimePods, err := w.runtimePodFetcher.GetPods(false)
if err != nil {
klog.Errorf("sync runtime pod failed: %s", err)
_ = w.emitter.StoreInt64(metricsNamePodCacheSync, 1, metrics.MetricTypeNameCount,
metrics.ConvertMapToTags(map[string]string{
"source": "runtime",
"success": "false",
})...)
return
}
_ = w.emitter.StoreInt64(metricsNamePodCacheSync, 1, metrics.MetricTypeNameCount,
metrics.ConvertMapToTags(map[string]string{
"source": "runtime",
"success": "true",
})...)
runtimePodsCache := make(map[string]*RuntimePod, len(runtimePods))
for _, p := range runtimePods {
runtimePodsCache[string(p.UID)] = p
}
w.runtimePodsCacheLock.Lock()
w.runtimePodsCache = runtimePodsCache
w.runtimePodsCacheLock.Unlock()
}
// syncKubeletPod sync local kubelet pod cache from kubelet pod fetcher.
func (w *podFetcherImpl) syncKubeletPod(ctx context.Context) {
kubeletPods, err := w.kubeletPodFetcher.GetPodList(ctx, nil)
if err != nil {
klog.Errorf("sync kubelet pod failed: %s", err)
_ = w.emitter.StoreInt64(metricsNamePodCacheSync, 1, metrics.MetricTypeNameCount,
metrics.ConvertMapToTags(map[string]string{
"source": "kubelet",
"success": "false",
"reason": "error",
})...)
return
} else if len(kubeletPods) == 0 {
klog.Error("kubelet pod is empty")
_ = w.emitter.StoreInt64(metricsNamePodCacheSync, 1, metrics.MetricTypeNameCount,
metrics.ConvertMapToTags(map[string]string{
"source": "kubelet",
"success": "false",
"reason": "empty",
})...)
return
}
_ = w.emitter.StoreInt64(metricsNamePodCacheSync, 1, metrics.MetricTypeNameCount,
metrics.ConvertMapToTags(map[string]string{
"source": "kubelet",
"success": "true",
})...)
kubeletPodsCache := make(map[string]*v1.Pod, len(kubeletPods))
for _, p := range kubeletPods {
kubeletPodsCache[string(p.GetUID())] = p
}
w.kubeletPodsCacheLock.Lock()
w.kubeletPodsCache = kubeletPodsCache
w.kubeletPodsCacheLock.Unlock()
}
// checkPodCache if the runtime pod and kubelet pod match, and send a metric alert if they don't.
func (w *podFetcherImpl) checkPodCache() {
w.kubeletPodsCacheLock.RLock()
kubeletPodsCache := w.kubeletPodsCache
w.kubeletPodsCacheLock.RUnlock()
w.runtimePodsCacheLock.RLock()
runtimePodsCache := w.runtimePodsCache
w.runtimePodsCacheLock.RUnlock()
_ = w.emitter.StoreInt64(metricsNamePodFetcherHealth, 1, metrics.MetricTypeNameRaw)
klog.Infof("total kubelet pod count is %d", len(kubeletPodsCache))
_ = w.emitter.StoreInt64(metricsNamePodCacheTotalCount, int64(len(kubeletPodsCache)), metrics.MetricTypeNameRaw,
metrics.ConvertMapToTags(map[string]string{
"source": "kubelet",
})...)
klog.Infof("total runtime pod count is %d", len(runtimePodsCache))
_ = w.emitter.StoreInt64(metricsNamePodCacheTotalCount, int64(len(runtimePodsCache)), metrics.MetricTypeNameRaw,
metrics.ConvertMapToTags(map[string]string{
"source": "runtime",
})...)
runtimeNotFoundPodCount := 0
for id, p := range kubeletPodsCache {
// we only care about running kubelet pods here, because pods in other stages may not exist in runtime
if _, ok := runtimePodsCache[id]; !ok && p.Status.Phase == v1.PodRunning {
klog.Warningf("running kubelet pod %s/%s with uid %s runtime not found", p.Namespace, p.Name, p.UID)
runtimeNotFoundPodCount += 1
}
}
_ = w.emitter.StoreInt64(metricsNamePodCacheNotFound, int64(runtimeNotFoundPodCount), metrics.MetricTypeNameRaw,
metrics.ConvertMapToTags(map[string]string{
"source": "runtime",
})...)
kubeletNotFoundPodCount := 0
for id, p := range runtimePodsCache {
if _, ok := kubeletPodsCache[id]; !ok {
klog.Warningf("runtime pod %s/%s with uid %s kubelet not found", p.Namespace, p.Name, p.UID)
kubeletNotFoundPodCount += 1
}
}
_ = w.emitter.StoreInt64(metricsNamePodCacheNotFound, int64(kubeletNotFoundPodCount), metrics.MetricTypeNameRaw,
metrics.ConvertMapToTags(map[string]string{
"source": "kubelet",
})...)
}