/
wrapper.go
149 lines (117 loc) · 4.21 KB
/
wrapper.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
package engine
import (
"context"
"fmt"
"io"
"sync"
"github.com/kyverno/kyverno/pkg/clients/dclient"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
eventsv1 "k8s.io/client-go/kubernetes/typed/events/v1"
)
// Overwrite write actions to dry run
// prevents performing actual operations
type Client struct {
inner dclient.Interface
created map[string]*unstructured.Unstructured
mx *sync.RWMutex
}
func (c *Client) GetKubeClient() kubernetes.Interface {
return c.inner.GetKubeClient()
}
func (c *Client) GetEventsInterface() eventsv1.EventsV1Interface {
return c.inner.GetEventsInterface()
}
func (c *Client) GetDynamicInterface() dynamic.Interface {
return c.inner.GetDynamicInterface()
}
func (c *Client) Discovery() dclient.IDiscovery {
return c.inner.Discovery()
}
func (c *Client) SetDiscovery(discoveryClient dclient.IDiscovery) {
c.inner.SetDiscovery(discoveryClient)
}
func (c *Client) RawAbsPath(ctx context.Context, path string, method string, dataReader io.Reader) ([]byte, error) {
if method == "" {
method = "GET"
}
return c.inner.RawAbsPath(ctx, path, method, dataReader)
}
func (c *Client) GetResource(ctx context.Context, apiVersion string, kind string, namespace string, name string, subresources ...string) (*unstructured.Unstructured, error) {
if obj, ok := c.getObject(apiVersion, kind, namespace, name); ok {
return obj, nil
}
return c.inner.GetResource(ctx, apiVersion, kind, namespace, name, subresources...)
}
func (c *Client) PatchResource(_ context.Context, _ string, _ string, _ string, _ string, _ []byte) (*unstructured.Unstructured, error) {
return nil, nil
}
func (c *Client) ListResource(ctx context.Context, apiVersion string, kind string, namespace string, lselector *metav1.LabelSelector) (*unstructured.UnstructuredList, error) {
return c.inner.ListResource(ctx, apiVersion, kind, namespace, lselector)
}
func (c *Client) DeleteResource(_ context.Context, _ string, _ string, _ string, _ string, _ bool) error {
return nil
}
func (c *Client) CreateResource(_ context.Context, _ string, _ string, _ string, obj interface{}, _ bool) (*unstructured.Unstructured, error) {
if o, ok := obj.(*unstructured.Unstructured); ok {
c.addObject(o)
return o, nil
}
return nil, nil
}
func (c *Client) UpdateResource(_ context.Context, _ string, _ string, _ string, obj interface{}, _ bool, _ ...string) (*unstructured.Unstructured, error) {
if o, ok := obj.(*unstructured.Unstructured); ok {
c.addObject(o)
return o, nil
}
return nil, nil
}
func (c *Client) UpdateStatusResource(_ context.Context, _ string, _ string, _ string, obj interface{}, _ bool) (*unstructured.Unstructured, error) {
if o, ok := obj.(*unstructured.Unstructured); ok {
c.addObject(o)
return o, nil
}
return nil, nil
}
func (c *Client) ApplyResource(_ context.Context, _, _, _, _ string, obj interface{}, _ bool, _ string, _ ...string) (*unstructured.Unstructured, error) {
if o, ok := obj.(*unstructured.Unstructured); ok {
c.addObject(o)
return o, nil
}
return nil, nil
}
func (c *Client) ApplyStatusResource(_ context.Context, _, _, _, _ string, obj interface{}, _ bool, _ string) (*unstructured.Unstructured, error) {
if o, ok := obj.(*unstructured.Unstructured); ok {
c.addObject(o)
return o, nil
}
return nil, nil
}
func (c *Client) addObject(obj *unstructured.Unstructured) {
c.mx.Lock()
defer c.mx.Unlock()
c.created[keyFromObj(obj)] = obj
}
func (c *Client) getObject(apiVersion, kind, namespace, name string) (*unstructured.Unstructured, bool) {
c.mx.RLock()
defer c.mx.RUnlock()
if obj, ok := c.created[keyFromValues(apiVersion, kind, namespace, name)]; ok {
return obj, ok
}
return nil, false
}
func NewWrapper(client dclient.Interface) dclient.Interface {
return &Client{
inner: client,
created: make(map[string]*unstructured.Unstructured),
mx: new(sync.RWMutex),
}
}
func keyFromObj(obj *unstructured.Unstructured) string {
return fmt.Sprintf("%s/%s/%s/%s", obj.GetAPIVersion(), obj.GetKind(), obj.GetNamespace(), obj.GetName())
}
func keyFromValues(apiVersion, kind, namespace, name string) string {
return fmt.Sprintf("%s/%s/%s/%s", apiVersion, kind, namespace, name)
}