Merge pull request #115053 from qingwave/remove-unuse-code

Remove unuse code in pkg/kubelet/util
This commit is contained in:
Kubernetes Prow Robot 2023-03-14 09:39:10 -07:00 committed by GitHub
commit af97bb9ac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 5 additions and 167 deletions

View File

@ -18,7 +18,6 @@ package format
import (
"fmt"
"time"
v1 "k8s.io/api/core/v1"
"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.
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
}

View File

@ -18,7 +18,6 @@ package format
import (
"testing"
"time"
"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) {
testCases := []struct {
caseName string
@ -69,7 +57,7 @@ func TestPodAndPodDesc(t *testing.T) {
testCases := []struct {
caseName string
podName string
podNamesapce string
podNamespace string
podUID types.UID
expectedValue string
}{
@ -78,37 +66,7 @@ func TestPodAndPodDesc(t *testing.T) {
}
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)
}
}
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)
}
}

View File

@ -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, ",")
}

View File

@ -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)
}
}

View File

@ -17,21 +17,10 @@ limitations under the License.
package sliceutils
import (
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
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
// timestamps in ascending order.
type PodsByCreationTime []*v1.Pod

View File

@ -18,37 +18,13 @@ package sliceutils
import (
"testing"
"time"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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 {
return []*v1.Pod{
{