@@ -1641,6 +1641,69 @@ var _ = Describe("With a running MachineSync Reconciler", func() {
16411641
16421642 })
16431643
1644+ FContext ("Validate creation of CAPI machine " , func () {
1645+ var vapName = "openshift-validate-capi-machine-creation"
1646+
1647+ BeforeEach (func () {
1648+ By ("Waiting for VAP to be ready" )
1649+ machineVap = & admissionregistrationv1.ValidatingAdmissionPolicy {}
1650+ Eventually (k8sClient .Get (ctx , client.ObjectKey {Name : vapName }, machineVap ), timeout ).Should (Succeed ())
1651+ resourceRules := machineVap .Spec .MatchConstraints .ResourceRules
1652+ Expect (resourceRules ).To (HaveLen (1 ))
1653+ resourceRules [0 ].Operations = append (resourceRules [0 ].Operations , admissionregistrationv1 .Update )
1654+ Eventually (k .Update (machineVap , func () {
1655+ admissiontestutils .AddSentinelValidation (machineVap )
1656+ // Updating the VAP so that it functions on "UPDATE" as well as "CREATE" only in this test suite to make it easier to test the functionality
1657+ machineVap .Spec .MatchConstraints .ResourceRules = resourceRules
1658+
1659+ })).Should (Succeed ())
1660+
1661+ Eventually (k .Object (machineVap ), timeout ).Should (
1662+ HaveField ("Status.ObservedGeneration" , BeNumerically (">=" , 2 )),
1663+ )
1664+
1665+ By ("Updating the VAP binding" )
1666+ policyBinding = & admissionregistrationv1.ValidatingAdmissionPolicyBinding {}
1667+ Eventually (k8sClient .Get (ctx , client.ObjectKey {
1668+ Name : vapName }, policyBinding ), timeout ).Should (Succeed ())
1669+
1670+ Eventually (k .Update (policyBinding , func () {
1671+ admissiontestutils .UpdateVAPBindingNamespaces (policyBinding , capiNamespace .GetName (), mapiNamespace .GetName ())
1672+ }), timeout ).Should (Succeed ())
1673+
1674+ // Wait until the binding shows the patched values
1675+ Eventually (k .Object (policyBinding ), timeout ).Should (
1676+ SatisfyAll (
1677+ HaveField ("Spec.MatchResources.NamespaceSelector.MatchLabels" ,
1678+ HaveKeyWithValue ("kubernetes.io/metadata.name" ,
1679+ mapiNamespace .GetName ())),
1680+ ),
1681+ )
1682+
1683+ By ("Creating a throwaway MAPI machine" )
1684+ sentinelMachine := mapiMachineBuilder .WithName ("sentinel-machine" ).WithAuthoritativeAPI (mapiv1beta1 .MachineAuthorityClusterAPI ).Build ()
1685+ Eventually (k8sClient .Create (ctx , sentinelMachine ), timeout ).Should (Succeed ())
1686+
1687+ capiSentinelMachine := clusterv1resourcebuilder .Machine ().WithName ("sentinel-machine" ).WithNamespace (capiNamespace .Name ).Build ()
1688+ Eventually (k8sClient .Create (ctx , capiSentinelMachine )).Should (Succeed ())
1689+
1690+ Eventually (k .Get (capiSentinelMachine )).Should (Succeed ())
1691+
1692+ admissiontestutils .VerifySentinelValidation (k , sentinelMachine , timeout )
1693+ })
1694+
1695+ // The Authoritative API defaults to MachineAPI so we can't test if it's unset.
1696+ It ("Foo" , func () {
1697+ By ("Create the Capi Machine" )
1698+ newCapiMachine := clusterv1resourcebuilder .Machine ().WithName ("validation-machine" ).WithNamespace (capiNamespace .Name ).Build ()
1699+ Eventually (k8sClient .Create (ctx , newCapiMachine )).Should (Succeed ())
1700+
1701+ By ("Create the Mapi Machine" )
1702+ newMapiMachine := mapiMachineBuilder .WithName ("validation-machine" ).WithAuthoritativeAPI (mapiv1beta1 .MachineAuthorityMachineAPI ).Build ()
1703+ Eventually (k8sClient .Create (ctx , newMapiMachine ), timeout ).Should (MatchError (ContainSubstring ("with authoritativeAPI=MachineAPI because a Cluster API Machine with the same name already exists." )))
1704+ })
1705+ })
1706+
16441707 Context ("Prevent updates to MAPI machine if migrating would be unpredictable" , func () {
16451708 BeforeEach (func () {
16461709 By ("Waiting for VAP to be ready" )
0 commit comments