-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathpackage_crd_rest.go
413 lines (348 loc) · 13.4 KB
/
package_crd_rest.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package datapackaging
import (
"context"
"fmt"
"time"
"carvel.dev/kapp-controller/pkg/apiserver/apis/datapackaging"
"carvel.dev/kapp-controller/pkg/apiserver/apis/datapackaging/validation"
installclient "carvel.dev/kapp-controller/pkg/client/clientset/versioned"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/kubernetes"
)
// PackageCRDREST is a rest implementation that proxies the rest endpoints provided by
// CRDs. This will allow us to introduce the api server without the
// complexities associated with custom storage options for now.
type PackageCRDREST struct {
crdClient installclient.Interface
nsClient kubernetes.Interface
globalNamespace string
logger logr.Logger
}
var (
_ rest.StandardStorage = &PackageCRDREST{}
_ rest.ShortNamesProvider = &PackageCRDREST{}
)
// NewPackageCRDREST creates a new instance of the PackageCRDREST type
func NewPackageCRDREST(crdClient installclient.Interface, nsClient kubernetes.Interface, globalNS string, logger logr.Logger) *PackageCRDREST {
return &PackageCRDREST{crdClient, nsClient, globalNS, logger}
}
func (r *PackageCRDREST) ShortNames() []string {
return []string{"pkg"}
}
// GetSingularName returns singular name of resources.
func (r *PackageCRDREST) GetSingularName() string {
return string("package")
}
func (r *PackageCRDREST) NamespaceScoped() bool {
return true
}
func (r *PackageCRDREST) New() runtime.Object {
return &datapackaging.Package{}
}
// Destroy satisfies the apiserver.registry.storage interface but it's a no-op
func (r *PackageCRDREST) Destroy() {
return
}
func (r *PackageCRDREST) NewList() runtime.Object {
return &datapackaging.PackageList{}
}
func (r *PackageCRDREST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
if createValidation != nil {
if err := createValidation(ctx, obj); err != nil {
return nil, err
}
}
pkg := obj.(*datapackaging.Package)
errs := validation.ValidatePackage(*pkg)
if len(errs) != 0 {
return nil, errors.NewInvalid(pkg.GroupVersionKind().GroupKind(), pkg.Name, errs)
}
return client.Create(ctx, namespace, pkg, *options)
}
func (r *PackageCRDREST) shouldFetchGlobal(ctx context.Context, namespace string) bool {
ns, err := r.nsClient.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
return false
}
_, exclude := ns.ObjectMeta.Annotations[excludeGlobalPackagesAnn]
return namespace != r.globalNamespace && namespace != "" && !exclude
}
func (r *PackageCRDREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
pkg, err := client.Get(ctx, namespace, name, *options)
if errors.IsNotFound(err) && r.shouldFetchGlobal(ctx, namespace) {
pkg, err = client.Get(ctx, r.globalNamespace, name, *options)
}
return pkg, err
}
func (r *PackageCRDREST) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
// field selector isnt supported by CRD's so reset it, we will apply it later
fs := options.FieldSelector
options.FieldSelector = fields.Everything()
var pkgs []datapackaging.Package
if r.shouldFetchGlobal(ctx, namespace) {
globalPkgList, err := client.List(ctx, r.globalNamespace, r.internalToMetaListOpts(*options))
if err != nil {
return nil, err
}
pkgs = globalPkgList.Items
}
namespacedPkgList, err := client.List(ctx, namespace, r.internalToMetaListOpts(*options))
if err != nil {
return nil, err
}
namespacedPkgs := namespacedPkgList.Items
pkgIndex := make(map[string]int)
for i, pkg := range pkgs {
identifier := pkg.Namespace + "/" + pkg.Spec.RefName + "." + pkg.Spec.Version
pkgIndex[identifier] = i
}
// if exists as global, overwrite, else append
for _, pkg := range namespacedPkgs {
identifier := pkg.Namespace + "/" + pkg.Spec.RefName + "." + pkg.Spec.Version
if index, found := pkgIndex[identifier]; found {
pkgs[index] = pkg
} else {
pkgs = append(pkgs, pkg)
}
}
pkgList := &datapackaging.PackageList{
TypeMeta: namespacedPkgList.TypeMeta,
ListMeta: namespacedPkgList.ListMeta,
Items: pkgs,
}
return r.applySelector(pkgList, fs), err
}
func (r *PackageCRDREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
pkg, err := client.Get(ctx, namespace, name, metav1.GetOptions{})
if errors.IsNotFound(err) {
// Because kubetl does a get before sending an update, the presence
// of a global package may cause it to send a patch request, even though
// the package doesn't exist in the namespace. To service this, we must check
// if the package exists globally and then patch that instead of patching an empty
// package. If we try patching an empty obj the patch UpdatedObjectInfo will blow up.
patchingGlobal := true
pkg, err := client.Get(ctx, r.globalNamespace, name, metav1.GetOptions{})
if errors.IsNotFound(err) {
pkg = &datapackaging.Package{}
patchingGlobal = false
}
updatedObj, err := objInfo.UpdatedObject(ctx, pkg)
if err != nil {
return nil, false, err
}
if createValidation != nil {
if err := createValidation(ctx, updatedObj); err != nil {
return nil, false, err
}
}
updatedPkg := updatedObj.(*datapackaging.Package)
if patchingGlobal {
// we have to do this in case we are "patching" a global package
annotations := updatedPkg.ObjectMeta.Annotations
labels := updatedPkg.ObjectMeta.Labels
updatedPkg.ObjectMeta = metav1.ObjectMeta{}
updatedPkg.ObjectMeta.Name = name
updatedPkg.ObjectMeta.Namespace = namespace
updatedPkg.ObjectMeta.Annotations = annotations
updatedPkg.ObjectMeta.Labels = labels
}
obj, err := r.Create(ctx, updatedPkg, createValidation, &metav1.CreateOptions{TypeMeta: options.TypeMeta, DryRun: options.DryRun, FieldManager: options.FieldManager})
if err != nil {
return nil, true, err
}
return obj, true, nil
}
if err != nil {
return nil, false, err
}
updatedObj, err := objInfo.UpdatedObject(ctx, pkg)
if err != nil {
return nil, false, err
}
updatedPkg := updatedObj.(*datapackaging.Package)
errs := validation.ValidatePackage(*updatedPkg)
if len(errs) != 0 {
return nil, false, errors.NewInvalid(updatedPkg.GroupVersionKind().GroupKind(), updatedPkg.Name, errs)
}
updatedPkg, err = client.Update(ctx, namespace, updatedPkg, *options)
return updatedPkg, false, err
}
func (r *PackageCRDREST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
pkg, err := client.Get(ctx, namespace, name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil, true, err
}
if err != nil {
return nil, false, err
}
if deleteValidation != nil {
if err := deleteValidation(ctx, pkg); err != nil {
return nil, true, err
}
}
err = client.Delete(ctx, namespace, pkg.Name, *options)
if err != nil {
return nil, false, err
}
return pkg, true, nil
}
func (r *PackageCRDREST) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
// clear unsupported field selectors
fs := listOptions.FieldSelector
listOptions.FieldSelector = fields.Everything()
list, err := client.List(ctx, namespace, r.internalToMetaListOpts(*listOptions))
if err != nil {
return nil, err
}
list = r.applySelector(list, fs)
deleteAllGlobal := false
{
filteredList, err := client.List(ctx, r.globalNamespace, r.internalToMetaListOpts(*listOptions))
if err != nil {
return nil, err
}
regularList, err := client.List(ctx, r.globalNamespace, metav1.ListOptions{})
if err != nil {
return nil, err
}
deleteAllGlobal = len(regularList.Items) == len(filteredList.Items)
}
if deleteAllGlobal {
err := r.deleteGlobalPackagesFromNS(ctx, namespace)
if err != nil {
return nil, errors.NewInternalError(fmt.Errorf("Removing global packages from ns '%s': %v", namespace, err))
}
}
var deletedPackages []datapackaging.Package
for _, pv := range list.Items {
// use crd delete for validations
_, _, err := r.Delete(ctx, pv.Name, deleteValidation, options)
if err != nil {
break
}
deletedPackages = append(deletedPackages, pv)
}
return &datapackaging.PackageList{Items: deletedPackages}, err
}
func (r *PackageCRDREST) Watch(ctx context.Context, options *internalversion.ListOptions) (watch.Interface, error) {
namespace := request.NamespaceValue(ctx)
client := NewPackageStorageClient(r.crdClient, NewPackageTranslator(namespace, r.logger))
watcher, err := client.Watch(ctx, namespace, r.internalToMetaListOpts(*options))
if errors.IsNotFound(err) && namespace != r.globalNamespace {
watcher, err = client.Watch(ctx, r.globalNamespace, r.internalToMetaListOpts(*options))
}
return watcher, err
}
func (r *PackageCRDREST) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
var table metav1.Table
fn := func(obj runtime.Object) error {
pkg := obj.(*datapackaging.Package)
table.Rows = append(table.Rows, metav1.TableRow{
Cells: []interface{}{
pkg.Name, pkg.Spec.RefName,
pkg.Spec.Version, time.Since(pkg.ObjectMeta.CreationTimestamp.Time).Round(1 * time.Second).String(),
},
Object: runtime.RawExtension{Object: obj},
})
return nil
}
switch {
case meta.IsListType(obj):
if err := meta.EachListItem(obj, fn); err != nil {
return nil, err
}
default:
if err := fn(obj); err != nil {
return nil, err
}
}
if m, err := meta.ListAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink()
table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount()
} else {
if m, err := meta.CommonAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion()
table.SelfLink = m.GetSelfLink()
}
}
if opt, ok := tableOptions.(*metav1.TableOptions); !ok || !opt.NoHeaders {
table.ColumnDefinitions = []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: "PackageMetadata Version resource name"},
{Name: "PackageMetadata Name", Type: "string", Format: "name", Description: "Associated PackageMetadata name"},
{Name: "Version", Type: "string", Description: "Version"},
{Name: "Age", Type: "date", Description: "Time since resource creation"},
}
}
return &table, nil
}
func (r *PackageCRDREST) internalToMetaListOpts(options internalversion.ListOptions) metav1.ListOptions {
lo := metav1.ListOptions{
TypeMeta: options.TypeMeta,
Watch: options.Watch,
AllowWatchBookmarks: options.AllowWatchBookmarks,
ResourceVersion: options.ResourceVersion,
ResourceVersionMatch: options.ResourceVersionMatch,
TimeoutSeconds: options.TimeoutSeconds,
Limit: options.Limit,
Continue: options.Continue,
}
if options.LabelSelector != nil {
lo.LabelSelector = options.LabelSelector.String()
}
if options.FieldSelector != nil {
lo.FieldSelector = options.FieldSelector.String()
}
return lo
}
func (r *PackageCRDREST) deleteGlobalPackagesFromNS(ctx context.Context, ns string) error {
namespace, err := r.nsClient.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{})
if err != nil {
return err
}
if namespace.ObjectMeta.Annotations == nil {
namespace.ObjectMeta.Annotations = make(map[string]string)
}
namespace.ObjectMeta.Annotations[excludeGlobalPackagesAnn] = ""
_, err = r.nsClient.CoreV1().Namespaces().Update(ctx, namespace, metav1.UpdateOptions{})
return err
}
func (r *PackageCRDREST) applySelector(list *datapackaging.PackageList, selector fields.Selector) *datapackaging.PackageList {
if selector == nil || selector.Empty() {
return list
}
filteredPVs := []datapackaging.Package{}
for _, pv := range list.Items {
fieldSet := fields.Set{"spec.refName": pv.Spec.RefName, "metadata.name": pv.Name, "metadata.namespace": pv.Namespace}
if selector.Matches(fieldSet) {
filteredPVs = append(filteredPVs, pv)
}
}
list.Items = filteredPVs
return list
}