From 584d994133132ddbfb5da300e634049e1d3198fa Mon Sep 17 00:00:00 2001 From: ahrtr Date: Mon, 15 Nov 2021 13:26:49 +0800 Subject: [PATCH] replace all the deprecated ioutil with io and os --- test/images/agnhost/VERSION | 2 +- test/images/agnhost/audit-proxy/main.go | 4 ++-- .../converter/framework.go | 4 ++-- test/images/agnhost/dns/common.go | 4 ++-- test/images/agnhost/guestbook/guestbook.go | 4 ++-- test/images/agnhost/mounttest/mt.go | 7 +++---- test/images/agnhost/net/main.go | 4 ++-- test/images/agnhost/netexec/netexec.go | 5 ++--- test/images/agnhost/nettest/nettest.go | 4 ++-- test/images/agnhost/no-snat-test-proxy/main.go | 4 ++-- test/images/agnhost/no-snat-test/main.go | 4 ++-- .../agnhost/openidmetadata/openidmetadata.go | 3 +-- test/images/agnhost/webhook/main.go | 4 ++-- test/images/apparmor-loader/VERSION | 2 +- test/images/apparmor-loader/loader.go | 18 +++++++++++------- test/images/pets/peer-finder/VERSION | 2 +- test/images/pets/peer-finder/peer-finder.go | 6 +++--- 17 files changed, 41 insertions(+), 40 deletions(-) diff --git a/test/images/agnhost/VERSION b/test/images/agnhost/VERSION index 90177b756a6..8ad350cc299 100644 --- a/test/images/agnhost/VERSION +++ b/test/images/agnhost/VERSION @@ -1 +1 @@ -2.36 +2.37 diff --git a/test/images/agnhost/audit-proxy/main.go b/test/images/agnhost/audit-proxy/main.go index 99ecb81e9b0..e6ac074f111 100644 --- a/test/images/agnhost/audit-proxy/main.go +++ b/test/images/agnhost/audit-proxy/main.go @@ -17,7 +17,7 @@ limitations under the License. package auditproxy import ( - "io/ioutil" + "io" "log" "net/http" "os" @@ -57,7 +57,7 @@ func main(cmd *cobra.Command, args []string) { } func handler(w http.ResponseWriter, req *http.Request) { - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { log.Printf("could not read request body: %v", err) w.WriteHeader(http.StatusInternalServerError) diff --git a/test/images/agnhost/crd-conversion-webhook/converter/framework.go b/test/images/agnhost/crd-conversion-webhook/converter/framework.go index 7fb6aa4e04a..b8c7ea87cd4 100644 --- a/test/images/agnhost/crd-conversion-webhook/converter/framework.go +++ b/test/images/agnhost/crd-conversion-webhook/converter/framework.go @@ -18,7 +18,7 @@ package converter import ( "fmt" - "io/ioutil" + "io" "net/http" "strings" @@ -117,7 +117,7 @@ func doConversionV1(convertRequest *v1.ConversionRequest, convert convertFunc) * func serve(w http.ResponseWriter, r *http.Request, convert convertFunc) { var body []byte if r.Body != nil { - if data, err := ioutil.ReadAll(r.Body); err == nil { + if data, err := io.ReadAll(r.Body); err == nil { body = data } } diff --git a/test/images/agnhost/dns/common.go b/test/images/agnhost/dns/common.go index cf2861e7b7f..721dcbea166 100644 --- a/test/images/agnhost/dns/common.go +++ b/test/images/agnhost/dns/common.go @@ -18,7 +18,7 @@ package dns import ( "fmt" - "io/ioutil" + "os" "strings" "github.com/spf13/cobra" @@ -66,7 +66,7 @@ func printHostsFile(cmd *cobra.Command, args []string) { } func readFile(fileName string) string { - fileData, err := ioutil.ReadFile(fileName) + fileData, err := os.ReadFile(fileName) if err != nil { panic(err) } diff --git a/test/images/agnhost/guestbook/guestbook.go b/test/images/agnhost/guestbook/guestbook.go index f4e0df05aee..a6a98b13910 100644 --- a/test/images/agnhost/guestbook/guestbook.go +++ b/test/images/agnhost/guestbook/guestbook.go @@ -19,7 +19,7 @@ package guestbook import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -278,7 +278,7 @@ func dialHTTP(request, hostPort string) (string, error) { defer transport.CloseIdleConnections() if err == nil { defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err == nil { return string(body), nil } diff --git a/test/images/agnhost/mounttest/mt.go b/test/images/agnhost/mounttest/mt.go index d5e15908fdc..5629e0209e5 100644 --- a/test/images/agnhost/mounttest/mt.go +++ b/test/images/agnhost/mounttest/mt.go @@ -18,7 +18,6 @@ package mounttest import ( "fmt" - "io/ioutil" "os" "time" @@ -146,7 +145,7 @@ func readFileContent(path string) error { return nil } - contentBytes, err := ioutil.ReadFile(path) + contentBytes, err := os.ReadFile(path) if err != nil { fmt.Printf("error reading file content for %q: %v\n", path, err) return err @@ -164,7 +163,7 @@ func readWriteNewFile(path string, perm os.FileMode) error { return nil } - err := ioutil.WriteFile(path, []byte(initialContent), perm) + err := os.WriteFile(path, []byte(initialContent), perm) if err != nil { fmt.Printf("error writing new file %q: %v\n", path, err) return err @@ -188,7 +187,7 @@ func testFileContent(filePath string, retryDuration int, breakOnExpectedContent retryTime := time.Second * time.Duration(retryDuration) for start := time.Now(); time.Since(start) < retryTime; time.Sleep(2 * time.Second) { - contentBytes, err = ioutil.ReadFile(filePath) + contentBytes, err = os.ReadFile(filePath) if err != nil { fmt.Printf("Error reading file %s: %v, retrying\n", filePath, err) continue diff --git a/test/images/agnhost/net/main.go b/test/images/agnhost/net/main.go index 71423e90aee..3e4a78500fe 100644 --- a/test/images/agnhost/net/main.go +++ b/test/images/agnhost/net/main.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -164,7 +164,7 @@ func handleRunRequest(w http.ResponseWriter, r *http.Request) { return } - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { http.Error(w, fmt.Sprintf("error reading body: %v", err), 400) return diff --git a/test/images/agnhost/netexec/netexec.go b/test/images/agnhost/netexec/netexec.go index 7362a59855b..b44a13d2fc9 100644 --- a/test/images/agnhost/netexec/netexec.go +++ b/test/images/agnhost/netexec/netexec.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net" "net/http" @@ -426,7 +425,7 @@ func dialHTTP(request string, addr net.Addr) (string, error) { defer transport.CloseIdleConnections() if err == nil { defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err == nil { return string(body), nil } @@ -524,7 +523,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { } defer file.Close() - f, err := ioutil.TempFile("/uploads", "upload") + f, err := os.CreateTemp("/uploads", "upload") if err != nil { result["error"] = "Unable to open file for write" bytes, err := json.Marshal(result) diff --git a/test/images/agnhost/nettest/nettest.go b/test/images/agnhost/nettest/nettest.go index d28bbe46539..600dd77feff 100644 --- a/test/images/agnhost/nettest/nettest.go +++ b/test/images/agnhost/nettest/nettest.go @@ -35,7 +35,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -349,7 +349,7 @@ func contactSingle(e string, state *State) { } defer resp.Body.Close() - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) if err != nil { state.Logf("Warning: unable to read response from '%v': '%v'", e, err) return diff --git a/test/images/agnhost/no-snat-test-proxy/main.go b/test/images/agnhost/no-snat-test-proxy/main.go index 1065ad579b4..c5b16718faf 100644 --- a/test/images/agnhost/no-snat-test-proxy/main.go +++ b/test/images/agnhost/no-snat-test-proxy/main.go @@ -18,7 +18,7 @@ package nosnatproxy import ( "fmt" - "io/ioutil" + "io" "net/http" "os" @@ -80,7 +80,7 @@ func checknosnat(w http.ResponseWriter, req *http.Request) { w.WriteHeader(500) fmt.Fprintf(w, "error querying %q, err: %v", url, err) } else { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { w.WriteHeader(500) fmt.Fprintf(w, "error reading body of response from %q, err: %v", url, err) diff --git a/test/images/agnhost/no-snat-test/main.go b/test/images/agnhost/no-snat-test/main.go index bfc520d5e5c..6d8b9c1959e 100644 --- a/test/images/agnhost/no-snat-test/main.go +++ b/test/images/agnhost/no-snat-test/main.go @@ -18,7 +18,7 @@ package nosnat import ( "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -141,7 +141,7 @@ func check(ip string, pip string, nip string) error { return err } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return err } diff --git a/test/images/agnhost/openidmetadata/openidmetadata.go b/test/images/agnhost/openidmetadata/openidmetadata.go index fe33656a4c2..29c76af0a29 100644 --- a/test/images/agnhost/openidmetadata/openidmetadata.go +++ b/test/images/agnhost/openidmetadata/openidmetadata.go @@ -21,7 +21,6 @@ package openidmetadata import ( "context" "fmt" - "io/ioutil" "log" "net" "net/http" @@ -163,7 +162,7 @@ func (k *claims) String() string { } func gettoken() (string, error) { - b, err := ioutil.ReadFile(tokenPath) + b, err := os.ReadFile(tokenPath) return string(b), err } diff --git a/test/images/agnhost/webhook/main.go b/test/images/agnhost/webhook/main.go index 3bb6fae3839..1caec1795be 100644 --- a/test/images/agnhost/webhook/main.go +++ b/test/images/agnhost/webhook/main.go @@ -19,7 +19,7 @@ package webhook import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "github.com/spf13/cobra" @@ -93,7 +93,7 @@ func delegateV1beta1AdmitToV1(f admitv1Func) admitv1beta1Func { func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) { var body []byte if r.Body != nil { - if data, err := ioutil.ReadAll(r.Body); err == nil { + if data, err := io.ReadAll(r.Body); err == nil { body = data } } diff --git a/test/images/apparmor-loader/VERSION b/test/images/apparmor-loader/VERSION index c068b2447cc..c239c60cba2 100644 --- a/test/images/apparmor-loader/VERSION +++ b/test/images/apparmor-loader/VERSION @@ -1 +1 @@ -1.4 +1.5 diff --git a/test/images/apparmor-loader/loader.go b/test/images/apparmor-loader/loader.go index d16177ae8e0..a6cf0d4ed56 100644 --- a/test/images/apparmor-loader/loader.go +++ b/test/images/apparmor-loader/loader.go @@ -21,7 +21,6 @@ import ( "bytes" "flag" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -118,7 +117,7 @@ func loadNewProfiles() (success bool, newProfiles []string) { success = true for _, dir := range dirs { - infos, err := ioutil.ReadDir(dir) + infos, err := os.ReadDir(dir) if err != nil { klog.Warningf("Error reading %s: %v", dir, err) success = false @@ -207,12 +206,17 @@ func loadProfiles(path string) error { // If the given fileinfo is a symlink, return the FileInfo of the target. Otherwise, return the // given fileinfo. -func resolveSymlink(basePath string, info os.FileInfo) (os.FileInfo, error) { - if info.Mode()&os.ModeSymlink == 0 { - // Not a symlink. - return info, nil +func resolveSymlink(basePath string, entry os.DirEntry) (os.FileInfo, error) { + if info, err := entry.Info(); err != nil { + return nil, fmt.Errorf("error getting the fileInfo: %v", err) + } else { + if info.Mode()&os.ModeSymlink == 0 { + // Not a symlink. + return info, nil + } } - fpath := filepath.Join(basePath, info.Name()) + + fpath := filepath.Join(basePath, entry.Name()) resolvedName, err := filepath.EvalSymlinks(fpath) if err != nil { return nil, fmt.Errorf("error resolving symlink %s: %v", fpath, err) diff --git a/test/images/pets/peer-finder/VERSION b/test/images/pets/peer-finder/VERSION index c239c60cba2..810ee4e91e2 100644 --- a/test/images/pets/peer-finder/VERSION +++ b/test/images/pets/peer-finder/VERSION @@ -1 +1 @@ -1.5 +1.6 diff --git a/test/images/pets/peer-finder/peer-finder.go b/test/images/pets/peer-finder/peer-finder.go index 6622a5c7421..9b2e90943b9 100644 --- a/test/images/pets/peer-finder/peer-finder.go +++ b/test/images/pets/peer-finder/peer-finder.go @@ -20,7 +20,7 @@ package main import ( "flag" "fmt" - "io/ioutil" + "io" "log" "net" "os" @@ -77,7 +77,7 @@ func shellOut(sendStdin, script string) { stdin.Write([]byte(sendStdin)) stdin.Close() - out, err := ioutil.ReadAll(stdout) + out, err := io.ReadAll(stdout) if err != nil { log.Fatalf("Failed to execute %v: %v, err: %v", script, string(out), err) } @@ -104,7 +104,7 @@ func main() { // If domain is not provided, try to get it from resolv.conf if *domain == "" { - resolvConfBytes, err := ioutil.ReadFile("/etc/resolv.conf") + resolvConfBytes, err := os.ReadFile("/etc/resolv.conf") resolvConf := string(resolvConfBytes) if err != nil { log.Fatal("Unable to read /etc/resolv.conf")