Skip to content

Commit 59d91dd

Browse files
committed
restructure how the source of related resources is configured
On-behalf-of: @SAP [email protected]
1 parent c53499a commit 59d91dd

File tree

10 files changed

+578
-83
lines changed

10 files changed

+578
-83
lines changed

deploy/crd/kcp.io/syncagent.kcp.io_publishedresources.yaml

Lines changed: 203 additions & 22 deletions
Large diffs are not rendered by default.

sdk/apis/syncagent/v1alpha1/published_resource.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,79 @@ type RelatedResourceSpec struct {
171171
// ConfigMap or Secret
172172
Kind string `json:"kind"`
173173

174-
Reference RelatedResourceReference `json:"reference"`
174+
// Source describes how the related resource can be found. The source expression is evaluated
175+
// on both source and destination side of the sync.
176+
Source RelatedResourceSource `json:"source"`
177+
178+
// Naming is used when a label selector is used in the source expression. Since the labels do
179+
// not dictate how the actual object shall be named on the destination side, and the agent also
180+
// does not simply want to re-use the source side's name/namespace (these could contain private
181+
// information and leak details about how the service is structured), explicit naming rules need
182+
// to be provided. These naming rules are used to form the destination name/namespace for the
183+
// related object.
184+
// When reference-based sources are used, this field is optional as the destination name/namespace
185+
// are read from the main object.
186+
Naming *ResourceNaming `json:"naming"`
175187

176188
// Mutation configures optional transformation rules for the related resource.
177189
// Status mutations are only performed when the related resource originates in kcp.
178190
Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
179191
}
180192

181-
type RelatedResourceReference struct {
182-
Name ResourceLocator `json:"name"`
183-
Namespace *ResourceLocator `json:"namespace,omitempty"`
193+
// RelatedResourceSource configures how a related object can be found.
194+
type RelatedResourceSource struct {
195+
// Name configures how the related object's name can be constructed or
196+
// found. This must always be configured for a related resource.
197+
Name RelatedResourceSourceSpec `json:"name"`
198+
// Namespace configures in what namespace the related object resides in. If
199+
// not specified, the same namespace as the main object is assumed. If the
200+
// main object is cluster-scoped, an error will be raised during syncing.
201+
Namespace *RelatedResourceSourceSpec `json:"namespace,omitempty"`
202+
}
203+
204+
// RelatedResourceSourceSpec configures how the related object itself or its
205+
// namespace can be found. Selector and Reference are mutally exclusive.
206+
type RelatedResourceSourceSpec struct {
207+
// Selector is a label selector that is useful if no reference is in the
208+
// main resource (i.e. if the related object links back to its parent, instead
209+
// of the parent pointing to the related object).
210+
Selector *RelatedResourceSelector `json:"selector,omitempty"`
211+
// Reference points to a field inside the main object. This reference is
212+
// evaluated on both source and destination sides to find the related object.
213+
Reference *RelatedResourceReference `json:"reference,omitempty"`
184214
}
185215

186-
type ResourceLocator struct {
187-
Path string `json:"path"`
188-
Regex *RegexResourceLocator `json:"regex,omitempty"`
216+
type RelatedResourceReference struct {
217+
// Path is a simplified JSONPath expression like "metadata.name". A reference
218+
// must always select at least _something_ in the object, even if the value
219+
// is discarded by the regular expression.
220+
Path string `json:"path"`
221+
// Regex is a Go regular expression that is optionally applied to the selected
222+
// value from the path.
223+
Regex *RegularExpression `json:"regex,omitempty"`
189224
}
190225

191-
type RegexResourceLocator struct {
226+
// RegularExpression models a Go regular expression string replacement. See
227+
// https://pkg.go.dev/regexp/syntax for more information on the syntax.
228+
type RegularExpression struct {
192229
// Pattern can be left empty to simply replace the entire value with the
193230
// replacement.
194-
Pattern string `json:"pattern,omitempty"`
231+
Pattern string `json:"pattern,omitempty"`
232+
// Replacement is the string that the matched pattern is replaced with. It
233+
// can contain references to groups in the pattern by using \N.
195234
Replacement string `json:"replacement,omitempty"`
196235
}
197236

237+
// RelatedResourceSelector is a dedicated struct in case we need additional options
238+
// for evaluating the label selector.
239+
240+
// RelatedResourceSelector describes how to locate a related object based on
241+
// labels. This is useful if the main resource has no and cannot construct a
242+
// reference to the related object because its name/namespace might be randomized.
243+
type RelatedResourceSelector struct {
244+
metav1.LabelSelector `json:",inline"`
245+
}
246+
198247
// SourceResourceDescriptor and ResourceProjection are very similar, but as we do not
199248
// want to burden service clusters with validation webhooks, it's easier to split them
200249
// into 2 structs here and rely on the schema for validation.

sdk/apis/syncagent/v1alpha1/zz_generated.deepcopy.go

Lines changed: 75 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/applyconfiguration/syncagent/v1alpha1/regularexpression.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/applyconfiguration/syncagent/v1alpha1/relatedresourcereference.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/applyconfiguration/syncagent/v1alpha1/relatedresourceselector.go

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)