@@ -18,15 +18,19 @@ package controllers
1818
1919import (
2020 "context"
21+ "encoding/json"
2122 "testing"
2223 "time"
2324
25+ asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
26+ "github.com/Azure/azure-service-operator/v2/pkg/genruntime"
2427 . "github.com/onsi/gomega"
2528 corev1 "k8s.io/api/core/v1"
2629 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2730 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2831 "k8s.io/apimachinery/pkg/runtime"
2932 "k8s.io/apimachinery/pkg/types"
33+ "k8s.io/utils/ptr"
3034 infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1"
3135 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3236 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
@@ -35,6 +39,17 @@ import (
3539 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3640)
3741
42+ type FakeClusterTracker struct {
43+ getClientFunc func (context.Context , types.NamespacedName ) (client.Client , error )
44+ }
45+
46+ func (c * FakeClusterTracker ) GetClient (ctx context.Context , name types.NamespacedName ) (client.Client , error ) {
47+ if c .getClientFunc == nil {
48+ return nil , nil
49+ }
50+ return c .getClientFunc (ctx , name )
51+ }
52+
3853func TestAzureASOManagedMachinePoolReconcile (t * testing.T ) {
3954 ctx := context .Background ()
4055
@@ -43,6 +58,7 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
4358 infrav1exp .AddToScheme ,
4459 clusterv1 .AddToScheme ,
4560 expv1 .AddToScheme ,
61+ asocontainerservicev1 .AddToScheme ,
4662 )
4763 NewGomegaWithT (t ).Expect (sb .AddToScheme (s )).To (Succeed ())
4864 fakeClientBuilder := func () * fakeclient.ClientBuilder {
@@ -210,6 +226,19 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
210226 clusterv1 .ClusterFinalizer ,
211227 },
212228 },
229+ Spec : infrav1exp.AzureASOManagedMachinePoolSpec {
230+ AzureASOManagedMachinePoolTemplateResourceSpec : infrav1exp.AzureASOManagedMachinePoolTemplateResourceSpec {
231+ Resources : []runtime.RawExtension {
232+ {
233+ Raw : apJSON (g , & asocontainerservicev1.ManagedClustersAgentPool {
234+ ObjectMeta : metav1.ObjectMeta {
235+ Name : "ap" ,
236+ },
237+ }),
238+ },
239+ },
240+ },
241+ },
213242 }
214243 machinePool := & expv1.MachinePool {
215244 ObjectMeta : metav1.ObjectMeta {
@@ -259,6 +288,30 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
259288 },
260289 },
261290 }
291+ asoManagedCluster := & asocontainerservicev1.ManagedCluster {
292+ ObjectMeta : metav1.ObjectMeta {
293+ Name : "mc" ,
294+ Namespace : cluster .Namespace ,
295+ },
296+ Status : asocontainerservicev1.ManagedCluster_STATUS {
297+ NodeResourceGroup : ptr .To ("MC_rg" ),
298+ },
299+ }
300+ asoAgentPool := & asocontainerservicev1.ManagedClustersAgentPool {
301+ ObjectMeta : metav1.ObjectMeta {
302+ Name : "ap" ,
303+ Namespace : cluster .Namespace ,
304+ },
305+ Spec : asocontainerservicev1.ManagedClusters_AgentPool_Spec {
306+ AzureName : "pool1" ,
307+ Owner : & genruntime.KnownResourceReference {
308+ Name : asoManagedCluster .Name ,
309+ },
310+ },
311+ Status : asocontainerservicev1.ManagedClusters_AgentPool_STATUS {
312+ Count : ptr .To (3 ),
313+ },
314+ }
262315 asoManagedMachinePool := & infrav1exp.AzureASOManagedMachinePool {
263316 ObjectMeta : metav1.ObjectMeta {
264317 Name : "ammp" ,
@@ -274,6 +327,15 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
274327 clusterv1 .ClusterFinalizer ,
275328 },
276329 },
330+ Spec : infrav1exp.AzureASOManagedMachinePoolSpec {
331+ AzureASOManagedMachinePoolTemplateResourceSpec : infrav1exp.AzureASOManagedMachinePoolTemplateResourceSpec {
332+ Resources : []runtime.RawExtension {
333+ {
334+ Raw : apJSON (g , asoAgentPool ),
335+ },
336+ },
337+ },
338+ },
277339 }
278340 machinePool := & expv1.MachinePool {
279341 ObjectMeta : metav1.ObjectMeta {
@@ -285,7 +347,7 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
285347 },
286348 }
287349 c := fakeClientBuilder ().
288- WithObjects (asoManagedMachinePool , machinePool , cluster ).
350+ WithObjects (asoManagedMachinePool , machinePool , cluster , asoAgentPool , asoManagedCluster ).
289351 Build ()
290352 r := & AzureASOManagedMachinePoolReconciler {
291353 Client : c ,
@@ -296,10 +358,48 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
296358 },
297359 }
298360 },
361+ Tracker : & FakeClusterTracker {
362+ getClientFunc : func (_ context.Context , _ types.NamespacedName ) (client.Client , error ) {
363+ return fakeclient .NewClientBuilder ().
364+ WithObjects (
365+ & corev1.Node {
366+ ObjectMeta : metav1.ObjectMeta {
367+ Name : "node1" ,
368+ Labels : expectedNodeLabels (asoAgentPool .AzureName (), * asoManagedCluster .Status .NodeResourceGroup ),
369+ },
370+ Spec : corev1.NodeSpec {
371+ ProviderID : "azure://node1" ,
372+ },
373+ },
374+ & corev1.Node {
375+ ObjectMeta : metav1.ObjectMeta {
376+ Name : "node2" ,
377+ Labels : expectedNodeLabels (asoAgentPool .AzureName (), * asoManagedCluster .Status .NodeResourceGroup ),
378+ },
379+ Spec : corev1.NodeSpec {
380+ ProviderID : "azure://node2" ,
381+ },
382+ },
383+ & corev1.Node {
384+ ObjectMeta : metav1.ObjectMeta {
385+ Name : "no-labels" ,
386+ },
387+ Spec : corev1.NodeSpec {
388+ ProviderID : "azure://node3" ,
389+ },
390+ },
391+ ).
392+ Build (), nil
393+ },
394+ },
299395 }
300396 result , err := r .Reconcile (ctx , ctrl.Request {NamespacedName : client .ObjectKeyFromObject (asoManagedMachinePool )})
301397 g .Expect (err ).NotTo (HaveOccurred ())
302398 g .Expect (result ).To (Equal (ctrl.Result {}))
399+
400+ g .Expect (r .Get (ctx , client .ObjectKeyFromObject (asoManagedMachinePool ), asoManagedMachinePool )).To (Succeed ())
401+ g .Expect (asoManagedMachinePool .Spec .ProviderIDList ).To (ConsistOf ("azure://node1" , "azure://node2" ))
402+ g .Expect (asoManagedMachinePool .Status .Replicas ).To (Equal (int32 (3 )))
303403 })
304404
305405 t .Run ("successfully reconciles pause" , func (t * testing.T ) {
@@ -410,3 +510,10 @@ func TestAzureASOManagedMachinePoolReconcile(t *testing.T) {
410510 g .Expect (result ).To (Equal (ctrl.Result {}))
411511 })
412512}
513+
514+ func apJSON (g Gomega , ap * asocontainerservicev1.ManagedClustersAgentPool ) []byte {
515+ ap .SetGroupVersionKind (asocontainerservicev1 .GroupVersion .WithKind ("ManagedClustersAgentPool" ))
516+ j , err := json .Marshal (ap )
517+ g .Expect (err ).NotTo (HaveOccurred ())
518+ return j
519+ }
0 commit comments