@@ -17,9 +17,47 @@ limitations under the License.
17
17
package structuredmerge
18
18
19
19
import (
20
+ "sigs.k8s.io/controller-runtime/pkg/client"
21
+
22
+ clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
20
23
"sigs.k8s.io/cluster-api/internal/contract"
21
24
)
22
25
26
+ var (
27
+ // defaultAllowedPaths are the allowed paths for all objects except Clusters.
28
+ defaultAllowedPaths = []contract.Path {
29
+ // apiVersion, kind, name and namespace are required field for a server side apply intent.
30
+ {"apiVersion" },
31
+ {"kind" },
32
+ {"metadata" , "name" },
33
+ {"metadata" , "namespace" },
34
+ // the topology controller controls/has an opinion for labels, annotation, ownerReferences and spec only.
35
+ {"metadata" , "labels" },
36
+ {"metadata" , "annotations" },
37
+ {"metadata" , "ownerReferences" },
38
+ {"spec" },
39
+ }
40
+
41
+ // allowedPathsCluster are the allowed paths specific for Clusters.
42
+ // The cluster object is not created by the topology controller and already contains fields which are
43
+ // not supposed for the topology controller to have an opinion on / take (co-)ownership of.
44
+ // Because of that the allowedPaths are different to other objects.
45
+ // NOTE: This is mostly the same as defaultAllowedPaths but having more restrictions.
46
+ allowedPathsCluster = []contract.Path {
47
+ // apiVersion, kind, name and namespace are required field for a server side apply intent.
48
+ {"apiVersion" },
49
+ {"kind" },
50
+ {"metadata" , "name" },
51
+ {"metadata" , "namespace" },
52
+ // the topology controller controls/has an opinion for the labels ClusterLabelName
53
+ // and ClusterTopologyOwnedLabel as well as infrastructureRef and controlPlaneRef in spec.
54
+ {"metadata" , "labels" , clusterv1 .ClusterLabelName },
55
+ {"metadata" , "labels" , clusterv1 .ClusterTopologyOwnedLabel },
56
+ {"spec" , "infrastructureRef" },
57
+ {"spec" , "controlPlaneRef" },
58
+ }
59
+ )
60
+
23
61
// HelperOption is some configuration that modifies options for Helper.
24
62
type HelperOption interface {
25
63
// ApplyToHelper applies this configuration to the given helper options.
@@ -42,6 +80,20 @@ type HelperOptions struct {
42
80
ignorePaths []contract.Path
43
81
}
44
82
83
+ // newHelperOptions returns initialized HelperOptions.
84
+ func newHelperOptions (target client.Object , opts ... HelperOption ) * HelperOptions {
85
+ helperOptions := & HelperOptions {
86
+ allowedPaths : defaultAllowedPaths ,
87
+ }
88
+ // Overwrite the allowedPaths for Cluster objects to prevent the topology controller
89
+ // to take ownership of fields it is not supposed to.
90
+ if _ , ok := target .(* clusterv1.Cluster ); ok {
91
+ helperOptions .allowedPaths = allowedPathsCluster
92
+ }
93
+ helperOptions = helperOptions .ApplyOptions (opts )
94
+ return helperOptions
95
+ }
96
+
45
97
// ApplyOptions applies the given patch options on these options,
46
98
// and then returns itself (for convenient chaining).
47
99
func (o * HelperOptions ) ApplyOptions (opts []HelperOption ) * HelperOptions {
0 commit comments