@@ -12,9 +12,9 @@ import (
1212
1313 "github.com/pkg/errors"
1414 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15- "k8s.io/apimachinery/pkg/types"
1615 "k8s.io/client-go/util/retry"
17- capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
16+ capiv1beta2 "sigs.k8s.io/cluster-api/api/core/v1beta2"
17+ "sigs.k8s.io/cluster-api/controllers/external"
1818 "sigs.k8s.io/cluster-api/util/patch"
1919 "sigs.k8s.io/controller-runtime/pkg/client"
2020
@@ -60,7 +60,7 @@ func (r *KamajiControlPlaneReconciler) patchControlPlaneEndpoint(ctx context.Con
6060 return errors .Wrap (scopedErr , "cannot retrieve *v1alpha1.KamajiControlPlane" )
6161 }
6262
63- controlPlane .Spec .ControlPlaneEndpoint = capiv1beta1 .APIEndpoint {
63+ controlPlane .Spec .ControlPlaneEndpoint = capiv1beta2 .APIEndpoint {
6464 Host : endpoint ,
6565 Port : int32 (port ), //nolint:gosec
6666 }
@@ -73,10 +73,12 @@ func (r *KamajiControlPlaneReconciler) patchControlPlaneEndpoint(ctx context.Con
7373 return nil
7474}
7575
76+ //+kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
77+
7678//nolint:cyclop
77- func (r * KamajiControlPlaneReconciler ) patchCluster (ctx context.Context , cluster capiv1beta1 .Cluster , controlPlane * v1alpha1.KamajiControlPlane , hostPort string ) error {
78- if cluster .Spec .InfrastructureRef == nil {
79- return errors .New ("capiv1beta1 .Cluster has no InfrastructureRef" )
79+ func (r * KamajiControlPlaneReconciler ) patchCluster (ctx context.Context , cluster capiv1beta2 .Cluster , controlPlane * v1alpha1.KamajiControlPlane , hostPort string ) error {
80+ if ! cluster .Spec .InfrastructureRef . IsDefined () {
81+ return errors .New ("capiv1beta2 .Cluster has no InfrastructureRef" )
8082 }
8183
8284 endpoint , port , err := r .controlPlaneEndpoint (controlPlane , hostPort )
@@ -121,7 +123,7 @@ func (r *KamajiControlPlaneReconciler) patchCluster(ctx context.Context, cluster
121123//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=proxmoxclusters;vsphereclusters;tinkerbellclusters,verbs=get;list;watch
122124//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=proxmoxclusters;vsphereclusters;tinkerbellclusters,verbs=patch
123125
124- func (r * KamajiControlPlaneReconciler ) checkOrPatchGenericCluster (ctx context.Context , cluster capiv1beta1 .Cluster , endpoint string , port int64 ) error {
126+ func (r * KamajiControlPlaneReconciler ) checkOrPatchGenericCluster (ctx context.Context , cluster capiv1beta2 .Cluster , endpoint string , port int64 ) error {
125127 if err := r .checkGenericCluster (ctx , cluster , endpoint , port ); err != nil {
126128 if errors .As (err , & UnmanagedControlPlaneAddressError {}) {
127129 return r .patchGenericCluster (ctx , cluster , endpoint , port , false )
@@ -136,18 +138,13 @@ func (r *KamajiControlPlaneReconciler) checkOrPatchGenericCluster(ctx context.Co
136138//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsclusters;azureclusters;hetznerclusters;kubevirtclusters;nutanixclusters;packetclusters;ionoscloudclusters,verbs=patch;get;list;watch
137139//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=kubevirtclusters/status;nutanixclusters/status;packetclusters/status,verbs=patch
138140
139- func (r * KamajiControlPlaneReconciler ) patchGenericCluster (ctx context.Context , cluster capiv1beta1.Cluster , endpoint string , port int64 , patchStatus bool ) error {
140- infraCluster := unstructured.Unstructured {}
141-
142- infraCluster .SetGroupVersionKind (cluster .Spec .InfrastructureRef .GroupVersionKind ())
143- infraCluster .SetName (cluster .Spec .InfrastructureRef .Name )
144- infraCluster .SetNamespace (cluster .Spec .InfrastructureRef .Namespace )
145-
146- if err := r .client .Get (ctx , types.NamespacedName {Name : infraCluster .GetName (), Namespace : infraCluster .GetNamespace ()}, & infraCluster ); err != nil {
147- return errors .Wrap (err , fmt .Sprintf ("cannot retrieve the %s resource" , infraCluster .GetKind ()))
141+ func (r * KamajiControlPlaneReconciler ) patchGenericCluster (ctx context.Context , cluster capiv1beta2.Cluster , endpoint string , port int64 , patchStatus bool ) error {
142+ infraCluster , err := external .GetObjectFromContractVersionedRef (ctx , r .client , cluster .Spec .InfrastructureRef , cluster .GetNamespace ())
143+ if err != nil {
144+ return errors .Wrap (err , fmt .Sprintf ("cannot get infrastructure reference %s" , cluster .Spec .InfrastructureRef .Name ))
148145 }
149146
150- patchHelper , err := patch .NewHelper (& infraCluster , r .client )
147+ patchHelper , err := patch .NewHelper (infraCluster , r .client )
151148 if err != nil {
152149 return errors .Wrap (err , "unable to create patch helper" )
153150 }
@@ -165,7 +162,7 @@ func (r *KamajiControlPlaneReconciler) patchGenericCluster(ctx context.Context,
165162 }
166163 }
167164
168- if err = patchHelper .Patch (ctx , & infraCluster ); err != nil {
165+ if err = patchHelper .Patch (ctx , infraCluster ); err != nil {
169166 return errors .Wrap (err , fmt .Sprintf ("cannot perform PATCH update for the %s resource" , infraCluster .GetKind ()))
170167 }
171168
@@ -174,15 +171,10 @@ func (r *KamajiControlPlaneReconciler) patchGenericCluster(ctx context.Context,
174171
175172//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=metal3clusters,verbs=get;list;watch
176173
177- func (r * KamajiControlPlaneReconciler ) checkGenericCluster (ctx context.Context , cluster capiv1beta1.Cluster , endpoint string , port int64 ) error {
178- gkc := unstructured.Unstructured {}
179-
180- gkc .SetGroupVersionKind (cluster .Spec .InfrastructureRef .GroupVersionKind ())
181- gkc .SetName (cluster .Spec .InfrastructureRef .Name )
182- gkc .SetNamespace (cluster .Spec .InfrastructureRef .Namespace )
183-
184- if err := r .client .Get (ctx , types.NamespacedName {Name : gkc .GetName (), Namespace : gkc .GetNamespace ()}, & gkc ); err != nil {
185- return errors .Wrap (err , fmt .Sprintf ("cannot retrieve the %s resource" , gkc .GetKind ()))
174+ func (r * KamajiControlPlaneReconciler ) checkGenericCluster (ctx context.Context , cluster capiv1beta2.Cluster , endpoint string , port int64 ) error {
175+ gkc , err := external .GetObjectFromContractVersionedRef (ctx , r .client , cluster .Spec .InfrastructureRef , cluster .GetNamespace ())
176+ if err != nil {
177+ return errors .Wrap (err , fmt .Sprintf ("cannot get infrastructure reference %s" , cluster .Spec .InfrastructureRef .Name ))
186178 }
187179
188180 cpHost , _ , err := unstructured .NestedString (gkc .Object , "spec" , "controlPlaneEndpoint" , "host" )
@@ -204,30 +196,25 @@ func (r *KamajiControlPlaneReconciler) checkGenericCluster(ctx context.Context,
204196 }
205197
206198 if cpHost != endpoint {
207- return fmt .Errorf ("the %s cluster has been provisioned with a mismatching host" , gkc .GetKind ())
199+ return fmt .Errorf ("the %s cluster has been provisioned with a mismatching host %s instead of %s " , gkc .GetKind (), cpHost , endpoint )
208200 }
209201
210202 if cpPort != port {
211- return fmt .Errorf ("the %s cluster has been provisioned with a mismatching port" , gkc .GetKind ())
203+ return fmt .Errorf ("the %s cluster has been provisioned with a mismatching port %d instead of %d " , gkc .GetKind (), cpPort , port )
212204 }
213205
214206 return nil
215207}
216208
217209//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=openstackclusters,verbs=patch;get;list;watch
218210
219- func (r * KamajiControlPlaneReconciler ) patchOpenStackCluster (ctx context.Context , cluster capiv1beta1.Cluster , endpoint string , port int64 ) error {
220- osc := unstructured.Unstructured {}
221-
222- osc .SetGroupVersionKind (cluster .Spec .InfrastructureRef .GroupVersionKind ())
223- osc .SetName (cluster .Spec .InfrastructureRef .Name )
224- osc .SetNamespace (cluster .Spec .InfrastructureRef .Namespace )
225-
226- if err := r .client .Get (ctx , types.NamespacedName {Name : osc .GetName (), Namespace : osc .GetNamespace ()}, & osc ); err != nil {
227- return errors .Wrap (err , fmt .Sprintf ("cannot retrieve the %s resource" , osc .GetKind ()))
211+ func (r * KamajiControlPlaneReconciler ) patchOpenStackCluster (ctx context.Context , cluster capiv1beta2.Cluster , endpoint string , port int64 ) error {
212+ osc , err := external .GetObjectFromContractVersionedRef (ctx , r .client , cluster .Spec .InfrastructureRef , cluster .GetNamespace ())
213+ if err != nil {
214+ return errors .Wrap (err , fmt .Sprintf ("cannot get infrastructure reference %s" , cluster .Spec .InfrastructureRef .Name ))
228215 }
229216
230- patchHelper , err := patch .NewHelper (& osc , r .client )
217+ patchHelper , err := patch .NewHelper (osc , r .client )
231218 if err != nil {
232219 return errors .Wrap (err , "unable to create patch helper" )
233220 }
@@ -240,7 +227,7 @@ func (r *KamajiControlPlaneReconciler) patchOpenStackCluster(ctx context.Context
240227 return errors .Wrap (err , fmt .Sprintf ("unable to set unstructured %s spec apiServerPort" , osc .GetKind ()))
241228 }
242229
243- if err = patchHelper .Patch (ctx , & osc ); err != nil {
230+ if err = patchHelper .Patch (ctx , osc ); err != nil {
244231 return errors .Wrap (err , "cannot perform PATCH update for the OpenStackCluster resource" )
245232 }
246233
0 commit comments