From 774ab1349bfd553c97a99b992e9f38659f541c84 Mon Sep 17 00:00:00 2001 From: inosato Date: Sat, 30 Jul 2022 22:31:16 +0900 Subject: [PATCH] Remove ioutil from kubectl Signed-off-by: inosato --- .../kubectl/pkg/cmd/annotate/annotate_test.go | 8 +- .../kubectl/pkg/cmd/apply/apply_test.go | 109 +++++++++--------- .../src/k8s.io/kubectl/pkg/cmd/auth/cani.go | 3 +- .../k8s.io/kubectl/pkg/cmd/auth/cani_test.go | 12 +- .../kubectl/pkg/cmd/auth/whoami_test.go | 4 +- .../pkg/cmd/certificates/certificates_test.go | 4 +- .../cmd/clusterinfo/clusterinfo_dump_test.go | 5 +- .../src/k8s.io/kubectl/pkg/cmd/cmd_test.go | 3 +- .../kubectl/pkg/cmd/config/config_test.go | 19 ++- .../pkg/cmd/config/current_context_test.go | 3 +- .../pkg/cmd/config/delete_cluster_test.go | 3 +- .../pkg/cmd/config/delete_context_test.go | 3 +- .../pkg/cmd/config/get_clusters_test.go | 3 +- .../pkg/cmd/config/get_contexts_test.go | 3 +- .../pkg/cmd/config/rename_context_test.go | 3 +- .../kubectl/pkg/cmd/config/set_cluster.go | 3 +- .../pkg/cmd/config/set_cluster_test.go | 3 +- .../pkg/cmd/config/set_context_test.go | 3 +- .../kubectl/pkg/cmd/config/set_credentials.go | 5 +- .../pkg/cmd/config/set_credentials_test.go | 3 +- .../k8s.io/kubectl/pkg/cmd/config/set_test.go | 3 +- .../kubectl/pkg/cmd/config/unset_test.go | 3 +- .../pkg/cmd/config/use_context_test.go | 3 +- .../kubectl/pkg/cmd/config/view_test.go | 3 +- staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go | 5 +- .../src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go | 31 +++-- .../pkg/cmd/create/create_configmap.go | 7 +- .../pkg/cmd/create/create_configmap_test.go | 7 +- .../pkg/cmd/create/create_deployment_test.go | 10 +- .../cmd/create/create_priorityclass_test.go | 4 +- .../kubectl/pkg/cmd/create/create_secret.go | 7 +- .../pkg/cmd/create/create_secret_test.go | 7 +- .../pkg/cmd/create/create_secret_tls.go | 4 +- .../pkg/cmd/create/create_token_test.go | 6 +- .../kubectl/pkg/cmd/delete/delete_test.go | 3 +- .../src/k8s.io/kubectl/pkg/cmd/diff/diff.go | 3 +- .../k8s.io/kubectl/pkg/cmd/diff/diff_test.go | 15 ++- .../kubectl/pkg/cmd/drain/drain_test.go | 6 +- .../k8s.io/kubectl/pkg/cmd/edit/edit_test.go | 17 ++- .../kubectl/pkg/cmd/edit/testdata/record.go | 16 +-- .../k8s.io/kubectl/pkg/cmd/exec/exec_test.go | 3 +- .../pkg/cmd/get/customcolumn_flags_test.go | 3 +- .../k8s.io/kubectl/pkg/cmd/get/get_test.go | 7 +- .../kubectl/pkg/cmd/label/label_test.go | 12 +- .../k8s.io/kubectl/pkg/cmd/logs/logs_test.go | 5 +- .../src/k8s.io/kubectl/pkg/cmd/patch/patch.go | 4 +- .../k8s.io/kubectl/pkg/cmd/plugin/plugin.go | 3 +- .../pkg/cmd/plugin/plugin_completion.go | 6 +- .../kubectl/pkg/cmd/plugin/plugin_test.go | 17 ++- .../k8s.io/kubectl/pkg/cmd/replace/replace.go | 3 +- .../pkg/cmd/rollout/rollout_history_test.go | 10 +- .../pkg/cmd/rollout/rollout_pause_test.go | 4 +- .../pkg/cmd/rollout/rollout_restart_test.go | 10 +- .../pkg/cmd/rollout/rollout_status_test.go | 10 +- .../k8s.io/kubectl/pkg/cmd/run/run_test.go | 8 +- .../kubectl/pkg/cmd/set/set_env_test.go | 8 +- .../kubectl/pkg/cmd/set/set_image_test.go | 6 +- .../kubectl/pkg/cmd/set/set_resources_test.go | 6 +- .../pkg/cmd/set/set_serviceaccount_test.go | 3 +- .../kubectl/pkg/cmd/taint/taint_test.go | 6 +- .../k8s.io/kubectl/pkg/cmd/testing/fake.go | 3 +- .../k8s.io/kubectl/pkg/cmd/testing/util.go | 7 +- .../kubectl/pkg/cmd/top/top_node_test.go | 22 ++-- .../kubectl/pkg/cmd/top/top_pod_test.go | 10 +- .../k8s.io/kubectl/pkg/cmd/top/top_test.go | 3 +- .../kubectl/pkg/cmd/util/editor/editor.go | 3 +- .../pkg/cmd/util/editor/editor_test.go | 3 +- .../kubectl/pkg/cmd/util/helpers_test.go | 5 +- .../k8s.io/kubectl/pkg/cmd/wait/wait_test.go | 8 +- .../kubectl/pkg/proxy/proxy_server_test.go | 12 +- .../kubectl/pkg/util/completion/completion.go | 6 +- 71 files changed, 267 insertions(+), 311 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go index c3ea155afae..b843890bb46 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/annotate/annotate_test.go @@ -18,7 +18,7 @@ package annotate import ( "bytes" - "io/ioutil" + "io" "net/http" "reflect" "strings" @@ -537,7 +537,7 @@ func TestAnnotateResourceVersion(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"test","resourceVersion":"10"}}`, ))}, nil default: @@ -547,7 +547,7 @@ func TestAnnotateResourceVersion(t *testing.T) { case "PATCH": switch req.URL.Path { case "/namespaces/test/pods/foo": - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -557,7 +557,7 @@ func TestAnnotateResourceVersion(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"test","resourceVersion":"11"}}`, ))}, nil default: diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go index d426f524c8d..255f8edf18c 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -230,7 +229,7 @@ func readBytesFromFile(t *testing.T, filename string) []byte { } defer file.Close() - data, err := ioutil.ReadAll(file) + data, err := io.ReadAll(file) if err != nil { t.Fatal(err) } @@ -343,7 +342,7 @@ func validatePatchApplication(t *testing.T, req *http.Request, patchType types.P t.Fatalf("unexpected content-type expected: %s but actual %s\n", wanted, got) } - patch, err := ioutil.ReadAll(req.Body) + patch, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -396,9 +395,9 @@ func TestRunApplyPrintsValidObjectList(t *testing.T) { switch p { case pathCM + "/test0": - body = ioutil.NopCloser(bytes.NewReader(configMapList[0])) + body = io.NopCloser(bytes.NewReader(configMapList[0])) case pathCM + "/test1": - body = ioutil.NopCloser(bytes.NewReader(configMapList[1])) + body = io.NopCloser(bytes.NewReader(configMapList[1])) default: t.Errorf("unexpected request to %s", p) } @@ -529,10 +528,10 @@ func TestRunApplyViewLastApplied(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(test.respBytes)) + bodyRC := io.NopCloser(bytes.NewReader(test.respBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == "/namespaces/test/replicationcontrollers" && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(test.respBytes)) + bodyRC := io.NopCloser(bytes.NewReader(test.respBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == "/namespaces/test/replicationcontrollers/no-match" && m == "GET": return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Pod{})}, nil @@ -585,10 +584,10 @@ func TestApplyObjectWithoutAnnotation(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(rcBytes)) + bodyRC := io.NopCloser(bytes.NewReader(rcBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "PATCH": - bodyRC := ioutil.NopCloser(bytes.NewReader(rcBytes)) + bodyRC := io.NopCloser(bytes.NewReader(rcBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -630,11 +629,11 @@ func TestApplyObject(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "PATCH": validatePatchApplication(t, req, types.StrategicMergePatchType) - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -679,11 +678,11 @@ func TestApplyPruneObjects(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "PATCH": validatePatchApplication(t, req, types.StrategicMergePatchType) - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -753,7 +752,7 @@ func TestApplyCSAMigration(t *testing.T) { // During retry loop for patch fetch is performed. // keep returning the unchanged data if patches < targetPatches { - bodyRC := ioutil.NopCloser(bytes.NewReader(rcWithManagedFields)) + bodyRC := io.NopCloser(bytes.NewReader(rcWithManagedFields)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil } @@ -769,7 +768,7 @@ func TestApplyCSAMigration(t *testing.T) { case 0: // initial apply. // Just return the same object but with managed fields - bodyRC := ioutil.NopCloser(bytes.NewReader(rcWithManagedFields)) + bodyRC := io.NopCloser(bytes.NewReader(rcWithManagedFields)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case 1: // Second apply should include only last apply annotation unmodified @@ -778,12 +777,12 @@ func TestApplyCSAMigration(t *testing.T) { // just return the same object unmodified. It is not so important // for this test for the last-applied to appear in new field // manager response, only that the client asks the server to do it - bodyRC := ioutil.NopCloser(bytes.NewReader(rcWithManagedFields)) + bodyRC := io.NopCloser(bytes.NewReader(rcWithManagedFields)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case 2, 3: // Before the last apply we have formed our JSONPAtch so it // should reply now with the upgraded object - bodyRC := ioutil.NopCloser(bytes.NewReader(postPatchData)) + bodyRC := io.NopCloser(bytes.NewReader(postPatchData)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: require.Fail(t, "sent more apply requests than expected") @@ -795,13 +794,13 @@ func TestApplyCSAMigration(t *testing.T) { }() // Require that the patch is equal to what is expected - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) require.NoError(t, err) require.Equal(t, expectedPatch, body) switch patches { case targetPatches - 1: - bodyRC := ioutil.NopCloser(bytes.NewReader(postPatchData)) + bodyRC := io.NopCloser(bytes.NewReader(postPatchData)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: // Return conflict until the client has retried enough times @@ -896,11 +895,11 @@ func TestApplyObjectOutput(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "PATCH": validatePatchApplication(t, req, types.StrategicMergePatchType) - bodyRC := ioutil.NopCloser(bytes.NewReader(postPatchData)) + bodyRC := io.NopCloser(bytes.NewReader(postPatchData)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -950,19 +949,19 @@ func TestApplyRetry(t *testing.T) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": getCount++ - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "PATCH": if firstPatch { firstPatch = false statusErr := apierrors.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first")) bodyBytes, _ := json.Marshal(statusErr) - bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes)) + bodyErr := io.NopCloser(bytes.NewReader(bodyBytes)) return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil } retry = true validatePatchApplication(t, req, types.StrategicMergePatchType) - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -1009,11 +1008,11 @@ func TestApplyNonExistObject(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api/v1/namespaces/test" && m == "GET": - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader(nil))}, nil case p == pathNameRC && m == "GET": - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader(nil))}, nil case p == pathRC && m == "POST": - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -1055,17 +1054,17 @@ func TestApplyEmptyPatch(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api/v1/namespaces/test" && m == "GET": - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader(nil))}, nil case p == pathNameRC && m == "GET": if body == nil { - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader(nil))}, nil } - bodyRC := ioutil.NopCloser(bytes.NewReader(body)) + bodyRC := io.NopCloser(bytes.NewReader(body)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "POST": - body, _ = ioutil.ReadAll(req.Body) + body, _ = io.ReadAll(req.Body) verifyPost = true - bodyRC := ioutil.NopCloser(bytes.NewReader(body)) + bodyRC := io.NopCloser(bytes.NewReader(body)) return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -1127,18 +1126,18 @@ func testApplyMultipleObjects(t *testing.T, asList bool) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathRC && m == "PATCH": validatePatchApplication(t, req, types.StrategicMergePatchType) - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == pathSVC && m == "GET": - bodySVC := ioutil.NopCloser(bytes.NewReader(currentSVC)) + bodySVC := io.NopCloser(bytes.NewReader(currentSVC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodySVC}, nil case p == pathSVC && m == "PATCH": validatePatchApplication(t, req, types.StrategicMergePatchType) - bodySVC := ioutil.NopCloser(bytes.NewReader(currentSVC)) + bodySVC := io.NopCloser(bytes.NewReader(currentSVC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodySVC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -1209,10 +1208,10 @@ func TestApplyNULLPreservation(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == deploymentPath && m == "GET": - body := ioutil.NopCloser(bytes.NewReader(deploymentBytes)) + body := io.NopCloser(bytes.NewReader(deploymentBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil case p == deploymentPath && m == "PATCH": - patch, err := ioutil.ReadAll(req.Body) + patch, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -1234,7 +1233,7 @@ func TestApplyNULLPreservation(t *testing.T) { // The real API server would had returned the patched object but Kubectl // is ignoring the actual return object. // TODO: Make this match actual server behavior by returning the patched object. - body := ioutil.NopCloser(bytes.NewReader(deploymentBytes)) + body := io.NopCloser(bytes.NewReader(deploymentBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -1285,7 +1284,7 @@ func TestUnstructuredApply(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == path && m == "GET": - body := ioutil.NopCloser(bytes.NewReader(curr)) + body := io.NopCloser(bytes.NewReader(curr)) return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), @@ -1294,7 +1293,7 @@ func TestUnstructuredApply(t *testing.T) { validatePatchApplication(t, req, types.MergePatchType) verifiedPatch = true - body := ioutil.NopCloser(bytes.NewReader(curr)) + body := io.NopCloser(bytes.NewReader(curr)) return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), @@ -1350,7 +1349,7 @@ func TestUnstructuredIdempotentApply(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == path && m == "GET": - body := ioutil.NopCloser(bytes.NewReader(serversideData)) + body := io.NopCloser(bytes.NewReader(serversideData)) return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), @@ -1358,7 +1357,7 @@ func TestUnstructuredIdempotentApply(t *testing.T) { case p == path && m == "PATCH": // In idempotent updates, kubectl will resolve to an empty patch and not send anything to the server // Thus, if we reach this branch, kubectl is unnecessarily sending a patch. - patch, err := ioutil.ReadAll(req.Body) + patch, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -1452,16 +1451,16 @@ func TestRunApplySetLastApplied(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == pathRC && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == noAnnotationPath && m == "GET": - bodyRC := ioutil.NopCloser(bytes.NewReader(noAnnotationRC)) + bodyRC := io.NopCloser(bytes.NewReader(noAnnotationRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == noExistPath && m == "GET": return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Pod{})}, nil case p == pathRC && m == "PATCH": checkPatchString(t, req) - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case p == "/api/v1/namespaces/test" && m == "GET": return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.Namespace{})}, nil @@ -1495,7 +1494,7 @@ func TestRunApplySetLastApplied(t *testing.T) { func checkPatchString(t *testing.T, req *http.Request) { checkString := string(readBytesFromFile(t, filenameRCPatchTest)) - patch, err := ioutil.ReadAll(req.Body) + patch, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -1547,7 +1546,7 @@ func TestForceApply(t *testing.T) { case strings.HasSuffix(p, pathRC) && m == "GET": if deleted { counts["getNotFound"]++ - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte{}))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte{}))}, nil } counts["getOk"]++ var bodyRC io.ReadCloser @@ -1558,9 +1557,9 @@ func TestForceApply(t *testing.T) { if err != nil { t.Fatal(err) } - bodyRC = ioutil.NopCloser(bytes.NewReader(rcBytes)) + bodyRC = io.NopCloser(bytes.NewReader(rcBytes)) } else { - bodyRC = ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC = io.NopCloser(bytes.NewReader(currentRC)) } return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case strings.HasSuffix(p, pathRCList) && m == "GET": @@ -1577,14 +1576,14 @@ func TestForceApply(t *testing.T) { if err != nil { t.Fatal(err) } - bodyRCList := ioutil.NopCloser(bytes.NewReader(listBytes)) + bodyRCList := io.NopCloser(bytes.NewReader(listBytes)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRCList}, nil case strings.HasSuffix(p, pathRC) && m == "PATCH": counts["patch"]++ if counts["patch"] <= 6 { statusErr := apierrors.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first")) bodyBytes, _ := json.Marshal(statusErr) - bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes)) + bodyErr := io.NopCloser(bytes.NewReader(bodyBytes)) return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil } t.Fatalf("unexpected request: %#v after %v tries\n%#v", req.URL, counts["patch"], req) @@ -1592,18 +1591,18 @@ func TestForceApply(t *testing.T) { case strings.HasSuffix(p, pathRC) && m == "DELETE": counts["delete"]++ deleted = true - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case strings.HasSuffix(p, pathRC) && m == "PUT": counts["put"]++ - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) isScaledDownToZero = true return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil case strings.HasSuffix(p, pathRCList) && m == "POST": counts["post"]++ deleted = false isScaledDownToZero = false - bodyRC := ioutil.NopCloser(bytes.NewReader(currentRC)) + bodyRC := io.NopCloser(bytes.NewReader(currentRC)) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: bodyRC}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani.go b/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani.go index 5b20ef18da8..200fc2629d9 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "sort" "strings" @@ -154,7 +153,7 @@ func (o *CanIOptions) Complete(f cmdutil.Factory, args []string) error { } } else { if o.Quiet { - o.Out = ioutil.Discard + o.Out = io.Discard } switch len(args) { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani_test.go index 2924ca4f526..4b59689a685 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/auth/cani_test.go @@ -19,7 +19,7 @@ package auth import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -122,8 +122,8 @@ func TestRunAccessCheck(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - test.o.Out = ioutil.Discard - test.o.ErrOut = ioutil.Discard + test.o.Out = io.Discard + test.o.ErrOut = io.Discard tf := cmdtesting.NewTestFactory().WithNamespace("test") defer tf.Cleanup() @@ -139,7 +139,7 @@ func TestRunAccessCheck(t *testing.T) { t.Errorf("%s: expected %v, got %v", test.name, expectPath, req.URL.Path) return nil, nil } - bodyBits, err := ioutil.ReadAll(req.Body) + bodyBits, err := io.ReadAll(req.Body) if err != nil { t.Errorf("%s: %v", test.name, err) return nil, nil @@ -154,7 +154,7 @@ func TestRunAccessCheck(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( fmt.Sprintf(`{"kind":"SelfSubjectAccessReview","apiVersion":"authorization.k8s.io/v1","status":{"allowed":%v}}`, test.allowed), )), }, @@ -207,7 +207,7 @@ func TestRunAccessList(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch req.URL.Path { case "/apis/authorization.k8s.io/v1/selfsubjectrulesreviews": - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, getSelfSubjectRulesReview())))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, getSelfSubjectRulesReview())))) return &http.Response{StatusCode: http.StatusOK, Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/auth/whoami_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/auth/whoami_test.go index 7a44f155d98..ea7dfec2c84 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/auth/whoami_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/auth/whoami_test.go @@ -19,7 +19,7 @@ package auth import ( "bytes" "fmt" - "io/ioutil" + "io" "strings" "testing" @@ -137,7 +137,7 @@ func TestWhoAmIRun(t *testing.T) { var b bytes.Buffer test.o.Out = &b - test.o.ErrOut = ioutil.Discard + test.o.ErrOut = io.Discard tf := cmdtesting.NewTestFactory().WithNamespace("test") defer tf.Cleanup() diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates_test.go index a901943dd06..e8d9eb05a36 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/certificates/certificates_test.go @@ -18,7 +18,7 @@ package certificates import ( "bytes" - "io/ioutil" + "io" "net/http" "reflect" "strings" @@ -193,7 +193,7 @@ func TestCertificates(t *testing.T) { actions = append(actions, req.Method+" "+req.URL.Path) switch p, m := req.URL.Path, req.Method; { case tc.nov1 && strings.HasPrefix(p, "/apis/certificates.k8s.io/v1/"): - return &http.Response{StatusCode: http.StatusNotFound, Body: ioutil.NopCloser(bytes.NewBuffer([]byte{}))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Body: io.NopCloser(bytes.NewBuffer([]byte{}))}, nil case p == "/apis/certificates.k8s.io/v1/certificatesigningrequests/missing" && m == http.MethodGet: return &http.Response{StatusCode: http.StatusNotFound}, nil diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/clusterinfo/clusterinfo_dump_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/clusterinfo/clusterinfo_dump_test.go index d4f150f32db..0b774ab59a0 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/clusterinfo/clusterinfo_dump_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/clusterinfo/clusterinfo_dump_test.go @@ -17,7 +17,6 @@ limitations under the License. package clusterinfo import ( - "io/ioutil" "os" "path" "testing" @@ -58,7 +57,7 @@ func TestSetupOutputWriterNoOp(t *testing.T) { func TestSetupOutputWriterFile(t *testing.T) { file := "output" extension := ".json" - dir, err := ioutil.TempDir(os.TempDir(), "out") + dir, err := os.MkdirTemp(os.TempDir(), "out") if err != nil { t.Errorf("unexpected error: %v", err) } @@ -76,7 +75,7 @@ func TestSetupOutputWriterFile(t *testing.T) { output := "some data here" writer.Write([]byte(output)) - data, err := ioutil.ReadFile(fullPath) + data, err := os.ReadFile(fullPath) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go index 0f8a6997efb..a8bc0c8422f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cmd_test.go @@ -18,7 +18,6 @@ package cmd import ( "fmt" - "io/ioutil" "os" "reflect" "testing" @@ -178,7 +177,7 @@ func (h *testPluginHandler) Lookup(filename string) (string, bool) { return "", false } - plugins, err := ioutil.ReadDir(h.pluginsDirectory) + plugins, err := os.ReadDir(h.pluginsDirectory) if err != nil { h.err = err return "", false diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/config_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/config_test.go index 36c3b494e51..bfdc57d8eb6 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/config_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/config_test.go @@ -18,7 +18,6 @@ package config import ( "fmt" - "io/ioutil" "os" "path" "reflect" @@ -261,10 +260,10 @@ func TestAdditionalAuth(t *testing.T) { } func TestEmbedClientCert(t *testing.T) { - fakeCertFile, _ := ioutil.TempFile(os.TempDir(), "") + fakeCertFile, _ := os.CreateTemp(os.TempDir(), "") defer os.Remove(fakeCertFile.Name()) fakeData := []byte("fake-data") - ioutil.WriteFile(fakeCertFile.Name(), fakeData, 0600) + os.WriteFile(fakeCertFile.Name(), fakeData, 0600) expectedConfig := newRedFederalCowHammerConfig() authInfo := clientcmdapi.NewAuthInfo() authInfo.ClientCertificateData = fakeData @@ -280,10 +279,10 @@ func TestEmbedClientCert(t *testing.T) { } func TestEmbedClientKey(t *testing.T) { - fakeKeyFile, _ := ioutil.TempFile(os.TempDir(), "") + fakeKeyFile, _ := os.CreateTemp(os.TempDir(), "") defer os.Remove(fakeKeyFile.Name()) fakeData := []byte("fake-data") - ioutil.WriteFile(fakeKeyFile.Name(), fakeData, 0600) + os.WriteFile(fakeKeyFile.Name(), fakeData, 0600) expectedConfig := newRedFederalCowHammerConfig() authInfo := clientcmdapi.NewAuthInfo() authInfo.ClientKeyData = fakeData @@ -326,7 +325,7 @@ func TestEmbedNoKeyOrCertDisallowed(t *testing.T) { } func TestEmptyTokenAndCertAllowed(t *testing.T) { - fakeCertFile, _ := ioutil.TempFile(os.TempDir(), "cert-file") + fakeCertFile, _ := os.CreateTemp(os.TempDir(), "cert-file") defer os.Remove(fakeCertFile.Name()) expectedConfig := newRedFederalCowHammerConfig() authInfo := clientcmdapi.NewAuthInfo() @@ -569,7 +568,7 @@ func TestUnsetBytes(t *testing.T) { } func TestCAClearsInsecure(t *testing.T) { - fakeCAFile, _ := ioutil.TempFile(os.TempDir(), "ca-file") + fakeCAFile, _ := os.CreateTemp(os.TempDir(), "ca-file") defer os.Remove(fakeCAFile.Name()) clusterInfoWithInsecure := clientcmdapi.NewCluster() clusterInfoWithInsecure.InsecureSkipTLSVerify = true @@ -638,10 +637,10 @@ func TestInsecureClearsCA(t *testing.T) { } func TestCADataClearsCA(t *testing.T) { - fakeCAFile, _ := ioutil.TempFile(os.TempDir(), "") + fakeCAFile, _ := os.CreateTemp(os.TempDir(), "") defer os.Remove(fakeCAFile.Name()) fakeData := []byte("cadata") - ioutil.WriteFile(fakeCAFile.Name(), fakeData, 0600) + os.WriteFile(fakeCAFile.Name(), fakeData, 0600) clusterInfoWithCAData := clientcmdapi.NewCluster() clusterInfoWithCAData.CertificateAuthorityData = fakeData @@ -852,7 +851,7 @@ func TestToBool(t *testing.T) { } func testConfigCommand(args []string, startingConfig clientcmdapi.Config, t *testing.T) (string, clientcmdapi.Config) { - fakeKubeFile, _ := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, _ := os.CreateTemp(os.TempDir(), "") defer os.Remove(fakeKubeFile.Name()) err := clientcmd.WriteToFile(startingConfig, fakeKubeFile.Name()) if err != nil { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/current_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/current_context_test.go index 139b79a1834..e545d36495e 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/current_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/current_context_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "strings" "testing" @@ -57,7 +56,7 @@ func TestCurrentContextWithUnsetContext(t *testing.T) { } func (test currentContextTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go index 3becba3fdf6..69d4a21d1ff 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_cluster_test.go @@ -19,7 +19,6 @@ package config import ( "bytes" "fmt" - "io/ioutil" "os" "reflect" "testing" @@ -53,7 +52,7 @@ func TestDeleteCluster(t *testing.T) { } func (test deleteClusterTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go index 04bbe17cbb0..26fd1a48f13 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/delete_context_test.go @@ -19,7 +19,6 @@ package config import ( "bytes" "fmt" - "io/ioutil" "os" "reflect" "testing" @@ -53,7 +52,7 @@ func TestDeleteContext(t *testing.T) { } func (test deleteContextTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/get_clusters_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/get_clusters_test.go index 13186a88515..52ff8e67d1f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/get_clusters_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/get_clusters_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "testing" @@ -57,7 +56,7 @@ func TestGetClustersEmpty(t *testing.T) { } func (test getClustersTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/get_contexts_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/get_contexts_test.go index d97d43ca853..20b00e486d4 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/get_contexts_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/get_contexts_test.go @@ -17,7 +17,6 @@ limitations under the License. package config import ( - "io/ioutil" "os" "testing" @@ -144,7 +143,7 @@ func TestGetContextsSelectOneOfTwo(t *testing.T) { } func (test getContextsTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go index cfacb90102a..6c40408f671 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/rename_context_test.go @@ -19,7 +19,6 @@ package config import ( "bytes" "fmt" - "io/ioutil" "os" "strings" "testing" @@ -103,7 +102,7 @@ func TestRenameToAlreadyExistingContext(t *testing.T) { } func (test renameContextTest) run(t *testing.T) { - fakeKubeFile, _ := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, _ := os.CreateTemp(os.TempDir(), "") defer os.Remove(fakeKubeFile.Name()) err := clientcmd.WriteToFile(test.initialConfig, fakeKubeFile.Name()) if err != nil { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster.go index 4ed92589e85..28930272c1c 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -147,7 +146,7 @@ func (o *setClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluster) if o.certificateAuthority.Provided() { caPath := o.certificateAuthority.Value() if o.embedCAData.Value() { - modifiedCluster.CertificateAuthorityData, _ = ioutil.ReadFile(caPath) + modifiedCluster.CertificateAuthorityData, _ = os.ReadFile(caPath) modifiedCluster.InsecureSkipTLSVerify = false modifiedCluster.CertificateAuthority = "" } else { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster_test.go index 0054c72d057..d19818419ce 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_cluster_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "testing" @@ -183,7 +182,7 @@ func TestModifyClusterServerAndTLS(t *testing.T) { } func (test setClusterTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_context_test.go index 2422d02028e..3f08a7b3c43 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_context_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "testing" @@ -107,7 +106,7 @@ func TestModifyCurrentContext(t *testing.T) { } func (test setContextTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials.go index 4140c07e9a3..182dadeddf3 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -221,7 +220,7 @@ func (o *setCredentialsOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.Aut if o.clientCertificate.Provided() { certPath := o.clientCertificate.Value() if o.embedCertData.Value() { - modifiedAuthInfo.ClientCertificateData, _ = ioutil.ReadFile(certPath) + modifiedAuthInfo.ClientCertificateData, _ = os.ReadFile(certPath) modifiedAuthInfo.ClientCertificate = "" } else { certPath, _ = filepath.Abs(certPath) @@ -234,7 +233,7 @@ func (o *setCredentialsOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.Aut if o.clientKey.Provided() { keyPath := o.clientKey.Value() if o.embedCertData.Value() { - modifiedAuthInfo.ClientKeyData, _ = ioutil.ReadFile(keyPath) + modifiedAuthInfo.ClientKeyData, _ = os.ReadFile(keyPath) modifiedAuthInfo.ClientKey = "" } else { keyPath, _ = filepath.Abs(keyPath) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials_test.go index 2eafcb27727..60c52611677 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_credentials_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "reflect" "testing" @@ -454,7 +453,7 @@ func TestSetCredentials(t *testing.T) { test.run(t) } func (test setCredentialsTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_test.go index 87afd7a1ac8..a22561a49ef 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/set_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/set_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "testing" @@ -55,7 +54,7 @@ func TestSetConfigCurrentContext(t *testing.T) { } func (test setConfigTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/unset_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/unset_test.go index 3af5487a778..0a6a2f79f94 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/unset_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/unset_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "testing" @@ -106,7 +105,7 @@ func TestUnsetUnexistConfig(t *testing.T) { } func (test unsetConfigTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go index 74424bc51b9..f36e9058215 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/use_context_test.go @@ -18,7 +18,6 @@ package config import ( "bytes" - "io/ioutil" "os" "testing" @@ -71,7 +70,7 @@ func TestUseContext(t *testing.T) { } func (test useContextTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/config/view_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/config/view_test.go index bce7768e507..b80e00a2e58 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/config/view_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/config/view_test.go @@ -17,7 +17,6 @@ limitations under the License. package config import ( - "io/ioutil" "os" "testing" @@ -171,7 +170,7 @@ users: } func (test viewClusterTest) run(t *testing.T) { - fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") + fakeKubeFile, err := os.CreateTemp(os.TempDir(), "") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go index 9a90d133ade..7db77ce9abb 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "strings" @@ -129,7 +128,7 @@ func NewCmdCp(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.C // listing the entire content of the current directory which could // be too many choices for the user) if len(comps) > 0 && len(toComplete) > 0 { - if files, err := ioutil.ReadDir("."); err == nil { + if files, err := os.ReadDir("."); err == nil { for _, file := range files { filename := file.Name() if strings.HasPrefix(filename, toComplete) { @@ -425,7 +424,7 @@ func recursiveTar(srcDir, srcFile localPath, destDir, destFile remotePath, tw *t return err } if stat.IsDir() { - files, err := ioutil.ReadDir(fpath) + files, err := os.ReadDir(fpath) if err != nil { return err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go index dffd89f88d7..383f34df62f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cp/cp_test.go @@ -21,7 +21,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -309,15 +308,15 @@ func checkErr(t *testing.T, err error) { } func TestTarUntar(t *testing.T) { - dir, err := ioutil.TempDir("", "input") + dir, err := os.MkdirTemp("", "input") checkErr(t, err) dir = dir + "/" - dir2, err := ioutil.TempDir("", "output") + dir2, err := os.MkdirTemp("", "output") checkErr(t, err) dir2 = dir2 + "/" - dir3, err := ioutil.TempDir("", "dir") + dir3, err := os.MkdirTemp("", "dir") checkErr(t, err) defer func() { @@ -450,11 +449,11 @@ func TestTarUntar(t *testing.T) { } func TestTarUntarWrongPrefix(t *testing.T) { - dir, err := ioutil.TempDir("", "input") + dir, err := os.MkdirTemp("", "input") checkErr(t, err) dir = dir + "/" - dir2, err := ioutil.TempDir("", "output") + dir2, err := os.MkdirTemp("", "output") checkErr(t, err) defer func() { @@ -483,8 +482,8 @@ func TestTarUntarWrongPrefix(t *testing.T) { } func TestTarDestinationName(t *testing.T) { - dir, err := ioutil.TempDir(os.TempDir(), "input") - dir2, err2 := ioutil.TempDir(os.TempDir(), "output") + dir, err := os.MkdirTemp(os.TempDir(), "input") + dir2, err2 := os.MkdirTemp(os.TempDir(), "output") if err != nil || err2 != nil { t.Errorf("unexpected error: %v | %v", err, err2) t.FailNow() @@ -554,7 +553,7 @@ func TestTarDestinationName(t *testing.T) { } func TestBadTar(t *testing.T) { - dir, err := ioutil.TempDir(os.TempDir(), "dest") + dir, err := os.MkdirTemp(os.TempDir(), "dest") if err != nil { t.Errorf("unexpected error: %v ", err) t.FailNow() @@ -614,7 +613,7 @@ func TestCopyToPod(t *testing.T) { NegotiatedSerializer: ns, Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { responsePod := &v1.Pod{} - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil }), } @@ -623,7 +622,7 @@ func TestCopyToPod(t *testing.T) { cmd := NewCmdCp(tf, ioStreams) - srcFile, err := ioutil.TempDir("", "test") + srcFile, err := os.MkdirTemp("", "test") if err != nil { t.Errorf("unexpected error: %v", err) t.FailNow() @@ -685,7 +684,7 @@ func TestCopyToPodNoPreserve(t *testing.T) { NegotiatedSerializer: ns, Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { responsePod := &v1.Pod{} - return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil + return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil }), } @@ -694,7 +693,7 @@ func TestCopyToPodNoPreserve(t *testing.T) { cmd := NewCmdCp(tf, ioStreams) - srcFile, err := ioutil.TempDir("", "test") + srcFile, err := os.MkdirTemp("", "test") if err != nil { t.Errorf("unexpected error: %v", err) t.FailNow() @@ -769,7 +768,7 @@ func TestValidate(t *testing.T) { } func TestUntar(t *testing.T) { - testdir, err := ioutil.TempDir("", "test-untar") + testdir, err := os.MkdirTemp("", "test-untar") require.NoError(t, err) defer os.RemoveAll(testdir) t.Logf("Test base: %s", testdir) @@ -935,7 +934,7 @@ func TestUntar(t *testing.T) { } func TestUntar_SingleFile(t *testing.T) { - testdir, err := ioutil.TempDir("", "test-untar") + testdir, err := os.MkdirTemp("", "test-untar") require.NoError(t, err) defer os.RemoveAll(testdir) @@ -981,7 +980,7 @@ func createTmpFile(t *testing.T, filepath, data string) { } func cmpFileData(t *testing.T, filePath, data string) { - actual, err := ioutil.ReadFile(filePath) + actual, err := os.ReadFile(filePath) require.NoError(t, err) assert.EqualValues(t, data, actual) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap.go index b980388bd21..899d275a187 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap.go @@ -19,7 +19,6 @@ package create import ( "context" "fmt" - "io/ioutil" "os" "path" "strings" @@ -331,13 +330,13 @@ func handleConfigMapFromFileSources(configMap *corev1.ConfigMap, fileSources []s if strings.Contains(fileSource, "=") { return fmt.Errorf("cannot give a key name for a directory path") } - fileList, err := ioutil.ReadDir(filePath) + fileList, err := os.ReadDir(filePath) if err != nil { return fmt.Errorf("error listing files in %s: %v", filePath, err) } for _, item := range fileList { itemPath := path.Join(filePath, item.Name()) - if item.Mode().IsRegular() { + if item.Type().IsRegular() { keyName = item.Name() err = addKeyFromFileToConfigMap(configMap, keyName, itemPath) if err != nil { @@ -385,7 +384,7 @@ func handleConfigMapFromEnvFileSources(configMap *corev1.ConfigMap, envFileSourc // addKeyFromFileToConfigMap adds a key with the given name to a ConfigMap, populating // the value with the content of the given file path, or returns an error. func addKeyFromFileToConfigMap(configMap *corev1.ConfigMap, keyName, filePath string) error { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap_test.go index 685a4b33cf9..c7a4739cc0c 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_configmap_test.go @@ -17,7 +17,6 @@ limitations under the License. package create import ( - "io/ioutil" "os" "testing" @@ -465,7 +464,7 @@ func setupEnvFile(lines [][]string) func(*testing.T, *ConfigMapOptions) func() { files := []*os.File{} filenames := configMapOptions.EnvFileSources for _, filename := range filenames { - file, err := ioutil.TempFile("", filename) + file, err := os.CreateTemp("", filename) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -489,11 +488,11 @@ func setupEnvFile(lines [][]string) func(*testing.T, *ConfigMapOptions) func() { func setupBinaryFile(data []byte) func(*testing.T, *ConfigMapOptions) func() { return func(t *testing.T, configMapOptions *ConfigMapOptions) func() { - tmp, _ := ioutil.TempDir("", "") + tmp, _ := os.MkdirTemp("", "") files := configMapOptions.FileSources for i, file := range files { f := tmp + "/" + file - ioutil.WriteFile(f, data, 0644) + os.WriteFile(f, data, 0644) configMapOptions.FileSources[i] = f } return func() { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go index f20af00844f..2a132c1f047 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go @@ -18,7 +18,7 @@ package create import ( "bytes" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -45,7 +45,7 @@ func TestCreateDeployment(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), + Body: io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), }, nil }), } @@ -76,7 +76,7 @@ func TestCreateDeploymentWithPort(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), + Body: io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), }, nil }), } @@ -107,7 +107,7 @@ func TestCreateDeploymentWithReplicas(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), + Body: io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), }, nil }), } @@ -137,7 +137,7 @@ func TestCreateDeploymentNoImage(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), + Body: io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), }, nil }), } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_priorityclass_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_priorityclass_test.go index a6e853b1bd0..8ea6b246df3 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_priorityclass_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_priorityclass_test.go @@ -18,7 +18,7 @@ package create import ( "bytes" - "io/ioutil" + "io" "net/http" "testing" @@ -43,7 +43,7 @@ func TestCreatePriorityClass(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(&bytes.Buffer{}), + Body: io.NopCloser(&bytes.Buffer{}), }, nil }), } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret.go index 84a3cea4cd5..c3fab63d3b8 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret.go @@ -19,7 +19,6 @@ package create import ( "context" "fmt" - "io/ioutil" "os" "path" "strings" @@ -352,13 +351,13 @@ func handleSecretFromFileSources(secret *corev1.Secret, fileSources []string) er if strings.Contains(fileSource, "=") { return fmt.Errorf("cannot give a key name for a directory path") } - fileList, err := ioutil.ReadDir(filePath) + fileList, err := os.ReadDir(filePath) if err != nil { return fmt.Errorf("error listing files in %s: %v", filePath, err) } for _, item := range fileList { itemPath := path.Join(filePath, item.Name()) - if item.Mode().IsRegular() { + if item.Type().IsRegular() { keyName = item.Name() if err := addKeyFromFileToSecret(secret, keyName, itemPath); err != nil { return err @@ -407,7 +406,7 @@ func handleSecretFromEnvFileSources(secret *corev1.Secret, envFileSources []stri // addKeyFromFileToSecret adds a key with the given name to a Secret, populating // the value with the content of the given file path, or returns an error. func addKeyFromFileToSecret(secret *corev1.Secret, keyName, filePath string) error { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_test.go index c34927384f4..60aef92ebec 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_test.go @@ -17,7 +17,6 @@ limitations under the License. package create import ( - "io/ioutil" "os" "testing" @@ -540,7 +539,7 @@ func setupSecretEnvFile(lines [][]string) func(*testing.T, *CreateSecretOptions) files := []*os.File{} filenames := secretOptions.EnvFileSources for _, filename := range filenames { - file, err := ioutil.TempFile("", filename) + file, err := os.CreateTemp("", filename) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -564,11 +563,11 @@ func setupSecretEnvFile(lines [][]string) func(*testing.T, *CreateSecretOptions) func setupSecretBinaryFile(data []byte) func(*testing.T, *CreateSecretOptions) func() { return func(t *testing.T, secretOptions *CreateSecretOptions) func() { - tmp, _ := ioutil.TempDir("", "") + tmp, _ := os.MkdirTemp("", "") files := secretOptions.FileSources for i, file := range files { f := tmp + "/" + file - ioutil.WriteFile(f, data, 0644) + os.WriteFile(f, data, 0644) secretOptions.FileSources[i] = f } return func() { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_tls.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_tls.go index 09e762a8b42..85776d89d04 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_tls.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_tls.go @@ -20,7 +20,7 @@ import ( "context" "crypto/tls" "fmt" - "io/ioutil" + "os" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" @@ -258,7 +258,7 @@ func (o *CreateSecretTLSOptions) createSecretTLS() (*corev1.Secret, error) { // readFile just reads a file into a byte array. func readFile(file string) ([]byte, error) { - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if err != nil { return []byte{}, fmt.Errorf("Cannot read file %v, %v", file, err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_token_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_token_test.go index f8edd049d67..9035af1caa4 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_token_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_token_test.go @@ -19,7 +19,7 @@ package create import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "reflect" "testing" @@ -280,7 +280,7 @@ status: if req.URL.Path != test.expectRequestPath { t.Fatalf("expected %q, got %q", test.expectRequestPath, req.URL.Path) } - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -293,7 +293,7 @@ status: return &http.Response{ StatusCode: code, - Body: ioutil.NopCloser(bytes.NewBuffer(body)), + Body: io.NopCloser(bytes.NewBuffer(body)), }, nil }), } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go index 50903f28c0e..77aefe6aad2 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete_test.go @@ -19,7 +19,6 @@ package delete import ( "encoding/json" "io" - "io/ioutil" "net/http" "strconv" "strings" @@ -104,7 +103,7 @@ func hasExpectedPropagationPolicy(body io.ReadCloser, policy *metav1.DeletionPro return body == nil && policy == nil } var parsedBody metav1.DeleteOptions - rawBody, _ := ioutil.ReadAll(body) + rawBody, _ := io.ReadAll(body) json.Unmarshal(rawBody, &parsedBody) if parsedBody.PropagationPolicy == nil { return false diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go b/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go index 82cf5133f4f..0b3a17e7866 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go @@ -19,7 +19,6 @@ package diff import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" "regexp" @@ -286,7 +285,7 @@ type Directory struct { // CreateDirectory does create the actual disk directory, and return a // new representation of it. func CreateDirectory(prefix string) (*Directory, error) { - name, err := ioutil.TempDir("", prefix+"-") + name, err := os.MkdirTemp("", prefix+"-") if err != nil { return nil, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff_test.go index ca86b7ab0a1..5bf171b267e 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff_test.go @@ -19,7 +19,6 @@ package diff import ( "bytes" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -134,7 +133,7 @@ func TestDiffVersion(t *testing.T) { if err != nil { t.Fatal(err) } - fcontent, err := ioutil.ReadFile(path.Join(diff.Dir.Name, obj.Name())) + fcontent, err := os.ReadFile(path.Join(diff.Dir.Name, obj.Name())) if err != nil { t.Fatal(err) } @@ -157,7 +156,7 @@ func TestDirectory(t *testing.T) { if !strings.HasPrefix(filepath.Base(dir.Name), "prefix") { t.Fatalf(`Directory doesn't start with "prefix": %q`, dir.Name) } - entries, err := ioutil.ReadDir(dir.Name) + entries, err := os.ReadDir(dir.Name) if err != nil { t.Fatal(err) } @@ -172,7 +171,7 @@ func TestDirectory(t *testing.T) { if err != nil { t.Fatal(err) } - entries, err = ioutil.ReadDir(dir.Name) + entries, err = os.ReadDir(dir.Name) if err != nil { t.Fatal(err) } @@ -205,7 +204,7 @@ func TestDiffer(t *testing.T) { if err != nil { t.Fatal(err) } - fcontent, err := ioutil.ReadFile(path.Join(diff.From.Dir.Name, obj.Name())) + fcontent, err := os.ReadFile(path.Join(diff.From.Dir.Name, obj.Name())) if err != nil { t.Fatal(err) } @@ -214,7 +213,7 @@ func TestDiffer(t *testing.T) { t.Fatalf("File has %q, expected %q", string(fcontent), econtent) } - fcontent, err = ioutil.ReadFile(path.Join(diff.To.Dir.Name, obj.Name())) + fcontent, err = os.ReadFile(path.Join(diff.To.Dir.Name, obj.Name())) if err != nil { t.Fatal(err) } @@ -290,12 +289,12 @@ metadata: t.Fatal(err) } - actualFromContent, _ := ioutil.ReadFile(path.Join(diff.From.Dir.Name, obj.Name())) + actualFromContent, _ := os.ReadFile(path.Join(diff.From.Dir.Name, obj.Name())) if string(actualFromContent) != tc.expectedFromContent { t.Fatalf("File has %q, expected %q", string(actualFromContent), tc.expectedFromContent) } - actualToContent, _ := ioutil.ReadFile(path.Join(diff.To.Dir.Name, obj.Name())) + actualToContent, _ := os.ReadFile(path.Join(diff.To.Dir.Name, obj.Name())) if string(actualToContent) != tc.expectedToContent { t.Fatalf("File has %q, expected %q", string(actualToContent), tc.expectedToContent) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go index aa602ed482a..3acf13d865f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/drain/drain_test.go @@ -18,7 +18,7 @@ package drain import ( "errors" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -182,7 +182,7 @@ func TestCordon(t *testing.T) { case m.isFor("PATCH", "/nodes/node2"): fallthrough case m.isFor("PATCH", "/nodes/node"): - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("%s: unexpected error: %v", test.description, err) } @@ -833,7 +833,7 @@ func TestDrain(t *testing.T) { case m.isFor("GET", "/replicationcontrollers"): return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.ReplicationControllerList{Items: test.rcs})}, nil case m.isFor("PATCH", "/nodes/node"): - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("%s: unexpected error: %v", test.description, err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go index 4bc92e81535..d3da69a4729 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/edit/edit_test.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -98,20 +97,20 @@ func TestEdit(t *testing.T) { body := []byte{} if req.Body != nil { - body, err = ioutil.ReadAll(req.Body) + body, err = io.ReadAll(req.Body) if err != nil { t.Fatalf("%s, step %d: %v", name, i, err) } } inputFile := filepath.Join("testdata", "testcase-"+name, step.Input) - expectedInput, err := ioutil.ReadFile(inputFile) + expectedInput, err := os.ReadFile(inputFile) if err != nil { t.Fatalf("%s, step %d: %v", name, i, err) } outputFile := filepath.Join("testdata", "testcase-"+name, step.Output) - resultingOutput, err := ioutil.ReadFile(outputFile) + resultingOutput, err := os.ReadFile(outputFile) if err != nil { t.Fatalf("%s, step %d: %v", name, i, err) } @@ -123,13 +122,13 @@ func TestEdit(t *testing.T) { if !bytes.Equal(body, expectedInput) { if updateInputFixtures { // Convenience to allow recapturing the input and persisting it here - ioutil.WriteFile(inputFile, body, os.FileMode(0644)) + os.WriteFile(inputFile, body, os.FileMode(0644)) } else { t.Errorf("%s, step %d: diff in edit content:\n%s", name, i, diff.StringDiff(string(body), string(expectedInput))) t.Logf("If the change in input is expected, rerun tests with %s=true to update input fixtures", updateEnvVar) } } - return &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(bytes.NewReader(resultingOutput))}, nil + return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader(resultingOutput))}, nil } if step.StepType != "request" { t.Fatalf("%s, step %d: expected request step, got %s %s", name, i, req.Method, req.URL.Path) @@ -146,13 +145,13 @@ func TestEdit(t *testing.T) { if !bytes.Equal(body, expectedInput) { if updateInputFixtures { // Convenience to allow recapturing the input and persisting it here - ioutil.WriteFile(inputFile, body, os.FileMode(0644)) + os.WriteFile(inputFile, body, os.FileMode(0644)) } else { t.Errorf("%s, step %d: diff in edit content:\n%s", name, i, diff.StringDiff(string(body), string(expectedInput))) t.Logf("If the change in input is expected, rerun tests with %s=true to update input fixtures", updateEnvVar) } } - return &http.Response{StatusCode: step.ResponseStatusCode, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader(resultingOutput))}, nil + return &http.Response{StatusCode: step.ResponseStatusCode, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader(resultingOutput))}, nil } @@ -202,7 +201,7 @@ func TestEdit(t *testing.T) { name = testcaseName testcase = EditTestCase{} testcaseDir := filepath.Join("testdata", "testcase-"+name) - testcaseData, err := ioutil.ReadFile(filepath.Join(testcaseDir, "test.yaml")) + testcaseData, err := os.ReadFile(filepath.Join(testcaseDir, "test.yaml")) if err != nil { t.Fatalf("%s: %v", name, err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/edit/testdata/record.go b/staging/src/k8s.io/kubectl/pkg/cmd/edit/testdata/record.go index 95f15c64a13..5f29c1a10b9 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/edit/testdata/record.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/edit/testdata/record.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -85,7 +85,7 @@ func main() { record = true } - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) checkErr(err) switch m, p := req.Method, req.URL.Path; { @@ -94,14 +94,14 @@ func main() { panic("cannot post input with step already in progress") } filename := fmt.Sprintf("%d.original", len(tc.Steps)) - checkErr(ioutil.WriteFile(filename, body, os.FileMode(0755))) + checkErr(os.WriteFile(filename, body, os.FileMode(0755))) currentStep = &EditStep{StepType: "edit", Input: filename} case m == "POST" && p == "/callback/out": if currentStep == nil || currentStep.StepType != "edit" { panic("cannot post output without posting input first") } filename := fmt.Sprintf("%d.edited", len(tc.Steps)) - checkErr(ioutil.WriteFile(filename, body, os.FileMode(0755))) + checkErr(os.WriteFile(filename, body, os.FileMode(0755))) currentStep.Output = filename tc.Steps = append(tc.Steps, *currentStep) currentStep = nil @@ -120,7 +120,7 @@ func main() { checkErr(err) defer resp.Body.Close() - bodyOut, err := ioutil.ReadAll(resp.Body) + bodyOut, err := io.ReadAll(resp.Body) checkErr(err) for k, vs := range resp.Header { @@ -134,8 +134,8 @@ func main() { if record { infile := fmt.Sprintf("%d.request", len(tc.Steps)) outfile := fmt.Sprintf("%d.response", len(tc.Steps)) - checkErr(ioutil.WriteFile(infile, tryIndent(body), os.FileMode(0755))) - checkErr(ioutil.WriteFile(outfile, tryIndent(bodyOut), os.FileMode(0755))) + checkErr(os.WriteFile(infile, tryIndent(body), os.FileMode(0755))) + checkErr(os.WriteFile(outfile, tryIndent(bodyOut), os.FileMode(0755))) tc.Steps = append(tc.Steps, EditStep{ StepType: "request", Input: infile, @@ -150,7 +150,7 @@ func main() { tcData, err := yaml.Marshal(tc) checkErr(err) - checkErr(ioutil.WriteFile("test.yaml", tcData, os.FileMode(0755))) + checkErr(os.WriteFile("test.yaml", tcData, os.FileMode(0755))) }))) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go index 814e3e87c44..bb899553d20 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/exec/exec_test.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "net/url" "reflect" @@ -373,7 +372,7 @@ func TestSetupTTY(t *testing.T) { stderr.Reset() o.TTY = true - overrideStdin := ioutil.NopCloser(&bytes.Buffer{}) + overrideStdin := io.NopCloser(&bytes.Buffer{}) overrideStdout := &bytes.Buffer{} overrideStderr := &bytes.Buffer{} o.overrideStreams = func() (io.ReadCloser, io.Writer, io.Writer) { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/get/customcolumn_flags_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/get/customcolumn_flags_test.go index 01b4f371fc0..a87c0c9d0bb 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/get/customcolumn_flags_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/get/customcolumn_flags_test.go @@ -19,7 +19,6 @@ package get import ( "bytes" "fmt" - "io/ioutil" "os" "strings" "testing" @@ -32,7 +31,7 @@ import ( func TestPrinterSupportsExpectedCustomColumnFormats(t *testing.T) { testObject := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} - customColumnsFile, err := ioutil.TempFile(os.TempDir(), "printers_jsonpath_flags") + customColumnsFile, err := os.CreateTemp(os.TempDir(), "printers_jsonpath_flags") if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go index e8abb3d025f..edccda4c8ac 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/get/get_test.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "path/filepath" "reflect" @@ -746,7 +745,7 @@ func TestGetEmptyTable(t *testing.T) { tf := cmdtesting.NewTestFactory().WithNamespace("test") defer tf.Cleanup() - emptyTable := ioutil.NopCloser(bytes.NewBufferString(`{ + emptyTable := io.NopCloser(bytes.NewBufferString(`{ "kind":"Table", "apiVersion":"meta.k8s.io/v1beta1", "metadata":{ @@ -959,7 +958,7 @@ func TestGetSortedObjectsUnstructuredTable(t *testing.T) { t.Fatal(err) } // t.Log(string(unstructuredBytes)) - body := ioutil.NopCloser(bytes.NewReader(unstructuredBytes)) + body := io.NopCloser(bytes.NewReader(unstructuredBytes)) tf := cmdtesting.NewTestFactory().WithNamespace("test") defer tf.Cleanup() @@ -2844,7 +2843,7 @@ func watchBody(codec runtime.Codec, events []watch.Event) io.ReadCloser { panic(err) } } - return ioutil.NopCloser(buf) + return io.NopCloser(buf) } var podColumns = []metav1.TableColumnDefinition{ diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go index c379455c0f4..f97e53837cf 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go @@ -19,7 +19,7 @@ package label import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "path/filepath" "reflect" @@ -535,7 +535,7 @@ func TestLabelResourceVersion(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"test","resourceVersion":"10"}}`, ))}, nil default: @@ -545,7 +545,7 @@ func TestLabelResourceVersion(t *testing.T) { case "PATCH": switch req.URL.Path { case "/namespaces/test/pods/foo": - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { t.Fatal(err) } @@ -555,7 +555,7 @@ func TestLabelResourceVersion(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"test","resourceVersion":"11"}}`, ))}, nil default: @@ -606,7 +606,7 @@ func TestRunLabelMsg(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"test","labels":{"existing":"abc"}}}`, ))}, nil default: @@ -619,7 +619,7 @@ func TestRunLabelMsg(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), - Body: ioutil.NopCloser(bytes.NewBufferString( + Body: io.NopCloser(bytes.NewBufferString( `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"foo","namespace":"test","labels":{"existing":"abc"}}}`, ))}, nil default: diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go index cb9b7cc9228..6f3ab056236 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "strings" "sync" @@ -839,12 +838,12 @@ type responseWrapperMock struct { } func (r *responseWrapperMock) DoRaw(context.Context) ([]byte, error) { - data, _ := ioutil.ReadAll(r.data) + data, _ := io.ReadAll(r.data) return data, r.err } func (r *responseWrapperMock) Stream(context.Context) (io.ReadCloser, error) { - return ioutil.NopCloser(r.data), r.err + return io.NopCloser(r.data), r.err } type logTestMock struct { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go b/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go index 4d682053740..b048caf007f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/patch/patch.go @@ -18,7 +18,7 @@ package patch import ( "fmt" - "io/ioutil" + "os" "reflect" "strings" @@ -219,7 +219,7 @@ func (o *PatchOptions) RunPatch() error { var patchBytes []byte if len(o.PatchFile) > 0 { var err error - patchBytes, err = ioutil.ReadFile(o.PatchFile) + patchBytes, err = os.ReadFile(o.PatchFile) if err != nil { return fmt.Errorf("unable to read patch file: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin.go b/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin.go index fa0acd46c76..401d3778a1e 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin.go @@ -19,7 +19,6 @@ package plugin import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -166,7 +165,7 @@ func (o *PluginListOptions) ListPlugins() ([]string, []error) { continue } - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { if _, ok := err.(*os.PathError); ok { fmt.Fprintf(o.ErrOut, "Unable to read directory %q from your PATH: %v. Skipping...\n", dir, err) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_completion.go b/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_completion.go index 357dc7aa6fa..ccca88bfbae 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_completion.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_completion.go @@ -19,7 +19,7 @@ package plugin import ( "bytes" "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -83,8 +83,8 @@ func addPluginCommands(cmd *cobra.Command) { kubectl := cmd.Root() streams := genericclioptions.IOStreams{ In: &bytes.Buffer{}, - Out: ioutil.Discard, - ErrOut: ioutil.Discard, + Out: io.Discard, + ErrOut: io.Discard, } o := &PluginListOptions{IOStreams: streams} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_test.go index 639bc6f8dac..817d24fdd62 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_test.go @@ -18,7 +18,6 @@ package plugin import ( "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -29,11 +28,11 @@ import ( ) func TestPluginPathsAreUnaltered(t *testing.T) { - tempDir, err := ioutil.TempDir(os.TempDir(), "test-cmd-plugins") + tempDir, err := os.MkdirTemp(os.TempDir(), "test-cmd-plugins") if err != nil { t.Fatalf("unexpected error: %v", err) } - tempDir2, err := ioutil.TempDir(os.TempDir(), "test-cmd-plugins2") + tempDir2, err := os.MkdirTemp(os.TempDir(), "test-cmd-plugins2") if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -58,10 +57,10 @@ func TestPluginPathsAreUnaltered(t *testing.T) { } // write at least one valid plugin file - if _, err := ioutil.TempFile(tempDir, "kubectl-"); err != nil { + if _, err := os.CreateTemp(tempDir, "kubectl-"); err != nil { t.Fatalf("unexpected error %v", err) } - if _, err := ioutil.TempFile(tempDir2, "kubectl-"); err != nil { + if _, err := os.CreateTemp(tempDir2, "kubectl-"); err != nil { t.Fatalf("unexpected error %v", err) } @@ -81,7 +80,7 @@ func TestPluginPathsAreUnaltered(t *testing.T) { } func TestPluginPathsAreValid(t *testing.T) { - tempDir, err := ioutil.TempDir(os.TempDir(), "test-cmd-plugins") + tempDir, err := os.MkdirTemp(os.TempDir(), "test-cmd-plugins") if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -107,7 +106,7 @@ func TestPluginPathsAreValid(t *testing.T) { pluginPaths: []string{tempDir}, verifier: newFakePluginPathVerifier(), pluginFile: func() (*os.File, error) { - return ioutil.TempFile(tempDir, "notkubectl-") + return os.CreateTemp(tempDir, "notkubectl-") }, expectErr: "error: unable to find any kubectl plugins in your PATH\n", }, @@ -116,7 +115,7 @@ func TestPluginPathsAreValid(t *testing.T) { pluginPaths: []string{tempDir, tempDir}, verifier: newFakePluginPathVerifier(), pluginFile: func() (*os.File, error) { - return ioutil.TempFile(tempDir, "kubectl-") + return os.CreateTemp(tempDir, "kubectl-") }, expectOut: "The following compatible plugins are available:", }, @@ -125,7 +124,7 @@ func TestPluginPathsAreValid(t *testing.T) { pluginPaths: []string{tempDir, "", " "}, verifier: newFakePluginPathVerifier(), pluginFile: func() (*os.File, error) { - return ioutil.TempFile(tempDir, "kubectl-") + return os.CreateTemp(tempDir, "kubectl-") }, expectOut: "The following compatible plugins are available:", }, diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/replace/replace.go b/staging/src/k8s.io/kubectl/pkg/cmd/replace/replace.go index c4ec8b3d95e..ee1e55b2314 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/replace/replace.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/replace/replace.go @@ -18,7 +18,6 @@ package replace import ( "fmt" - "io/ioutil" "net/url" "os" "path/filepath" @@ -331,7 +330,7 @@ func (o *ReplaceOptions) forceReplace() error { stdinInUse := false for i, filename := range o.DeleteOptions.FilenameOptions.Filenames { if filename == "-" { - tempDir, err := ioutil.TempDir("", "kubectl_replace_") + tempDir, err := os.MkdirTemp("", "kubectl_replace_") if err != nil { return err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go index b2f7b8327bc..a9482d8d0c1 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_history_test.go @@ -18,7 +18,7 @@ package rollout import ( "bytes" - "io/ioutil" + "io" "net/http" "testing" @@ -80,7 +80,7 @@ func TestRolloutHistory(t *testing.T) { case p == "/namespaces/test/deployments/foo" && m == "GET": responseDeployment := &appsv1.Deployment{} responseDeployment.Name = "foo" - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -176,12 +176,12 @@ func TestMultipleResourceRolloutHistory(t *testing.T) { case p == "/namespaces/test/deployments/foo" && m == "GET": responseDeployment := &appsv1.Deployment{} responseDeployment.Name = "foo" - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil case p == "/namespaces/test/deployments/bar" && m == "GET": responseDeployment := &appsv1.Deployment{} responseDeployment.Name = "bar" - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -259,7 +259,7 @@ func TestRolloutHistoryWithOutput(t *testing.T) { case p == "/namespaces/test/deployments/foo" && m == "GET": responseDeployment := &appsv1.Deployment{} responseDeployment.Name = "foo" - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go index 589528accb5..47e36931e7b 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go @@ -18,7 +18,7 @@ package rollout import ( "bytes" - "io/ioutil" + "io" "net/http" "testing" @@ -51,7 +51,7 @@ func TestRolloutPause(t *testing.T) { case p == "/namespaces/test/deployments/nginx-deployment" && (m == "GET" || m == "PATCH"): responseDeployment := &appsv1.Deployment{} responseDeployment.Name = deploymentName - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go index f1dfc86f5e1..5dc47d21a8f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_restart_test.go @@ -18,7 +18,7 @@ package rollout import ( "bytes" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -50,7 +50,7 @@ func TestRolloutRestartOne(t *testing.T) { case p == "/namespaces/test/deployments/nginx-deployment" && (m == "GET" || m == "PATCH"): responseDeployment := &appsv1.Deployment{} responseDeployment.Name = deploymentName - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -88,7 +88,7 @@ func TestRolloutRestartSelectorNone(t *testing.T) { case p == "/namespaces/test/deployments" && m == "GET" && q.Get("labelSelector") == labelSelector: // Return an empty list responseDeployments := &appsv1.DeploymentList{} - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployments)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployments)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) @@ -135,7 +135,7 @@ func TestRolloutRestartSelectorMany(t *testing.T) { // Return the list of 2 deployments responseDeployments := &appsv1.DeploymentList{} responseDeployments.Items = []appsv1.Deployment{firstDeployment, secondDeployment} - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployments)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployments)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil case (p == "/namespaces/test/deployments/nginx-deployment-1" || p == "/namespaces/test/deployments/nginx-deployment-2") && m == "PATCH": // Pick deployment based on path @@ -143,7 +143,7 @@ func TestRolloutRestartSelectorMany(t *testing.T) { if strings.HasSuffix(p, "nginx-deployment-2") { responseDeployment = secondDeployment } - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, &responseDeployment)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, &responseDeployment)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go index 83c8b1412ab..5f3fa2456db 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/rollout/rollout_status_test.go @@ -18,7 +18,7 @@ package rollout import ( "bytes" - "io/ioutil" + "io" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/watch" @@ -50,7 +50,7 @@ func TestRolloutStatus(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { dep := &appsv1.Deployment{} dep.Name = deploymentName - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil }), } @@ -106,7 +106,7 @@ func TestRolloutStatusWithSelector(t *testing.T) { dep.Name = deploymentName dep.Labels = make(map[string]string) dep.Labels["app"] = "api" - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil }), } @@ -163,7 +163,7 @@ func TestRolloutStatusWatchDisabled(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { dep := &appsv1.Deployment{} dep.Name = deploymentName - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil }), } @@ -218,7 +218,7 @@ func TestRolloutStatusWatchDisabledUnavailable(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { dep := &appsv1.Deployment{} dep.Name = deploymentName - body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, dep)))) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil }), } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/run/run_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/run/run_test.go index 0d3491a37bb..6b0f7804632 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/run/run_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/run/run_test.go @@ -19,7 +19,7 @@ package run import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "os" "reflect" @@ -180,7 +180,7 @@ func TestRunArgsFollowDashRules(t *testing.T) { } return &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewBuffer([]byte("{}"))), + Body: io.NopCloser(bytes.NewBuffer([]byte("{}"))), }, nil }), } @@ -338,7 +338,7 @@ func TestGenerateService(t *testing.T) { case test.expectPOST && m == "POST" && p == "/namespaces/test/services": sawPOST = true body := cmdtesting.ObjBody(codec, &test.service) - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -579,7 +579,7 @@ func TestExpose(t *testing.T) { body := cmdtesting.ObjBody(codec, pod) return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil case m == "POST" && p == "/namespaces/test/services": - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_env_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_env_test.go index eb6454edfbf..469dde8c6ac 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_env_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_env_test.go @@ -18,7 +18,7 @@ package set import ( "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -498,7 +498,7 @@ func TestSetEnvRemote(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } @@ -690,7 +690,7 @@ func TestSetEnvFromResource(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } @@ -798,7 +798,7 @@ func TestSetEnvRemoteWithSpecificContainers(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_image_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_image_test.go index 83089294b57..e97bc198ea6 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_image_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_image_test.go @@ -18,7 +18,7 @@ package set import ( "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -636,7 +636,7 @@ func TestSetImageRemote(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } @@ -748,7 +748,7 @@ func TestSetImageRemoteWithSpecificContainers(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_resources_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_resources_test.go index c462476928f..aca74c85b60 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_resources_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_resources_test.go @@ -18,7 +18,7 @@ package set import ( "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -482,7 +482,7 @@ func TestSetResourcesRemote(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } @@ -588,7 +588,7 @@ func TestSetResourcesRemoteWithSpecificContainers(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_serviceaccount_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_serviceaccount_test.go index 064b074103d..ed3233f784b 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/set/set_serviceaccount_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/set/set_serviceaccount_test.go @@ -19,7 +19,6 @@ package set import ( "fmt" "io" - "io/ioutil" "net/http" "testing" @@ -333,7 +332,7 @@ func TestSetServiceAccountRemote(t *testing.T) { if err != nil { return nil, err } - bytes, err := ioutil.ReadAll(stream) + bytes, err := io.ReadAll(stream) if err != nil { return nil, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go index 02fd13be92f..79baacda1a7 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/taint/taint_test.go @@ -17,7 +17,7 @@ limitations under the License. package taint import ( - "io/ioutil" + "io" "net/http" "reflect" "strings" @@ -263,7 +263,7 @@ func TestTaint(t *testing.T) { return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, oldNode)}, nil case m.isFor("PATCH", "/nodes/node-name"): tainted = true - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("%s: unexpected error: %v", test.description, err) } @@ -289,7 +289,7 @@ func TestTaint(t *testing.T) { return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, newNode)}, nil case m.isFor("PUT", "/nodes/node-name"): tainted = true - data, err := ioutil.ReadAll(req.Body) + data, err := io.ReadAll(req.Body) if err != nil { t.Fatalf("%s: unexpected error: %v", test.description, err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/testing/fake.go b/staging/src/k8s.io/kubectl/pkg/cmd/testing/fake.go index ebbee905bf5..d52d52b9848 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/testing/fake.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/testing/fake.go @@ -19,7 +19,6 @@ package testing import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "time" @@ -428,7 +427,7 @@ type TestFactory struct { func NewTestFactory() *TestFactory { // specify an optionalClientConfig to explicitly use in testing // to avoid polluting an existing user config. - tmpFile, err := ioutil.TempFile(os.TempDir(), "cmdtests_temp") + tmpFile, err := os.CreateTemp(os.TempDir(), "cmdtests_temp") if err != nil { panic(fmt.Sprintf("unable to create a fake client config: %v", err)) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/testing/util.go b/staging/src/k8s.io/kubectl/pkg/cmd/testing/util.go index 960fb6e13c7..09913a04461 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/testing/util.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/testing/util.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "net/http" "testing" @@ -55,15 +54,15 @@ func DefaultClientConfig() *restclient.Config { } func ObjBody(codec runtime.Codec, obj runtime.Object) io.ReadCloser { - return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj)))) + return io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj)))) } func BytesBody(bodyBytes []byte) io.ReadCloser { - return ioutil.NopCloser(bytes.NewReader(bodyBytes)) + return io.NopCloser(bytes.NewReader(bodyBytes)) } func StringBody(body string) io.ReadCloser { - return ioutil.NopCloser(bytes.NewReader([]byte(body))) + return io.NopCloser(bytes.NewReader([]byte(body))) } func TestData() (*corev1.PodList, *corev1.ServiceList, *corev1.ReplicationControllerList) { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go index dca44b83f24..2fcba725f23 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_node_test.go @@ -19,7 +19,7 @@ package top import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "reflect" "strings" @@ -57,9 +57,9 @@ func TestTopNodeAllMetricsFrom(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil case p == expectedNodePath && m == "GET": return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, nodes)}, nil default: @@ -124,9 +124,9 @@ func TestTopNodeWithNameMetricsFrom(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil case p == expectedNodePath && m == "GET": return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &expectedNode)}, nil default: @@ -201,9 +201,9 @@ func TestTopNodeWithLabelSelectorMetricsFrom(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m, _ := req.URL.Path, req.Method, req.URL.RawQuery; { case p == "/api": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil case p == expectedNodePath && m == "GET": return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &expectedNodes)}, nil default: @@ -278,9 +278,9 @@ func TestTopNodeWithSortByCpuMetricsFrom(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api": - return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil case p == expectedNodePath && m == "GET": return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &expectedNodes)}, nil default: @@ -364,9 +364,9 @@ func TestTopNodeWithSortByMemoryMetricsFrom(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/api": - return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil case p == expectedNodePath && m == "GET": return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &expectedNodes)}, nil default: diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go index e6a0096deb7..e9150eac94f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_pod_test.go @@ -18,7 +18,7 @@ package top import ( "bytes" - "io/ioutil" + "io" "net/http" "net/url" "reflect" @@ -215,9 +215,9 @@ func TestTopPod(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p := req.URL.Path; { case p == "/api": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil default: t.Fatalf("%s: unexpected request: %#v\nGot URL: %#v", testCase.name, req, req.URL) @@ -346,9 +346,9 @@ func TestTopPodNoResourcesFound(t *testing.T) { Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p := req.URL.Path; { case p == "/api": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apibody)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apibody)))}, nil case p == "/apis": - return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil + return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(apisbodyWithMetrics)))}, nil case p == "/api/v1/namespaces/"+testNS+"/pods": // Top Pod calls this endpoint to check if there are pods whenever it gets no metrics, // so we need to return no pods for this test scenario diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_test.go index 4f8a03f5978..e82ac3752df 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/top/top_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/top/top_test.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "time" "testing" @@ -50,7 +49,7 @@ func marshallBody(metrics interface{}) (io.ReadCloser, error) { if err != nil { return nil, err } - return ioutil.NopCloser(bytes.NewReader(result)), nil + return io.NopCloser(bytes.NewReader(result)), nil } func testNodeV1beta1MetricsData() (*metricsv1beta1api.NodeMetricsList, *v1.NodeList) { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor.go index 1a31d68148d..f7601887e78 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor.go @@ -19,7 +19,6 @@ package editor import ( "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -156,7 +155,7 @@ func (e Editor) LaunchTempFile(prefix, suffix string, r io.Reader) ([]byte, stri if err := e.Launch(path); err != nil { return nil, path, err } - bytes, err := ioutil.ReadFile(path) + bytes, err := os.ReadFile(path) return bytes, path, err } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor_test.go index 1ff23aaa238..3e38a440828 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/util/editor/editor_test.go @@ -18,7 +18,6 @@ package editor import ( "bytes" - "io/ioutil" "os" "reflect" "strings" @@ -51,7 +50,7 @@ func TestEditor(t *testing.T) { t.Fatalf("no temp file: %s", path) } defer os.Remove(path) - if disk, err := ioutil.ReadFile(path); err != nil || !bytes.Equal(contents, disk) { + if disk, err := os.ReadFile(path); err != nil || !bytes.Equal(contents, disk) { t.Errorf("unexpected file on disk: %v %s", err, string(disk)) } if !bytes.Equal(contents, []byte(testStr)) { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers_test.go index 2c436e6e138..a024f3f3435 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/util/helpers_test.go @@ -19,7 +19,6 @@ package util import ( goerrors "errors" "fmt" - "io/ioutil" "net/http" "os" "strings" @@ -462,7 +461,7 @@ func testCheckError(t *testing.T, tests []checkErrTestCase) { func TestDumpReaderToFile(t *testing.T) { testString := "TEST STRING" - tempFile, err := ioutil.TempFile(os.TempDir(), "hlpers_test_dump_") + tempFile, err := os.CreateTemp(os.TempDir(), "hlpers_test_dump_") if err != nil { t.Errorf("unexpected error setting up a temporary file %v", err) } @@ -477,7 +476,7 @@ func TestDumpReaderToFile(t *testing.T) { if err != nil { t.Errorf("error in DumpReaderToFile: %v", err) } - data, err := ioutil.ReadFile(tempFile.Name()) + data, err := os.ReadFile(tempFile.Name()) if err != nil { t.Errorf("error when reading %s: %v", tempFile.Name(), err) } diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go index ae0cd59a4be..0e85e09ed1d 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go @@ -17,7 +17,7 @@ limitations under the License. package wait import ( - "io/ioutil" + "io" "strings" "testing" "time" @@ -963,7 +963,7 @@ func TestWaitForCondition(t *testing.T) { Timeout: test.timeout, Printer: printers.NewDiscardingPrinter(), - ConditionFn: ConditionalWait{conditionName: "the-condition", conditionStatus: "status-value", errOut: ioutil.Discard}.IsConditionMet, + ConditionFn: ConditionalWait{conditionName: "the-condition", conditionStatus: "status-value", errOut: io.Discard}.IsConditionMet, IOStreams: genericclioptions.NewTestIOStreamsDiscard(), } err := o.RunWait() @@ -1181,7 +1181,7 @@ func TestWaitForDifferentJSONPathExpression(t *testing.T) { ConditionFn: JSONPathWait{ jsonPathCondition: test.jsonPathCond, jsonPathParser: j, - errOut: ioutil.Discard}.IsJSONPathConditionMet, + errOut: io.Discard}.IsJSONPathConditionMet, IOStreams: genericclioptions.NewTestIOStreamsDiscard(), } @@ -1444,7 +1444,7 @@ func TestWaitForJSONPathCondition(t *testing.T) { Printer: printers.NewDiscardingPrinter(), ConditionFn: JSONPathWait{ jsonPathCondition: test.jsonPathCond, - jsonPathParser: j, errOut: ioutil.Discard}.IsJSONPathConditionMet, + jsonPathParser: j, errOut: io.Discard}.IsJSONPathConditionMet, IOStreams: genericclioptions.NewTestIOStreamsDiscard(), } diff --git a/staging/src/k8s.io/kubectl/pkg/proxy/proxy_server_test.go b/staging/src/k8s.io/kubectl/pkg/proxy/proxy_server_test.go index 7055b751d54..310ee8b40fa 100644 --- a/staging/src/k8s.io/kubectl/pkg/proxy/proxy_server_test.go +++ b/staging/src/k8s.io/kubectl/pkg/proxy/proxy_server_test.go @@ -18,7 +18,7 @@ package proxy import ( "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -361,12 +361,12 @@ func TestFileServing(t *testing.T) { fname = "test.txt" data = "This is test data" ) - dir, err := ioutil.TempDir("", "data") + dir, err := os.MkdirTemp("", "data") if err != nil { t.Fatalf("error creating tmp dir: %v", err) } defer os.RemoveAll(dir) - if err := ioutil.WriteFile(filepath.Join(dir, fname), []byte(data), 0755); err != nil { + if err := os.WriteFile(filepath.Join(dir, fname), []byte(data), 0755); err != nil { t.Fatalf("error writing tmp file: %v", err) } @@ -385,7 +385,7 @@ func TestFileServing(t *testing.T) { if res.StatusCode != http.StatusOK { t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK) } - b, err := ioutil.ReadAll(res.Body) + b, err := io.ReadAll(res.Body) if err != nil { t.Fatalf("error reading resp body: %v", err) } @@ -402,7 +402,7 @@ func newProxy(target *url.URL) http.Handler { func TestAPIRequests(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -487,7 +487,7 @@ func TestPathHandling(t *testing.T) { if err != nil { t.Fatalf("%#v: %v", tt, err) } - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() if err != nil { t.Fatalf("%#v: %v", tt, err) diff --git a/staging/src/k8s.io/kubectl/pkg/util/completion/completion.go b/staging/src/k8s.io/kubectl/pkg/util/completion/completion.go index 154c5e68591..9a1b0ed99cc 100644 --- a/staging/src/k8s.io/kubectl/pkg/util/completion/completion.go +++ b/staging/src/k8s.io/kubectl/pkg/util/completion/completion.go @@ -19,7 +19,7 @@ package completion import ( "bytes" "fmt" - "io/ioutil" + "io" "os" "strings" "time" @@ -169,7 +169,7 @@ func CompGetContainers(f cmdutil.Factory, cmd *cobra.Command, podName string, to // which begin with `toComplete`. func CompGetFromTemplate(template *string, f cmdutil.Factory, namespace string, cmd *cobra.Command, args []string, toComplete string) []string { buf := new(bytes.Buffer) - streams := genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: ioutil.Discard} + streams := genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: io.Discard} o := get.NewGetOptions("kubectl", streams) // Get the list of names of the specified resource @@ -259,7 +259,7 @@ func ListUsersInConfig(toComplete string) []string { // compGetResourceList returns the list of api resources which begin with `toComplete`. func compGetResourceList(restClientGetter genericclioptions.RESTClientGetter, cmd *cobra.Command, toComplete string) []string { buf := new(bytes.Buffer) - streams := genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: ioutil.Discard} + streams := genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: io.Discard} o := apiresources.NewAPIResourceOptions(streams) o.Complete(restClientGetter, cmd, nil)