@@ -31,6 +31,7 @@ import (
3131 "github.com/openshift/machine-config-operator/pkg/controller/build/imagebuilder"
3232 "github.com/openshift/machine-config-operator/pkg/controller/build/utils"
3333 ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
34+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
3435 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3536 "k8s.io/apimachinery/pkg/runtime"
3637 "k8s.io/apimachinery/pkg/util/wait"
@@ -904,52 +905,79 @@ func waitForBuildToStart(t *testing.T, cs *framework.ClientSet, build *mcfgv1.Ma
904905 return mosb
905906}
906907
908+ // Waits for a MachineOSBuild with a specific UID to be deleted.
907909func waitForMOSBToBeDeleted (t * testing.T , cs * framework.ClientSet , mosb * mcfgv1.MachineOSBuild ) {
908910 t .Helper ()
909911
910912 ctx , cancel := context .WithTimeout (context .Background (), time .Minute * 5 )
911913 defer cancel ()
912914
913915 start := time .Now ()
914- kubeassert := helpers .AssertClientSet (t , cs ).WithContext (ctx )
915916
916- // Get the MOSB from the API to get the UID
917- mosb , err := cs .MachineconfigurationV1Interface .MachineOSBuilds ().Get (context .Background (), mosb .Name , metav1.GetOptions {})
918- require .NoError (t , err )
917+ // If the given MachineOSBuild does not have a UID, e.g., from the
918+ // NewMachineOSBuildFromAPIOrDie() helper, then we query the API server to
919+ // find it.
920+ if mosb .UID == "" {
921+ t .Logf ("No UID provided for MachineOSBuild %s, querying API for UID" , mosb .Name )
922+ // Get the MOSB from the API to get the UID
923+ apiMosb , err := cs .MachineconfigurationV1Interface .MachineOSBuilds ().Get (context .Background (), mosb .Name , metav1.GetOptions {})
924+ require .NoError (t , err )
925+
926+ if k8serrors .IsNotFound (err ) {
927+ t .Logf ("MachineOSBuild %s is not found, must have already been deleted" , mosb .Name )
928+ return
929+ }
930+
931+ require .NoError (t , err )
932+
933+ mosb = apiMosb
934+ }
935+
936+ mosbName := mosb .Name
919937 mosbUID := mosb .UID
920- t .Logf ("Waiting for MachineOSBuild with UID %s to be deleted" , mosbUID )
921938
922- mosbs , err := cs .MachineconfigurationV1Interface .MachineOSBuilds ().List (ctx , metav1.ListOptions {})
923- require .NoError (t , err )
939+ t .Logf ("Waiting for MachineOSBuild %s with UID %s to be deleted" , mosbName , mosbUID )
924940
925- mosbWithUIDFound := false
926- for _ , mosb := range mosbs .Items {
927- if mosb .UID == mosbUID {
928- kubeassert .Eventually ().MachineOSBuildDoesNotExist (& mosb )
929- t .Logf ("MachineOSBuild with UID %s deleted after %s" , mosbUID , time .Since (start ))
930- mosbWithUIDFound = true
931- return
941+ // Assert does not adequately handle the case where the object is deleted.
942+ // See https://issues.redhat.com/browse/OCPBUGS-63048 for details.
943+ err := wait .PollImmediate (time .Second , time .Minute * 5 , func () (bool , error ) {
944+ mosbs , err := cs .MachineconfigurationV1Interface .MachineOSBuilds ().List (ctx , metav1.ListOptions {})
945+ if err != nil {
946+ return false , err
932947 }
933- }
934948
935- if ! mosbWithUIDFound {
936- t .Logf ("MachineOSBuild with UID %s not found, must have already been deleted" , mosbUID )
937- }
949+ for _ , mosb := range mosbs .Items {
950+ // If we find a MachineOSBuild with the same name and UID, then we know
951+ // it has not been deleted yet.
952+ if mosb .Name == mosbName && mosb .UID == mosbUID {
953+ return false , nil
954+ }
955+ }
956+
957+ return true , nil
958+ })
959+
960+ t .Logf ("MachineOSBuild %s with UID %s deleted after %s" , mosb .Name , mosb .UID , time .Since (start ))
961+
962+ require .NoError (t , err , "MachineOSBuild %s with UID %s not deleted after %s" , mosb .Name , mosb .UID , time .Since (start ))
938963}
939964
940- // Waits for a MachineOSBuild to be deleted.
965+ // Waits for a MachineOSBuild to be deleted. This is different than
966+ // waitForMOSBToBeDeleted since it then asserts that all of the objects
967+ // associated with the MOSB are deleted.
941968func waitForBuildToBeDeleted (t * testing.T , cs * framework.ClientSet , build * mcfgv1.MachineOSBuild ) {
942969 t .Helper ()
943970
944971 ctx , cancel := context .WithTimeout (context .Background (), time .Minute * 5 )
945972 defer cancel ()
946973
974+ kubeassert := helpers .AssertClientSet (t , cs ).WithContext (ctx )
975+
947976 t .Logf ("Waiting for MachineOSBuild %s to be deleted" , build .Name )
948977
949978 start := time .Now ()
950- kubeassert := helpers .AssertClientSet (t , cs ).WithContext (ctx )
951- kubeassert .Eventually ().MachineOSBuildDoesNotExist (build )
952- t .Logf ("MachineOSBuild %s deleted after %s" , build .Name , time .Since (start ))
979+
980+ waitForMOSBToBeDeleted (t , cs , build )
953981
954982 assertBuildObjectsAreDeleted (t , kubeassert .Eventually (), build )
955983 t .Logf ("Build objects deleted after %s" , time .Since (start ))
0 commit comments