@@ -11,6 +11,7 @@ import (
1111
1212 batchv1 "k8s.io/api/batch/v1"
1313 corev1 "k8s.io/api/core/v1"
14+ apierrors "k8s.io/apimachinery/pkg/api/errors"
1415 "k8s.io/apimachinery/pkg/api/meta"
1516 "k8s.io/apimachinery/pkg/api/resource"
1617 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -76,12 +77,43 @@ func verifyCatalogEndpoint(ctx SpecContext, catalog, endpoint, query string) {
7677 strings .ReplaceAll (endpoint , "?" , "" ),
7778 strings .ReplaceAll (catalog , "-" , "" ))
7879
79- job := buildCurlJob (jobNamePrefix , "default" , serviceURL )
80+ // create service account object
81+ serviceAccount := & corev1.ServiceAccount {
82+ ObjectMeta : metav1.ObjectMeta {
83+ Name : jobNamePrefix ,
84+ Namespace : "default" ,
85+ },
86+ }
87+
88+ serviceAccount .SetName (jobNamePrefix )
89+ serviceAccount .SetNamespace ("default" )
90+
91+ err = k8sClient .Create (ctx , serviceAccount )
92+ Expect (err ).NotTo (HaveOccurred (), "failed to create Service Account" )
93+
94+ if err != nil && ! apierrors .IsAlreadyExists (err ) {
95+ Fail (fmt .Sprintf ("Failed to ensure ServiceAccount %s: %v" , jobNamePrefix , err ))
96+ }
97+
98+ job := buildCurlJob (jobNamePrefix , "default" , serviceURL , serviceAccount )
99+
80100 err = k8sClient .Create (ctx , job )
81101 Expect (err ).NotTo (HaveOccurred (), "failed to create Job" )
82102
103+ DeferCleanup (func (ctx SpecContext ) {
104+ _ = k8sClient .Delete (ctx , serviceAccount )
105+ })
106+
83107 DeferCleanup (func (ctx SpecContext ) {
84108 _ = k8sClient .Delete (ctx , job )
109+ // We poll for deletion success so that the cleanup succeeds only when
110+ // the job is deleted. Then, we can move onto deleting the service
111+ // account without issue.
112+ Eventually (func (g Gomega ) {
113+ recheck := & batchv1.Job {}
114+ err := k8sClient .Get (ctx , client .ObjectKeyFromObject (job ), recheck )
115+ g .Expect (apierrors .IsNotFound (err )).To (BeTrue (), fmt .Sprintf ("Job %v should be deleted" , job .Name ))
116+ }).WithTimeout (helpers .DefaultTimeout ).WithPolling (helpers .DefaultPolling ).Should (Succeed ())
85117 })
86118
87119 By ("Waiting for Job to succeed" )
@@ -203,7 +235,7 @@ var _ = Describe("[sig-olmv1][OCPFeatureGate:NewOLM][Skipped:Disconnected] OLMv1
203235 })
204236})
205237
206- func buildCurlJob (prefix , namespace , url string ) * batchv1.Job {
238+ func buildCurlJob (prefix , namespace , url string , serviceAccount * corev1. ServiceAccount ) * batchv1.Job {
207239 backoff := int32 (1 )
208240 // This means the k8s garbage collector will automatically delete the job 5 minutes
209241 // after it has completed or failed.
@@ -232,7 +264,8 @@ func buildCurlJob(prefix, namespace, url string) *batchv1.Job {
232264 BackoffLimit : & backoff ,
233265 Template : corev1.PodTemplateSpec {
234266 Spec : corev1.PodSpec {
235- RestartPolicy : corev1 .RestartPolicyNever ,
267+ ServiceAccountName : serviceAccount .Name ,
268+ RestartPolicy : corev1 .RestartPolicyNever ,
236269 Containers : []corev1.Container {{
237270 Name : "api-tester" ,
238271 Image : "registry.redhat.io/rhel8/httpd-24:latest" ,
0 commit comments