From a9d92bcb252c5f62a5bb5ea77dd394fe254c7122 Mon Sep 17 00:00:00 2001 From: qingwave Date: Fri, 13 Jan 2023 08:27:30 +0000 Subject: [PATCH] remove unuse code in pkg/kubelet/util Signed-off-by: qingwave --- pkg/kubelet/util/format/pod.go | 14 ------ pkg/kubelet/util/format/pod_test.go | 46 +------------------ pkg/kubelet/util/format/resources.go | 36 --------------- pkg/kubelet/util/format/resources_test.go | 35 -------------- pkg/kubelet/util/ioutils/ioutils.go | 18 -------- pkg/kubelet/util/sliceutils/sliceutils.go | 13 +----- .../util/sliceutils/sliceutils_test.go | 28 +---------- 7 files changed, 5 insertions(+), 185 deletions(-) delete mode 100644 pkg/kubelet/util/format/resources.go delete mode 100644 pkg/kubelet/util/format/resources_test.go diff --git a/pkg/kubelet/util/format/pod.go b/pkg/kubelet/util/format/pod.go index 734a4ffd959..65fc8cf54a5 100644 --- a/pkg/kubelet/util/format/pod.go +++ b/pkg/kubelet/util/format/pod.go @@ -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 "" - } - var deletionTimestamp string - if pod.DeletionTimestamp != nil { - deletionTimestamp = ":DeletionTimestamp=" + pod.DeletionTimestamp.UTC().Format(time.RFC3339) - } - return Pod(pod) + deletionTimestamp -} diff --git a/pkg/kubelet/util/format/pod_test.go b/pkg/kubelet/util/format/pod_test.go index 21e2a0d0622..849bbb4c7f4 100644 --- a/pkg/kubelet/util/format/pod_test.go +++ b/pkg/kubelet/util/format/pod_test.go @@ -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, ""}, - } - - 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) - } -} diff --git a/pkg/kubelet/util/format/resources.go b/pkg/kubelet/util/format/resources.go deleted file mode 100644 index 2a64c5b724f..00000000000 --- a/pkg/kubelet/util/format/resources.go +++ /dev/null @@ -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, ",") -} diff --git a/pkg/kubelet/util/format/resources_test.go b/pkg/kubelet/util/format/resources_test.go deleted file mode 100644 index 7507daeb047..00000000000 --- a/pkg/kubelet/util/format/resources_test.go +++ /dev/null @@ -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) - } -} diff --git a/pkg/kubelet/util/ioutils/ioutils.go b/pkg/kubelet/util/ioutils/ioutils.go index 1b2b5a6d5dd..21760543085 100644 --- a/pkg/kubelet/util/ioutils/ioutils.go +++ b/pkg/kubelet/util/ioutils/ioutils.go @@ -18,24 +18,6 @@ package ioutils import "io" -// writeCloserWrapper represents a WriteCloser whose closer operation is noop. -type writeCloserWrapper struct { - Writer io.Writer -} - -func (w *writeCloserWrapper) Write(buf []byte) (int, error) { - return w.Writer.Write(buf) -} - -func (w *writeCloserWrapper) Close() error { - return nil -} - -// WriteCloserWrapper returns a writeCloserWrapper. -func WriteCloserWrapper(w io.Writer) io.WriteCloser { - return &writeCloserWrapper{w} -} - // LimitWriter is a copy of the standard library ioutils.LimitReader, // applied to the writer interface. // LimitWriter returns a Writer that writes to w diff --git a/pkg/kubelet/util/sliceutils/sliceutils.go b/pkg/kubelet/util/sliceutils/sliceutils.go index 88098f073e2..ab341a42a78 100644 --- a/pkg/kubelet/util/sliceutils/sliceutils.go +++ b/pkg/kubelet/util/sliceutils/sliceutils.go @@ -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 diff --git a/pkg/kubelet/util/sliceutils/sliceutils_test.go b/pkg/kubelet/util/sliceutils/sliceutils_test.go index 56c2a2073d0..745474e46cc 100644 --- a/pkg/kubelet/util/sliceutils/sliceutils_test.go +++ b/pkg/kubelet/util/sliceutils/sliceutils_test.go @@ -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{ {