github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/tensorflow/testutil/pod.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 testutil
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	. "github.com/onsi/gomega"
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    29  	"github.com/kubeflow/training-operator/pkg/util/testutil"
    30  )
    31  
    32  const (
    33  	DummyContainerName  = "dummy"
    34  	DummyContainerImage = "dummy/dummy:latest"
    35  )
    36  
    37  func NewBasePod(name string, job metav1.Object, refs []metav1.OwnerReference) *corev1.Pod {
    38  
    39  	return &corev1.Pod{
    40  		ObjectMeta: metav1.ObjectMeta{
    41  			Name:            name,
    42  			Labels:          map[string]string{},
    43  			Namespace:       job.GetNamespace(),
    44  			OwnerReferences: refs,
    45  		},
    46  		Spec: corev1.PodSpec{
    47  			Containers: []corev1.Container{
    48  				{
    49  					Name:  DummyContainerName,
    50  					Image: DummyContainerImage,
    51  				},
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func NewPod(job metav1.Object, typ kubeflowv1.ReplicaType, index int, refs []metav1.OwnerReference) *corev1.Pod {
    58  	pod := NewBasePod(fmt.Sprintf("%s-%s-%d", job.GetName(), strings.ToLower(string(typ)), index), job, refs)
    59  	pod.Labels[kubeflowv1.ReplicaTypeLabel] = strings.ToLower(string(typ))
    60  	pod.Labels[kubeflowv1.ReplicaIndexLabel] = fmt.Sprintf("%d", index)
    61  	return pod
    62  }
    63  
    64  // NewPodList create count pods with the given phase for the given tfJob
    65  func NewPodList(count int32, status corev1.PodPhase, job metav1.Object, typ kubeflowv1.ReplicaType, start int32, refs []metav1.OwnerReference) []*corev1.Pod {
    66  	pods := []*corev1.Pod{}
    67  	for i := int32(0); i < count; i++ {
    68  		newPod := NewPod(job, typ, int(start+i), refs)
    69  		newPod.Status = corev1.PodStatus{Phase: status}
    70  		pods = append(pods, newPod)
    71  	}
    72  	return pods
    73  }
    74  
    75  func SetPodsStatuses(client client.Client, job metav1.Object, typ kubeflowv1.ReplicaType,
    76  	pendingPods, activePods, succeededPods, failedPods int32, restartCounts []int32,
    77  	refs []metav1.OwnerReference, basicLabels map[string]string) {
    78  	var index int32
    79  	taskMap := map[corev1.PodPhase]int32{
    80  		corev1.PodFailed:    failedPods,
    81  		corev1.PodPending:   pendingPods,
    82  		corev1.PodRunning:   activePods,
    83  		corev1.PodSucceeded: succeededPods,
    84  	}
    85  	ctx := context.Background()
    86  
    87  	for podPhase, desiredCount := range taskMap {
    88  		for i, pod := range NewPodList(desiredCount, podPhase, job, typ, index, refs) {
    89  			for k, v := range basicLabels {
    90  				pod.Labels[k] = v
    91  			}
    92  			_ = client.Create(ctx, pod)
    93  			launcherKey := types.NamespacedName{
    94  				Namespace: metav1.NamespaceDefault,
    95  				Name:      pod.GetName(),
    96  			}
    97  			Eventually(func() error {
    98  				po := &corev1.Pod{}
    99  				if err := client.Get(ctx, launcherKey, po); err != nil {
   100  					return err
   101  				}
   102  				po.Status.Phase = podPhase
   103  				if podPhase == corev1.PodRunning && restartCounts != nil {
   104  					po.Status.ContainerStatuses = []corev1.ContainerStatus{{RestartCount: restartCounts[i]}}
   105  				}
   106  				return client.Status().Update(ctx, po)
   107  			}, testutil.Timeout, testutil.Interval).Should(BeNil())
   108  		}
   109  		index += desiredCount
   110  	}
   111  }