-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
80 changed files
with
20,708 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: 2 | ||
|
||
jobs: | ||
test: | ||
working_directory: /go/src/github.com/robscott/kube-capacity | ||
|
||
docker: | ||
- image: circleci/golang:1.11 | ||
|
||
steps: | ||
- checkout | ||
- run: go test -v ./pkg/... | ||
|
||
workflows: | ||
version: 2 | ||
test: | ||
jobs: | ||
- test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
// Copyright 2019 Rob Scott | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package capacity | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" | ||
) | ||
|
||
func TestBuildClusterMetricEmpty(t *testing.T) { | ||
cm := buildClusterMetric( | ||
&corev1.PodList{}, &v1beta1.PodMetricsList{}, &corev1.NodeList{}, &v1beta1.NodeMetricsList{}, | ||
) | ||
|
||
expected := clusterMetric{ | ||
cpu: &resourceMetric{ | ||
allocatable: resource.Quantity{}, | ||
request: resource.Quantity{}, | ||
limit: resource.Quantity{}, | ||
utilization: resource.Quantity{}, | ||
}, | ||
memory: &resourceMetric{ | ||
allocatable: resource.Quantity{}, | ||
request: resource.Quantity{}, | ||
limit: resource.Quantity{}, | ||
utilization: resource.Quantity{}, | ||
}, | ||
nodeMetrics: map[string]*nodeMetric{}, | ||
podMetrics: map[string]*podMetric{}, | ||
} | ||
|
||
assert.EqualValues(t, cm, expected) | ||
} | ||
|
||
func TestBuildClusterMetricFull(t *testing.T) { | ||
cm := buildClusterMetric( | ||
&corev1.PodList{ | ||
Items: []corev1.Pod{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example-pod", | ||
Namespace: "default", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: "example-node-1", | ||
Containers: []corev1.Container{ | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{ | ||
"cpu": resource.MustParse("250m"), | ||
"memory": resource.MustParse("250Mi"), | ||
}, | ||
Limits: corev1.ResourceList{ | ||
"cpu": resource.MustParse("250m"), | ||
"memory": resource.MustParse("500Mi"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{ | ||
"cpu": resource.MustParse("100m"), | ||
"memory": resource.MustParse("150Mi"), | ||
}, | ||
Limits: corev1.ResourceList{ | ||
"cpu": resource.MustParse("150m"), | ||
"memory": resource.MustParse("200Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, &v1beta1.PodMetricsList{ | ||
Items: []v1beta1.PodMetrics{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example-pod", | ||
Namespace: "default", | ||
}, | ||
Containers: []v1beta1.ContainerMetrics{ | ||
{ | ||
Usage: corev1.ResourceList{ | ||
"cpu": resource.MustParse("10m"), | ||
"memory": resource.MustParse("188Mi"), | ||
}, | ||
}, | ||
{ | ||
Usage: corev1.ResourceList{ | ||
"cpu": resource.MustParse("13m"), | ||
"memory": resource.MustParse("111Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, &corev1.NodeList{ | ||
Items: []corev1.Node{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example-node-1", | ||
}, | ||
Status: corev1.NodeStatus{ | ||
Allocatable: corev1.ResourceList{ | ||
"cpu": resource.MustParse("1000m"), | ||
"memory": resource.MustParse("4000Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, &v1beta1.NodeMetricsList{ | ||
Items: []v1beta1.NodeMetrics{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example-node-1", | ||
}, | ||
Usage: corev1.ResourceList{ | ||
"cpu": resource.MustParse("28m"), | ||
"memory": resource.MustParse("438Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
cpuExpected := &resourceMetric{ | ||
allocatable: resource.MustParse("1000m"), | ||
request: resource.MustParse("350m"), | ||
limit: resource.MustParse("400m"), | ||
utilization: resource.MustParse("28m"), | ||
} | ||
|
||
memoryExpected := &resourceMetric{ | ||
allocatable: resource.MustParse("4000Mi"), | ||
request: resource.MustParse("400Mi"), | ||
limit: resource.MustParse("700Mi"), | ||
utilization: resource.MustParse("438Mi"), | ||
} | ||
|
||
assert.Len(t, cm.podMetrics, 1) | ||
|
||
assert.NotNil(t, cm.cpu) | ||
ensureEqualResourceMetric(t, cm.cpu, cpuExpected) | ||
assert.NotNil(t, cm.memory) | ||
ensureEqualResourceMetric(t, cm.memory, memoryExpected) | ||
|
||
assert.NotNil(t, cm.nodeMetrics["example-node-1"]) | ||
assert.NotNil(t, cm.nodeMetrics["example-node-1"].cpu) | ||
ensureEqualResourceMetric(t, cm.nodeMetrics["example-node-1"].cpu, cpuExpected) | ||
assert.NotNil(t, cm.nodeMetrics["example-node-1"].memory) | ||
ensureEqualResourceMetric(t, cm.nodeMetrics["example-node-1"].memory, memoryExpected) | ||
|
||
// Change to pod specific util numbers | ||
cpuExpected.utilization = resource.MustParse("23m") | ||
memoryExpected.utilization = resource.MustParse("299Mi") | ||
|
||
assert.NotNil(t, cm.podMetrics["default-example-pod"]) | ||
assert.NotNil(t, cm.podMetrics["default-example-pod"].cpu) | ||
ensureEqualResourceMetric(t, cm.podMetrics["default-example-pod"].cpu, cpuExpected) | ||
assert.NotNil(t, cm.podMetrics["default-example-pod"].memory) | ||
ensureEqualResourceMetric(t, cm.podMetrics["default-example-pod"].memory, memoryExpected) | ||
} | ||
|
||
func ensureEqualResourceMetric(t *testing.T, actual *resourceMetric, expected *resourceMetric) { | ||
assert.Equal(t, actual.allocatable.MilliValue(), expected.allocatable.MilliValue()) | ||
assert.Equal(t, actual.utilization.MilliValue(), expected.utilization.MilliValue()) | ||
assert.Equal(t, actual.request.MilliValue(), expected.request.MilliValue()) | ||
assert.Equal(t, actual.limit.MilliValue(), expected.limit.MilliValue()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.