@@ -18,13 +18,17 @@ package scope
1818
1919import (
2020 "context"
21+ "crypto/sha256"
22+ "encoding/base64"
2123 "fmt"
24+ "io"
2225 "reflect"
2326 "testing"
2427 "time"
2528
2629 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
2730 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
31+ "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
2832 azureautorest "github.com/Azure/go-autorest/autorest/azure"
2933 "github.com/Azure/go-autorest/autorest/azure/auth"
3034 . "github.com/onsi/gomega"
@@ -1576,3 +1580,148 @@ func TestMachinePoolScope_applyAzureMachinePoolMachines(t *testing.T) {
15761580 })
15771581 }
15781582}
1583+
1584+ func TestBootstrapDataChanges (t * testing.T ) {
1585+ ctx , cancel := context .WithCancel (context .Background ())
1586+ defer cancel ()
1587+ scheme := runtime .NewScheme ()
1588+ _ = clusterv1 .AddToScheme (scheme )
1589+ _ = infrav1 .AddToScheme (scheme )
1590+ _ = infrav1exp .AddToScheme (scheme )
1591+ _ = corev1 .AddToScheme (scheme )
1592+
1593+ var (
1594+ g = NewWithT (t )
1595+ mockCtrl = gomock .NewController (t )
1596+ cb = fake .NewClientBuilder ().WithScheme (scheme )
1597+ cluster = & clusterv1.Cluster {
1598+ ObjectMeta : metav1.ObjectMeta {
1599+ Name : "cluster1" ,
1600+ Namespace : "default" ,
1601+ },
1602+ Spec : clusterv1.ClusterSpec {
1603+ InfrastructureRef : & corev1.ObjectReference {
1604+ Name : "azCluster1" ,
1605+ },
1606+ },
1607+ Status : clusterv1.ClusterStatus {
1608+ InfrastructureReady : true ,
1609+ },
1610+ }
1611+ azureCluster = & infrav1.AzureCluster {
1612+ ObjectMeta : metav1.ObjectMeta {
1613+ Name : "azCluster1" ,
1614+ Namespace : "default" ,
1615+ },
1616+ Spec : infrav1.AzureClusterSpec {
1617+ AzureClusterClassSpec : infrav1.AzureClusterClassSpec {
1618+ Location : "test" ,
1619+ },
1620+ },
1621+ }
1622+ mp = & expv1.MachinePool {
1623+ ObjectMeta : metav1.ObjectMeta {
1624+ Name : "mp1" ,
1625+ Namespace : "default" ,
1626+ OwnerReferences : []metav1.OwnerReference {
1627+ {
1628+ Name : "cluster1" ,
1629+ Kind : "Cluster" ,
1630+ APIVersion : clusterv1 .GroupVersion .String (),
1631+ },
1632+ },
1633+ },
1634+ Spec : expv1.MachinePoolSpec {
1635+ Template : clusterv1.MachineTemplateSpec {
1636+ Spec : clusterv1.MachineSpec {
1637+ Bootstrap : clusterv1.Bootstrap {
1638+ DataSecretName : ptr .To ("mp-secret" ),
1639+ },
1640+ Version : ptr .To ("v1.31.0" ),
1641+ },
1642+ },
1643+ },
1644+ }
1645+ bootstrapData = "test"
1646+ bootstrapDataHash = sha256Hash (base64 .StdEncoding .EncodeToString ([]byte (bootstrapData )))
1647+ bootstrapSecret = corev1.Secret {
1648+ ObjectMeta : metav1.ObjectMeta {
1649+ Name : "mp-secret" ,
1650+ Namespace : "default" ,
1651+ },
1652+ Data : map [string ][]byte {"value" : []byte (bootstrapData )},
1653+ }
1654+ amp = & infrav1exp.AzureMachinePool {
1655+ ObjectMeta : metav1.ObjectMeta {
1656+ Name : "amp1" ,
1657+ Namespace : "default" ,
1658+ OwnerReferences : []metav1.OwnerReference {
1659+ {
1660+ Name : "mp1" ,
1661+ Kind : "MachinePool" ,
1662+ APIVersion : expv1 .GroupVersion .String (),
1663+ },
1664+ },
1665+ Annotations : map [string ]string {
1666+ azure .CustomDataHashAnnotation : fmt .Sprintf ("%x" , bootstrapDataHash ),
1667+ },
1668+ },
1669+ Spec : infrav1exp.AzureMachinePoolSpec {
1670+ Template : infrav1exp.AzureMachinePoolMachineTemplate {
1671+ Image : & infrav1.Image {},
1672+ NetworkInterfaces : []infrav1.NetworkInterface {
1673+ {
1674+ SubnetName : "test" ,
1675+ },
1676+ },
1677+ VMSize : "VM_SIZE" ,
1678+ },
1679+ },
1680+ }
1681+ vmssState = & azure.VMSS {}
1682+ )
1683+ defer mockCtrl .Finish ()
1684+
1685+ s := & MachinePoolScope {
1686+ client : cb .
1687+ WithObjects (& bootstrapSecret ).
1688+ Build (),
1689+ ClusterScoper : & ClusterScope {
1690+ Cluster : cluster ,
1691+ AzureCluster : azureCluster ,
1692+ },
1693+ skuCache : resourceskus .NewStaticCache ([]armcompute.ResourceSKU {
1694+ {
1695+ Name : ptr .To ("VM_SIZE" ),
1696+ },
1697+ }, "test" ),
1698+ MachinePool : mp ,
1699+ AzureMachinePool : amp ,
1700+ vmssState : vmssState ,
1701+ }
1702+
1703+ g .Expect (s .InitMachinePoolCache (ctx )).NotTo (HaveOccurred ())
1704+
1705+ spec := s .ScaleSetSpec (ctx )
1706+ sSpec := spec .(* scalesets.ScaleSetSpec )
1707+ g .Expect (sSpec .ShouldPatchCustomData ).To (Equal (false ))
1708+
1709+ amp .Annotations [azure .CustomDataHashAnnotation ] = "old"
1710+
1711+ // reset cache to be able to build up the cache again
1712+ s .cache = nil
1713+ g .Expect (s .InitMachinePoolCache (ctx )).NotTo (HaveOccurred ())
1714+
1715+ spec = s .ScaleSetSpec (ctx )
1716+ sSpec = spec .(* scalesets.ScaleSetSpec )
1717+ g .Expect (sSpec .ShouldPatchCustomData ).To (Equal (true ))
1718+ }
1719+
1720+ func sha256Hash (text string ) []byte {
1721+ h := sha256 .New ()
1722+ _ , err := io .WriteString (h , text )
1723+ if err != nil {
1724+ panic (err )
1725+ }
1726+ return h .Sum (nil )
1727+ }
0 commit comments