@@ -33,6 +33,7 @@ import (
3333 "github.com/oracle/oci-go-sdk/v65/core"
3434 corev1 "k8s.io/api/core/v1"
3535 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36+ "k8s.io/apimachinery/pkg/runtime"
3637 "k8s.io/apimachinery/pkg/types"
3738 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
3839 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -1876,6 +1877,22 @@ func TestInstancePoolUpdate(t *testing.T) {
18761877 }, nil )
18771878 },
18781879 },
1880+ {
1881+ name : "no update due to change in replica size as annotation is set" ,
1882+ errorExpected : false ,
1883+ instancepool : & core.InstancePool {
1884+ Size : common .Int (3 ),
1885+ InstanceConfigurationId : common .String ("config_id" ),
1886+ },
1887+ testSpecificSetup : func (ms * MachinePoolScope ) {
1888+ ms .MachinePool .Annotations = map [string ]string {
1889+ clusterv1 .ReplicasManagedByAnnotation : "" , // empty value counts as true (= externally managed)
1890+ }
1891+ newReplicas := int32 (4 )
1892+ ms .MachinePool .Spec .Replicas = & newReplicas
1893+ ms .OCIMachinePool .Spec .InstanceConfiguration .InstanceConfigurationId = common .String ("config_id" )
1894+ },
1895+ },
18791896 }
18801897
18811898 for _ , tc := range tests {
@@ -1893,3 +1910,120 @@ func TestInstancePoolUpdate(t *testing.T) {
18931910 })
18941911 }
18951912}
1913+
1914+ func TestSyncReplicasFromInstancePool (t * testing.T ) {
1915+ var (
1916+ ms * MachinePoolScope
1917+ mockCtrl * gomock.Controller
1918+ )
1919+
1920+ setup := func (t * testing.T , g * WithT ) {
1921+ var err error
1922+ mockCtrl = gomock .NewController (t )
1923+ secret := & corev1.Secret {
1924+ ObjectMeta : metav1.ObjectMeta {
1925+ Name : "bootstrap" ,
1926+ Namespace : "default" ,
1927+ },
1928+ Data : map [string ][]byte {
1929+ "value" : []byte ("test" ),
1930+ },
1931+ }
1932+ ociCluster := & infrastructurev1beta2.OCICluster {
1933+ ObjectMeta : metav1.ObjectMeta {
1934+ UID : "cluster_uid" ,
1935+ },
1936+ Spec : infrastructurev1beta2.OCIClusterSpec {
1937+ CompartmentId : "test-compartment" ,
1938+ OCIResourceIdentifier : "resource_uid" ,
1939+ },
1940+ }
1941+ replicas := int32 (3 )
1942+ infraMachinePool := & infrav2exp.OCIMachinePool {
1943+ ObjectMeta : metav1.ObjectMeta {
1944+ Name : "test" ,
1945+ Namespace : "default" ,
1946+ },
1947+ }
1948+ machinePool := & clusterv1.MachinePool {
1949+ ObjectMeta : metav1.ObjectMeta {
1950+ Name : "test" ,
1951+ Namespace : "default" ,
1952+ },
1953+ Spec : clusterv1.MachinePoolSpec {
1954+ Replicas : & replicas ,
1955+ Template : clusterv1.MachineTemplateSpec {
1956+ Spec : clusterv1.MachineSpec {
1957+ Bootstrap : clusterv1.Bootstrap {
1958+ DataSecretName : common .String ("bootstrap" ),
1959+ },
1960+ },
1961+ },
1962+ },
1963+ }
1964+ scheme := runtime .NewScheme ()
1965+ g .Expect (corev1 .AddToScheme (scheme )).To (Succeed ())
1966+ g .Expect (clusterv1 .AddToScheme (scheme )).To (Succeed ())
1967+ g .Expect (infrav2exp .AddToScheme (scheme )).To (Succeed ())
1968+
1969+ client := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (secret , infraMachinePool , machinePool ).Build ()
1970+ ms , err = NewMachinePoolScope (MachinePoolScopeParams {
1971+ ComputeManagementClient : mock_computemanagement .NewMockClient (mockCtrl ),
1972+ OCIMachinePool : infraMachinePool ,
1973+ OCIClusterAccessor : OCISelfManagedCluster {
1974+ OCICluster : ociCluster ,
1975+ },
1976+ Cluster : & clusterv1.Cluster {},
1977+ MachinePool : machinePool ,
1978+ Client : client ,
1979+ })
1980+ g .Expect (err ).To (BeNil ())
1981+ }
1982+ teardown := func (t * testing.T , g * WithT ) {
1983+ mockCtrl .Finish ()
1984+ }
1985+
1986+ tests := []struct {
1987+ name string
1988+ setup func (ms * MachinePoolScope )
1989+ instancePool * core.InstancePool
1990+ expectedReplicas int32
1991+ }{
1992+ {
1993+ name : "does not patch replicas when annotation is not set" ,
1994+ setup : func (ms * MachinePoolScope ) {
1995+ ms .MachinePool .Annotations = nil
1996+ },
1997+ instancePool : & core.InstancePool {Size : common .Int (4 )},
1998+ expectedReplicas : 3 ,
1999+ },
2000+ {
2001+ name : "patches replicas from observed size when annotation is set" ,
2002+ setup : func (ms * MachinePoolScope ) {
2003+ ms .MachinePool .Annotations = map [string ]string {
2004+ clusterv1 .ReplicasManagedByAnnotation : "" , // empty value counts as true (= externally managed)
2005+ }
2006+ },
2007+ instancePool : & core.InstancePool {Size : common .Int (4 )},
2008+ expectedReplicas : 4 ,
2009+ },
2010+ }
2011+
2012+ for _ , tc := range tests {
2013+ t .Run (tc .name , func (t * testing.T ) {
2014+ g := NewWithT (t )
2015+ defer teardown (t , g )
2016+ setup (t , g )
2017+ tc .setup (ms )
2018+
2019+ err := ms .SyncReplicasFromInstancePool (context .Background (), tc .instancePool )
2020+ g .Expect (err ).To (BeNil ())
2021+
2022+ updatedMachinePool := & clusterv1.MachinePool {}
2023+ err = ms .Client .Get (context .Background (), client.ObjectKey {Name : ms .MachinePool .Name , Namespace : ms .MachinePool .Namespace }, updatedMachinePool )
2024+ g .Expect (err ).To (BeNil ())
2025+ g .Expect (updatedMachinePool .Spec .Replicas ).ToNot (BeNil ())
2026+ g .Expect (* updatedMachinePool .Spec .Replicas ).To (Equal (tc .expectedReplicas ))
2027+ })
2028+ }
2029+ }
0 commit comments