-
Notifications
You must be signed in to change notification settings - Fork 671
/
k8s.go
103 lines (82 loc) · 3.71 KB
/
k8s.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
package otelutils
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const k8sSpanPathPrefix = "controller-runtime.pkg.client"
type K8sCacheWrapper struct {
cache.Cache
}
func WrapK8sCache(c cache.Cache) cache.Cache {
return &K8sCacheWrapper{c}
}
func (c *K8sCacheWrapper) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Cache/Get", k8sSpanPathPrefix))
defer span.End()
return c.Cache.Get(ctx, key, obj, opts...)
}
func (c *K8sCacheWrapper) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Cache/List", k8sSpanPathPrefix))
defer span.End()
return c.Cache.List(ctx, list, opts...)
}
type K8sClientWrapper struct {
client.Client
statusWriter *K8sStatusWriterWrapper
}
func WrapK8sClient(c client.Client) client.Client {
return &K8sClientWrapper{c, &K8sStatusWriterWrapper{c.Status()}}
}
func (c *K8sClientWrapper) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/Get", k8sSpanPathPrefix))
defer span.End()
return c.Client.Get(ctx, key, obj, opts...)
}
func (c *K8sClientWrapper) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/List", k8sSpanPathPrefix))
defer span.End()
return c.Client.List(ctx, list, opts...)
}
func (c *K8sClientWrapper) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/Create", k8sSpanPathPrefix))
defer span.End()
return c.Client.Create(ctx, obj, opts...)
}
func (c *K8sClientWrapper) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/Delete", k8sSpanPathPrefix))
defer span.End()
return c.Client.Delete(ctx, obj, opts...)
}
func (c *K8sClientWrapper) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/Update", k8sSpanPathPrefix))
defer span.End()
return c.Client.Update(ctx, obj, opts...)
}
func (c *K8sClientWrapper) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/Patch", k8sSpanPathPrefix))
defer span.End()
return c.Client.Patch(ctx, obj, patch, opts...)
}
func (c *K8sClientWrapper) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.Client/DeleteAllOf", k8sSpanPathPrefix))
defer span.End()
return c.Client.DeleteAllOf(ctx, obj, opts...)
}
func (c *K8sClientWrapper) Status() client.StatusWriter {
return c.statusWriter
}
type K8sStatusWriterWrapper struct {
client.StatusWriter
}
func (s *K8sStatusWriterWrapper) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.StatusWriter/Update", k8sSpanPathPrefix))
defer span.End()
return s.StatusWriter.Update(ctx, obj, opts...)
}
func (s *K8sStatusWriterWrapper) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
ctx, span := NewSpan(ctx, K8sClientTracer, fmt.Sprintf("%s.StatusWriter/Patch", k8sSpanPathPrefix))
defer span.End()
return s.StatusWriter.Patch(ctx, obj, patch, opts...)
}