mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #115053 from qingwave/remove-unuse-code
Remove unuse code in pkg/kubelet/util
This commit is contained in:
commit
af97bb9ac5
@ -18,7 +18,6 @@ package format
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
@ -40,16 +39,3 @@ func PodDesc(podName, podNamespace string, podUID types.UID) string {
|
|||||||
// (DNS subdomain format), while allowed in the container name format.
|
// (DNS subdomain format), while allowed in the container name format.
|
||||||
return fmt.Sprintf("%s_%s(%s)", podName, podNamespace, podUID)
|
return fmt.Sprintf("%s_%s(%s)", podName, podNamespace, podUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodWithDeletionTimestamp is the same as Pod. In addition, it prints the
|
|
||||||
// deletion timestamp of the pod if it's not nil.
|
|
||||||
func PodWithDeletionTimestamp(pod *v1.Pod) string {
|
|
||||||
if pod == nil {
|
|
||||||
return "<nil>"
|
|
||||||
}
|
|
||||||
var deletionTimestamp string
|
|
||||||
if pod.DeletionTimestamp != nil {
|
|
||||||
deletionTimestamp = ":DeletionTimestamp=" + pod.DeletionTimestamp.UTC().Format(time.RFC3339)
|
|
||||||
}
|
|
||||||
return Pod(pod) + deletionTimestamp
|
|
||||||
}
|
|
||||||
|
@ -18,7 +18,6 @@ package format
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
@ -37,17 +36,6 @@ func fakeCreatePod(name, namespace string, uid types.UID) *v1.Pod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeCreatePodWithDeletionTimestamp(name, namespace string, uid types.UID, deletionTimestamp *metav1.Time) *v1.Pod {
|
|
||||||
return &v1.Pod{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: name,
|
|
||||||
Namespace: namespace,
|
|
||||||
UID: uid,
|
|
||||||
DeletionTimestamp: deletionTimestamp,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPod(t *testing.T) {
|
func TestPod(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
caseName string
|
caseName string
|
||||||
@ -69,7 +57,7 @@ func TestPodAndPodDesc(t *testing.T) {
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
caseName string
|
caseName string
|
||||||
podName string
|
podName string
|
||||||
podNamesapce string
|
podNamespace string
|
||||||
podUID types.UID
|
podUID types.UID
|
||||||
expectedValue string
|
expectedValue string
|
||||||
}{
|
}{
|
||||||
@ -78,37 +66,7 @@ func TestPodAndPodDesc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
realPodDesc := PodDesc(testCase.podName, testCase.podNamesapce, testCase.podUID)
|
realPodDesc := PodDesc(testCase.podName, testCase.podNamespace, testCase.podUID)
|
||||||
assert.Equalf(t, testCase.expectedValue, realPodDesc, "Failed to test: %s", testCase.caseName)
|
assert.Equalf(t, testCase.expectedValue, realPodDesc, "Failed to test: %s", testCase.caseName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPodWithDeletionTimestamp(t *testing.T) {
|
|
||||||
normalDeletionTime := metav1.Date(2017, time.September, 26, 14, 37, 50, 00, time.UTC)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
caseName string
|
|
||||||
isPodNil bool
|
|
||||||
isdeletionTimestampNil bool
|
|
||||||
deletionTimestamp metav1.Time
|
|
||||||
expectedValue string
|
|
||||||
}{
|
|
||||||
{"timestamp_is_nil_case", false, true, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
|
|
||||||
{"timestamp_is_normal_case", false, false, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
|
|
||||||
{"pod_is_nil_case", true, false, normalDeletionTime, "<nil>"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
|
||||||
fakePod := fakeCreatePodWithDeletionTimestamp("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", &testCase.deletionTimestamp)
|
|
||||||
|
|
||||||
if testCase.isdeletionTimestampNil {
|
|
||||||
fakePod.SetDeletionTimestamp(nil)
|
|
||||||
}
|
|
||||||
if testCase.isPodNil {
|
|
||||||
fakePod = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
realPodWithDeletionTimestamp := PodWithDeletionTimestamp(fakePod)
|
|
||||||
assert.Equalf(t, testCase.expectedValue, realPodWithDeletionTimestamp, "Failed to test: %s", testCase.caseName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2016 The Kubernetes Authors.
|
|
||||||
|
|
||||||
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 format
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ResourceList returns a string representation of a resource list in a human readable format.
|
|
||||||
func ResourceList(resources v1.ResourceList) string {
|
|
||||||
resourceStrings := make([]string, 0, len(resources))
|
|
||||||
for key, value := range resources {
|
|
||||||
resourceStrings = append(resourceStrings, fmt.Sprintf("%v=%v", key, value.String()))
|
|
||||||
}
|
|
||||||
// sort the results for consistent log output
|
|
||||||
sort.Strings(resourceStrings)
|
|
||||||
return strings.Join(resourceStrings, ",")
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2016 The Kubernetes Authors.
|
|
||||||
|
|
||||||
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 format
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestResourceList(t *testing.T) {
|
|
||||||
resourceList := v1.ResourceList{}
|
|
||||||
resourceList[v1.ResourceCPU] = resource.MustParse("100m")
|
|
||||||
resourceList[v1.ResourceMemory] = resource.MustParse("5Gi")
|
|
||||||
actual := ResourceList(resourceList)
|
|
||||||
expected := "cpu=100m,memory=5Gi"
|
|
||||||
if actual != expected {
|
|
||||||
t.Errorf("Unexpected result, actual: %v, expected: %v", actual, expected)
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,21 +17,10 @@ limitations under the License.
|
|||||||
package sliceutils
|
package sliceutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StringInSlice returns true if s is in list
|
|
||||||
func StringInSlice(s string, list []string) bool {
|
|
||||||
for _, v := range list {
|
|
||||||
if v == s {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// PodsByCreationTime makes an array of pods sortable by their creation
|
// PodsByCreationTime makes an array of pods sortable by their creation
|
||||||
// timestamps in ascending order.
|
// timestamps in ascending order.
|
||||||
type PodsByCreationTime []*v1.Pod
|
type PodsByCreationTime []*v1.Pod
|
||||||
|
@ -18,37 +18,13 @@ package sliceutils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStringInSlice(t *testing.T) {
|
|
||||||
fooTests := []struct {
|
|
||||||
s string
|
|
||||||
list []string
|
|
||||||
er bool
|
|
||||||
}{
|
|
||||||
{"first", []string{"first", "second"}, true},
|
|
||||||
{"FIRST", []string{"first", "second"}, false},
|
|
||||||
{"third", []string{"first", "second"}, false},
|
|
||||||
{"first", nil, false},
|
|
||||||
|
|
||||||
{"", []string{"first", "second"}, false},
|
|
||||||
{"", []string{"first", "second", ""}, true},
|
|
||||||
{"", nil, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, fooTest := range fooTests {
|
|
||||||
r := StringInSlice(fooTest.s, fooTest.list)
|
|
||||||
if r != fooTest.er {
|
|
||||||
t.Errorf("returned %t but expected %t for s=%s & list=%s", r, fooTest.er, fooTest.s, fooTest.list)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildPodsByCreationTime() PodsByCreationTime {
|
func buildPodsByCreationTime() PodsByCreationTime {
|
||||||
return []*v1.Pod{
|
return []*v1.Pod{
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user