-
Notifications
You must be signed in to change notification settings - Fork 246
/
mock.go
188 lines (156 loc) · 6.16 KB
/
mock.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
package kubetest
import (
"context"
"sync"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/managedfields"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/openapi"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
)
type KubectlOutput struct {
Output string
Err error
}
type MockKubectlCmd struct {
APIResources []kube.APIResourceInfo
Commands map[string]KubectlOutput
Events chan watch.Event
Version string
DynamicClient dynamic.Interface
lastCommandPerResource map[kube.ResourceKey]string
lastValidate bool
serverSideApply bool
recordLock sync.RWMutex
convertToVersionFunc *func(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error)
getResourceFunc *func(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string) (*unstructured.Unstructured, error)
}
// WithConvertToVersionFunc overrides the default ConvertToVersion behavior.
func (k *MockKubectlCmd) WithConvertToVersionFunc(convertToVersionFunc func(*unstructured.Unstructured, string, string) (*unstructured.Unstructured, error)) *MockKubectlCmd {
k.convertToVersionFunc = &convertToVersionFunc
return k
}
// WithGetResourceFunc overrides the default ConvertToVersion behavior.
func (k *MockKubectlCmd) WithGetResourceFunc(getResourcefunc func(context.Context, *rest.Config, schema.GroupVersionKind, string, string) (*unstructured.Unstructured, error)) *MockKubectlCmd {
k.getResourceFunc = &getResourcefunc
return k
}
func (k *MockKubectlCmd) GetLastResourceCommand(key kube.ResourceKey) string {
k.recordLock.Lock()
defer k.recordLock.Unlock()
if k.lastCommandPerResource == nil {
return ""
}
return k.lastCommandPerResource[key]
}
func (k *MockKubectlCmd) SetLastResourceCommand(key kube.ResourceKey, cmd string) {
k.recordLock.Lock()
if k.lastCommandPerResource == nil {
k.lastCommandPerResource = map[kube.ResourceKey]string{}
}
k.lastCommandPerResource[key] = cmd
k.recordLock.Unlock()
}
func (k *MockKubectlCmd) SetLastValidate(validate bool) {
k.recordLock.Lock()
k.lastValidate = validate
k.recordLock.Unlock()
}
func (k *MockKubectlCmd) GetLastValidate() bool {
k.recordLock.RLock()
validate := k.lastValidate
k.recordLock.RUnlock()
return validate
}
func (k *MockKubectlCmd) SetLastServerSideApply(serverSideApply bool) {
k.recordLock.Lock()
k.serverSideApply = serverSideApply
k.recordLock.Unlock()
}
func (k *MockKubectlCmd) GetLastServerSideApply() bool {
k.recordLock.RLock()
serverSideApply := k.serverSideApply
k.recordLock.RUnlock()
return serverSideApply
}
func (k *MockKubectlCmd) NewDynamicClient(config *rest.Config) (dynamic.Interface, error) {
return k.DynamicClient, nil
}
func (k *MockKubectlCmd) GetAPIResources(config *rest.Config, preferred bool, resourceFilter kube.ResourceFilter) ([]kube.APIResourceInfo, error) {
return k.APIResources, nil
}
func (k *MockKubectlCmd) GetResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string) (*unstructured.Unstructured, error) {
if k.getResourceFunc != nil {
return (*k.getResourceFunc)(ctx, config, gvk, name, namespace)
}
return nil, nil
}
func (k *MockKubectlCmd) PatchResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, patchType types.PatchType, patchBytes []byte, subresources ...string) (*unstructured.Unstructured, error) {
return nil, nil
}
func (k *MockKubectlCmd) DeleteResource(ctx context.Context, config *rest.Config, gvk schema.GroupVersionKind, name string, namespace string, deleteOptions metav1.DeleteOptions) error {
command, ok := k.Commands[name]
if !ok {
return nil
}
return command.Err
}
func (k *MockKubectlCmd) CreateResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, validate bool) (string, error) {
k.SetLastResourceCommand(kube.GetResourceKey(obj), "create")
command, ok := k.Commands[obj.GetName()]
if !ok {
return "", nil
}
return command.Output, command.Err
}
func (k *MockKubectlCmd) UpdateResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy) (*unstructured.Unstructured, error) {
k.SetLastResourceCommand(kube.GetResourceKey(obj), "update")
command, ok := k.Commands[obj.GetName()]
if !ok {
return obj, nil
}
return obj, command.Err
}
func (k *MockKubectlCmd) ApplyResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force, validate, serverSideApply bool) (string, error) {
k.SetLastValidate(validate)
k.SetLastServerSideApply(serverSideApply)
k.SetLastResourceCommand(kube.GetResourceKey(obj), "apply")
command, ok := k.Commands[obj.GetName()]
if !ok {
return "", nil
}
return command.Output, command.Err
}
func (k *MockKubectlCmd) ReplaceResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force bool) (string, error) {
command, ok := k.Commands[obj.GetName()]
k.SetLastResourceCommand(kube.GetResourceKey(obj), "replace")
if !ok {
return "", nil
}
return command.Output, command.Err
}
// ConvertToVersion converts an unstructured object into the specified group/version
func (k *MockKubectlCmd) ConvertToVersion(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error) {
if k.convertToVersionFunc != nil {
return (*k.convertToVersionFunc)(obj, group, version)
}
return obj, nil
}
func (k *MockKubectlCmd) GetServerVersion(config *rest.Config) (string, error) {
return k.Version, nil
}
func (k *MockKubectlCmd) LoadOpenAPISchema(config *rest.Config) (openapi.Resources, *managedfields.GvkParser, error) {
return nil, nil, nil
}
func (k *MockKubectlCmd) SetOnKubectlRun(onKubectlRun kube.OnKubectlRunFunc) {
}
func (k *MockKubectlCmd) ManageResources(config *rest.Config, openAPISchema openapi.Resources) (kube.ResourceOperations, func(), error) {
return k, func() {
}, nil
}