Skip to content

Commit 6fc5891

Browse files
Merge pull request #6560 from thaJeztah/28.x_backport_deprecations
[28.x backport] opts, image/build: deprecate IsArchive, ValidateMACAddress, ListOpts.Delete()
2 parents 9af6cbc + d8bab71 commit 6fc5891

File tree

7 files changed

+67
-64
lines changed

7 files changed

+67
-64
lines changed

cli/command/container/opts.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"net"
78
"os"
89
"path"
910
"path/filepath"
@@ -350,7 +351,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
350351

351352
// Validate the input mac address
352353
if copts.macAddress != "" {
353-
if _, err := opts.ValidateMACAddress(copts.macAddress); err != nil {
354+
if _, err := net.ParseMAC(strings.TrimSpace(copts.macAddress)); err != nil {
354355
return nil, errors.Errorf("%s is not a valid mac address", copts.macAddress)
355356
}
356357
}
@@ -883,7 +884,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*networktypes.End
883884
}
884885
}
885886
if ep.MacAddress != "" {
886-
if _, err := opts.ValidateMACAddress(ep.MacAddress); err != nil {
887+
if _, err := net.ParseMAC(strings.TrimSpace(ep.MacAddress)); err != nil {
887888
return nil, errors.Errorf("%s is not a valid mac address", ep.MacAddress)
888889
}
889890
epConfig.MacAddress = ep.MacAddress

cli/command/image/build.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ func (out *lastProgressOutput) WriteProgress(prog progress.Progress) error {
184184
//nolint:gocyclo
185185
func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) error {
186186
var (
187-
err error
188187
buildCtx io.ReadCloser
189188
dockerfileCtx io.ReadCloser
190189
contextDir string
@@ -266,7 +265,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
266265
}
267266

268267
if err := build.ValidateContextDirectory(contextDir, excludes); err != nil {
269-
return errors.Wrap(err, "error checking context")
268+
return errors.Wrap(err, "checking context")
270269
}
271270

272271
// And canonicalize dockerfile name to a platform-independent one
@@ -353,7 +352,6 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
353352
}
354353
}
355354
buildOpts := imageBuildOptions(dockerCli, options)
356-
buildOpts.Version = buildtypes.BuilderV1
357355
buildOpts.Dockerfile = relDockerfile
358356
buildOpts.AuthConfigs = authConfigs
359357
buildOpts.RemoteContext = remote
@@ -363,7 +361,6 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
363361
if options.quiet {
364362
_, _ = fmt.Fprintf(dockerCli.Err(), "%s", progBuff)
365363
}
366-
cancel()
367364
return err
368365
}
369366
defer response.Body.Close()
@@ -380,7 +377,8 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
380377

381378
err = jsonstream.Display(ctx, response.Body, streams.NewOut(buildBuff), jsonstream.WithAuxCallback(aux))
382379
if err != nil {
383-
if jerr, ok := err.(*jsonstream.JSONError); ok {
380+
var jerr *jsonstream.JSONError
381+
if errors.As(err, &jerr) {
384382
// If no error code is set, default to 1
385383
if jerr.Code == 0 {
386384
jerr.Code = 1
@@ -544,6 +542,7 @@ func replaceDockerfileForContentTrust(ctx context.Context, inputTarStream io.Rea
544542
func imageBuildOptions(dockerCli command.Cli, options buildOptions) buildtypes.ImageBuildOptions {
545543
configFile := dockerCli.ConfigFile()
546544
return buildtypes.ImageBuildOptions{
545+
Version: buildtypes.BuilderV1,
547546
Memory: options.memory.Value(),
548547
MemorySwap: options.memorySwap.Value(),
549548
Tags: options.tags.GetSlice(),

cli/command/image/build/context.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
8080
if err != nil && os.IsPermission(err) {
8181
return errors.Errorf("no permission to read from '%s'", filePath)
8282
}
83-
currentFile.Close()
83+
_ = currentFile.Close()
8484
}
8585
return nil
8686
})
@@ -97,18 +97,18 @@ func filepathMatches(matcher *patternmatcher.PatternMatcher, file string) (bool,
9797

9898
// DetectArchiveReader detects whether the input stream is an archive or a
9999
// Dockerfile and returns a buffered version of input, safe to consume in lieu
100-
// of input. If an archive is detected, isArchive is set to true, and to false
100+
// of input. If an archive is detected, ok is set to true, and to false
101101
// otherwise, in which case it is safe to assume input represents the contents
102102
// of a Dockerfile.
103-
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, isArchive bool, err error) {
103+
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, ok bool, err error) {
104104
buf := bufio.NewReader(input)
105105

106106
magic, err := buf.Peek(archiveHeaderSize * 2)
107107
if err != nil && err != io.EOF {
108108
return nil, false, errors.Errorf("failed to peek context header from STDIN: %v", err)
109109
}
110110

111-
return newReadCloserWrapper(buf, func() error { return input.Close() }), IsArchive(magic), nil
111+
return newReadCloserWrapper(buf, func() error { return input.Close() }), isArchive(magic), nil
112112
}
113113

114114
// WriteTempDockerfile writes a Dockerfile stream to a temporary file with a
@@ -141,12 +141,12 @@ func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) {
141141
// Dockerfile or tar archive. Returns a tar archive used as a context and a
142142
// path to the Dockerfile inside the tar.
143143
func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) {
144-
rc, isArchive, err := DetectArchiveReader(rc)
144+
rc, ok, err := DetectArchiveReader(rc)
145145
if err != nil {
146146
return nil, "", err
147147
}
148148

149-
if isArchive {
149+
if ok {
150150
return rc, dockerfileName, nil
151151
}
152152

@@ -171,14 +171,22 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
171171

172172
return newReadCloserWrapper(tarArchive, func() error {
173173
err := tarArchive.Close()
174-
os.RemoveAll(dockerfileDir)
174+
_ = os.RemoveAll(dockerfileDir)
175175
return err
176176
}), DefaultDockerfileName, nil
177177
}
178178

179179
// IsArchive checks for the magic bytes of a tar or any supported compression
180180
// algorithm.
181+
//
182+
// Deprecated: this utility was used internally and will be removed in the next release.
181183
func IsArchive(header []byte) bool {
184+
return isArchive(header)
185+
}
186+
187+
// isArchive checks for the magic bytes of a tar or any supported compression
188+
// algorithm.
189+
func isArchive(header []byte) bool {
182190
if compression.Detect(header) != compression.None {
183191
return true
184192
}
@@ -242,7 +250,7 @@ func getWithStatusError(url string) (resp *http.Response, err error) {
242250
}
243251
msg := fmt.Sprintf("failed to GET %s with status %s", url, resp.Status)
244252
body, err := io.ReadAll(resp.Body)
245-
resp.Body.Close()
253+
_ = resp.Body.Close()
246254
if err != nil {
247255
return nil, errors.Wrapf(err, "%s: error reading body", msg)
248256
}
@@ -374,7 +382,7 @@ func isUNC(path string) bool {
374382
// the relative path to the dockerfile in the context.
375383
func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
376384
file, err := io.ReadAll(dockerfileCtx)
377-
dockerfileCtx.Close()
385+
_ = dockerfileCtx.Close()
378386
if err != nil {
379387
return nil, "", err
380388
}
@@ -438,17 +446,19 @@ func Compress(buildCtx io.ReadCloser) (io.ReadCloser, error) {
438446
go func() {
439447
compressWriter, err := compression.CompressStream(pipeWriter, archive.Gzip)
440448
if err != nil {
441-
pipeWriter.CloseWithError(err)
449+
_ = pipeWriter.CloseWithError(err)
442450
}
443-
defer buildCtx.Close()
451+
defer func() {
452+
_ = buildCtx.Close()
453+
}()
444454

445455
if _, err := io.Copy(compressWriter, buildCtx); err != nil {
446-
pipeWriter.CloseWithError(errors.Wrap(err, "failed to compress context"))
447-
compressWriter.Close()
456+
_ = pipeWriter.CloseWithError(errors.Wrap(err, "failed to compress context"))
457+
_ = compressWriter.Close()
448458
return
449459
}
450-
compressWriter.Close()
451-
pipeWriter.Close()
460+
_ = compressWriter.Close()
461+
_ = pipeWriter.Close()
452462
}()
453463

454464
return pipeReader, nil

cli/command/image/build/context_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestGetContextFromReaderString(t *testing.T) {
135135
}
136136

137137
buff := new(bytes.Buffer)
138-
buff.ReadFrom(tarReader)
138+
_, _ = buff.ReadFrom(tarReader)
139139
contents := buff.String()
140140

141141
_, err = tarReader.Next()
@@ -182,7 +182,7 @@ func TestGetContextFromReaderTar(t *testing.T) {
182182
}
183183

184184
buff := new(bytes.Buffer)
185-
buff.ReadFrom(tarReader)
185+
_, _ = buff.ReadFrom(tarReader)
186186
contents := buff.String()
187187

188188
_, err = tarReader.Next()
@@ -263,7 +263,7 @@ func chdir(t *testing.T, dir string) {
263263
}
264264

265265
func TestIsArchive(t *testing.T) {
266-
testcases := []struct {
266+
tests := []struct {
267267
doc string
268268
header []byte
269269
expected bool
@@ -289,13 +289,15 @@ func TestIsArchive(t *testing.T) {
289289
expected: false,
290290
},
291291
}
292-
for _, testcase := range testcases {
293-
assert.Check(t, is.Equal(testcase.expected, IsArchive(testcase.header)), testcase.doc)
292+
for _, tc := range tests {
293+
t.Run(tc.doc, func(t *testing.T) {
294+
assert.Check(t, is.Equal(tc.expected, isArchive(tc.header)), tc.doc)
295+
})
294296
}
295297
}
296298

297299
func TestDetectArchiveReader(t *testing.T) {
298-
testcases := []struct {
300+
tests := []struct {
299301
file string
300302
desc string
301303
expected bool
@@ -316,14 +318,18 @@ func TestDetectArchiveReader(t *testing.T) {
316318
expected: false,
317319
},
318320
}
319-
for _, testcase := range testcases {
320-
content, err := os.Open(testcase.file)
321-
assert.NilError(t, err)
322-
defer content.Close()
323-
324-
_, isArchive, err := DetectArchiveReader(content)
325-
assert.NilError(t, err)
326-
assert.Check(t, is.Equal(testcase.expected, isArchive), testcase.file)
321+
for _, tc := range tests {
322+
t.Run(tc.desc, func(t *testing.T) {
323+
content, err := os.Open(tc.file)
324+
assert.NilError(t, err)
325+
defer func() {
326+
_ = content.Close()
327+
}()
328+
329+
_, isArchive, err := DetectArchiveReader(content)
330+
assert.NilError(t, err)
331+
assert.Check(t, is.Equal(tc.expected, isArchive), tc.file)
332+
})
327333
}
328334
}
329335

cli/command/image/build/dockerignore.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func ReadDockerignore(contextDir string) ([]string, error) {
2121
case err != nil:
2222
return nil, err
2323
}
24-
defer f.Close()
24+
defer func() {
25+
_ = f.Close()
26+
}()
2527

2628
patterns, err := ignorefile.ReadAll(f)
2729
if err != nil {

opts/opts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func (opts *ListOpts) Set(value string) error {
6060
}
6161

6262
// Delete removes the specified element from the slice.
63+
//
64+
// Deprecated: this method is no longer used and will be removed in the next release.
6365
func (opts *ListOpts) Delete(key string) {
6466
for i, k := range *opts.values {
6567
if k == key {
@@ -264,6 +266,8 @@ func ValidateIPAddress(val string) (string, error) {
264266
}
265267

266268
// ValidateMACAddress validates a MAC address.
269+
//
270+
// Deprecated: use [net.ParseMAC]. This function will be removed in the next release.
267271
func ValidateMACAddress(val string) (string, error) {
268272
_, err := net.ParseMAC(strings.TrimSpace(val))
269273
if err != nil {

opts/opts_test.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,14 @@ func TestListOptsWithoutValidator(t *testing.T) {
142142
if o.Get("baz") {
143143
t.Error(`o.Get("baz") == true`)
144144
}
145-
o.Delete("foo")
146-
if o.String() != "[bar bar]" {
147-
t.Errorf("%s != [bar bar]", o.String())
145+
if listOpts := o.GetSlice(); len(listOpts) != 3 || listOpts[0] != "foo" || listOpts[1] != "bar" || listOpts[2] != "bar" {
146+
t.Errorf("Expected [[foo bar bar]], got [%v]", listOpts)
148147
}
149-
if listOpts := o.GetAll(); len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
150-
t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
148+
if listOpts := o.GetAll(); len(listOpts) != 3 || listOpts[0] != "foo" || listOpts[1] != "bar" || listOpts[2] != "bar" {
149+
t.Errorf("Expected [[foo bar bar]], got [%v]", listOpts)
151150
}
152-
if listOpts := o.GetSlice(); len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
153-
t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
154-
}
155-
if mapListOpts := o.GetMap(); len(mapListOpts) != 1 {
156-
t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
151+
if mapListOpts := o.GetMap(); len(mapListOpts) != 2 {
152+
t.Errorf("Expected [map[bar:{} foo:{}]], got [%v]", mapListOpts)
157153
}
158154
}
159155

@@ -186,9 +182,8 @@ func TestListOptsWithValidator(t *testing.T) {
186182
if o.Get("baz") {
187183
t.Error(`o.Get("baz") == true`)
188184
}
189-
o.Delete("valid-option2=2")
190-
if o.String() != "" {
191-
t.Errorf(`%s != ""`, o.String())
185+
if expected := "[valid-option2=2]"; o.String() != expected {
186+
t.Errorf(`%s != %q`, o.String(), expected)
192187
}
193188
}
194189

@@ -396,20 +391,6 @@ func TestNamedMapOpts(t *testing.T) {
396391
}
397392
}
398393

399-
func TestValidateMACAddress(t *testing.T) {
400-
if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil {
401-
t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err)
402-
}
403-
404-
if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil {
405-
t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC")
406-
}
407-
408-
if _, err := ValidateMACAddress(`random invalid string`); err == nil {
409-
t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC")
410-
}
411-
}
412-
413394
func TestValidateLink(t *testing.T) {
414395
valid := []string{
415396
"name",

0 commit comments

Comments
 (0)