github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/helper/job.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package helper
    20  
    21  import (
    22  	"context"
    23  	"strings"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/client-go/kubernetes"
    27  )
    28  
    29  // Since operator generates a unique ID to postfix the job's name with, this function
    30  // obtains the job's unique identifier
    31  func GetJobID(kclient *kubernetes.Clientset, namespace, name string) (string, error) {
    32  	jobs, err := kclient.BatchV1().Jobs(namespace).List(context.TODO(), metav1.ListOptions{})
    33  	if err != nil {
    34  		return "", err
    35  	}
    36  
    37  	for _, j := range jobs.Items {
    38  		if strings.Contains(j.Name, name) {
    39  			return j.Name, nil
    40  		}
    41  	}
    42  
    43  	return "", err
    44  }