-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathboxcutter.go
More file actions
253 lines (193 loc) · 9.36 KB
/
Copy pathboxcutter.go
File metadata and controls
253 lines (193 loc) · 9.36 KB
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
// Package boxcutter provides a object reconciliation library based on Package Operator.
package boxcutter
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"
"pkg.package-operator.run/boxcutter/machinery"
"pkg.package-operator.run/boxcutter/machinery/types"
"pkg.package-operator.run/boxcutter/validation"
)
// Revision represents multiple phases at a given point in time.
type Revision = types.Revision
// RevisionBuilder is a Revision with methods to attach options.
type RevisionBuilder = types.RevisionBuilder
// NewRevision creates a new RevisionBuilder with the given name, rev and phases.
var NewRevision = types.NewRevision
// NewRevisionWithOwner creates a new RevisionBuilder
// with the given name, rev, phases and owner.
var NewRevisionWithOwner = types.NewRevisionWithOwner
// NewRevisionWithOwnerAndSiblings creates a new RevisionBuilder
// with the given name, rev, phases, owner and sibling owners for revision handover.
var NewRevisionWithOwnerAndSiblings = types.NewRevisionWithOwnerAndSiblings
// Phase represents a collection of objects lifecycled together.
type Phase = types.Phase
// PhaseBuilder is a Phase with methods to attach options.
type PhaseBuilder = types.PhaseBuilder
// NewPhase creates a new PhaseBuilder with the given name and objects.
var NewPhase = types.NewPhase
// NewPhaseWithOwner creates a new PhaseBuilder with the given name, objects and owner.
var NewPhaseWithOwner = types.NewPhaseWithOwner
// NewPhaseWithOwnerAndSiblings creates a new PhaseBuilder with the given name, objects, owner
// and sibling owners for revision handover.
var NewPhaseWithOwnerAndSiblings = types.NewPhaseWithOwnerAndSiblings
// ObjectReconcileOption is the common interface for object reconciliation options.
type ObjectReconcileOption = types.ObjectReconcileOption
// ObjectTeardownOption holds configuration options changing object teardown.
type ObjectTeardownOption = types.ObjectTeardownOption
// PhaseReconcileOption is the common interface for phase reconciliation options.
type PhaseReconcileOption = types.PhaseReconcileOption
// PhaseTeardownOption holds configuration options changing phase teardown.
type PhaseTeardownOption = types.PhaseTeardownOption
// RevisionReconcileOption is the common interface for revision reconciliation options.
type RevisionReconcileOption = types.RevisionReconcileOption
// RevisionTeardownOption holds configuration options changing revision teardown.
type RevisionTeardownOption = types.RevisionTeardownOption
// WithSiblingOwnerClassifier sets a callback to classify unknown controllers.
// When the callback returns true, the unknown controller is treated as a
// sibling revision of the same deployment instead of a collision.
type WithSiblingOwnerClassifier = types.WithSiblingOwnerClassifier
// WithSiblingOwners returns a WithSiblingOwnerClassifier option that
// compares against UIDs of all passed `siblings` objects.
var WithSiblingOwners = types.WithSiblingOwners
// WithSiblingOwnerRefs returns a WithSiblingOwnerClassifier option that
// compares against UIDs of all passed owner references.
var WithSiblingOwnerRefs = types.WithSiblingOwnerRefs
const (
// CollisionProtectionPrevent prevents owner collisions entirely
// by not allowing to work with objects already present on the cluster.
CollisionProtectionPrevent = types.CollisionProtectionPrevent
// CollisionProtectionIfNoController allows to patch and override
// objects already present if they are not owned by another controller.
CollisionProtectionIfNoController = types.CollisionProtectionIfNoController
// CollisionProtectionNone allows to patch and override objects already
// present and owned by other controllers.
//
// Be careful!
// This setting may cause multiple controllers to fight over a resource,
// causing load on the Kubernetes API server and etcd.
CollisionProtectionNone = types.CollisionProtectionNone
)
// WithCollisionProtection instructs the given CollisionProtection setting to be used.
type WithCollisionProtection = types.WithCollisionProtection
// WithPaused skips reconciliation and just reports status information.
// Can also be described as dry-run, as no modification will occur.
type WithPaused = types.WithPaused
// WithAggregatePhaseReconcileErrors causes phase reconciliation to aggregate all object
// errors as a single error instead of returning on the first error.
var WithAggregatePhaseReconcileErrors = types.WithAggregatePhaseReconcileErrors
// WithAggregatePhaseTeardownErrors causes phase teardown to aggregate all object
// errors as a single error instead of returning on the first error.
var WithAggregatePhaseTeardownErrors = types.WithAggregatePhaseTeardownErrors
// Prober needs to be implemented by any probing implementation.
type Prober = types.Prober
// ProbeFunc wraps the given function to work with the Prober interface.
var ProbeFunc = types.ProbeFunc
// WithProbe registers the given probe to evaluate state of objects.
var WithProbe = types.WithProbe
// WithObjectReconcileOptions applies the given options only to the given object.
var WithObjectReconcileOptions = types.WithObjectReconcileOptions
// WithObjectTeardownOptions applies the given options only to the given object.
var WithObjectTeardownOptions = types.WithObjectTeardownOptions
// WithPhaseReconcileOptions applies the given options only to the given Phase.
var WithPhaseReconcileOptions = types.WithPhaseReconcileOptions
// WithPhaseTeardownOptions applies the given options only to the given Phase.
var WithPhaseTeardownOptions = types.WithPhaseTeardownOptions
// WithOwner sets an owning object and the strategy to use with it.
// Ensures controller-refs are set to track the owner and
// enables handover between owners.
var WithOwner = types.WithOwner
// ProgressProbeType is a well-known probe type used to guard phase progression.
const ProgressProbeType = types.ProgressProbeType
// RevisionEngine manages rollout and teardown of multiple phases.
type RevisionEngine = machinery.RevisionEngine
// OwnerStrategy interface needed for RevisionEngine.
type OwnerStrategy = types.OwnerStrategy
// RevisionEngineOptions holds all configuration options for the RevisionEngine.
type RevisionEngineOptions struct {
Scheme *runtime.Scheme
FieldOwner string
SystemPrefix string
DiscoveryClient discovery.OpenAPIV3SchemaInterface
RestMapper meta.RESTMapper
Writer client.Writer
Reader client.Reader
// Optional
// ManagedBy is the value to use for the app.kubernetes.io/managed-by label.
// If unset, defaults to "boxcutter".
ManagedBy string
// UnfilteredReader is a client.Reader which is not subject to filtering
// which may be applied to Reader if it is cached using object selectors.
// UnfilteredReader is used rarely in edge cases that do not persist, so it
// is safe that it should not be cached. It is additionally recommended that
// it should not be cached, as caching it would negate the benefits of cache
// filtering.
UnfilteredReader client.Reader
PhaseValidator *validation.PhaseValidator
}
// NewPhaseEngine returns a new PhaseEngine instance.
func NewPhaseEngine(opts RevisionEngineOptions) (*machinery.PhaseEngine, error) {
if err := validateRevisionEngineOpts(opts); err != nil {
return nil, err
}
if opts.PhaseValidator == nil {
opts.PhaseValidator = validation.NewNamespacedPhaseValidator(opts.RestMapper, opts.Writer)
}
comp := machinery.NewComparator(
opts.DiscoveryClient, opts.Scheme, opts.FieldOwner)
oe := machinery.NewObjectEngine(
opts.Scheme, opts.Reader, opts.Writer,
comp, opts.FieldOwner, opts.SystemPrefix,
opts.ManagedBy, opts.UnfilteredReader,
)
return machinery.NewPhaseEngine(oe, opts.PhaseValidator), nil
}
// NewRevisionEngine returns a new RevisionEngine instance.
func NewRevisionEngine(opts RevisionEngineOptions) (*RevisionEngine, error) {
if err := validateRevisionEngineOpts(opts); err != nil {
return nil, err
}
pval := validation.NewNamespacedPhaseValidator(opts.RestMapper, opts.Writer)
rval := validation.NewRevisionValidator()
comp := machinery.NewComparator(
opts.DiscoveryClient, opts.Scheme, opts.FieldOwner)
oe := machinery.NewObjectEngine(
opts.Scheme, opts.Reader, opts.Writer,
comp, opts.FieldOwner, opts.SystemPrefix,
opts.ManagedBy, opts.UnfilteredReader,
)
pe := machinery.NewPhaseEngine(oe, pval)
return machinery.NewRevisionEngine(pe, rval, opts.Writer), nil
}
// RevisionEngineOptionsError is returned for errors with the RevisionEngineOptions.
type RevisionEngineOptionsError struct {
msg string
}
func (e RevisionEngineOptionsError) Error() string {
return e.msg
}
func validateRevisionEngineOpts(opts RevisionEngineOptions) error {
if opts.Scheme == nil {
return RevisionEngineOptionsError{msg: "scheme must be provided"}
}
if len(opts.FieldOwner) == 0 {
return RevisionEngineOptionsError{msg: "fieldOwner must be provided"}
}
if len(opts.SystemPrefix) == 0 {
return RevisionEngineOptionsError{msg: "systemPrefix must be provided"}
}
if opts.DiscoveryClient == nil {
return RevisionEngineOptionsError{msg: "discoveryClient must be provided"}
}
if opts.RestMapper == nil {
return RevisionEngineOptionsError{msg: "restMapper must be provided"}
}
if opts.Writer == nil {
return RevisionEngineOptionsError{msg: "writer must be provided"}
}
if opts.Reader == nil {
return RevisionEngineOptionsError{msg: "reader must be provided"}
}
return nil
}