-
Notifications
You must be signed in to change notification settings - Fork 48
/
strategy.go
220 lines (183 loc) · 7.62 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
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
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
//nolint:gocritic
package miner
import (
"context"
"fmt"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"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/registry/rest"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
"github.com/superproj/onex/pkg/apis/apps"
"github.com/superproj/onex/pkg/apis/apps/validation"
)
// minerStrategy implements behavior for Miner objects.
type minerStrategy struct {
runtime.ObjectTyper
names.NameGenerator
}
// Strategy is the default logic that applies when creating and updating Miner
// objects via the REST API.
var Strategy = minerStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
var (
// Make sure we correctly implement the interface.
_ = rest.GarbageCollectionDeleteStrategy(Strategy)
// Strategy should implement rest.RESTCreateStrategy.
_ rest.RESTCreateStrategy = Strategy
// Strategy should implement rest.RESTUpdateStrategy.
_ rest.RESTUpdateStrategy = Strategy
)
// DefaultGarbageCollectionPolicy returns DeleteDependents for all currently served versions.
func (minerStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
return rest.DeleteDependents
}
// NamespaceScoped is true for miners.
func (minerStrategy) NamespaceScoped() bool {
return true
}
// GetResetFields returns the set of fields that get reset by the strategy
// and should not be modified by the user.
func (minerStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
fields := map[fieldpath.APIVersion]*fieldpath.Set{
"apps.onex.io/v1beta1": fieldpath.NewSet(
fieldpath.MakePathOrDie("status"),
),
}
return fields
}
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (minerStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
miner := obj.(*apps.Miner)
miner.Status = apps.MinerStatus{}
miner.Generation = 1
dropMinerDisabledFields(miner, nil)
// Be explicit that users cannot create pre-provisioned miners.
miner.Status.Conditions = []apps.Condition{}
}
// Validate validates a new miner.
func (minerStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
miner := obj.(*apps.Miner)
return validation.ValidateMiner(miner)
}
// WarningsOnCreate returns warnings for the creation of the given object.
func (minerStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil }
// Canonicalize normalizes the object after validation.
func (minerStrategy) Canonicalize(obj runtime.Object) {
}
// AllowCreateOnUpdate is false for miners.
func (minerStrategy) AllowCreateOnUpdate() bool {
return false
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (minerStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newMiner := obj.(*apps.Miner)
oldMiner := old.(*apps.Miner)
// Update is not allowed to set status
newMiner.Status = oldMiner.Status
dropMinerDisabledFields(newMiner, oldMiner)
// Any changes to the spec increment the generation number, any changes to the
// status should reflect the generation number of the corresponding object.
// See metav1.ObjectMeta description for more information on Generation.
if !apiequality.Semantic.DeepEqual(oldMiner.Spec, newMiner.Spec) {
newMiner.Generation = oldMiner.Generation + 1
}
}
// ValidateUpdate is the default update validation for an end user.
func (minerStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateMinerUpdate(obj.(*apps.Miner), old.(*apps.Miner))
}
// WarningsOnUpdate returns warnings for the given update.
func (minerStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}
// If AllowUnconditionalUpdate() is true and the object specified by
// the user does not have a resource version, then generic Update()
// populates it with the latest version. Else, it checks that the
// version specified by the user matches the version of latest etcd
// object.
func (minerStrategy) AllowUnconditionalUpdate() bool {
return true
}
// Storage strategy for the Status subresource.
type minerStatusStrategy struct {
minerStrategy
}
// StatusStrategy is the default logic invoked when updating object status.
var StatusStrategy = minerStatusStrategy{Strategy}
// GetResetFields returns the set of fields that get reset by the strategy
// and should not be modified by the user.
func (minerStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
return map[fieldpath.APIVersion]*fieldpath.Set{
"apps.onex.io/v1beta1": fieldpath.NewSet(
fieldpath.MakePathOrDie("spec"),
fieldpath.MakePathOrDie("status", "conditions"),
),
}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update of status.
func (minerStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newMiner := obj.(*apps.Miner)
oldMiner := old.(*apps.Miner)
// Updating /status should not modify spec
newMiner.Spec = oldMiner.Spec
newMiner.DeletionTimestamp = nil
// don't allow the miners/status endpoint to touch owner references since old kubelets corrupt them in a way
// that breaks garbage collection
newMiner.OwnerReferences = oldMiner.OwnerReferences
}
// ValidateUpdate is the default update validation for an end user updating status.
func (minerStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateMinerStatusUpdate(obj.(*apps.Miner), old.(*apps.Miner))
}
// WarningsOnUpdate returns warnings for the given update.
func (minerStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}
// Canonicalize normalizes the object after validation.
func (minerStatusStrategy) Canonicalize(obj runtime.Object) {
}
// ToSelectableFields returns a field set that can be used for filter selection.
func ToSelectableFields(obj *apps.Miner) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true)
minerSpecificFieldsSet := fields.Set{
"spec.minerType": obj.Spec.MinerType,
}
return generic.MergeFieldsSets(objectMetaFieldsSet, minerSpecificFieldsSet)
}
// GetAttrs returns labels and fields of a given object for filtering purposes.
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
m, ok := obj.(*apps.Miner)
if !ok {
return nil, nil, fmt.Errorf("given object is not a miner")
}
return labels.Set(m.Labels), ToSelectableFields(m), nil
}
// Matcher 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 Matcher(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
return storage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
}
}
// dropMinerDisabledFields drops fields that are not used if their associated feature gates
// are not enabled.
// The typical pattern is:
//
// if !utilfeature.DefaultFeatureGate.Enabled(features.MyFeature) && !myFeatureInUse(oldSvc) {
// newSvc.Spec.MyFeature = nil
// }
func dropMinerDisabledFields(newMiner *apps.Miner, oldMiner *apps.Miner) {
}