-
Notifications
You must be signed in to change notification settings - Fork 925
/
composition_common.go
351 lines (295 loc) · 12.8 KB
/
composition_common.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
/*
Copyright 2020 The Crossplane 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 v1
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane/internal/validation/errors"
)
/*
NOTE(negz): This file contains types that are shared between the Composition
and CompositionRevision types. It exists so we can copy these types to the
apiextensions/v1beta1 package without copying the entire Composition type.
Once we no longer support v1beta1 CompositionRevisions it can be merged back
into composition_revision_types.go.
*/
// A CompositionMode determines what mode of Composition is used.
type CompositionMode string
const (
// CompositionModeResources indicates that a Composition uses what is
// commonly referred to as "Patch & Transform" or P&T composition. This mode
// of Composition uses an array of resources, each a template for a composed
// resource.
CompositionModeResources CompositionMode = "Resources"
// CompositionModePipeline indicates that a Composition specifies a pipeline
// of Composition Functions, each of which is responsible for producing
// composed resources that Crossplane should create or update.
//
// THIS IS A BETA FEATURE. It is not honored if the relevant Crossplane
// feature flag is disabled.
CompositionModePipeline CompositionMode = "Pipeline"
)
// TypeReference is used to refer to a type for declaring compatibility.
type TypeReference struct {
// APIVersion of the type.
APIVersion string `json:"apiVersion"`
// Kind of the type.
Kind string `json:"kind"`
}
// TypeReferenceTo returns a reference to the supplied GroupVersionKind.
func TypeReferenceTo(gvk schema.GroupVersionKind) TypeReference {
return TypeReference{APIVersion: gvk.GroupVersion().String(), Kind: gvk.Kind}
}
// A PatchSet is a set of patches that can be reused from all resources within
// a Composition.
type PatchSet struct {
// Name of this PatchSet.
Name string `json:"name"`
// Patches will be applied as an overlay to the base resource.
Patches []Patch `json:"patches"`
}
// ComposedTemplate is used to provide information about how the composed resource
// should be processed.
type ComposedTemplate struct {
// TODO(negz): Name should be a required field in v2 of this API.
// A Name uniquely identifies this entry within its Composition's resources
// array. Names are optional but *strongly* recommended. When all entries in
// the resources array are named entries may added, deleted, and reordered
// as long as their names do not change. When entries are not named the
// length and order of the resources array should be treated as immutable.
// Either all or no entries must be named.
// +optional
Name *string `json:"name,omitempty"`
// Base is the target resource that the patches will be applied on.
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:EmbeddedResource
Base runtime.RawExtension `json:"base"`
// Patches will be applied as overlay to the base resource.
// +optional
Patches []Patch `json:"patches,omitempty"`
// ConnectionDetails lists the propagation secret keys from this target
// resource to the composition instance connection secret.
// +optional
ConnectionDetails []ConnectionDetail `json:"connectionDetails,omitempty"`
// ReadinessChecks allows users to define custom readiness checks. All checks
// have to return true in order for resource to be considered ready. The
// default readiness check is to have the "Ready" condition to be "True".
// +optional
// +kubebuilder:default={{type:"MatchCondition",matchCondition:{type:"Ready",status:"True"}}}
ReadinessChecks []ReadinessCheck `json:"readinessChecks,omitempty"`
}
// GetName returns the name of the composed template or an empty string if it is nil.
func (ct *ComposedTemplate) GetName() string {
if ct.Name != nil {
return *ct.Name
}
return ""
}
// ReadinessCheckType is used for readiness check types.
type ReadinessCheckType string
// The possible values for readiness check type.
const (
ReadinessCheckTypeNonEmpty ReadinessCheckType = "NonEmpty"
ReadinessCheckTypeMatchString ReadinessCheckType = "MatchString"
ReadinessCheckTypeMatchInteger ReadinessCheckType = "MatchInteger"
ReadinessCheckTypeMatchTrue ReadinessCheckType = "MatchTrue"
ReadinessCheckTypeMatchFalse ReadinessCheckType = "MatchFalse"
ReadinessCheckTypeMatchCondition ReadinessCheckType = "MatchCondition"
ReadinessCheckTypeNone ReadinessCheckType = "None"
)
// IsValid returns nil if the readiness check type is valid, or an error otherwise.
func (t *ReadinessCheckType) IsValid() bool {
switch *t {
case ReadinessCheckTypeNonEmpty, ReadinessCheckTypeMatchString, ReadinessCheckTypeMatchInteger, ReadinessCheckTypeMatchTrue, ReadinessCheckTypeMatchFalse, ReadinessCheckTypeMatchCondition, ReadinessCheckTypeNone:
return true
}
return false
}
// ReadinessCheck is used to indicate how to tell whether a resource is ready
// for consumption.
type ReadinessCheck struct {
// TODO(negz): Optional fields should be nil in the next version of this
// API. How would we know if we actually wanted to match the empty string,
// or 0?
// Type indicates the type of probe you'd like to use.
// +kubebuilder:validation:Enum="MatchString";"MatchInteger";"NonEmpty";"MatchCondition";"MatchTrue";"MatchFalse";"None"
Type ReadinessCheckType `json:"type"`
// FieldPath shows the path of the field whose value will be used.
// +optional
FieldPath string `json:"fieldPath,omitempty"`
// MatchString is the value you'd like to match if you're using "MatchString" type.
// +optional
MatchString string `json:"matchString,omitempty"`
// MatchInt is the value you'd like to match if you're using "MatchInt" type.
// +optional
MatchInteger int64 `json:"matchInteger,omitempty"`
// MatchCondition specifies the condition you'd like to match if you're using "MatchCondition" type.
// +optional
MatchCondition *MatchConditionReadinessCheck `json:"matchCondition,omitempty"`
}
// MatchConditionReadinessCheck is used to indicate how to tell whether a resource is ready
// for consumption.
type MatchConditionReadinessCheck struct {
// Type indicates the type of condition you'd like to use.
// +kubebuilder:default="Ready"
Type xpv1.ConditionType `json:"type"`
// Status is the status of the condition you'd like to match.
// +kubebuilder:default="True"
Status corev1.ConditionStatus `json:"status"`
}
// Validate checks if the match condition is logically valid.
func (m *MatchConditionReadinessCheck) Validate() *field.Error {
if m == nil {
return nil
}
if m.Type == "" {
return field.Required(field.NewPath("type"), "cannot be empty for type MatchCondition")
}
if m.Status == "" {
return field.Required(field.NewPath("status"), "cannot be empty for type MatchCondition")
}
return nil
}
// Validate checks if the readiness check is logically valid.
func (r *ReadinessCheck) Validate() *field.Error {
if !r.Type.IsValid() {
return field.Invalid(field.NewPath("type"), string(r.Type), "unknown readiness check type")
}
switch r.Type {
case ReadinessCheckTypeNone:
return nil
// NOTE: ComposedTemplate doesn't use pointer values for optional
// strings, so today the empty string and 0 are equivalent to "unset".
case ReadinessCheckTypeMatchString:
if r.MatchString == "" {
return field.Required(field.NewPath("matchString"), "cannot be empty for type MatchString")
}
case ReadinessCheckTypeMatchInteger:
if r.MatchInteger == 0 {
return field.Required(field.NewPath("matchInteger"), "cannot be 0 for type MatchInteger")
}
case ReadinessCheckTypeMatchCondition:
if err := r.MatchCondition.Validate(); err != nil {
return errors.WrapFieldError(err, field.NewPath("matchCondition"))
}
return nil
case ReadinessCheckTypeNonEmpty, ReadinessCheckTypeMatchFalse, ReadinessCheckTypeMatchTrue:
// No specific validation required.
}
if r.FieldPath == "" {
return field.Required(field.NewPath("fieldPath"), "cannot be empty")
}
return nil
}
// A ConnectionDetailType is a type of connection detail.
type ConnectionDetailType string
// ConnectionDetailType types.
const (
ConnectionDetailTypeUnknown ConnectionDetailType = "Unknown"
ConnectionDetailTypeFromConnectionSecretKey ConnectionDetailType = "FromConnectionSecretKey"
ConnectionDetailTypeFromFieldPath ConnectionDetailType = "FromFieldPath"
ConnectionDetailTypeFromValue ConnectionDetailType = "FromValue"
)
// ConnectionDetail includes the information about the propagation of the connection
// information from one secret to another.
type ConnectionDetail struct {
// Name of the connection secret key that will be propagated to the
// connection secret of the composition instance. Leave empty if you'd like
// to use the same key name.
// +optional
Name *string `json:"name,omitempty"`
// Type sets the connection detail fetching behaviour to be used. Each
// connection detail type may require its own fields to be set on the
// ConnectionDetail object. If the type is omitted Crossplane will attempt
// to infer it based on which other fields were specified. If multiple
// fields are specified the order of precedence is:
// 1. FromValue
// 2. FromConnectionSecretKey
// 3. FromFieldPath
// +optional
// +kubebuilder:validation:Enum=FromConnectionSecretKey;FromFieldPath;FromValue
Type *ConnectionDetailType `json:"type,omitempty"`
// FromConnectionSecretKey is the key that will be used to fetch the value
// from the composed resource's connection secret.
// +optional
FromConnectionSecretKey *string `json:"fromConnectionSecretKey,omitempty"`
// FromFieldPath is the path of the field on the composed resource whose
// value to be used as input. Name must be specified if the type is
// FromFieldPath.
// +optional
FromFieldPath *string `json:"fromFieldPath,omitempty"`
// Value that will be propagated to the connection secret of the composite
// resource. May be set to inject a fixed, non-sensitive connection secret
// value, for example a well-known port.
// +optional
Value *string `json:"value,omitempty"`
}
// A PipelineStep in a Composition Function pipeline.
type PipelineStep struct {
// Step name. Must be unique within its Pipeline.
Step string `json:"step"`
// FunctionRef is a reference to the Composition Function this step should
// execute.
FunctionRef FunctionReference `json:"functionRef"`
// Input is an optional, arbitrary Kubernetes resource (i.e. a resource
// with an apiVersion and kind) that will be passed to the Composition
// Function as the 'input' of its RunFunctionRequest.
// +optional
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:EmbeddedResource
Input *runtime.RawExtension `json:"input,omitempty"`
// Credentials are optional credentials that the Composition Function needs.
// +optional
// +listType=map
// +listMapKey=name
Credentials []FunctionCredentials `json:"credentials,omitempty"`
}
// A FunctionReference references a Composition Function that may be used in a
// Composition pipeline.
type FunctionReference struct {
// Name of the referenced Function.
Name string `json:"name"`
}
// FunctionCredentials are optional credentials that a Composition Function
// needs to run.
type FunctionCredentials struct {
// Name of this set of credentials.
Name string `json:"name"`
// Source of the function credentials.
// +kubebuilder:validation:Enum=None;Secret
Source FunctionCredentialsSource `json:"source"`
// A SecretRef is a reference to a secret containing credentials that should
// be supplied to the function.
// +optional
SecretRef *xpv1.SecretReference `json:"secretRef,omitempty"`
}
// A FunctionCredentialsSource is a source from which Composition Function
// credentials may be acquired.
type FunctionCredentialsSource string
const (
// FunctionCredentialsSourceNone indicates that a function does not require
// credentials.
FunctionCredentialsSourceNone FunctionCredentialsSource = "None"
// FunctionCredentialsSourceSecret indicates that a function should acquire
// credentials from a secret.
FunctionCredentialsSourceSecret FunctionCredentialsSource = "Secret"
)
// A StoreConfigReference references a secret store config that may be used to
// write connection details.
type StoreConfigReference struct {
// Name of the referenced StoreConfig.
Name string `json:"name"`
}