@@ -29,6 +29,7 @@ import (
29
29
kerrors "k8s.io/apimachinery/pkg/util/errors"
30
30
"k8s.io/klog/v2"
31
31
ctrl "sigs.k8s.io/controller-runtime"
32
+ "sigs.k8s.io/controller-runtime/pkg/client"
32
33
33
34
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
34
35
runtimehooksv1 "sigs.k8s.io/cluster-api/api/runtime/hooks/v1alpha1"
@@ -48,14 +49,16 @@ type Engine interface {
48
49
}
49
50
50
51
// NewEngine creates a new patch engine.
51
- func NewEngine (runtimeClient runtimeclient.Client ) Engine {
52
+ func NewEngine (c client. Client , runtimeClient runtimeclient.Client ) Engine {
52
53
return & engine {
54
+ client : c ,
53
55
runtimeClient : runtimeClient ,
54
56
}
55
57
}
56
58
57
59
// engine implements the Engine interface.
58
60
type engine struct {
61
+ client client.Client
59
62
runtimeClient runtimeclient.Client
60
63
}
61
64
@@ -73,8 +76,14 @@ func (e *engine) Apply(ctx context.Context, blueprint *scope.ClusterBlueprint, d
73
76
74
77
log := ctrl .LoggerFrom (ctx )
75
78
79
+ // Determine contract version used by the ControlPlane.
80
+ controlPlaneContractVersion , err := contract .GetContractVersionForVersion (ctx , e .client , desired .ControlPlane .Object .GroupVersionKind ().GroupKind (), desired .ControlPlane .Object .GroupVersionKind ().Version )
81
+ if err != nil {
82
+ return errors .Wrapf (err , "failed to generate patch request: failed to get contract version for the ControlPlane object" )
83
+ }
84
+
76
85
// Create a patch generation request.
77
- req , err := createRequest (blueprint , desired )
86
+ req , err := createRequest (blueprint , desired , controlPlaneContractVersion )
78
87
if err != nil {
79
88
return errors .Wrapf (err , "failed to generate patch request" )
80
89
}
@@ -91,7 +100,7 @@ func (e *engine) Apply(ctx context.Context, blueprint *scope.ClusterBlueprint, d
91
100
if clusterClassPatch .External == nil {
92
101
definitionFrom = clusterv1 .VariableDefinitionFromInline
93
102
}
94
- if err := addVariablesForPatch (blueprint , desired , req , definitionFrom ); err != nil {
103
+ if err := addVariablesForPatch (blueprint , desired , req , definitionFrom , controlPlaneContractVersion ); err != nil {
95
104
return errors .Wrapf (err , "failed to calculate variables for patch %q" , clusterClassPatch .Name )
96
105
}
97
106
log .V (5 ).Info ("Applying patch to templates" )
@@ -144,15 +153,15 @@ func (e *engine) Apply(ctx context.Context, blueprint *scope.ClusterBlueprint, d
144
153
145
154
// Use patched templates to update the desired state objects.
146
155
log .V (5 ).Info ("Applying patched templates to desired state" )
147
- if err := updateDesiredState (ctx , req , blueprint , desired ); err != nil {
156
+ if err := updateDesiredState (ctx , req , blueprint , desired , controlPlaneContractVersion ); err != nil {
148
157
return errors .Wrapf (err , "failed to apply patches to desired state" )
149
158
}
150
159
151
160
return nil
152
161
}
153
162
154
163
// addVariablesForPatch adds variables for a given ClusterClassPatch to the items in the PatchRequest.
155
- func addVariablesForPatch (blueprint * scope.ClusterBlueprint , desired * scope.ClusterState , req * runtimehooksv1.GeneratePatchesRequest , definitionFrom string ) error {
164
+ func addVariablesForPatch (blueprint * scope.ClusterBlueprint , desired * scope.ClusterState , req * runtimehooksv1.GeneratePatchesRequest , definitionFrom , controlPlaneContractVersion string ) error {
156
165
// If there is no definitionFrom return an error.
157
166
if definitionFrom == "" {
158
167
return errors .New ("failed to calculate variables: no patch name provided" )
@@ -187,7 +196,7 @@ func addVariablesForPatch(blueprint *scope.ClusterBlueprint, desired *scope.Clus
187
196
}
188
197
// If the item holder reference is a Control Plane machine add the Control Plane variables.
189
198
if blueprint .HasControlPlaneInfrastructureMachine () &&
190
- item .HolderReference .FieldPath == strings . Join ( contract . ControlPlane (). MachineTemplate (). InfrastructureRef (). Path (), "." ) {
199
+ item .HolderReference .FieldPath == getControlPlaneHolderFieldPath ( controlPlaneContractVersion ) {
191
200
item .Variables = controlPlaneVariables
192
201
}
193
202
// If the item holder reference is a MachineDeployment calculate the variables for each MachineDeploymentTopology
@@ -261,7 +270,7 @@ func getMPTopologyFromMP(blueprint *scope.ClusterBlueprint, mp *clusterv1.Machin
261
270
// replicas of a MachineDeployment.
262
271
// NOTE: A single GeneratePatchesRequest object is used to carry templates state across subsequent Generate calls.
263
272
// NOTE: This function does not add variables to items for the request, as the variables depend on the specific patch.
264
- func createRequest (blueprint * scope.ClusterBlueprint , desired * scope.ClusterState ) (* runtimehooksv1.GeneratePatchesRequest , error ) {
273
+ func createRequest (blueprint * scope.ClusterBlueprint , desired * scope.ClusterState , controlPlaneContractVersion string ) (* runtimehooksv1.GeneratePatchesRequest , error ) {
265
274
req := & runtimehooksv1.GeneratePatchesRequest {}
266
275
267
276
// Add the InfrastructureClusterTemplate.
@@ -288,7 +297,7 @@ func createRequest(blueprint *scope.ClusterBlueprint, desired *scope.ClusterStat
288
297
// add the InfrastructureMachineTemplate for control plane machines.
289
298
if blueprint .HasControlPlaneInfrastructureMachine () {
290
299
t , err := newRequestItemBuilder (blueprint .ControlPlane .InfrastructureMachineTemplate ).
291
- WithHolder (desired .ControlPlane .Object , desired .ControlPlane .Object .GroupVersionKind (), strings . Join ( contract . ControlPlane (). MachineTemplate (). InfrastructureRef (). Path (), "." )).
300
+ WithHolder (desired .ControlPlane .Object , desired .ControlPlane .Object .GroupVersionKind (), getControlPlaneHolderFieldPath ( controlPlaneContractVersion )).
292
301
Build ()
293
302
if err != nil {
294
303
return nil , errors .Wrapf (err , "failed to prepare ControlPlane's %s %s for patching" ,
@@ -523,7 +532,7 @@ func convertToValidationRequest(generateRequest *runtimehooksv1.GeneratePatchesR
523
532
524
533
// updateDesiredState uses the patched templates of a GeneratePatchesRequest to update the desired state.
525
534
// NOTE: This func should be called after all the patches have been applied to the GeneratePatchesRequest.
526
- func updateDesiredState (ctx context.Context , req * runtimehooksv1.GeneratePatchesRequest , blueprint * scope.ClusterBlueprint , desired * scope.ClusterState ) error {
535
+ func updateDesiredState (ctx context.Context , req * runtimehooksv1.GeneratePatchesRequest , blueprint * scope.ClusterBlueprint , desired * scope.ClusterState , controlPlaneContractVersion string ) error {
527
536
var err error
528
537
529
538
// Update the InfrastructureCluster.
@@ -563,7 +572,7 @@ func updateDesiredState(ctx context.Context, req *runtimehooksv1.GeneratePatches
563
572
// If the ClusterClass mandates the ControlPlane has InfrastructureMachines,
564
573
// update the InfrastructureMachineTemplate for ControlPlane machines.
565
574
if blueprint .HasControlPlaneInfrastructureMachine () {
566
- infrastructureMachineTemplate , err := getTemplateAsUnstructured (req , desired .ControlPlane .Object .GetKind (), strings . Join ( contract . ControlPlane (). MachineTemplate (). InfrastructureRef (). Path (), "." ), requestTopologyName {})
575
+ infrastructureMachineTemplate , err := getTemplateAsUnstructured (req , desired .ControlPlane .Object .GetKind (), getControlPlaneHolderFieldPath ( controlPlaneContractVersion ), requestTopologyName {})
567
576
if err != nil {
568
577
return err
569
578
}
@@ -631,3 +640,10 @@ func definitionsForPatch(blueprint *scope.ClusterBlueprint, definitionFrom strin
631
640
}
632
641
return variableDefinitionsForPatch
633
642
}
643
+
644
+ func getControlPlaneHolderFieldPath (contractVersion string ) string {
645
+ if contractVersion == "v1beta1" {
646
+ return strings .Join (contract .ControlPlane ().MachineTemplate ().InfrastructureV1Beta1Ref ().Path (), "." )
647
+ }
648
+ return strings .Join (contract .ControlPlane ().MachineTemplate ().InfrastructureRef ().Path (), "." )
649
+ }
0 commit comments