github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/gangway/client/common.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/util/wait"
    25  	pb "sigs.k8s.io/prow/pkg/gangway"
    26  )
    27  
    28  // Common has helper client methods that are common to all Prow API (Gangway)
    29  // clients.
    30  type Common struct {
    31  	// GRPC is the auto-generated gRPC client interface for gangway.
    32  	GRPC pb.ProwClient
    33  }
    34  
    35  // WaitForJobExecutionStatus polls until the expected job status is detected.
    36  func (c *Common) WaitForJobExecutionStatus(ctx context.Context, jobExecutionId string, pollInterval, timeout time.Duration, expectedStatus pb.JobExecutionStatus) error {
    37  	expectJobStatus := func(ctx context.Context) (bool, error) {
    38  		jobExecution, err := c.GRPC.GetJobExecution(ctx, &pb.GetJobExecutionRequest{Id: jobExecutionId})
    39  		if err != nil {
    40  			// Keep trying.
    41  			return false, nil
    42  		}
    43  
    44  		if jobExecution.JobStatus == expectedStatus {
    45  			return true, nil
    46  		}
    47  
    48  		return true, nil
    49  	}
    50  
    51  	if waitErr := wait.PollUntilContextTimeout(ctx, pollInterval, timeout, true, expectJobStatus); waitErr != nil {
    52  		return fmt.Errorf("timed out waiting for the condition: %v", waitErr)
    53  	}
    54  
    55  	return nil
    56  }