@@ -667,6 +667,109 @@ var _ = Describe("agentmachine reconcile", func() {
667
667
Expect (agent .Spec .ClusterDeploymentName .Name ).To (BeEquivalentTo ("cluster-deployment-agentMachine-1" ))
668
668
})
669
669
})
670
+ Context ("reconciling ignition token secret" , func () {
671
+ var (
672
+ agent * aiv1beta1.Agent
673
+ agentMachine * capiproviderv1.AgentMachine
674
+ machine * clusterv1.Machine
675
+ )
676
+
677
+ BeforeEach (func () {
678
+ agent = newAgent ("agent-1" , testNamespace , aiv1beta1.AgentSpec {Approved : false })
679
+ agent .Status .Conditions = append (agent .Status .Conditions , v1.Condition {Type : aiv1beta1 .BoundCondition , Status : "True" })
680
+ agent .Status .Conditions = append (agent .Status .Conditions , v1.Condition {Type : aiv1beta1 .ValidatedCondition , Status : "True" })
681
+ agent .Spec .ClusterDeploymentName = & aiv1beta1.ClusterReference {Name : "cluster-deployment-agentMachine-1" , Namespace : testNamespace }
682
+ agentMachine , machine = newAgentMachine ("agentMachine-1" , testNamespace , capiproviderv1.AgentMachineSpec {}, ctx , c )
683
+
684
+ Expect (c .Create (ctx , agent )).To (BeNil ())
685
+ Expect (c .Create (ctx , agentMachine )).To (BeNil ())
686
+ })
687
+ AfterEach (func () {
688
+ mockCtrl .Finish ()
689
+ })
690
+ It ("creates the ignition token secret upon first reconcile of an AgentMachine" , func () {
691
+ result , err := amr .Reconcile (ctx , newAgentMachineRequest (agentMachine ))
692
+ Expect (err ).To (BeNil ())
693
+ Expect (result ).To (Equal (ctrl.Result {}))
694
+
695
+ ignitionTokenSecretName := machine .Spec .Bootstrap .DataSecretName
696
+ ignitionTokenSecret := & corev1.Secret {}
697
+ Expect (c .Get (ctx , types.NamespacedName {Name : * ignitionTokenSecretName , Namespace : testNamespace }, ignitionTokenSecret )).To (Succeed ())
698
+ ignitionConfig := & ignitionapi.Config {}
699
+ Expect (json .Unmarshal (ignitionTokenSecret .Data ["value" ], ignitionConfig )).To (Succeed ())
700
+ expectedPrefix := "Bearer "
701
+ ignitionToken := (* ignitionConfig .Ignition .Config .Merge [0 ].HTTPHeaders [0 ].Value )[len (expectedPrefix ):]
702
+
703
+ agentSecret := & corev1.Secret {}
704
+ Expect (c .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("agent-%s" , * ignitionTokenSecretName ), Namespace : testNamespace }, agentSecret )).To (Succeed ())
705
+ Expect (agentSecret .Data ).NotTo (BeEmpty ())
706
+ Expect (agentSecret .Data ).To (HaveKey ("ignition-token" ))
707
+ Expect (string (agentSecret .Data ["ignition-token" ])).To (Equal (ignitionToken ))
708
+ })
709
+ It ("updates the ignition token secret if the token changed" , func () {
710
+ result , err := amr .Reconcile (ctx , newAgentMachineRequest (agentMachine ))
711
+ Expect (err ).To (BeNil ())
712
+ Expect (result ).To (Equal (ctrl.Result {}))
713
+
714
+ ignitionTokenSecretName := machine .Spec .Bootstrap .DataSecretName
715
+ ignitionTokenSecret := & corev1.Secret {}
716
+ Expect (c .Get (ctx , types.NamespacedName {Name : * ignitionTokenSecretName , Namespace : testNamespace }, ignitionTokenSecret )).To (Succeed ())
717
+ ignitionConfig := & ignitionapi.Config {}
718
+ Expect (json .Unmarshal (ignitionTokenSecret .Data ["value" ], ignitionConfig )).To (Succeed ())
719
+ expectedPrefix := "Bearer "
720
+ ignitionToken := (* ignitionConfig .Ignition .Config .Merge [0 ].HTTPHeaders [0 ].Value )[len (expectedPrefix ):]
721
+
722
+ agentSecret := & corev1.Secret {}
723
+ Expect (c .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("agent-%s" , * ignitionTokenSecretName ), Namespace : testNamespace }, agentSecret )).To (Succeed ())
724
+ Expect (agentSecret .Data ).NotTo (BeEmpty ())
725
+ Expect (agentSecret .Data ).To (HaveKey ("ignition-token" ))
726
+ Expect (string (agentSecret .Data ["ignition-token" ])).To (Equal (ignitionToken ))
727
+
728
+ ignitionConfig .Ignition .Config .Merge [0 ].HTTPHeaders [0 ].Value = swag .String ("Bearer new-token" )
729
+ updatedIgnitionConfig , err := json .Marshal (ignitionConfig )
730
+ Expect (err ).To (BeNil ())
731
+ ignitionTokenSecret .Data ["value" ] = updatedIgnitionConfig
732
+ Expect (c .Update (ctx , ignitionTokenSecret )).To (Succeed ())
733
+
734
+ result , err = amr .Reconcile (ctx , newAgentMachineRequest (agentMachine ))
735
+ Expect (err ).To (BeNil ())
736
+ Expect (result ).To (Equal (ctrl.Result {}))
737
+
738
+ Expect (c .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("agent-%s" , * ignitionTokenSecretName ), Namespace : testNamespace }, agentSecret )).To (Succeed ())
739
+ Expect (agentSecret .Data ).NotTo (BeEmpty ())
740
+ Expect (agentSecret .Data ).To (HaveKey ("ignition-token" ))
741
+ Expect (string (agentSecret .Data ["ignition-token" ])).To (Equal ("new-token" ))
742
+
743
+ })
744
+ It ("creates the ignition token secret even when the AgentMachine is already ready" , func () {
745
+ agentMachine .Status .AgentRef = & capiproviderv1.AgentReference {Namespace : agent .Namespace , Name : agent .Name }
746
+ agentMachine .Status .Ready = true
747
+ Expect (c .Update (ctx , agentMachine )).To (Succeed ())
748
+
749
+ ignitionTokenSecretName := machine .Spec .Bootstrap .DataSecretName
750
+ agentSecret := & corev1.Secret {}
751
+ err := c .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("agent-%s" , * ignitionTokenSecretName ), Namespace : testNamespace }, agentSecret )
752
+ Expect (err ).To (HaveOccurred ())
753
+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
754
+
755
+ result , err := amr .Reconcile (ctx , newAgentMachineRequest (agentMachine ))
756
+ Expect (err ).To (BeNil ())
757
+ Expect (result ).To (Equal (ctrl.Result {}))
758
+
759
+ ignitionTokenSecret := & corev1.Secret {}
760
+ Expect (c .Get (ctx , types.NamespacedName {Name : * ignitionTokenSecretName , Namespace : testNamespace }, ignitionTokenSecret )).To (Succeed ())
761
+ ignitionConfig := & ignitionapi.Config {}
762
+ Expect (json .Unmarshal (ignitionTokenSecret .Data ["value" ], ignitionConfig )).To (Succeed ())
763
+ expectedPrefix := "Bearer "
764
+ ignitionToken := (* ignitionConfig .Ignition .Config .Merge [0 ].HTTPHeaders [0 ].Value )[len (expectedPrefix ):]
765
+
766
+ Expect (c .Get (ctx , types.NamespacedName {Name : fmt .Sprintf ("agent-%s" , * ignitionTokenSecretName ), Namespace : testNamespace }, agentSecret )).To (Succeed ())
767
+ Expect (agentSecret .Data ).NotTo (BeEmpty ())
768
+ Expect (agentSecret .Data ).To (HaveKey ("ignition-token" ))
769
+ Expect (string (agentSecret .Data ["ignition-token" ])).To (Equal (ignitionToken ))
770
+
771
+ })
772
+ })
670
773
})
671
774
672
775
var _ = Describe ("mapMachineToAgentMachine" , func () {
0 commit comments