volcano.sh/volcano@v1.9.0/pkg/scheduler/api/test_utils.go (about)

     1  /*
     2  Copyright 2018 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 api
    18  
    19  import (
    20  	"fmt"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/api/resource"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  )
    27  
    28  func buildNode(name string, alloc v1.ResourceList) *v1.Node {
    29  	return &v1.Node{
    30  		ObjectMeta: metav1.ObjectMeta{
    31  			Name: name,
    32  		},
    33  		Status: v1.NodeStatus{
    34  			Capacity:    alloc,
    35  			Allocatable: alloc,
    36  		},
    37  	}
    38  }
    39  
    40  func buildPod(ns, n, nn string, p v1.PodPhase, req v1.ResourceList, owner []metav1.OwnerReference, labels map[string]string) *v1.Pod {
    41  	return &v1.Pod{
    42  		ObjectMeta: metav1.ObjectMeta{
    43  			UID:             types.UID(fmt.Sprintf("%v-%v", ns, n)),
    44  			Name:            n,
    45  			Namespace:       ns,
    46  			OwnerReferences: owner,
    47  			Labels:          labels,
    48  		},
    49  		Status: v1.PodStatus{
    50  			Phase: p,
    51  		},
    52  		Spec: v1.PodSpec{
    53  			NodeName: nn,
    54  			Containers: []v1.Container{
    55  				{
    56  					Resources: v1.ResourceRequirements{
    57  						Requests: req,
    58  					},
    59  				},
    60  			},
    61  		},
    62  	}
    63  }
    64  
    65  func buildResource(cpu string, memory string, scalarResources map[string]string, maxTaskNum int) *Resource {
    66  	resourceList := v1.ResourceList{
    67  		v1.ResourceCPU:    resource.MustParse(cpu),
    68  		v1.ResourceMemory: resource.MustParse(memory),
    69  	}
    70  	for key, value := range scalarResources {
    71  		resourceList[v1.ResourceName(key)] = resource.MustParse(value)
    72  	}
    73  	resource := NewResource(resourceList)
    74  	if maxTaskNum != -1 {
    75  		resource.MaxTaskNum = maxTaskNum
    76  	}
    77  	return resource
    78  }
    79  
    80  func buildOwnerReference(owner string) metav1.OwnerReference {
    81  	controller := true
    82  	return metav1.OwnerReference{
    83  		Controller: &controller,
    84  		UID:        types.UID(owner),
    85  	}
    86  }
    87  
    88  type ScalarResource struct {
    89  	Name  string
    90  	Value string
    91  }
    92  
    93  // BuildResourceList builts resource list object
    94  func BuildResourceList(cpu string, memory string, scalarResources ...ScalarResource) v1.ResourceList {
    95  	resourceList := v1.ResourceList{
    96  		v1.ResourceCPU:    resource.MustParse(cpu),
    97  		v1.ResourceMemory: resource.MustParse(memory),
    98  	}
    99  	for _, scalar := range scalarResources {
   100  		resourceList[v1.ResourceName(scalar.Name)] = resource.MustParse(scalar.Value)
   101  	}
   102  
   103  	return resourceList
   104  }
   105  
   106  // BuildResourceListWithGPU builts resource list with GPU
   107  func BuildResourceListWithGPU(cpu string, memory string, GPU string, scalarResources ...ScalarResource) v1.ResourceList {
   108  	resourceList := v1.ResourceList{
   109  		v1.ResourceCPU:    resource.MustParse(cpu),
   110  		v1.ResourceMemory: resource.MustParse(memory),
   111  		GPUResourceName:   resource.MustParse(GPU),
   112  	}
   113  	for _, scalar := range scalarResources {
   114  		resourceList[v1.ResourceName(scalar.Name)] = resource.MustParse(scalar.Value)
   115  	}
   116  
   117  	return resourceList
   118  }