Skip to content

Commit b871c4f

Browse files
authored
Merge pull request #1250 from surik/deprecated-funcs-cleanup
Cleanup deprecations
2 parents 56c0336 + 544d479 commit b871c4f

36 files changed

+16007
-57
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ linters:
2121
- prealloc
2222
- predeclared
2323
- promlinter
24+
- staticcheck
2425
- tagliatelle
2526
- typecheck
2627
- unused
@@ -66,7 +67,6 @@ linters:
6667
# - rowserrcheck
6768
# - scopelint
6869
# - sqlclosecheck
69-
# - staticcheck
7070
# - stylecheck
7171
# - testpackage
7272
# - thelper
@@ -77,6 +77,8 @@ linters:
7777
# - wrapcheck
7878
# - wsl
7979
linters-settings:
80+
staticcheck:
81+
checks: ["SA1019"]
8082
gocritic:
8183
enabled-checks:
8284
# Diagnostic

cmd/crictl/attach.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ var runtimeAttachCommand = &cli.Command{
5959
return err
6060
}
6161

62+
ctx, cancel := context.WithCancel(c.Context)
63+
defer cancel()
64+
6265
var opts = attachOptions{
6366
id: id,
6467
tty: c.Bool("tty"),
6568
stdin: c.Bool("stdin"),
6669
}
67-
if err = Attach(runtimeClient, opts); err != nil {
70+
if err = Attach(ctx, runtimeClient, opts); err != nil {
6871
return fmt.Errorf("attaching running container failed: %w", err)
6972

7073
}
@@ -73,7 +76,7 @@ var runtimeAttachCommand = &cli.Command{
7376
}
7477

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

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

108111
logrus.Debugf("Attach URL: %v", URL)
109-
return stream(opts.stdin, opts.tty, URL)
112+
return stream(ctx, opts.stdin, opts.tty, URL)
110113
}

cmd/crictl/exec.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ var runtimeExecCommand = &cli.Command{
9191
return fmt.Errorf("execing command in container synchronously: %w", err)
9292
}
9393
if exitCode != 0 {
94-
return cli.NewExitError("non-zero exit code", exitCode)
94+
return cli.Exit("non-zero exit code", exitCode)
9595
}
9696
return nil
9797
}
98-
err = Exec(runtimeClient, opts)
98+
99+
ctx, cancel := context.WithCancel(c.Context)
100+
defer cancel()
101+
102+
err = Exec(ctx, runtimeClient, opts)
99103
if err != nil {
100104
return fmt.Errorf("execing command in container: %w", err)
101105
}
@@ -124,7 +128,7 @@ func ExecSync(client internalapi.RuntimeService, opts execOptions) (int, error)
124128
}
125129

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

137141
logrus.Debugf("ExecRequest: %v", request)
138-
r, err := client.Exec(context.TODO(), request)
142+
r, err := client.Exec(ctx, request)
139143
logrus.Debugf("ExecResponse: %v", r)
140144
if err != nil {
141145
return err
@@ -156,10 +160,10 @@ func Exec(client internalapi.RuntimeService, opts execOptions) error {
156160
}
157161

158162
logrus.Debugf("Exec URL: %v", URL)
159-
return stream(opts.stdin, opts.tty, URL)
163+
return stream(ctx, opts.stdin, opts.tty, URL)
160164
}
161165

162-
func stream(in, tty bool, url *url.URL) error {
166+
func stream(ctx context.Context, in, tty bool, url *url.URL) error {
163167
executor, err := remoteclient.NewSPDYExecutor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}, "POST", url)
164168
if err != nil {
165169
return err
@@ -176,7 +180,7 @@ func stream(in, tty bool, url *url.URL) error {
176180
}
177181
logrus.Debugf("StreamOptions: %v", streamOptions)
178182
if !tty {
179-
return executor.Stream(streamOptions)
183+
return executor.StreamWithContext(ctx, streamOptions)
180184
} else {
181185
var detachKeys []byte
182186
detachKeys, err = mobyterm.ToBytes(detachSequence)
@@ -198,5 +202,5 @@ func stream(in, tty bool, url *url.URL) error {
198202
return fmt.Errorf("input is not a terminal")
199203
}
200204
streamOptions.TerminalSizeQueue = t.MonitorSize(t.GetSize())
201-
return t.Safe(func() error { return executor.Stream(streamOptions) })
205+
return t.Safe(func() error { return executor.StreamWithContext(ctx, streamOptions) })
202206
}

cmd/crictl/sandbox.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/docker/go-units"
2929
"github.com/sirupsen/logrus"
3030
"github.com/urfave/cli/v2"
31+
"golang.org/x/text/cases"
32+
"golang.org/x/text/language"
3133

3234
errorUtils "k8s.io/apimachinery/pkg/util/errors"
3335
internalapi "k8s.io/cri-api/pkg/apis"
@@ -509,6 +511,7 @@ func ListPodSandboxes(client internalapi.RuntimeService, opts listOptions) error
509511
columnPodRuntime,
510512
})
511513
}
514+
c := cases.Title(language.Und)
512515
for _, pod := range r {
513516
if opts.quiet {
514517
fmt.Printf("%s\n", pod.Id)
@@ -564,7 +567,7 @@ func ListPodSandboxes(client internalapi.RuntimeService, opts listOptions) error
564567
}
565568
}
566569
fmt.Printf("%s: %s\n",
567-
strings.Title(strings.ToLower(columnPodRuntime)),
570+
c.String(columnPodRuntime),
568571
getSandboxesRuntimeHandler(pod))
569572

570573
fmt.Println()

cmd/crictl/templates.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23-
"strings"
2423
"text/template"
24+
25+
"golang.org/x/text/cases"
26+
"golang.org/x/text/language"
2527
)
2628

2729
func builtinTmplFuncs() template.FuncMap {
30+
t := cases.Title(language.Und)
31+
l := cases.Lower(language.Und)
32+
u := cases.Upper(language.Und)
2833
return template.FuncMap{
2934
"json": jsonBuiltinTmplFunc,
30-
"title": strings.Title,
31-
"lower": strings.ToLower,
32-
"upper": strings.ToUpper,
35+
"title": t.String,
36+
"lower": l.String,
37+
"upper": u.String,
3338
}
3439
}
3540

cmd/crictl/util.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ import (
3131
"sync"
3232
"time"
3333

34-
"github.com/golang/protobuf/jsonpb"
35-
"github.com/golang/protobuf/proto"
34+
"github.com/golang/protobuf/jsonpb" //nolint:staticcheck
35+
"github.com/golang/protobuf/proto" //nolint:staticcheck
3636
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
37-
cri "k8s.io/cri-api/pkg/apis"
3837
internalapi "k8s.io/cri-api/pkg/apis"
3938
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
4039
"sigs.k8s.io/yaml"
@@ -389,9 +388,9 @@ func getRepoImage(imageClient internalapi.ImageManagerService, image string) (st
389388

390389
func handleDisplay(
391390
ctx context.Context,
392-
client cri.RuntimeService,
391+
client internalapi.RuntimeService,
393392
watch bool,
394-
displayFunc func(context.Context, cri.RuntimeService) error,
393+
displayFunc func(context.Context, internalapi.RuntimeService) error,
395394
) error {
396395
if !watch {
397396
return displayFunc(ctx, client)

cmd/critest/cri_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ package main
1919
import (
2020
"flag"
2121
"fmt"
22-
"io/ioutil"
2322
"math/rand"
2423
"os"
2524
"os/exec"
2625
"path/filepath"
2726
"strings"
2827
"testing"
29-
"time"
3028

3129
"github.com/onsi/ginkgo/v2"
3230
ginkgotypes "github.com/onsi/ginkgo/v2/types"
@@ -55,7 +53,6 @@ var (
5553

5654
func init() {
5755
framework.RegisterFlags()
58-
rand.Seed(time.Now().UnixNano())
5956
getConfigFromFile()
6057
}
6158

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

103-
dir, err := ioutil.TempDir("", "cri-test")
100+
dir, err := os.MkdirTemp("", "cri-test")
104101
if err != nil {
105102
return "", err
106103
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
golang.org/x/net v0.15.0
2020
golang.org/x/sys v0.12.0
2121
golang.org/x/term v0.12.0
22+
golang.org/x/text v0.13.0
2223
gopkg.in/yaml.v3 v3.0.1
2324
k8s.io/api v0.0.0
2425
k8s.io/apimachinery v0.0.0
@@ -83,7 +84,6 @@ require (
8384
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
8485
golang.org/x/mod v0.12.0 // indirect
8586
golang.org/x/oauth2 v0.8.0 // indirect
86-
golang.org/x/text v0.13.0 // indirect
8787
golang.org/x/time v0.3.0 // indirect
8888
golang.org/x/tools v0.12.0 // indirect
8989
google.golang.org/appengine v1.6.7 // indirect

pkg/benchmark/benchmark.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ limitations under the License.
1717
package benchmark
1818

1919
import (
20-
"math/rand"
2120
"testing"
22-
"time"
2321

2422
. "github.com/onsi/ginkgo/v2"
2523
. "github.com/onsi/gomega"
@@ -30,7 +28,6 @@ import (
3028
// If a "report directory" is specified, one or more JUnit test reports will be
3129
// generated in this directory.
3230
func TestPerformance(t *testing.T) {
33-
rand.Seed(time.Now().UTC().UnixNano())
3431
RegisterFailHandler(Fail)
3532
RunSpecs(t, "Benchmark Test Suite")
3633
}

pkg/benchmark/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package benchmark
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"io/ioutil"
22+
"os"
2323
"time"
2424

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

166166
data, err := json.MarshalIndent(lbrm.resultsSet, "", " ")
167167
if err == nil {
168-
err = ioutil.WriteFile(filepath, data, 0644)
168+
err = os.WriteFile(filepath, data, 0644)
169169
if err != nil {
170170
return fmt.Errorf("Failed to write benchmarks results to file: %v", filepath)
171171
}

pkg/common/file.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package common
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
gofilepath "path/filepath"
2423
"strconv"
@@ -41,7 +40,7 @@ type Config struct {
4140
// ReadConfig reads from a file with the given name and returns a config or
4241
// an error if the file was unable to be parsed.
4342
func ReadConfig(filepath string) (*Config, error) {
44-
data, err := ioutil.ReadFile(filepath)
43+
data, err := os.ReadFile(filepath)
4544
if err != nil {
4645
return nil, err
4746
}
@@ -75,7 +74,7 @@ func WriteConfig(c *Config, filepath string) error {
7574
if err := os.MkdirAll(gofilepath.Dir(filepath), 0o755); err != nil {
7675
return err
7776
}
78-
return ioutil.WriteFile(filepath, data, 0o644)
77+
return os.WriteFile(filepath, data, 0o644)
7978
}
8079

8180
// Extracts config options from the yaml data which is loaded from file

pkg/validate/apparmor_linux.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"os/exec"
2625
"time"
@@ -148,7 +147,7 @@ func checkContainerApparmor(rc internalapi.RuntimeService, containerID string, s
148147
}
149148

150149
func loadTestProfiles() error {
151-
f, err := ioutil.TempFile("/tmp", "apparmor")
150+
f, err := os.CreateTemp("/tmp", "apparmor")
152151
if err != nil {
153152
return fmt.Errorf("open temp file: %w", err)
154153
}

pkg/validate/container.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"io/ioutil"
2524
"os"
2625
"path/filepath"
2726
"strings"
@@ -490,7 +489,7 @@ func verifyExecSyncOutput(c internalapi.RuntimeService, containerID string, comm
490489

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

496495
flagFile := "testVolume.file"

pkg/validate/container_linux.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package validate
1818

1919
import (
2020
"context"
21-
"io/ioutil"
2221
"os"
2322
"path"
2423
"path/filepath"
@@ -94,7 +93,7 @@ var _ = framework.KubeDescribe("Container Mount Propagation", func() {
9493
execSyncContainer(rc, containerID, command)
9594

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

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

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

176175
mntSource := filepath.Join(hostPath, "mnt")

pkg/validate/e2e.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ limitations under the License.
1717
package validate
1818

1919
import (
20-
"math/rand"
2120
"testing"
22-
"time"
2321

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

0 commit comments

Comments
 (0)