github.com/kubeflow/training-operator@v1.7.0/test_job/test_util/v1/util.go (about)

     1  // Copyright 2018 The Kubeflow Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v1
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	apiv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/client-go/tools/cache"
    25  
    26  	testjobv1 "github.com/kubeflow/training-operator/test_job/apis/test_job/v1"
    27  )
    28  
    29  const (
    30  	LabelGroupName   = "group-name"
    31  	LabelTestJobName = "test-job-name"
    32  )
    33  
    34  var (
    35  	// KeyFunc is the short name to DeletionHandlingMetaNamespaceKeyFunc.
    36  	// IndexerInformer uses a delta queue, therefore for deletes we have to use this
    37  	// key function but it should be just fine for non delete events.
    38  	KeyFunc       = cache.DeletionHandlingMetaNamespaceKeyFunc
    39  	TestGroupName = testjobv1.GroupName
    40  )
    41  
    42  func GenLabels(jobName string) map[string]string {
    43  	return map[string]string{
    44  		LabelGroupName:   TestGroupName,
    45  		LabelTestJobName: strings.Replace(jobName, "/", "-", -1),
    46  	}
    47  }
    48  
    49  func GenOwnerReference(testjob *testjobv1.TestJob) *metav1.OwnerReference {
    50  	boolPtr := func(b bool) *bool { return &b }
    51  	controllerRef := &metav1.OwnerReference{
    52  		APIVersion:         testjobv1.SchemeGroupVersion.String(),
    53  		Kind:               testjobv1.Kind,
    54  		Name:               testjob.Name,
    55  		UID:                testjob.UID,
    56  		BlockOwnerDeletion: boolPtr(true),
    57  		Controller:         boolPtr(true),
    58  	}
    59  
    60  	return controllerRef
    61  }
    62  
    63  func GetKey(testJob *testjobv1.TestJob, t *testing.T) string {
    64  	key, err := KeyFunc(testJob)
    65  	if err != nil {
    66  		t.Errorf("Unexpected error getting key for job %v: %v", testJob.Name, err)
    67  		return ""
    68  	}
    69  	return key
    70  }
    71  
    72  func CheckCondition(testJob *testjobv1.TestJob, condition apiv1.JobConditionType, reason string) bool {
    73  	for _, v := range testJob.Status.Conditions {
    74  		if v.Type == condition && v.Status == v1.ConditionTrue && v.Reason == reason {
    75  			return true
    76  		}
    77  	}
    78  	return false
    79  }