...

Source file src/k8s.io/kubernetes/pkg/kubelet/kubelet_resources_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet

     1  /*
     2  Copyright 2016 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 kubelet
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"k8s.io/api/core/v1"
    25  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    26  	"k8s.io/apimachinery/pkg/api/resource"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  )
    29  
    30  func TestPodResourceLimitsDefaulting(t *testing.T) {
    31  	tk := newTestKubelet(t, true)
    32  	defer tk.Cleanup()
    33  	tk.kubelet.nodeLister = &testNodeLister{
    34  		nodes: []*v1.Node{
    35  			{
    36  				ObjectMeta: metav1.ObjectMeta{
    37  					Name: string(tk.kubelet.nodeName),
    38  				},
    39  				Status: v1.NodeStatus{
    40  					Allocatable: v1.ResourceList{
    41  						v1.ResourceCPU:    resource.MustParse("6"),
    42  						v1.ResourceMemory: resource.MustParse("4Gi"),
    43  					},
    44  				},
    45  			},
    46  		},
    47  	}
    48  	cases := []struct {
    49  		pod      *v1.Pod
    50  		expected *v1.Pod
    51  	}{
    52  		{
    53  			pod:      getPod("0", "0"),
    54  			expected: getPod("6", "4Gi"),
    55  		},
    56  		{
    57  			pod:      getPod("1", "0"),
    58  			expected: getPod("1", "4Gi"),
    59  		},
    60  		{
    61  			pod:      getPod("", ""),
    62  			expected: getPod("6", "4Gi"),
    63  		},
    64  		{
    65  			pod:      getPod("0", "1Mi"),
    66  			expected: getPod("6", "1Mi"),
    67  		},
    68  	}
    69  	as := assert.New(t)
    70  	for idx, tc := range cases {
    71  		actual, _, err := tk.kubelet.defaultPodLimitsForDownwardAPI(tc.pod, nil)
    72  		as.Nil(err, "failed to default pod limits: %v", err)
    73  		if !apiequality.Semantic.DeepEqual(tc.expected, actual) {
    74  			as.Fail("test case [%d] failed.  Expected: %+v, Got: %+v", idx, tc.expected, actual)
    75  		}
    76  	}
    77  }
    78  
    79  func getPod(cpuLimit, memoryLimit string) *v1.Pod {
    80  	resources := v1.ResourceRequirements{}
    81  	if cpuLimit != "" || memoryLimit != "" {
    82  		resources.Limits = make(v1.ResourceList)
    83  	}
    84  	if cpuLimit != "" {
    85  		resources.Limits[v1.ResourceCPU] = resource.MustParse(cpuLimit)
    86  	}
    87  	if memoryLimit != "" {
    88  		resources.Limits[v1.ResourceMemory] = resource.MustParse(memoryLimit)
    89  	}
    90  	return &v1.Pod{
    91  		Spec: v1.PodSpec{
    92  			Containers: []v1.Container{
    93  				{
    94  					Name:      "foo",
    95  					Resources: resources,
    96  				},
    97  			},
    98  		},
    99  	}
   100  }
   101  

View as plain text