/
strategy.go
158 lines (129 loc) · 5.37 KB
/
strategy.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
/*
Copyright 2021 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package search
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
searchapis "github.com/karmada-io/karmada/pkg/apis/search"
)
// NewStrategy creates and returns a ResourceRegistry Strategy instance.
func NewStrategy(typer runtime.ObjectTyper) Strategy {
return Strategy{typer, names.SimpleNameGenerator}
}
// GetAttrs returns labels.Set, fields.Set, and error in case the given runtime.Object is not a ResourceRegistry.
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
resourceRegistry, ok := obj.(*searchapis.ResourceRegistry)
if !ok {
return nil, nil, fmt.Errorf("given object is not a ResourceRegistry")
}
return resourceRegistry.ObjectMeta.Labels, SelectableFields(resourceRegistry), nil
}
// MatchResourceRegistry is the filter used by the generic etcd backend to watch events
// from etcd to clients of the apiserver only interested in specific labels/fields.
func MatchResourceRegistry(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
return storage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
}
}
// SelectableFields returns a field set that represents the object.
func SelectableFields(obj *searchapis.ResourceRegistry) fields.Set {
return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false)
}
// Strategy implements behavior for ResourceRegistry.
type Strategy struct {
runtime.ObjectTyper
names.NameGenerator
}
// NamespaceScoped returns if the object must be in a namespace.
func (Strategy) NamespaceScoped() bool {
return false
}
// GetResetFields returns the set of fields that get reset by the strategy
// and should not be modified by the user.
func (Strategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
return map[fieldpath.APIVersion]*fieldpath.Set{
"search.karmada.io/v1alpha1": fieldpath.NewSet(
fieldpath.MakePathOrDie("status"),
),
}
}
// PrepareForCreate is invoked on create before validation to normalize the object.
func (Strategy) PrepareForCreate(_ context.Context, _ runtime.Object) {
}
// PrepareForUpdate is invoked on update before validation to normalize the object.
func (Strategy) PrepareForUpdate(_ context.Context, _, _ runtime.Object) {
}
// Validate returns an ErrorList with validation errors or nil.
func (Strategy) Validate(_ context.Context, _ runtime.Object) field.ErrorList {
// TODO: add validation for ResourceRegistry
return field.ErrorList{}
}
// WarningsOnCreate returns warnings for the creation of the given object.
func (Strategy) WarningsOnCreate(_ context.Context, _ runtime.Object) []string { return nil }
// AllowCreateOnUpdate returns true if the object can be created by a PUT.
func (Strategy) AllowCreateOnUpdate() bool {
return false
}
// AllowUnconditionalUpdate returns true if the object can be updated
// unconditionally (irrespective of the latest resource version), when
// there is no resource version specified in the object.
func (Strategy) AllowUnconditionalUpdate() bool {
return true
}
// Canonicalize allows an object to be mutated into a canonical form.
func (Strategy) Canonicalize(_ runtime.Object) {
}
// ValidateUpdate is invoked after default fields in the object have been
// filled in before the object is persisted.
func (Strategy) ValidateUpdate(_ context.Context, _, _ runtime.Object) field.ErrorList {
// TODO: add validation for ResourceRegistry
return field.ErrorList{}
}
// WarningsOnUpdate returns warnings for the given update.
func (Strategy) WarningsOnUpdate(_ context.Context, _, _ runtime.Object) []string {
return nil
}
// StatusStrategy implements behavior for ResourceRegistryStatus.
type StatusStrategy struct {
Strategy
}
// NewStatusStrategy creates and returns a StatusStrategy instance.
func NewStatusStrategy(strategy Strategy) StatusStrategy {
return StatusStrategy{strategy}
}
// GetResetFields returns the set of fields that get reset by the strategy
// and should not be modified by the user.
func (StatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
return map[fieldpath.APIVersion]*fieldpath.Set{}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
func (StatusStrategy) PrepareForUpdate(_ context.Context, _, _ runtime.Object) {
}
// ValidateUpdate is the default update validation for an end user updating status
func (StatusStrategy) ValidateUpdate(_ context.Context, _, _ runtime.Object) field.ErrorList {
return field.ErrorList{}
}
// WarningsOnUpdate returns warnings for the given update.
func (StatusStrategy) WarningsOnUpdate(_ context.Context, _, _ runtime.Object) []string {
return nil
}