@@ -39,12 +39,14 @@ import (
3939 utilfeature "k8s.io/component-base/featuregate/testing"
4040 "k8s.io/utils/ptr"
4141 ctrl "sigs.k8s.io/controller-runtime"
42+ "sigs.k8s.io/controller-runtime/pkg/client"
4243 "sigs.k8s.io/controller-runtime/pkg/client/fake"
4344
4445 clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
4546 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4647 runtimehooksv1 "sigs.k8s.io/cluster-api/api/runtime/hooks/v1alpha1"
4748 runtimev1 "sigs.k8s.io/cluster-api/api/runtime/v1beta2"
49+ "sigs.k8s.io/cluster-api/controllers/clustercache"
4850 runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
4951 "sigs.k8s.io/cluster-api/exp/topology/scope"
5052 "sigs.k8s.io/cluster-api/feature"
@@ -4446,6 +4448,105 @@ func TestCalculateRefDesiredAPIVersion(t *testing.T) {
44464448 }
44474449}
44484450
4451+ func TestGenerate (t * testing.T ) {
4452+ // templates and ClusterClass
4453+ infrastructureClusterTemplate := builder .InfrastructureClusterTemplate (metav1 .NamespaceDefault , "template1" ).
4454+ Build ()
4455+ controlPlaneTemplate := builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "template1" ).
4456+ Build ()
4457+ clusterClass := builder .ClusterClass (metav1 .NamespaceDefault , "class1" ).
4458+ WithInfrastructureClusterTemplate (infrastructureClusterTemplate ).
4459+ WithControlPlaneTemplate (controlPlaneTemplate ).
4460+ Build ()
4461+
4462+ // aggregating templates and cluster class into a blueprint
4463+ blueprint := & scope.ClusterBlueprint {
4464+ ClusterClass : clusterClass ,
4465+ InfrastructureClusterTemplate : infrastructureClusterTemplate ,
4466+ ControlPlane : & scope.ControlPlaneBlueprint {
4467+ Template : controlPlaneTemplate ,
4468+ },
4469+ }
4470+
4471+ // current cluster objects
4472+ cluster := & clusterv1.Cluster {
4473+ ObjectMeta : metav1.ObjectMeta {
4474+ Name : "cluster1" ,
4475+ Namespace : metav1 .NamespaceDefault ,
4476+ },
4477+ }
4478+
4479+ version := "v1.33.0"
4480+ workerInfrastructureMachinePool := builder .InfrastructureMachinePoolTemplate (metav1 .NamespaceDefault , "linux-worker-inframachinepool" ).
4481+ Build ()
4482+ workerBootstrapConfig := builder .BootstrapTemplate (metav1 .NamespaceDefault , "linux-worker-bootstrap" ).
4483+ Build ()
4484+
4485+ node := & corev1.Node {
4486+ ObjectMeta : metav1.ObjectMeta {
4487+ Name : "node-0" ,
4488+ },
4489+ Status : corev1.NodeStatus {
4490+ NodeInfo : corev1.NodeSystemInfo {
4491+ KubeletVersion : "v1.32.0" , // Not yet upgraded to v1.33.0.
4492+ },
4493+ },
4494+ }
4495+ crd := builder .GenericControlPlaneCRD .DeepCopy ()
4496+
4497+ t .Run ("Generate desired state and verify MP is marked as upgrading" , func (t * testing.T ) {
4498+ g := NewWithT (t )
4499+
4500+ fakeClient := fake .NewClientBuilder ().WithScheme (fakeScheme ).WithObjects (node , crd ).Build ()
4501+ fakeRuntimeClient := fakeruntimeclient .NewRuntimeClientBuilder ().Build ()
4502+ clusterCache := clustercache .NewFakeClusterCache (fakeClient , client.ObjectKey {Name : cluster .Name , Namespace : cluster .Namespace })
4503+
4504+ desiredStateGenerator , err := NewGenerator (
4505+ fakeClient ,
4506+ clusterCache ,
4507+ fakeRuntimeClient ,
4508+ )
4509+ g .Expect (err ).ToNot (HaveOccurred ())
4510+
4511+ s := scope .New (cluster )
4512+ s .Blueprint = blueprint
4513+
4514+ mp := builder .MachinePool (metav1 .NamespaceDefault , "existing-pool" ).
4515+ WithVersion (version ).
4516+ WithReplicas (3 ).
4517+ WithBootstrap (workerBootstrapConfig ).
4518+ WithInfrastructure (workerInfrastructureMachinePool ).
4519+ WithStatus (clusterv1.MachinePoolStatus {
4520+ NodeRefs : []corev1.ObjectReference {
4521+ {
4522+ Kind : "Node" ,
4523+ Namespace : metav1 .NamespaceDefault ,
4524+ Name : "node-0" ,
4525+ },
4526+ },
4527+ }).
4528+ Build ()
4529+
4530+ s .Current .MachinePools = map [string ]* scope.MachinePoolState {
4531+ "pool-of-machines" : {
4532+ Object : mp ,
4533+ BootstrapObject : workerBootstrapConfig ,
4534+ InfrastructureMachinePoolObject : workerInfrastructureMachinePool ,
4535+ },
4536+ }
4537+
4538+ // Get the desired state.
4539+ desiredState , err := desiredStateGenerator .Generate (ctx , s )
4540+ g .Expect (err ).ToNot (HaveOccurred ())
4541+ g .Expect (desiredState ).ToNot (BeNil ())
4542+ g .Expect (desiredState .Cluster ).ToNot (BeNil ())
4543+ g .Expect (desiredState .InfrastructureCluster ).ToNot (BeNil ())
4544+ g .Expect (desiredState .ControlPlane ).ToNot (BeNil ())
4545+ // Verify MP is marked as upgrading
4546+ g .Expect (s .UpgradeTracker .MachinePools .UpgradingNames ()).To (ConsistOf (mp .Name ))
4547+ })
4548+ }
4549+
44494550func validateClusterParameter (originalCluster * clusterv1.Cluster ) func (req runtimehooksv1.RequestObject ) error {
44504551 // return a func that allows to check if expected transformations are applied to the Cluster parameter which is
44514552 // included in the payload for lifecycle hooks calls.
0 commit comments