Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup deprecations #1250

Merged
merged 7 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
- prealloc
- predeclared
- promlinter
- staticcheck
- tagliatelle
- typecheck
- unused
Expand Down Expand Up @@ -66,7 +67,6 @@ linters:
# - rowserrcheck
# - scopelint
# - sqlclosecheck
# - staticcheck
# - stylecheck
# - testpackage
# - thelper
Expand All @@ -77,6 +77,8 @@ linters:
# - wrapcheck
# - wsl
linters-settings:
staticcheck:
checks: ["SA1019"]
gocritic:
enabled-checks:
# Diagnostic
Expand Down
11 changes: 7 additions & 4 deletions cmd/crictl/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ var runtimeAttachCommand = &cli.Command{
return err
}

ctx, cancel := context.WithCancel(c.Context)
defer cancel()

var opts = attachOptions{
id: id,
tty: c.Bool("tty"),
stdin: c.Bool("stdin"),
}
if err = Attach(runtimeClient, opts); err != nil {
if err = Attach(ctx, runtimeClient, opts); err != nil {
return fmt.Errorf("attaching running container failed: %w", err)

}
Expand All @@ -73,7 +76,7 @@ var runtimeAttachCommand = &cli.Command{
}

// Attach sends an AttachRequest to server, and parses the returned AttachResponse
func Attach(client internalapi.RuntimeService, opts attachOptions) error {
func Attach(ctx context.Context, client internalapi.RuntimeService, opts attachOptions) error {
if opts.id == "" {
return fmt.Errorf("ID cannot be empty")

Expand All @@ -86,7 +89,7 @@ func Attach(client internalapi.RuntimeService, opts attachOptions) error {
Stderr: !opts.tty,
}
logrus.Debugf("AttachRequest: %v", request)
r, err := client.Attach(context.TODO(), request)
r, err := client.Attach(ctx, request)
logrus.Debugf("AttachResponse: %v", r)
if err != nil {
return err
Expand All @@ -106,5 +109,5 @@ func Attach(client internalapi.RuntimeService, opts attachOptions) error {
}

logrus.Debugf("Attach URL: %v", URL)
return stream(opts.stdin, opts.tty, URL)
return stream(ctx, opts.stdin, opts.tty, URL)
}
20 changes: 12 additions & 8 deletions cmd/crictl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ var runtimeExecCommand = &cli.Command{
return fmt.Errorf("execing command in container synchronously: %w", err)
}
if exitCode != 0 {
return cli.NewExitError("non-zero exit code", exitCode)
return cli.Exit("non-zero exit code", exitCode)
}
return nil
}
err = Exec(runtimeClient, opts)

ctx, cancel := context.WithCancel(c.Context)
defer cancel()

err = Exec(ctx, runtimeClient, opts)
if err != nil {
return fmt.Errorf("execing command in container: %w", err)
}
Expand Down Expand Up @@ -124,7 +128,7 @@ func ExecSync(client internalapi.RuntimeService, opts execOptions) (int, error)
}

// Exec sends an ExecRequest to server, and parses the returned ExecResponse
func Exec(client internalapi.RuntimeService, opts execOptions) error {
func Exec(ctx context.Context, client internalapi.RuntimeService, opts execOptions) error {
request := &pb.ExecRequest{
ContainerId: opts.id,
Cmd: opts.cmd,
Expand All @@ -135,7 +139,7 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
}

logrus.Debugf("ExecRequest: %v", request)
r, err := client.Exec(context.TODO(), request)
r, err := client.Exec(ctx, request)
logrus.Debugf("ExecResponse: %v", r)
if err != nil {
return err
Expand All @@ -156,10 +160,10 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
}

logrus.Debugf("Exec URL: %v", URL)
return stream(opts.stdin, opts.tty, URL)
return stream(ctx, opts.stdin, opts.tty, URL)
}

func stream(in, tty bool, url *url.URL) error {
func stream(ctx context.Context, in, tty bool, url *url.URL) error {
executor, err := remoteclient.NewSPDYExecutor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}, "POST", url)
if err != nil {
return err
Expand All @@ -176,7 +180,7 @@ func stream(in, tty bool, url *url.URL) error {
}
logrus.Debugf("StreamOptions: %v", streamOptions)
if !tty {
return executor.Stream(streamOptions)
return executor.StreamWithContext(ctx, streamOptions)
} else {
var detachKeys []byte
detachKeys, err = mobyterm.ToBytes(detachSequence)
Expand All @@ -198,5 +202,5 @@ func stream(in, tty bool, url *url.URL) error {
return fmt.Errorf("input is not a terminal")
}
streamOptions.TerminalSizeQueue = t.MonitorSize(t.GetSize())
return t.Safe(func() error { return executor.Stream(streamOptions) })
return t.Safe(func() error { return executor.StreamWithContext(ctx, streamOptions) })
}
5 changes: 4 additions & 1 deletion cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"

errorUtils "k8s.io/apimachinery/pkg/util/errors"
internalapi "k8s.io/cri-api/pkg/apis"
Expand Down Expand Up @@ -509,6 +511,7 @@ func ListPodSandboxes(client internalapi.RuntimeService, opts listOptions) error
columnPodRuntime,
})
}
c := cases.Title(language.Und)
for _, pod := range r {
if opts.quiet {
fmt.Printf("%s\n", pod.Id)
Expand Down Expand Up @@ -564,7 +567,7 @@ func ListPodSandboxes(client internalapi.RuntimeService, opts listOptions) error
}
}
fmt.Printf("%s: %s\n",
strings.Title(strings.ToLower(columnPodRuntime)),
c.String(columnPodRuntime),
getSandboxesRuntimeHandler(pod))

fmt.Println()
Expand Down
13 changes: 9 additions & 4 deletions cmd/crictl/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func builtinTmplFuncs() template.FuncMap {
t := cases.Title(language.Und)
l := cases.Lower(language.Und)
u := cases.Upper(language.Und)
return template.FuncMap{
"json": jsonBuiltinTmplFunc,
"title": strings.Title,
"lower": strings.ToLower,
"upper": strings.ToUpper,
"title": t.String,
"lower": l.String,
"upper": u.String,
}
}

Expand Down
9 changes: 4 additions & 5 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ import (
"sync"
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/jsonpb" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
cri "k8s.io/cri-api/pkg/apis"
internalapi "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -389,9 +388,9 @@ func getRepoImage(imageClient internalapi.ImageManagerService, image string) (st

func handleDisplay(
ctx context.Context,
client cri.RuntimeService,
client internalapi.RuntimeService,
watch bool,
displayFunc func(context.Context, cri.RuntimeService) error,
displayFunc func(context.Context, internalapi.RuntimeService) error,
) error {
if !watch {
return displayFunc(ctx, client)
Expand Down
5 changes: 1 addition & 4 deletions cmd/critest/cri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/onsi/ginkgo/v2"
ginkgotypes "github.com/onsi/ginkgo/v2/types"
Expand Down Expand Up @@ -55,7 +53,6 @@ var (

func init() {
framework.RegisterFlags()
rand.Seed(time.Now().UnixNano())
getConfigFromFile()
}

Expand Down Expand Up @@ -100,7 +97,7 @@ func generateTempTestName() (string, error) {
suffix[i] = letterBytes[rand.Intn(len(letterBytes))]
}

dir, err := ioutil.TempDir("", "cri-test")
dir, err := os.MkdirTemp("", "cri-test")
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
golang.org/x/net v0.15.0
golang.org/x/sys v0.12.0
golang.org/x/term v0.12.0
golang.org/x/text v0.13.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.0.0
k8s.io/apimachinery v0.0.0
Expand Down Expand Up @@ -83,7 +84,6 @@ require (
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
3 changes: 0 additions & 3 deletions pkg/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package benchmark

import (
"math/rand"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -30,7 +28,6 @@ import (
// If a "report directory" is specified, one or more JUnit test reports will be
// generated in this directory.
func TestPerformance(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
RegisterFailHandler(Fail)
RunSpecs(t, "Benchmark Test Suite")
}
4 changes: 2 additions & 2 deletions pkg/benchmark/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package benchmark
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -165,7 +165,7 @@ func (lbrm *LifecycleBenchmarksResultsManager) WriteResultsFile(filepath string)

data, err := json.MarshalIndent(lbrm.resultsSet, "", " ")
if err == nil {
err = ioutil.WriteFile(filepath, data, 0644)
err = os.WriteFile(filepath, data, 0644)
if err != nil {
return fmt.Errorf("Failed to write benchmarks results to file: %v", filepath)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/common/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package common

import (
"fmt"
"io/ioutil"
"os"
gofilepath "path/filepath"
"strconv"
Expand All @@ -41,7 +40,7 @@ type Config struct {
// ReadConfig reads from a file with the given name and returns a config or
// an error if the file was unable to be parsed.
func ReadConfig(filepath string) (*Config, error) {
data, err := ioutil.ReadFile(filepath)
data, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,7 +74,7 @@ func WriteConfig(c *Config, filepath string) error {
if err := os.MkdirAll(gofilepath.Dir(filepath), 0o755); err != nil {
return err
}
return ioutil.WriteFile(filepath, data, 0o644)
return os.WriteFile(filepath, data, 0o644)
}

// Extracts config options from the yaml data which is loaded from file
Expand Down
3 changes: 1 addition & 2 deletions pkg/validate/apparmor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"time"
Expand Down Expand Up @@ -148,7 +147,7 @@ func checkContainerApparmor(rc internalapi.RuntimeService, containerID string, s
}

func loadTestProfiles() error {
f, err := ioutil.TempFile("/tmp", "apparmor")
f, err := os.CreateTemp("/tmp", "apparmor")
if err != nil {
return fmt.Errorf("open temp file: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/validate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -490,7 +489,7 @@ func verifyExecSyncOutput(c internalapi.RuntimeService, containerID string, comm

// createHostPath creates the hostPath and flagFile for volume.
func createHostPath(podID string) (string, string) {
hostPath, err := ioutil.TempDir("", "test"+podID)
hostPath, err := os.MkdirTemp("", "test"+podID)
framework.ExpectNoError(err, "failed to create TempDir %q: %v", hostPath, err)

flagFile := "testVolume.file"
Expand Down
5 changes: 2 additions & 3 deletions pkg/validate/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package validate

import (
"context"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -94,7 +93,7 @@ var _ = framework.KubeDescribe("Container Mount Propagation", func() {
execSyncContainer(rc, containerID, command)

By("check whether containerMntPoint contains file or dir in host")
fileInfo, err := ioutil.ReadDir(containerMntPoint)
fileInfo, err := os.ReadDir(containerMntPoint)
framework.ExpectNoError(err, "failed to ReadDir %q in Host", containerMntPoint)

switch propagation {
Expand Down Expand Up @@ -170,7 +169,7 @@ var _ = framework.KubeDescribe("Container OOM", func() {

// createHostPath creates the hostPath for mount propagation test.
func createHostPathForMountPropagation(podID string, propagationOpt runtimeapi.MountPropagation) (string, string, string, func()) {
hostPath, err := ioutil.TempDir("", "test"+podID)
hostPath, err := os.MkdirTemp("", "test"+podID)
framework.ExpectNoError(err, "failed to create TempDir %q: %v", hostPath, err)

mntSource := filepath.Join(hostPath, "mnt")
Expand Down
3 changes: 0 additions & 3 deletions pkg/validate/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package validate

import (
"math/rand"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -31,7 +29,6 @@ import (
// generated in this directory.
// This function is called on each Ginkgo node in parallel mode.
func TestE2ECRI(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
RegisterFailHandler(Fail)
RunSpecs(t, "E2ECRI Suite")
}
Loading