Skip to content

Commit a07391c

Browse files
authored
Merge pull request #5906 from thaJeztah/remove_client_warnings
fix duplicate warnings on docker run / docker create, and slight refactor
2 parents 650b45a + bc90bb6 commit a07391c

8 files changed

Lines changed: 29 additions & 51 deletions

cli/command/container/create.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"net/netip"
78
"os"
8-
"regexp"
99

1010
"github.com/containerd/platforms"
1111
"github.com/distribution/reference"
@@ -207,9 +207,6 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
207207
hostConfig := containerCfg.HostConfig
208208
networkingConfig := containerCfg.NetworkingConfig
209209

210-
warnOnOomKillDisable(*hostConfig, dockerCli.Err())
211-
warnOnLocalhostDNS(*hostConfig, dockerCli.Err())
212-
213210
var (
214211
trustedRef reference.Canonical
215212
namedRef reference.Named
@@ -292,40 +289,27 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
292289
}
293290
}
294291

292+
if warn := localhostDNSWarning(*hostConfig); warn != "" {
293+
response.Warnings = append(response.Warnings, warn)
294+
}
295295
for _, w := range response.Warnings {
296296
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING:", w)
297297
}
298298
err = containerIDFile.Write(response.ID)
299299
return response.ID, err
300300
}
301301

302-
func warnOnOomKillDisable(hostConfig container.HostConfig, stderr io.Writer) {
303-
if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
304-
_, _ = fmt.Fprintln(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.")
305-
}
306-
}
307-
308302
// check the DNS settings passed via --dns against localhost regexp to warn if
309-
// they are trying to set a DNS to a localhost address
310-
func warnOnLocalhostDNS(hostConfig container.HostConfig, stderr io.Writer) {
303+
// they are trying to set a DNS to a localhost address.
304+
//
305+
// TODO(thaJeztah): move this to the daemon, which can make a better call if it will work or not (depending on networking mode).
306+
func localhostDNSWarning(hostConfig container.HostConfig) string {
311307
for _, dnsIP := range hostConfig.DNS {
312-
if isLocalhost(dnsIP) {
313-
_, _ = fmt.Fprintf(stderr, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
314-
return
308+
if addr, err := netip.ParseAddr(dnsIP); err == nil && addr.IsLoopback() {
309+
return fmt.Sprintf("Localhost DNS (%s) may fail in containers.", addr)
315310
}
316311
}
317-
}
318-
319-
// IPLocalhost is a regex pattern for IPv4 or IPv6 loopback range.
320-
const ipLocalhost = `((127\.([0-9]{1,3}\.){2}[0-9]{1,3})|(::1)$)`
321-
322-
var localhostIPRegexp = regexp.MustCompile(ipLocalhost)
323-
324-
// IsLocalhost returns true if ip matches the localhost IP regular expression.
325-
// Used for determining if nameserver settings are being passed which are
326-
// localhost addresses
327-
func isLocalhost(ip string) bool {
328-
return localhostIPRegexp.MatchString(ip)
312+
return ""
329313
}
330314

331315
func validatePullOpt(val string) error {

cli/command/container/create_test.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -270,31 +270,24 @@ func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
270270

271271
func TestNewCreateCommandWithWarnings(t *testing.T) {
272272
testCases := []struct {
273-
name string
274-
args []string
275-
warning bool
273+
name string
274+
args []string
275+
warnings []string
276+
warning bool
276277
}{
277278
{
278-
name: "container-create-without-oom-kill-disable",
279+
name: "container-create-no-warnings",
279280
args: []string{"image:tag"},
280281
},
281282
{
282-
name: "container-create-oom-kill-disable-false",
283-
args: []string{"--oom-kill-disable=false", "image:tag"},
283+
name: "container-create-daemon-single-warning",
284+
args: []string{"image:tag"},
285+
warnings: []string{"warning from daemon"},
284286
},
285287
{
286-
name: "container-create-oom-kill-without-memory-limit",
287-
args: []string{"--oom-kill-disable", "image:tag"},
288-
warning: true,
289-
},
290-
{
291-
name: "container-create-oom-kill-true-without-memory-limit",
292-
args: []string{"--oom-kill-disable=true", "image:tag"},
293-
warning: true,
294-
},
295-
{
296-
name: "container-create-oom-kill-true-with-memory-limit",
297-
args: []string{"--oom-kill-disable=true", "--memory=100M", "image:tag"},
288+
name: "container-create-daemon-multiple-warnings",
289+
args: []string{"image:tag"},
290+
warnings: []string{"warning from daemon", "another warning from daemon"},
298291
},
299292
{
300293
name: "container-create-localhost-dns",
@@ -316,15 +309,15 @@ func TestNewCreateCommandWithWarnings(t *testing.T) {
316309
platform *specs.Platform,
317310
containerName string,
318311
) (container.CreateResponse, error) {
319-
return container.CreateResponse{}, nil
312+
return container.CreateResponse{Warnings: tc.warnings}, nil
320313
},
321314
})
322315
cmd := NewCreateCommand(fakeCLI)
323316
cmd.SetOut(io.Discard)
324317
cmd.SetArgs(tc.args)
325318
err := cmd.Execute()
326319
assert.NilError(t, err)
327-
if tc.warning {
320+
if tc.warning || len(tc.warnings) > 0 {
328321
golden.Assert(t, fakeCLI.ErrBuffer().String(), tc.name+".golden")
329322
} else {
330323
assert.Equal(t, fakeCLI.ErrBuffer().String(), "")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
WARNING: warning from daemon
2+
WARNING: another warning from daemon
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WARNING: warning from daemon
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
WARNING: Localhost DNS setting (--dns=::1) may fail in containers.
1+
WARNING: Localhost DNS (::1) may fail in containers.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
WARNING: Localhost DNS setting (--dns=127.0.0.11) may fail in containers.
1+
WARNING: Localhost DNS (127.0.0.11) may fail in containers.

cli/command/container/testdata/container-create-oom-kill-true-without-memory-limit.golden

Lines changed: 0 additions & 1 deletion
This file was deleted.

cli/command/container/testdata/container-create-oom-kill-without-memory-limit.golden

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)