@@ -18,12 +18,16 @@ package scope
1818
1919import (
2020 "context"
21+ "crypto/sha256"
22+ "encoding/base64"
2123 "fmt"
24+ "io"
2225 "reflect"
2326 "testing"
2427
2528 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
2629 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
30+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
2731 azureautorest "github.com/Azure/go-autorest/autorest/azure"
2832 "github.com/Azure/go-autorest/autorest/azure/auth"
2933 . "github.com/onsi/gomega"
@@ -1515,3 +1519,148 @@ func TestMachinePoolScope_applyAzureMachinePoolMachines(t *testing.T) {
15151519 })
15161520 }
15171521}
1522+
1523+ func TestBootstrapDataChanges (t * testing.T ) {
1524+ ctx , cancel := context .WithCancel (context .Background ())
1525+ defer cancel ()
1526+ scheme := runtime .NewScheme ()
1527+ _ = clusterv1 .AddToScheme (scheme )
1528+ _ = infrav1 .AddToScheme (scheme )
1529+ _ = infrav1exp .AddToScheme (scheme )
1530+ _ = corev1 .AddToScheme (scheme )
1531+
1532+ var (
1533+ g = NewWithT (t )
1534+ mockCtrl = gomock .NewController (t )
1535+ cb = fake .NewClientBuilder ().WithScheme (scheme )
1536+ cluster = & clusterv1.Cluster {
1537+ ObjectMeta : metav1.ObjectMeta {
1538+ Name : "cluster1" ,
1539+ Namespace : "default" ,
1540+ },
1541+ Spec : clusterv1.ClusterSpec {
1542+ InfrastructureRef : & corev1.ObjectReference {
1543+ Name : "azCluster1" ,
1544+ },
1545+ },
1546+ Status : clusterv1.ClusterStatus {
1547+ InfrastructureReady : true ,
1548+ },
1549+ }
1550+ azureCluster = & infrav1.AzureCluster {
1551+ ObjectMeta : metav1.ObjectMeta {
1552+ Name : "azCluster1" ,
1553+ Namespace : "default" ,
1554+ },
1555+ Spec : infrav1.AzureClusterSpec {
1556+ AzureClusterClassSpec : infrav1.AzureClusterClassSpec {
1557+ Location : "test" ,
1558+ },
1559+ },
1560+ }
1561+ mp = & expv1.MachinePool {
1562+ ObjectMeta : metav1.ObjectMeta {
1563+ Name : "mp1" ,
1564+ Namespace : "default" ,
1565+ OwnerReferences : []metav1.OwnerReference {
1566+ {
1567+ Name : "cluster1" ,
1568+ Kind : "Cluster" ,
1569+ APIVersion : clusterv1 .GroupVersion .String (),
1570+ },
1571+ },
1572+ },
1573+ Spec : expv1.MachinePoolSpec {
1574+ Template : clusterv1.MachineTemplateSpec {
1575+ Spec : clusterv1.MachineSpec {
1576+ Bootstrap : clusterv1.Bootstrap {
1577+ DataSecretName : ptr .To ("mp-secret" ),
1578+ },
1579+ Version : ptr .To ("v1.31.0" ),
1580+ },
1581+ },
1582+ },
1583+ }
1584+ bootstrapData = "test"
1585+ bootstrapDataHash = sha256Hash (base64 .StdEncoding .EncodeToString ([]byte (bootstrapData )))
1586+ bootstrapSecret = corev1.Secret {
1587+ ObjectMeta : metav1.ObjectMeta {
1588+ Name : "mp-secret" ,
1589+ Namespace : "default" ,
1590+ },
1591+ Data : map [string ][]byte {"value" : []byte (bootstrapData )},
1592+ }
1593+ amp = & infrav1exp.AzureMachinePool {
1594+ ObjectMeta : metav1.ObjectMeta {
1595+ Name : "amp1" ,
1596+ Namespace : "default" ,
1597+ OwnerReferences : []metav1.OwnerReference {
1598+ {
1599+ Name : "mp1" ,
1600+ Kind : "MachinePool" ,
1601+ APIVersion : expv1 .GroupVersion .String (),
1602+ },
1603+ },
1604+ Annotations : map [string ]string {
1605+ azure .CustomDataHashAnnotation : fmt .Sprintf ("%x" , bootstrapDataHash ),
1606+ },
1607+ },
1608+ Spec : infrav1exp.AzureMachinePoolSpec {
1609+ Template : infrav1exp.AzureMachinePoolMachineTemplate {
1610+ Image : & infrav1.Image {},
1611+ NetworkInterfaces : []infrav1.NetworkInterface {
1612+ {
1613+ SubnetName : "test" ,
1614+ },
1615+ },
1616+ VMSize : "VM_SIZE" ,
1617+ },
1618+ },
1619+ }
1620+ vmssState = & azure.VMSS {}
1621+ )
1622+ defer mockCtrl .Finish ()
1623+
1624+ s := & MachinePoolScope {
1625+ client : cb .
1626+ WithObjects (& bootstrapSecret ).
1627+ Build (),
1628+ ClusterScoper : & ClusterScope {
1629+ Cluster : cluster ,
1630+ AzureCluster : azureCluster ,
1631+ },
1632+ skuCache : resourceskus .NewStaticCache ([]armcompute.ResourceSKU {
1633+ {
1634+ Name : ptr .To ("VM_SIZE" ),
1635+ },
1636+ }, "test" ),
1637+ MachinePool : mp ,
1638+ AzureMachinePool : amp ,
1639+ vmssState : vmssState ,
1640+ }
1641+
1642+ g .Expect (s .InitMachinePoolCache (ctx )).NotTo (HaveOccurred ())
1643+
1644+ spec := s .ScaleSetSpec (ctx )
1645+ sSpec := spec .(* scalesets.ScaleSetSpec )
1646+ g .Expect (sSpec .ShouldPatchCustomData ).To (Equal (false ))
1647+
1648+ amp .Annotations [azure .CustomDataHashAnnotation ] = "old"
1649+
1650+ // reset cache to be able to build up the cache again
1651+ s .cache = nil
1652+ g .Expect (s .InitMachinePoolCache (ctx )).NotTo (HaveOccurred ())
1653+
1654+ spec = s .ScaleSetSpec (ctx )
1655+ sSpec = spec .(* scalesets.ScaleSetSpec )
1656+ g .Expect (sSpec .ShouldPatchCustomData ).To (Equal (true ))
1657+ }
1658+
1659+ func sha256Hash (text string ) []byte {
1660+ h := sha256 .New ()
1661+ _ , err := io .WriteString (h , text )
1662+ if err != nil {
1663+ panic (err )
1664+ }
1665+ return h .Sum (nil )
1666+ }
0 commit comments