@@ -31,6 +31,7 @@ import (
3131 "sigs.k8s.io/cluster-api-provider-aws/v2/bootstrap/eks/internal/userdata"
3232 ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
3333 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
34+ "sigs.k8s.io/cluster-api/exp/api/v1beta1"
3435 "sigs.k8s.io/cluster-api/util"
3536 "sigs.k8s.io/cluster-api/util/conditions"
3637)
@@ -55,7 +56,7 @@ func TestEKSConfigReconciler(t *testing.T) {
5556 }
5657 t .Logf (fmt .Sprintf ("Calling reconcile on cluster '%s' and config '%s' should requeue" , cluster .Name , config .Name ))
5758 g .Eventually (func (gomega Gomega ) {
58- result , err := reconciler .joinWorker (ctx , cluster , config )
59+ result , err := reconciler .joinWorker (ctx , cluster , config , configOwner ( "Machine" ) )
5960 gomega .Expect (err ).NotTo (HaveOccurred ())
6061 gomega .Expect (result .Requeue ).To (BeFalse ())
6162 }).Should (Succeed ())
@@ -74,16 +75,26 @@ func TestEKSConfigReconciler(t *testing.T) {
7475
7576 g .Expect (string (secret .Data ["value" ])).To (Equal (string (expectedUserData )))
7677 })
77-
7878 t .Run ("Should reconcile an EKSConfig and update data Secret" , func (t * testing.T ) {
7979 g := NewWithT (t )
8080 amcp := newAMCP ("test-cluster" )
8181 cluster := newCluster (amcp .Name )
82- machine := newMachine (cluster , "test-machine" )
83- config := newEKSConfig (machine )
82+ mp := newMachinePool (cluster , "test-machine" )
83+ config := newEKSConfig (nil )
84+ config .ObjectMeta .Name = mp .Name
85+ config .ObjectMeta .UID = types .UID (fmt .Sprintf ("%s uid" , mp .Name ))
86+ config .ObjectMeta .OwnerReferences = []metav1.OwnerReference {
87+ {
88+ Kind : "MachinePool" ,
89+ APIVersion : v1beta1 .GroupVersion .String (),
90+ Name : mp .Name ,
91+ UID : types .UID (fmt .Sprintf ("%s uid" , mp .Name )),
92+ },
93+ }
94+ config .Status .DataSecretName = & mp .Name
8495 t .Logf (dump ("amcp" , amcp ))
8596 t .Logf (dump ("config" , config ))
86- t .Logf (dump ("machine " , machine ))
97+ t .Logf (dump ("machinepool " , mp ))
8798 t .Logf (dump ("cluster" , cluster ))
8899 oldUserData , err := newUserData (cluster .Name , map [string ]string {"test-arg" : "test-value" })
89100 g .Expect (err ).To (BeNil ())
@@ -100,7 +111,7 @@ func TestEKSConfigReconciler(t *testing.T) {
100111 }
101112 t .Logf (fmt .Sprintf ("Calling reconcile on cluster '%s' and config '%s' should requeue" , cluster .Name , config .Name ))
102113 g .Eventually (func (gomega Gomega ) {
103- result , err := reconciler .joinWorker (ctx , cluster , config )
114+ result , err := reconciler .joinWorker (ctx , cluster , config , configOwner ( "MachinePool" ) )
104115 gomega .Expect (err ).NotTo (HaveOccurred ())
105116 gomega .Expect (result .Requeue ).To (BeFalse ())
106117 }).Should (Succeed ())
@@ -125,7 +136,7 @@ func TestEKSConfigReconciler(t *testing.T) {
125136 }
126137 t .Logf (dump ("config" , config ))
127138 g .Eventually (func (gomega Gomega ) {
128- result , err := reconciler .joinWorker (ctx , cluster , config )
139+ result , err := reconciler .joinWorker (ctx , cluster , config , configOwner ( "MachinePool" ) )
129140 gomega .Expect (err ).NotTo (HaveOccurred ())
130141 gomega .Expect (result .Requeue ).To (BeFalse ())
131142 }).Should (Succeed ())
@@ -141,6 +152,57 @@ func TestEKSConfigReconciler(t *testing.T) {
141152 gomega .Expect (string (secret .Data ["value" ])).To (Equal (string (expectedUserData )))
142153 }).Should (Succeed ())
143154 })
155+
156+ t .Run ("Should reconcile an EKSConfig and not update data if secret exists and config owner is Machine kind" , func (t * testing.T ) {
157+ g := NewWithT (t )
158+ amcp := newAMCP ("test-cluster" )
159+ cluster := newCluster (amcp .Name )
160+ machine := newMachine (cluster , "test-machine" )
161+ config := newEKSConfig (machine )
162+ t .Logf (dump ("amcp" , amcp ))
163+ t .Logf (dump ("config" , config ))
164+ t .Logf (dump ("machine" , machine ))
165+ t .Logf (dump ("cluster" , cluster ))
166+ expectedUserData , err := newUserData (cluster .Name , map [string ]string {"test-arg" : "test-value" })
167+ g .Expect (err ).To (BeNil ())
168+ g .Expect (testEnv .Client .Create (ctx , amcp )).To (Succeed ())
169+
170+ secret := & corev1.Secret {
171+ ObjectMeta : metav1.ObjectMeta {
172+ Namespace : "default" ,
173+ Name : machine .Name ,
174+ },
175+ }
176+ g .Expect (testEnv .Client .Create (ctx , secret )).To (Succeed ())
177+
178+ amcpList := & ekscontrolplanev1.AWSManagedControlPlaneList {}
179+ testEnv .Client .List (ctx , amcpList )
180+ t .Logf (dump ("stored-amcps" , amcpList ))
181+
182+ reconciler := EKSConfigReconciler {
183+ Client : testEnv .Client ,
184+ }
185+ t .Logf (fmt .Sprintf ("Calling reconcile on cluster '%s' and config '%s' should requeue" , cluster .Name , config .Name ))
186+ g .Eventually (func (gomega Gomega ) {
187+ result , err := reconciler .joinWorker (ctx , cluster , config , configOwner ("Machine" ))
188+ gomega .Expect (err ).NotTo (HaveOccurred ())
189+ gomega .Expect (result .Requeue ).To (BeFalse ())
190+ }).Should (Succeed ())
191+
192+ t .Logf (fmt .Sprintf ("Secret '%s' should exist and be out of date" , config .Name ))
193+ secretList := & corev1.SecretList {}
194+ testEnv .Client .List (ctx , secretList )
195+ t .Logf (dump ("secrets" , secretList ))
196+
197+ secret = & corev1.Secret {}
198+ g .Eventually (func (gomega Gomega ) {
199+ gomega .Expect (testEnv .Client .Get (ctx , client.ObjectKey {
200+ Name : config .Name ,
201+ Namespace : "default" ,
202+ }, secret )).To (Succeed ())
203+ gomega .Expect (string (secret .Data ["value" ])).To (Not (Equal (string (expectedUserData ))))
204+ }).Should (Succeed ())
205+ })
144206}
145207
146208// newCluster return a CAPI cluster object.
@@ -204,6 +266,40 @@ func newMachine(cluster *clusterv1.Cluster, name string) *clusterv1.Machine {
204266 return machine
205267}
206268
269+ // newMachinePool returns a CAPI machine object; if cluster is not nil, the MachinePool is linked to the cluster as well.
270+ func newMachinePool (cluster * clusterv1.Cluster , name string ) * v1beta1.MachinePool {
271+ generatedName := fmt .Sprintf ("%s-%s" , name , util .RandomString (5 ))
272+ mp := & v1beta1.MachinePool {
273+ TypeMeta : metav1.TypeMeta {
274+ Kind : "MachinePool" ,
275+ APIVersion : v1beta1 .GroupVersion .String (),
276+ },
277+ ObjectMeta : metav1.ObjectMeta {
278+ Namespace : "default" ,
279+ Name : generatedName ,
280+ },
281+ Spec : v1beta1.MachinePoolSpec {
282+ Template : clusterv1.MachineTemplateSpec {
283+ Spec : clusterv1.MachineSpec {
284+ Bootstrap : clusterv1.Bootstrap {
285+ ConfigRef : & corev1.ObjectReference {
286+ Kind : "EKSConfig" ,
287+ APIVersion : eksbootstrapv1 .GroupVersion .String (),
288+ },
289+ },
290+ },
291+ },
292+ },
293+ }
294+ if cluster != nil {
295+ mp .Spec .ClusterName = cluster .Name
296+ mp .ObjectMeta .Labels = map [string ]string {
297+ clusterv1 .ClusterLabelName : cluster .Name ,
298+ }
299+ }
300+ return mp
301+ }
302+
207303// newEKSConfig return an EKSConfig object; if machine is not nil, the EKSConfig is linked to the machine as well.
208304func newEKSConfig (machine * clusterv1.Machine ) * eksbootstrapv1.EKSConfig {
209305 config := & eksbootstrapv1.EKSConfig {
@@ -219,6 +315,7 @@ func newEKSConfig(machine *clusterv1.Machine) *eksbootstrapv1.EKSConfig {
219315 "test-arg" : "test-value" ,
220316 },
221317 },
318+ Status : eksbootstrapv1.EKSConfigStatus {},
222319 }
223320 if machine != nil {
224321 config .ObjectMeta .Name = machine .Name
@@ -231,6 +328,7 @@ func newEKSConfig(machine *clusterv1.Machine) *eksbootstrapv1.EKSConfig {
231328 UID : types .UID (fmt .Sprintf ("%s uid" , machine .Name )),
232329 },
233330 }
331+ config .Status .DataSecretName = & machine .Name
234332 machine .Spec .Bootstrap .ConfigRef .Name = config .Name
235333 machine .Spec .Bootstrap .ConfigRef .Namespace = config .Namespace
236334 }
0 commit comments