-
Notifications
You must be signed in to change notification settings - Fork 11
/
generic_lookup_func.go
299 lines (244 loc) · 7.86 KB
/
generic_lookup_func.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
// Copyright (c) 2020 Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
package templates
import (
"context"
"errors"
"fmt"
"strings"
"github.com/stolostron/kubernetes-dependency-watches/client"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/klog"
)
type ClusterScopedLookupRestrictedError struct {
kind string
name string
}
func (e ClusterScopedLookupRestrictedError) Error() string {
return fmt.Sprintf("lookup of cluster-scoped resource '%v/%v' is not allowed", e.kind, e.name)
}
// getNamespace checks that the target namespace is allowed based on the configured
// lookupNamespace. If it's not, an error is returned. It then returns the namespace
// that should be used. If the target namespace is not set and the lookupNamespace
// configuration is, then the namespace of lookupNamespace is returned for convenience.
func (t *TemplateResolver) getNamespace(namespace string, lookupNamespace string) (string, error) {
// When lookupNamespace is an empty string, there are no namespace restrictions.
if lookupNamespace != "" {
// If lookupNamespace is set but namespace is an empty string, then default
// to lookupNamespace for convenience
if namespace == "" {
return lookupNamespace, nil
}
if lookupNamespace != namespace {
return "", fmt.Errorf("%w to %s", ErrRestrictedNamespace, lookupNamespace)
}
}
return namespace, nil
}
func (t *TemplateResolver) getOrList(
options *ResolveOptions,
templateResult *TemplateResult,
apiVersion string,
kind string,
namespace string,
name string,
labelSelector ...string,
) (
map[string]interface{}, error,
) {
if options == nil {
options = &ResolveOptions{}
}
if apiVersion == "" || kind == "" {
return nil, errors.New("the apiVersion and kind are required")
}
ns, err := t.getNamespace(namespace, options.LookupNamespace)
if err != nil {
return nil, err
}
gv, err := schema.ParseGroupVersion(apiVersion)
if err != nil {
return nil, err
}
gvk := schema.GroupVersionKind{
Group: gv.Group,
Version: gv.Version,
Kind: kind,
}
parsedSelector := labels.NewSelector()
// If labelSelector is defined, and is not an empty string, then add the labels to the listOptions
// Note there can be multiple values passed to labelSelector so we need to treat it as an array
// The ListOption requires a single string value.
if len(labelSelector) > 0 && labelSelector[0] != "" {
// We use the labels.Parse to validate the selector given.
// this should give us a better error output if the user misconfigured the selector
parsedSelector, err = labels.Parse(strings.Join(labelSelector, ","))
if err != nil {
return nil, err
}
}
var scopedGVRObj client.ScopedGVR
if t.dynamicWatcher != nil {
scopedGVRObj, err = t.dynamicWatcher.GVKToGVR(gvk)
} else {
scopedGVRObj, err = t.tempCallCache.GVKToGVR(gvk)
}
if err != nil {
if errors.Is(err, client.ErrNoVersionedResource) {
return nil, ErrMissingAPIResource
}
return nil, err
}
if !scopedGVRObj.Namespaced && options.LookupNamespace != "" {
rsrcIdentifier := ClusterScopedObjectIdentifier{
Group: scopedGVRObj.Group,
Kind: kind,
Name: name,
}
if !onAllowlist(options.ClusterScopedAllowList, rsrcIdentifier) {
return nil, ClusterScopedLookupRestrictedError{kind, name}
}
}
if t.dynamicWatcher != nil {
if name == "" {
result, err := t.dynamicWatcher.List(*options.Watcher, gvk, ns, parsedSelector)
if err != nil {
return nil, err
}
resultList := unstructured.UnstructuredList{Items: result}
if templateResult != nil && kind == "Secret" && len(resultList.Items) > 0 {
templateResult.HasSensitiveData = true
}
return resultList.UnstructuredContent(), nil
}
result, err := t.dynamicWatcher.Get(*options.Watcher, gvk, ns, name)
if err != nil {
return nil, err
}
if result == nil {
return nil, apierrors.NewNotFound(scopedGVRObj.GroupResource(), name)
}
if templateResult != nil && kind == "Secret" {
templateResult.HasSensitiveData = true
}
return result.UnstructuredContent(), nil
}
// The dynamic watcher is not used, so use the temporary call cache
lookupID := client.ObjectIdentifier{
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind,
Namespace: ns,
Name: name,
Selector: parsedSelector.String(),
}
cachedResults, err := t.tempCallCache.FromObjectIdentifier(lookupID)
if err != nil {
if !errors.Is(err, client.ErrNoCacheEntry) {
return nil, err
}
} else {
// Check if this is a Get or List query
if name != "" {
if len(cachedResults) > 0 {
return cachedResults[0].UnstructuredContent(), nil
}
return nil, nil
}
resultList := unstructured.UnstructuredList{Items: cachedResults}
return resultList.UnstructuredContent(), nil
}
// It's not cached so it must be retrieved using the dynamic client and then cached
var dynamciClientRes dynamic.ResourceInterface
if scopedGVRObj.Namespaced && ns != "" {
dynamciClientRes = t.dynamicClient.Resource(scopedGVRObj.GroupVersionResource).Namespace(ns)
} else {
dynamciClientRes = t.dynamicClient.Resource(scopedGVRObj.GroupVersionResource)
}
if name == "" {
resultUnstructuredList, err := dynamciClientRes.List(
context.TODO(), metav1.ListOptions{LabelSelector: parsedSelector.String()},
)
if err != nil {
return nil, err
}
t.tempCallCache.CacheFromObjectIdentifier(lookupID, resultUnstructuredList.Items)
// Strip out the other metadata to match what is returned from the cache
resultUnstructuredList = &unstructured.UnstructuredList{Items: resultUnstructuredList.Items}
if templateResult != nil && kind == "Secret" && len(resultUnstructuredList.Items) > 0 {
templateResult.HasSensitiveData = true
}
return resultUnstructuredList.UnstructuredContent(), nil
}
resultUnstructured, err := dynamciClientRes.Get(context.TODO(), name, metav1.GetOptions{})
if err == nil {
t.tempCallCache.CacheFromObjectIdentifier(lookupID, []unstructured.Unstructured{*resultUnstructured})
}
if err != nil {
// Cache a not found result
if apierrors.IsNotFound(err) {
t.tempCallCache.CacheFromObjectIdentifier(lookupID, []unstructured.Unstructured{})
}
return nil, err
}
if templateResult != nil && kind == "Secret" {
templateResult.HasSensitiveData = true
}
return resultUnstructured.UnstructuredContent(), nil
}
func (t *TemplateResolver) lookupHelper(
options *ResolveOptions,
templateResult *TemplateResult,
) func(string, string, string, string, ...string) (map[string]interface{}, error) {
return func(
apiVersion string,
kind string,
namespace string,
name string,
labelSelector ...string,
) (map[string]interface{}, error) {
return t.lookup(options, templateResult, apiVersion, kind, namespace, name, labelSelector...)
}
}
func (t *TemplateResolver) lookup(
options *ResolveOptions,
templateResult *TemplateResult,
apiVersion string,
kind string,
namespace string,
name string,
labelSelector ...string,
) (
map[string]interface{}, error,
) {
klog.V(2).Infof("lookup : %v, %v, %v, %v", apiVersion, kind, namespace, name)
result, lookupErr := t.getOrList(options, templateResult, apiVersion, kind, namespace, name, labelSelector...)
// lookups don't fail on errors
if apierrors.IsNotFound(lookupErr) {
lookupErr = nil
}
klog.V(2).Infof("lookup result: %v", result)
return result, lookupErr
}
func onAllowlist(allowlist []ClusterScopedObjectIdentifier, rsrc ClusterScopedObjectIdentifier) bool {
if len(allowlist) == 0 {
return false
}
for _, item := range allowlist {
if item.Group != "*" && item.Group != rsrc.Group {
continue
}
if item.Kind != "*" && item.Kind != rsrc.Kind {
continue
}
if item.Name == "*" || item.Name == rsrc.Name {
return true
}
}
return false
}