diff --git a/builder/builder-next/builder.go b/builder/builder-next/builder.go index 047c09b8e3..98a52d39df 100644 --- a/builder/builder-next/builder.go +++ b/builder/builder-next/builder.go @@ -17,10 +17,10 @@ import ( "github.com/docker/docker/builder" "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/images" + "github.com/docker/docker/libnetwork" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/system" - "github.com/docker/docker/libnetwork" controlapi "github.com/moby/buildkit/api/services/control" "github.com/moby/buildkit/client" "github.com/moby/buildkit/control" diff --git a/builder/builder-next/executor_unix.go b/builder/builder-next/executor_unix.go index 83ce290d54..df723886f4 100644 --- a/builder/builder-next/executor_unix.go +++ b/builder/builder-next/executor_unix.go @@ -10,9 +10,9 @@ import ( "sync" "github.com/docker/docker/daemon/config" + "github.com/docker/docker/libnetwork" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/stringid" - "github.com/docker/docker/libnetwork" "github.com/moby/buildkit/executor" "github.com/moby/buildkit/executor/oci" "github.com/moby/buildkit/executor/runcexecutor" diff --git a/builder/builder-next/executor_windows.go b/builder/builder-next/executor_windows.go index 988c7b2b06..77d1fa096d 100644 --- a/builder/builder-next/executor_windows.go +++ b/builder/builder-next/executor_windows.go @@ -5,8 +5,8 @@ import ( "errors" "github.com/docker/docker/daemon/config" - "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/libnetwork" + "github.com/docker/docker/pkg/idtools" "github.com/moby/buildkit/executor" "github.com/moby/buildkit/executor/oci" ) diff --git a/cmd/dockerd/daemon_unix.go b/cmd/dockerd/daemon_unix.go index 7217b2d6b7..10a65e5042 100644 --- a/cmd/dockerd/daemon_unix.go +++ b/cmd/dockerd/daemon_unix.go @@ -16,8 +16,8 @@ import ( "github.com/docker/docker/daemon" "github.com/docker/docker/daemon/config" "github.com/docker/docker/libcontainerd/supervisor" - "github.com/docker/docker/pkg/homedir" "github.com/docker/docker/libnetwork/portallocator" + "github.com/docker/docker/pkg/homedir" "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" diff --git a/daemon/cluster/cluster.go b/daemon/cluster/cluster.go index 0bf8c3b2fc..b47fad5936 100644 --- a/daemon/cluster/cluster.go +++ b/daemon/cluster/cluster.go @@ -53,8 +53,8 @@ import ( types "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/daemon/cluster/controllers/plugin" executorpkg "github.com/docker/docker/daemon/cluster/executor" - "github.com/docker/docker/pkg/signal" lncluster "github.com/docker/docker/libnetwork/cluster" + "github.com/docker/docker/pkg/signal" swarmapi "github.com/docker/swarmkit/api" swarmnode "github.com/docker/swarmkit/node" "github.com/pkg/errors" diff --git a/daemon/cluster/executor/backend.go b/daemon/cluster/executor/backend.go index f9ccc78969..f342d522fc 100644 --- a/daemon/cluster/executor/backend.go +++ b/daemon/cluster/executor/backend.go @@ -17,11 +17,11 @@ import ( containerpkg "github.com/docker/docker/container" clustertypes "github.com/docker/docker/daemon/cluster/provider" networkSettings "github.com/docker/docker/daemon/network" - "github.com/docker/docker/plugin" - volumeopts "github.com/docker/docker/volume/service/opts" "github.com/docker/docker/libnetwork" "github.com/docker/docker/libnetwork/cluster" networktypes "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/plugin" + volumeopts "github.com/docker/docker/volume/service/opts" "github.com/docker/swarmkit/agent/exec" specs "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/daemon/cluster/executor/container/adapter.go b/daemon/cluster/executor/container/adapter.go index f5e7b6c6e3..57a4a23db3 100644 --- a/daemon/cluster/executor/container/adapter.go +++ b/daemon/cluster/executor/container/adapter.go @@ -20,8 +20,8 @@ import ( "github.com/docker/docker/daemon" "github.com/docker/docker/daemon/cluster/convert" executorpkg "github.com/docker/docker/daemon/cluster/executor" - volumeopts "github.com/docker/docker/volume/service/opts" "github.com/docker/docker/libnetwork" + volumeopts "github.com/docker/docker/volume/service/opts" "github.com/docker/swarmkit/agent/exec" "github.com/docker/swarmkit/api" "github.com/docker/swarmkit/log" diff --git a/daemon/cluster/executor/container/container.go b/daemon/cluster/executor/container/container.go index 6f3c822c03..7ee0b0dee5 100644 --- a/daemon/cluster/executor/container/container.go +++ b/daemon/cluster/executor/container/container.go @@ -20,9 +20,9 @@ import ( "github.com/docker/docker/daemon/cluster/convert" executorpkg "github.com/docker/docker/daemon/cluster/executor" clustertypes "github.com/docker/docker/daemon/cluster/provider" + netconst "github.com/docker/docker/libnetwork/datastore" "github.com/docker/go-connections/nat" "github.com/docker/go-units" - netconst "github.com/docker/docker/libnetwork/datastore" "github.com/docker/swarmkit/agent/exec" "github.com/docker/swarmkit/api" "github.com/docker/swarmkit/api/genericresource" diff --git a/daemon/cluster/executor/container/controller.go b/daemon/cluster/executor/container/controller.go index fb15a0d7cc..e3491a4b95 100644 --- a/daemon/cluster/executor/container/controller.go +++ b/daemon/cluster/executor/container/controller.go @@ -11,8 +11,8 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/events" executorpkg "github.com/docker/docker/daemon/cluster/executor" - "github.com/docker/go-connections/nat" "github.com/docker/docker/libnetwork" + "github.com/docker/go-connections/nat" "github.com/docker/swarmkit/agent/exec" "github.com/docker/swarmkit/api" "github.com/docker/swarmkit/log" diff --git a/daemon/config/config_test.go b/daemon/config/config_test.go index edc9b5f129..a0706339f2 100644 --- a/daemon/config/config_test.go +++ b/daemon/config/config_test.go @@ -7,8 +7,8 @@ import ( "testing" "github.com/docker/docker/daemon/discovery" - "github.com/docker/docker/opts" "github.com/docker/docker/libnetwork/ipamutils" + "github.com/docker/docker/opts" "github.com/spf13/pflag" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" diff --git a/daemon/container_operations.go b/daemon/container_operations.go index 91590cde11..4c3d37468a 100644 --- a/daemon/container_operations.go +++ b/daemon/container_operations.go @@ -14,15 +14,15 @@ import ( "github.com/docker/docker/container" "github.com/docker/docker/daemon/network" "github.com/docker/docker/errdefs" - "github.com/docker/docker/opts" - "github.com/docker/docker/pkg/stringid" - "github.com/docker/docker/runconfig" - "github.com/docker/go-connections/nat" "github.com/docker/docker/libnetwork" netconst "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/opts" + "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/runconfig" + "github.com/docker/go-connections/nat" "github.com/sirupsen/logrus" ) diff --git a/daemon/container_operations_unix.go b/daemon/container_operations_unix.go index f8732cda70..b8f99e7920 100644 --- a/daemon/container_operations_unix.go +++ b/daemon/container_operations_unix.go @@ -12,11 +12,11 @@ import ( "github.com/docker/docker/container" "github.com/docker/docker/daemon/links" "github.com/docker/docker/errdefs" + "github.com/docker/docker/libnetwork" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/system" "github.com/docker/docker/runconfig" - "github.com/docker/docker/libnetwork" "github.com/moby/sys/mount" "github.com/opencontainers/selinux/go-selinux/label" "github.com/pkg/errors" diff --git a/daemon/container_operations_windows.go b/daemon/container_operations_windows.go index 4b35851f9b..9b6ad34fb2 100644 --- a/daemon/container_operations_windows.go +++ b/daemon/container_operations_windows.go @@ -6,8 +6,8 @@ import ( "os" "github.com/docker/docker/container" - "github.com/docker/docker/pkg/system" "github.com/docker/docker/libnetwork" + "github.com/docker/docker/pkg/system" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) diff --git a/daemon/daemon.go b/daemon/daemon.go index 72f48cac41..c4a2d5e36e 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -55,6 +55,9 @@ import ( "github.com/docker/docker/layer" "github.com/docker/docker/libcontainerd" libcontainerdtypes "github.com/docker/docker/libcontainerd/types" + "github.com/docker/docker/libnetwork" + "github.com/docker/docker/libnetwork/cluster" + nwconfig "github.com/docker/docker/libnetwork/config" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/system" @@ -65,9 +68,6 @@ import ( "github.com/docker/docker/registry" "github.com/docker/docker/runconfig" volumesservice "github.com/docker/docker/volume/service" - "github.com/docker/docker/libnetwork" - "github.com/docker/docker/libnetwork/cluster" - nwconfig "github.com/docker/docker/libnetwork/config" "github.com/moby/locker" "github.com/pkg/errors" "golang.org/x/sync/semaphore" diff --git a/daemon/daemon_test.go b/daemon/daemon_test.go index 56c6a4e36c..52f8434bd9 100644 --- a/daemon/daemon_test.go +++ b/daemon/daemon_test.go @@ -10,12 +10,12 @@ import ( containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/container" "github.com/docker/docker/errdefs" + "github.com/docker/docker/libnetwork" _ "github.com/docker/docker/pkg/discovery/memory" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/truncindex" volumesservice "github.com/docker/docker/volume/service" "github.com/docker/go-connections/nat" - "github.com/docker/docker/libnetwork" "github.com/pkg/errors" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" diff --git a/daemon/daemon_unix.go b/daemon/daemon_unix.go index 925bb6eee4..e09dc4fefd 100644 --- a/daemon/daemon_unix.go +++ b/daemon/daemon_unix.go @@ -28,6 +28,13 @@ import ( "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/initlayer" "github.com/docker/docker/errdefs" + "github.com/docker/docker/libnetwork" + nwconfig "github.com/docker/docker/libnetwork/config" + "github.com/docker/docker/libnetwork/drivers/bridge" + "github.com/docker/docker/libnetwork/netlabel" + "github.com/docker/docker/libnetwork/netutils" + "github.com/docker/docker/libnetwork/options" + lntypes "github.com/docker/docker/libnetwork/types" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/containerfs" "github.com/docker/docker/pkg/idtools" @@ -36,13 +43,6 @@ import ( "github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/runconfig" volumemounts "github.com/docker/docker/volume/mounts" - "github.com/docker/docker/libnetwork" - nwconfig "github.com/docker/docker/libnetwork/config" - "github.com/docker/docker/libnetwork/drivers/bridge" - "github.com/docker/docker/libnetwork/netlabel" - "github.com/docker/docker/libnetwork/netutils" - "github.com/docker/docker/libnetwork/options" - lntypes "github.com/docker/docker/libnetwork/types" "github.com/moby/sys/mount" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/selinux/go-selinux" diff --git a/daemon/daemon_windows.go b/daemon/daemon_windows.go index 2aed3178ce..740a127a61 100644 --- a/daemon/daemon_windows.go +++ b/daemon/daemon_windows.go @@ -14,6 +14,12 @@ import ( containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/container" "github.com/docker/docker/daemon/config" + "github.com/docker/docker/libnetwork" + nwconfig "github.com/docker/docker/libnetwork/config" + "github.com/docker/docker/libnetwork/datastore" + winlibnetwork "github.com/docker/docker/libnetwork/drivers/windows" + "github.com/docker/docker/libnetwork/netlabel" + "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/pkg/containerfs" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/parsers" @@ -21,12 +27,6 @@ import ( "github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/system" "github.com/docker/docker/runconfig" - "github.com/docker/docker/libnetwork" - nwconfig "github.com/docker/docker/libnetwork/config" - "github.com/docker/docker/libnetwork/datastore" - winlibnetwork "github.com/docker/docker/libnetwork/drivers/windows" - "github.com/docker/docker/libnetwork/netlabel" - "github.com/docker/docker/libnetwork/options" "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/sys/windows" diff --git a/daemon/network.go b/daemon/network.go index 2f8ebebe3b..71bd92f1e6 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -17,10 +17,6 @@ import ( clustertypes "github.com/docker/docker/daemon/cluster/provider" internalnetwork "github.com/docker/docker/daemon/network" "github.com/docker/docker/errdefs" - "github.com/docker/docker/opts" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/docker/runconfig" - "github.com/docker/go-connections/nat" "github.com/docker/docker/libnetwork" lncluster "github.com/docker/docker/libnetwork/cluster" "github.com/docker/docker/libnetwork/driverapi" @@ -29,6 +25,10 @@ import ( "github.com/docker/docker/libnetwork/networkdb" "github.com/docker/docker/libnetwork/options" networktypes "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/opts" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/docker/runconfig" + "github.com/docker/go-connections/nat" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) diff --git a/daemon/oci_linux_test.go b/daemon/oci_linux_test.go index 76ed3ee56a..8b228eabbf 100644 --- a/daemon/oci_linux_test.go +++ b/daemon/oci_linux_test.go @@ -10,9 +10,9 @@ import ( "github.com/docker/docker/container" "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/network" + "github.com/docker/docker/libnetwork" "github.com/docker/docker/pkg/containerfs" "github.com/docker/docker/pkg/idtools" - "github.com/docker/docker/libnetwork" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/skip" diff --git a/daemon/prune.go b/daemon/prune.go index 6a1a2c19e8..e3ef4668be 100644 --- a/daemon/prune.go +++ b/daemon/prune.go @@ -13,8 +13,8 @@ import ( "github.com/docker/docker/api/types/filters" timetypes "github.com/docker/docker/api/types/time" "github.com/docker/docker/errdefs" - "github.com/docker/docker/runconfig" "github.com/docker/docker/libnetwork" + "github.com/docker/docker/runconfig" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) diff --git a/daemon/reload_test.go b/daemon/reload_test.go index 9973a5839b..6e8866ebb6 100644 --- a/daemon/reload_test.go +++ b/daemon/reload_test.go @@ -9,10 +9,10 @@ import ( "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/images" + "github.com/docker/docker/libnetwork" "github.com/docker/docker/pkg/discovery" _ "github.com/docker/docker/pkg/discovery/memory" "github.com/docker/docker/registry" - "github.com/docker/docker/libnetwork" "github.com/sirupsen/logrus" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" diff --git a/hack/make/.install b/hack/make/.install index 3a3e3538c6..0e80a8e196 100644 --- a/hack/make/.install +++ b/hack/make/.install @@ -11,4 +11,4 @@ install_binary() { echo "Install is only supported on linux" return 1 fi -} \ No newline at end of file +} diff --git a/hack/make/install-proxy b/hack/make/install-proxy index 2f3fe3622e..04ea1bf88e 100644 --- a/hack/make/install-proxy +++ b/hack/make/install-proxy @@ -8,4 +8,4 @@ source "${MAKEDIR}/.install" ( DEST="$(dirname $DEST)/binary-proxy" install_binary "${DEST}/docker-proxy" -) \ No newline at end of file +) diff --git a/hack/test/unit b/hack/test/unit index fffbe31157..00880adb36 100755 --- a/hack/test/unit +++ b/hack/test/unit @@ -18,8 +18,8 @@ TESTDIRS="${TESTDIRS:-./...}" exclude_paths='/vendor/|/integration' pkg_list=$(go list $TESTDIRS | grep -vE "($exclude_paths)") -echo "${pkg_list}" | grep --fixed-strings "libnetwork/drivers/bridge" && \ - if ! type docker-proxy; then +echo "${pkg_list}" | grep --fixed-strings "libnetwork/drivers/bridge" \ + && if ! type docker-proxy; then hack/make.sh binary-proxy install-proxy fi @@ -30,4 +30,4 @@ gotestsum --format=standard-quiet --jsonfile=bundles/go-test-report.json --junit -coverprofile=bundles/profile.out \ -covermode=atomic \ ${TESTFLAGS} \ - ${pkg_list} \ No newline at end of file + ${pkg_list} diff --git a/hack/validate/golangci-lint.yml b/hack/validate/golangci-lint.yml index 4afd71e802..c4494b9d18 100644 --- a/hack/validate/golangci-lint.yml +++ b/hack/validate/golangci-lint.yml @@ -25,6 +25,8 @@ linters: skip-dirs: - bundles - docs + # TODO: This package should be completely removed + - libnetwork/client/mflag linters-settings: govet: @@ -59,6 +61,11 @@ issues: - text: "G304: Potential file inclusion via variable" linters: - gosec + - text: "G306: Expect WriteFile permissions to be 0600 or less" + linters: + - gosec + - text: 'G307: Deferring unsafe method "Close" on type "*os.File"' + linters: gosec # Exclude some linters from running on tests files. - path: _test\.go @@ -86,3 +93,16 @@ issues: - text: "SA1019: httputil.ErrPersistEOF is deprecated" linters: - staticcheck + # This code is doing some fun stuff with reflect and it trips up the linter. + - text: "field `foo` is unused" + path: "libnetwork/options/options_test.go" + linters: + - structcheck + - unused + # This field is only used on windows but is defined in a platform agnostic file. + # The linter doesn't understand that the field is used. + - text: "`resolverOnce` is unused" + path: libnetwork/network.go + linters: + - structcheck + - unused \ No newline at end of file diff --git a/hack/validate/shfmt b/hack/validate/shfmt index 3a3901cb74..748e774e16 100755 --- a/hack/validate/shfmt +++ b/hack/validate/shfmt @@ -4,7 +4,7 @@ set -e -o pipefail shfmtflags="-bn -ci -sr" # NOTE: `git grep '^#!'` may also pick up non-shell script files. # Add exceptional files to `egrep -v` if any false-positive is detected. -if git grep --name-only '^#!' | egrep -v '(vendor|\.go|Jenkinsfile)' \ +if git grep --name-only '^#!' | egrep -v '(vendor|\.go|Jenkinsfile|\.py|\.bats)' \ | xargs shfmt -d $shfmtflags; then echo 'Congratulations! The shell scripts are properly formatted.' else diff --git a/integration-cli/docker_cli_daemon_test.go b/integration-cli/docker_cli_daemon_test.go index 4b437feae5..f1c71cc2c4 100644 --- a/integration-cli/docker_cli_daemon_test.go +++ b/integration-cli/docker_cli_daemon_test.go @@ -31,10 +31,10 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/daemon" + "github.com/docker/docker/libnetwork/iptables" "github.com/docker/docker/opts" testdaemon "github.com/docker/docker/testutil/daemon" units "github.com/docker/go-units" - "github.com/docker/docker/libnetwork/iptables" "github.com/docker/libtrust" "github.com/moby/sys/mount" "golang.org/x/sys/unix" diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index 087810bc41..04e99dbcf5 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -18,14 +18,14 @@ import ( "github.com/docker/docker/api/types/versions/v1p20" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/daemon" - "github.com/docker/docker/pkg/stringid" - "github.com/docker/docker/runconfig" - testdaemon "github.com/docker/docker/testutil/daemon" "github.com/docker/docker/libnetwork/driverapi" remoteapi "github.com/docker/docker/libnetwork/drivers/remote/api" "github.com/docker/docker/libnetwork/ipamapi" remoteipam "github.com/docker/docker/libnetwork/ipams/remote/api" "github.com/docker/docker/libnetwork/netlabel" + "github.com/docker/docker/pkg/stringid" + "github.com/docker/docker/runconfig" + testdaemon "github.com/docker/docker/testutil/daemon" "github.com/vishvananda/netlink" "golang.org/x/sys/unix" "gotest.tools/v3/assert" diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 3a22cede41..c60e7badde 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -27,13 +27,13 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" + "github.com/docker/docker/libnetwork/resolvconf" + "github.com/docker/docker/libnetwork/types" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/runconfig" "github.com/docker/docker/testutil" "github.com/docker/docker/testutil/fakecontext" "github.com/docker/go-connections/nat" - "github.com/docker/docker/libnetwork/resolvconf" - "github.com/docker/docker/libnetwork/types" "github.com/moby/sys/mountinfo" "gotest.tools/v3/assert" "gotest.tools/v3/icmd" diff --git a/libcontainerd/remote/client.go b/libcontainerd/remote/client.go index 43f549fc5c..7561706241 100644 --- a/libcontainerd/remote/client.go +++ b/libcontainerd/remote/client.go @@ -569,7 +569,7 @@ func (c *client) CreateCheckpoint(ctx context.Context, containerID, checkpointDi var cpDesc *v1.Descriptor for _, m := range index.Manifests { if m.MediaType == images.MediaTypeContainerd1Checkpoint { - cpDesc = &m + cpDesc = &m // nolint:gosec break } } diff --git a/libnetwork/agent.go b/libnetwork/agent.go index 18388cd89f..feca147ea8 100644 --- a/libnetwork/agent.go +++ b/libnetwork/agent.go @@ -9,13 +9,13 @@ import ( "sort" "sync" - "github.com/docker/go-events" "github.com/docker/docker/libnetwork/cluster" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/networkdb" "github.com/docker/docker/libnetwork/types" + "github.com/docker/go-events" "github.com/gogo/protobuf/proto" "github.com/sirupsen/logrus" ) @@ -793,7 +793,7 @@ func (n *network) addDriverWatches() { return } - agent.networkDB.WalkTable(table.name, func(nid, key string, value []byte, deleted bool) bool { + err = agent.networkDB.WalkTable(table.name, func(nid, key string, value []byte, deleted bool) bool { // skip the entries that are mark for deletion, this is safe because this function is // called at initialization time so there is no state to delete if nid == n.ID() && !deleted { @@ -801,6 +801,9 @@ func (n *network) addDriverWatches() { } return false }) + if err != nil { + logrus.WithError(err).Warn("Error while walking networkdb") + } } } diff --git a/libnetwork/api/api.go b/libnetwork/api/api.go index 0249597412..958edfc009 100644 --- a/libnetwork/api/api.go +++ b/libnetwork/api/api.go @@ -18,7 +18,6 @@ import ( var ( successResponse = responseStatus{Status: "Success", StatusCode: http.StatusOK} createdResponse = responseStatus{Status: "Created", StatusCode: http.StatusCreated} - mismatchResponse = responseStatus{Status: "Body/URI parameter mismatch", StatusCode: http.StatusBadRequest} badQueryResponse = responseStatus{Status: "Unsupported query", StatusCode: http.StatusBadRequest} ) @@ -29,11 +28,9 @@ const ( regex = "[a-zA-Z_0-9-]+" qregx = "$|" + regex // Router URL variable definition - nwName = "{" + urlNwName + ":" + regex + "}" nwNameQr = "{" + urlNwName + ":" + qregx + "}" nwID = "{" + urlNwID + ":" + regex + "}" nwPIDQr = "{" + urlNwPID + ":" + qregx + "}" - epName = "{" + urlEpName + ":" + regex + "}" epNameQr = "{" + urlEpName + ":" + qregx + "}" epID = "{" + urlEpID + ":" + regex + "}" epPIDQr = "{" + urlEpPID + ":" + qregx + "}" @@ -260,11 +257,6 @@ func (sc *sandboxCreate) parseOptions() []libnetwork.SandboxOption { return setFctList } -func (ej *endpointJoin) parseOptions() []libnetwork.EndpointOption { - // priority will go here - return []libnetwork.EndpointOption{} -} - /****************** Process functions *******************/ diff --git a/libnetwork/api/api_linux_test.go b/libnetwork/api/api_linux_test.go index 737ac7c24e..1d37d0aca8 100644 --- a/libnetwork/api/api_linux_test.go +++ b/libnetwork/api/api_linux_test.go @@ -24,7 +24,6 @@ import ( const ( bridgeNetType = "bridge" - bridgeName = "docker0" ) func i2s(i interface{}) string { @@ -1664,7 +1663,7 @@ func newWriter() *localResponseWriter { } func (f *localResponseWriter) Header() http.Header { - return make(map[string][]string, 0) + return make(map[string][]string) } func (f *localResponseWriter) Write(data []byte) (int, error) { diff --git a/libnetwork/bitseq/sequence.go b/libnetwork/bitseq/sequence.go index 3f68a5851d..b53721d212 100644 --- a/libnetwork/bitseq/sequence.go +++ b/libnetwork/bitseq/sequence.go @@ -144,11 +144,7 @@ func (s *sequence) equal(o *sequence) bool { this = this.next other = other.next } - // Check if other is longer than this - if other != nil { - return false - } - return true + return other == nil } // ToByteArray converts the sequence into a byte array @@ -699,7 +695,6 @@ func removeCurrentIfEmpty(head **sequence, previous, current *sequence) { *head = current.next } else { previous.next = current.next - current = current.next } } } diff --git a/libnetwork/bitseq/sequence_test.go b/libnetwork/bitseq/sequence_test.go index b791a608b1..af9d61c6d4 100644 --- a/libnetwork/bitseq/sequence_test.go +++ b/libnetwork/bitseq/sequence_test.go @@ -849,7 +849,7 @@ func TestSetAnyInRange(t *testing.T) { func TestMethods(t *testing.T) { numBits := uint64(256 * blockLen) - hnd, err := NewHandle("path/to/data", nil, "sequence1", uint64(numBits)) + hnd, err := NewHandle("path/to/data", nil, "sequence1", numBits) if err != nil { t.Fatal(err) } diff --git a/libnetwork/bitseq/store.go b/libnetwork/bitseq/store.go index 58cb526d2b..30657123e5 100644 --- a/libnetwork/bitseq/store.go +++ b/libnetwork/bitseq/store.go @@ -2,7 +2,6 @@ package bitseq import ( "encoding/json" - "fmt" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/types" @@ -106,17 +105,6 @@ func (h *Handle) DataScope() string { return h.store.Scope() } -func (h *Handle) fromDsValue(value []byte) error { - var ba []byte - if err := json.Unmarshal(value, &ba); err != nil { - return fmt.Errorf("failed to decode json: %s", err.Error()) - } - if err := h.FromByteArray(ba); err != nil { - return fmt.Errorf("failed to decode handle: %s", err.Error()) - } - return nil -} - func (h *Handle) writeToStore() error { h.Lock() store := h.store diff --git a/libnetwork/client/mflag/flag.go b/libnetwork/client/mflag/flag.go index 795ea5257d..04b2dd917a 100644 --- a/libnetwork/client/mflag/flag.go +++ b/libnetwork/client/mflag/flag.go @@ -231,7 +231,7 @@ func (s *stringValue) Set(val string) error { func (s *stringValue) Get() interface{} { return string(*s) } -func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) } +func (s *stringValue) String() string { return string(*s) } // -- float64 Value type float64Value float64 diff --git a/libnetwork/client/mflag/flag_test.go b/libnetwork/client/mflag/flag_test.go index 53163803a8..1a9f67b684 100644 --- a/libnetwork/client/mflag/flag_test.go +++ b/libnetwork/client/mflag/flag_test.go @@ -97,9 +97,7 @@ func TestEverything(t *testing.T) { // Now test they're visited in sort order. var flagNames []string Visit(func(f *Flag) { - for _, name := range f.Names { - flagNames = append(flagNames, name) - } + flagNames = append(flagNames, f.Names...) }) if !sort.StringsAreSorted(flagNames) { t.Errorf("flag names not sorted: %v", flagNames) diff --git a/libnetwork/client/network.go b/libnetwork/client/network.go index cd65e89f94..bc050f7e2d 100644 --- a/libnetwork/client/network.go +++ b/libnetwork/client/network.go @@ -8,9 +8,9 @@ import ( "strings" "text/tabwriter" - "github.com/docker/docker/pkg/stringid" flag "github.com/docker/docker/libnetwork/client/mflag" "github.com/docker/docker/libnetwork/netlabel" + "github.com/docker/docker/pkg/stringid" ) type command struct { diff --git a/libnetwork/client/service.go b/libnetwork/client/service.go index e202853f3a..a246bc6061 100644 --- a/libnetwork/client/service.go +++ b/libnetwork/client/service.go @@ -9,9 +9,9 @@ import ( "strings" "text/tabwriter" - "github.com/docker/docker/pkg/stringid" flag "github.com/docker/docker/libnetwork/client/mflag" "github.com/docker/docker/libnetwork/netutils" + "github.com/docker/docker/pkg/stringid" ) var ( diff --git a/libnetwork/client/types.go b/libnetwork/client/types.go index 1faa78eccb..cdfc9d8045 100644 --- a/libnetwork/client/types.go +++ b/libnetwork/client/types.go @@ -86,11 +86,3 @@ type extraHost struct { Name string `json:"name"` Address string `json:"address"` } - -// sandboxParentUpdate is the object carrying the information about the -// sandbox parent that needs to be updated. -type sandboxParentUpdate struct { - ContainerID string `json:"container_id"` - Name string `json:"name"` - Address string `json:"address"` -} diff --git a/libnetwork/cmd/diagnostic/main.go b/libnetwork/cmd/diagnostic/main.go index c223975094..ca741465ba 100644 --- a/libnetwork/cmd/diagnostic/main.go +++ b/libnetwork/cmd/diagnostic/main.go @@ -117,7 +117,7 @@ func fetchNodePeers(ip string, port int, network string) map[string]string { path = fmt.Sprintf(clusterPeers, ip, port) } - resp, err := http.Get(path) + resp, err := http.Get(path) // nolint:gosec if err != nil { logrus.WithError(err).Fatalf("Failed fetching path") } diff --git a/libnetwork/cmd/dnet/dnet.go b/libnetwork/cmd/dnet/dnet.go index f43e49180f..d5d0db0b9d 100644 --- a/libnetwork/cmd/dnet/dnet.go +++ b/libnetwork/cmd/dnet/dnet.go @@ -21,8 +21,6 @@ import ( "github.com/BurntSushi/toml" "github.com/docker/docker/api/types/network" - "github.com/docker/docker/pkg/discovery" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork" "github.com/docker/docker/libnetwork/api" "github.com/docker/docker/libnetwork/cluster" @@ -33,6 +31,8 @@ import ( "github.com/docker/docker/libnetwork/netutils" "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/discovery" + "github.com/docker/docker/pkg/reexec" "github.com/gorilla/mux" "github.com/moby/term" "github.com/sirupsen/logrus" @@ -43,13 +43,11 @@ const ( // DefaultHTTPHost is used if only port is provided to -H flag e.g. docker -d -H tcp://:8080 DefaultHTTPHost = "0.0.0.0" // DefaultHTTPPort is the default http port used by dnet - DefaultHTTPPort = 2385 - // DefaultUnixSocket exported - DefaultUnixSocket = "/var/run/dnet.sock" - cfgFileEnv = "LIBNETWORK_CFG" - defaultCfgFile = "/etc/default/libnetwork.toml" - defaultHeartbeat = time.Duration(10) * time.Second - ttlFactor = 2 + DefaultHTTPPort = 2385 + cfgFileEnv = "LIBNETWORK_CFG" + defaultCfgFile = "/etc/default/libnetwork.toml" + defaultHeartbeat = time.Duration(10) * time.Second + ttlFactor = 2 ) var epConn *dnetConnection @@ -172,11 +170,9 @@ func startDiscovery(cfg *config.ClusterCfg) ([]config.Option, error) { options := []config.Option{config.OptionDiscoveryWatcher(d), config.OptionDiscoveryAddress(cfg.Address)} go func() { for { - select { - case <-time.After(hb): - if err := d.Register(cfg.Address + ":0"); err != nil { - logrus.Warn(err) - } + time.Sleep(hb) + if err := d.Register(cfg.Address + ":0"); err != nil { + logrus.Warn(err) } } }() @@ -507,7 +503,7 @@ func (d *dnetConnection) httpCall(method, path string, data interface{}, headers return nil, nil, -1, err } - req, err := http.NewRequest(method, fmt.Sprintf("%s", path), in) + req, err := http.NewRequest(method, path, in) if err != nil { return nil, nil, -1, err } @@ -552,10 +548,8 @@ func setupRequestHeaders(method string, data interface{}, req *http.Request, hea req.Header.Set("Content-Type", "text/plain") } - if headers != nil { - for k, v := range headers { - req.Header[k] = v - } + for k, v := range headers { + req.Header[k] = v } } diff --git a/libnetwork/cmd/networkdb-test/dbclient/ndbClient.go b/libnetwork/cmd/networkdb-test/dbclient/ndbClient.go index 2d116a3399..47b95e1280 100644 --- a/libnetwork/cmd/networkdb-test/dbclient/ndbClient.go +++ b/libnetwork/cmd/networkdb-test/dbclient/ndbClient.go @@ -118,19 +118,6 @@ func dbTableEntriesNumber(ip, port, networkName, tableName string, doneCh chan r doneCh <- resultTuple{id: ip, result: entriesNum} } -func dbEntriesNumber(ip, port, networkName string, doneCh chan resultTuple) { - body, err := httpGet(ip, port, "/networkstats?nid="+networkName) - - if err != nil { - logrus.Errorf("entriesNumber %s there was an error: %s", ip, err) - doneCh <- resultTuple{id: ip, result: -1} - return - } - elementsRegexp := regexp.MustCompile(`entries: ([0-9]+)`) - entriesNum, _ := strconv.Atoi(elementsRegexp.FindStringSubmatch(string(body))[1]) - doneCh <- resultTuple{id: ip, result: entriesNum} -} - func dbQueueLength(ip, port, networkName string, doneCh chan resultTuple) { body, err := httpGet(ip, port, "/networkstats?nid="+networkName) diff --git a/libnetwork/cmd/networkdb-test/dummyclient/dummyClient.go b/libnetwork/cmd/networkdb-test/dummyclient/dummyClient.go index ccb68146bf..20c2d41328 100644 --- a/libnetwork/cmd/networkdb-test/dummyclient/dummyClient.go +++ b/libnetwork/cmd/networkdb-test/dummyclient/dummyClient.go @@ -5,9 +5,9 @@ import ( "log" "net/http" - events "github.com/docker/go-events" "github.com/docker/docker/libnetwork/diagnostic" "github.com/docker/docker/libnetwork/networkdb" + events "github.com/docker/go-events" "github.com/sirupsen/logrus" ) @@ -29,11 +29,11 @@ type tableHandler struct { var clientWatchTable = map[string]tableHandler{} func watchTable(ctx interface{}, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck diagnostic.DebugHTTPForm(r) if len(r.Form["tname"]) < 1 { rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path)) - diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) + diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) // nolint:errcheck return } @@ -54,11 +54,11 @@ func watchTable(ctx interface{}, w http.ResponseWriter, r *http.Request) { } func watchTableEntries(ctx interface{}, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck diagnostic.DebugHTTPForm(r) if len(r.Form["tname"]) < 1 { rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path)) - diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) + diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) // nolint:errcheck return } diff --git a/libnetwork/cmd/ovrouter/ovrouter.go b/libnetwork/cmd/ovrouter/ovrouter.go index bdce1f5708..6b7bba5dc7 100644 --- a/libnetwork/cmd/ovrouter/ovrouter.go +++ b/libnetwork/cmd/ovrouter/ovrouter.go @@ -6,12 +6,12 @@ import ( "os" "os/signal" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/drivers/overlay" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/docker/pkg/reexec" "github.com/vishvananda/netlink" ) @@ -161,14 +161,13 @@ func main() { } sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, os.Interrupt, os.Kill) + signal.Notify(sigCh, os.Interrupt) - for { - select { - case <-sigCh: - r.d.Leave("testnetwork", "testep") - overlay.Fini(r.d) - os.Exit(0) + for range sigCh { + if err := r.d.Leave("testnetwork", "testep"); err != nil { + fmt.Printf("Error leaving: %v", err) } + overlay.Fini(r.d) + os.Exit(0) } } diff --git a/libnetwork/cmd/proxy/stub_proxy.go b/libnetwork/cmd/proxy/stub_proxy.go deleted file mode 100644 index 95e0e36352..0000000000 --- a/libnetwork/cmd/proxy/stub_proxy.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "net" -) - -// StubProxy is a proxy that is a stub (does nothing). -type StubProxy struct { - frontendAddr net.Addr - backendAddr net.Addr -} - -// Run does nothing. -func (p *StubProxy) Run() {} - -// Close does nothing. -func (p *StubProxy) Close() {} - -// FrontendAddr returns the frontend address. -func (p *StubProxy) FrontendAddr() net.Addr { return p.frontendAddr } - -// BackendAddr returns the backend address. -func (p *StubProxy) BackendAddr() net.Addr { return p.backendAddr } - -// NewStubProxy creates a new StubProxy -func NewStubProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) { - return &StubProxy{ - frontendAddr: frontendAddr, - backendAddr: backendAddr, - }, nil -} diff --git a/libnetwork/cmd/readme_test/readme.go b/libnetwork/cmd/readme_test/readme.go index 800eec3258..8be24ab9b6 100644 --- a/libnetwork/cmd/readme_test/readme.go +++ b/libnetwork/cmd/readme_test/readme.go @@ -4,11 +4,11 @@ import ( "fmt" "log" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork" "github.com/docker/docker/libnetwork/config" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/options" + "github.com/docker/docker/pkg/reexec" ) func main() { diff --git a/libnetwork/config/config.go b/libnetwork/config/config.go index 0fd37d64bf..e856ea8152 100644 --- a/libnetwork/config/config.go +++ b/libnetwork/config/config.go @@ -5,16 +5,16 @@ import ( "strings" "github.com/BurntSushi/toml" - "github.com/docker/docker/pkg/discovery" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/go-connections/tlsconfig" - "github.com/docker/libkv/store" "github.com/docker/docker/libnetwork/cluster" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/ipamutils" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/portallocator" + "github.com/docker/docker/pkg/discovery" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/go-connections/tlsconfig" + "github.com/docker/libkv/store" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/controller.go b/libnetwork/controller.go index 6b6a35f196..b5d8f6375f 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -52,10 +52,6 @@ import ( "sync" "time" - "github.com/docker/docker/pkg/discovery" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/docker/pkg/plugins" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/libnetwork/cluster" "github.com/docker/docker/libnetwork/config" "github.com/docker/docker/libnetwork/datastore" @@ -69,6 +65,10 @@ import ( "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/discovery" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/docker/pkg/plugins" + "github.com/docker/docker/pkg/stringid" "github.com/moby/locker" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -156,28 +156,27 @@ type SandboxWalker func(sb Sandbox) bool type sandboxTable map[string]*sandbox type controller struct { - id string - drvRegistry *drvregistry.DrvRegistry - sandboxes sandboxTable - cfg *config.Config - stores []datastore.DataStore - discovery hostdiscovery.HostDiscovery - extKeyListener net.Listener - watchCh chan *endpoint - unWatchCh chan *endpoint - svcRecords map[string]svcInfo - nmap map[string]*netWatch - serviceBindings map[serviceKey]*service - defOsSbox osl.Sandbox - ingressSandbox *sandbox - sboxOnce sync.Once - agent *agent - networkLocker *locker.Locker - agentInitDone chan struct{} - agentStopDone chan struct{} - keys []*types.EncryptionKey - clusterConfigAvailable bool - DiagnosticServer *diagnostic.Server + id string + drvRegistry *drvregistry.DrvRegistry + sandboxes sandboxTable + cfg *config.Config + stores []datastore.DataStore + discovery hostdiscovery.HostDiscovery + extKeyListener net.Listener + watchCh chan *endpoint + unWatchCh chan *endpoint + svcRecords map[string]svcInfo + nmap map[string]*netWatch + serviceBindings map[serviceKey]*service + defOsSbox osl.Sandbox + ingressSandbox *sandbox + sboxOnce sync.Once + agent *agent + networkLocker *locker.Locker + agentInitDone chan struct{} + agentStopDone chan struct{} + keys []*types.EncryptionKey + DiagnosticServer *diagnostic.Server sync.Mutex } @@ -278,10 +277,6 @@ func (c *controller) SetClusterProvider(provider cluster.Provider) { go c.clusterAgentInit() } -func isValidClusteringIP(addr string) bool { - return addr != "" && !net.ParseIP(addr).IsLoopback() && !net.ParseIP(addr).IsUnspecified() -} - // libnetwork side of agent depends on the keys. On the first receipt of // keys setup the agent. For subsequent key set handle the key change func (c *controller) SetKeys(keys []*types.EncryptionKey) error { @@ -559,13 +554,6 @@ func (c *controller) BuiltinIPAMDrivers() []string { return drivers } -func (c *controller) validateHostDiscoveryConfig() bool { - if c.cfg == nil || c.cfg.Cluster.Discovery == "" || c.cfg.Cluster.Address == "" { - return false - } - return true -} - func (c *controller) clusterHostID() string { c.Lock() defer c.Unlock() @@ -576,21 +564,6 @@ func (c *controller) clusterHostID() string { return addr[0] } -func (c *controller) isNodeAlive(node string) bool { - if c.discovery == nil { - return false - } - - nodes := c.discovery.Fetch() - for _, n := range nodes { - if n.String() == node { - return true - } - } - - return false -} - func (c *controller) initDiscovery(watcher discovery.Watcher) error { if c.cfg == nil { return fmt.Errorf("discovery initialization requires a valid configuration") @@ -716,7 +689,7 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ... if id != "" { c.networkLocker.Lock(id) - defer c.networkLocker.Unlock(id) + defer c.networkLocker.Unlock(id) // nolint:errcheck if _, err = c.NetworkByID(id); err == nil { return nil, NetworkNameError(id) @@ -827,7 +800,7 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ... err = c.addNetwork(network) if err != nil { - if _, ok := err.(types.MaskableError); ok { + if _, ok := err.(types.MaskableError); ok { // nolint:gosimple // This error can be ignored and set this boolean // value to skip a refcount increment for configOnly networks skipCfgEpCount = true diff --git a/libnetwork/datastore/datastore.go b/libnetwork/datastore/datastore.go index 826cdc7a20..4e176724d0 100644 --- a/libnetwork/datastore/datastore.go +++ b/libnetwork/datastore/datastore.go @@ -8,10 +8,10 @@ import ( "sync" "time" - "github.com/docker/libkv" - "github.com/docker/libkv/store" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/types" + "github.com/docker/libkv" + "github.com/docker/libkv/store" ) //DataStore exported diff --git a/libnetwork/datastore/mock_store.go b/libnetwork/datastore/mock_store.go index 1ec9ae9a54..80e43db53d 100644 --- a/libnetwork/datastore/mock_store.go +++ b/libnetwork/datastore/mock_store.go @@ -3,8 +3,8 @@ package datastore import ( "errors" - "github.com/docker/libkv/store" "github.com/docker/docker/libnetwork/types" + "github.com/docker/libkv/store" ) var ( @@ -125,5 +125,4 @@ func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) (bool, erro // Close closes the client connection func (s *MockStore) Close() { - return } diff --git a/libnetwork/default_gateway.go b/libnetwork/default_gateway.go index 08e19b0d62..44431a017a 100644 --- a/libnetwork/default_gateway.go +++ b/libnetwork/default_gateway.go @@ -162,18 +162,6 @@ func (ep *endpoint) endpointInGWNetwork() bool { return false } -func (sb *sandbox) getEPwithoutGateway() *endpoint { - for _, ep := range sb.getConnectedEndpoints() { - if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" { - continue - } - if len(ep.Gateway()) == 0 { - return ep - } - } - return nil -} - // Looks for the default gw network and creates it if not there. // Parallel executions are serialized. func (c *controller) defaultGwNetwork() (Network, error) { diff --git a/libnetwork/diagnostic/server.go b/libnetwork/diagnostic/server.go index d48e399b90..bab792b575 100644 --- a/libnetwork/diagnostic/server.go +++ b/libnetwork/diagnostic/server.go @@ -8,8 +8,8 @@ import ( "sync" "sync/atomic" - stackdump "github.com/docker/docker/pkg/signal" "github.com/docker/docker/libnetwork/internal/caller" + stackdump "github.com/docker/docker/pkg/signal" "github.com/sirupsen/logrus" ) @@ -108,7 +108,7 @@ func (s *Server) DisableDiagnostic() { s.Lock() defer s.Unlock() - s.srv.Shutdown(context.Background()) + s.srv.Shutdown(context.Background()) // nolint:errcheck s.srv = nil s.enable = 0 logrus.Info("Disabling the diagnostic server") @@ -122,7 +122,7 @@ func (s *Server) IsDiagnosticEnabled() bool { } func notImplemented(ctx interface{}, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck _, json := ParseHTTPFormOptions(r) rsp := WrongCommand("not implemented", fmt.Sprintf("URL path: %s no method implemented check /help\n", r.URL.Path)) @@ -130,11 +130,11 @@ func notImplemented(ctx interface{}, w http.ResponseWriter, r *http.Request) { log := logrus.WithFields(logrus.Fields{"component": "diagnostic", "remoteIP": r.RemoteAddr, "method": caller.Name(0), "url": r.URL.String()}) log.Info("command not implemented done") - HTTPReply(w, rsp, json) + HTTPReply(w, rsp, json) // nolint:errcheck } func help(ctx interface{}, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck _, json := ParseHTTPFormOptions(r) // audit logs @@ -147,22 +147,22 @@ func help(ctx interface{}, w http.ResponseWriter, r *http.Request) { for path := range n.registeredHanders { result += fmt.Sprintf("%s\n", path) } - HTTPReply(w, CommandSucceed(&StringCmd{Info: result}), json) + HTTPReply(w, CommandSucceed(&StringCmd{Info: result}), json) // nolint:errcheck } } func ready(ctx interface{}, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck _, json := ParseHTTPFormOptions(r) // audit logs log := logrus.WithFields(logrus.Fields{"component": "diagnostic", "remoteIP": r.RemoteAddr, "method": caller.Name(0), "url": r.URL.String()}) log.Info("ready done") - HTTPReply(w, CommandSucceed(&StringCmd{Info: "OK"}), json) + HTTPReply(w, CommandSucceed(&StringCmd{Info: "OK"}), json) // nolint:errcheck } func stackTrace(ctx interface{}, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck _, json := ParseHTTPFormOptions(r) // audit logs @@ -172,10 +172,10 @@ func stackTrace(ctx interface{}, w http.ResponseWriter, r *http.Request) { path, err := stackdump.DumpStacks("/tmp/") if err != nil { log.WithError(err).Error("failed to write goroutines dump") - HTTPReply(w, FailCommand(err), json) + HTTPReply(w, FailCommand(err), json) // nolint:errcheck } else { log.Info("stack trace done") - HTTPReply(w, CommandSucceed(&StringCmd{Info: fmt.Sprintf("goroutine stacks written to %s", path)}), json) + HTTPReply(w, CommandSucceed(&StringCmd{Info: fmt.Sprintf("goroutine stacks written to %s", path)}), json) // nolint:errcheck } } diff --git a/libnetwork/driverapi/driverapi.go b/libnetwork/driverapi/driverapi.go index 59d8e73c25..e9a0b5b1d3 100644 --- a/libnetwork/driverapi/driverapi.go +++ b/libnetwork/driverapi/driverapi.go @@ -3,8 +3,8 @@ package driverapi import ( "net" - "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/libnetwork/discoverapi" + "github.com/docker/docker/pkg/plugingetter" ) // NetworkPluginEndpointType represents the Endpoint Type used by Plugin system diff --git a/libnetwork/drivers/bridge/bridge.go b/libnetwork/drivers/bridge/bridge.go index a4adf17b86..925690847b 100644 --- a/libnetwork/drivers/bridge/bridge.go +++ b/libnetwork/drivers/bridge/bridge.go @@ -144,7 +144,6 @@ type bridgeNetwork struct { type driver struct { config *configuration - network *bridgeNetwork natChain *iptables.ChainInfo filterChain *iptables.ChainInfo isolationChain1 *iptables.ChainInfo @@ -399,7 +398,9 @@ func (d *driver) configure(option map[string]interface{}) error { // Make sure on firewall reload, first thing being re-played is chains creation iptables.OnReloaded(func() { logrus.Debugf("Recreating iptables chains on firewall reload") - setupIPChains(config, iptables.IPv4) + if _, _, _, _, err := setupIPChains(config, iptables.IPv4); err != nil { + logrus.WithError(err).Error("Error reloading iptables chains") + } }) } @@ -414,7 +415,9 @@ func (d *driver) configure(option map[string]interface{}) error { // Make sure on firewall reload, first thing being re-played is chains creation iptables.OnReloaded(func() { logrus.Debugf("Recreating ip6tables chains on firewall reload") - setupIPChains(config, iptables.IPv6) + if _, _, _, _, err := setupIPChains(config, iptables.IPv6); err != nil { + logrus.WithError(err).Error("Error reloading ip6tables chains") + } }) } @@ -549,7 +552,7 @@ func parseNetworkOptions(id string, option options.Generic) (*networkConfigurati return nil, err } - if config.BridgeName == "" && config.DefaultBridge == false { + if config.BridgeName == "" && !config.DefaultBridge { config.BridgeName = "br-" + id[:12] } @@ -568,18 +571,6 @@ func parseNetworkOptions(id string, option options.Generic) (*networkConfigurati return config, nil } -// Returns the non link-local IPv6 subnet for the containers attached to this bridge if found, nil otherwise -func getV6Network(config *networkConfiguration, i *bridgeInterface) *net.IPNet { - if config.AddressIPv6 != nil { - return config.AddressIPv6 - } - if i.bridgeIPv6 != nil && i.bridgeIPv6.IP != nil && !i.bridgeIPv6.IP.IsLinkLocalUnicast() { - return i.bridgeIPv6 - } - - return nil -} - // Return a slice of networks over which caller can iterate safely func (d *driver) getNetworks() []*bridgeNetwork { d.Lock() @@ -643,7 +634,9 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d } // Got a conflict with a stale default network, clean that up and continue logrus.Warn(nerr) - d.deleteNetwork(nerr.ID) + if err := d.deleteNetwork(nerr.ID); err != nil { + logrus.WithError(err).Debug("Error while cleaning up network on conflict") + } } // there is no conflict, now create the network diff --git a/libnetwork/drivers/bridge/bridge_test.go b/libnetwork/drivers/bridge/bridge_test.go index 2495787c2e..242a5c9c55 100644 --- a/libnetwork/drivers/bridge/bridge_test.go +++ b/libnetwork/drivers/bridge/bridge_test.go @@ -455,16 +455,24 @@ func TestCreateMultipleNetworks(t *testing.T) { verifyV4INCEntries(d.networks, t) - d.DeleteNetwork("1") + if err := d.DeleteNetwork("1"); err != nil { + t.Log(err) + } verifyV4INCEntries(d.networks, t) - d.DeleteNetwork("2") + if err := d.DeleteNetwork("2"); err != nil { + t.Log(err) + } verifyV4INCEntries(d.networks, t) - d.DeleteNetwork("3") + if err := d.DeleteNetwork("3"); err != nil { + t.Log(err) + } verifyV4INCEntries(d.networks, t) - d.DeleteNetwork("4") + if err := d.DeleteNetwork("4"); err != nil { + t.Log(err) + } verifyV4INCEntries(d.networks, t) } @@ -503,12 +511,10 @@ type testInterface struct { } type testEndpoint struct { - iface *testInterface - gw net.IP - gw6 net.IP - hostsPath string - resolvConfPath string - routes []types.StaticRoute + iface *testInterface + gw net.IP + gw6 net.IP + routes []types.StaticRoute } func newTestEndpoint(nw *net.IPNet, ordinal byte) *testEndpoint { @@ -662,7 +668,7 @@ func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) { if !ok { t.Fatalf("Cannot find network %s inside driver", "net1") } - ep, _ := network.endpoints["ep1"] + ep := network.endpoints["ep1"] data, err := d.EndpointOperInfo(network.id, ep.id) if err != nil { t.Fatalf("Failed to ask for endpoint operational data: %v", err) @@ -794,7 +800,7 @@ func TestLinkContainers(t *testing.T) { t.Fatalf("Failed to program external connectivity: %v", err) } - out, err := iptable.Raw("-L", DockerChain) + out, _ := iptable.Raw("-L", DockerChain) for _, pm := range exposedPorts { regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port) re := regexp.MustCompile(regex) @@ -820,7 +826,7 @@ func TestLinkContainers(t *testing.T) { t.Fatal("Failed to unlink ep1 and ep2") } - out, err = iptable.Raw("-L", DockerChain) + out, _ = iptable.Raw("-L", DockerChain) for _, pm := range exposedPorts { regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port) re := regexp.MustCompile(regex) @@ -848,7 +854,7 @@ func TestLinkContainers(t *testing.T) { } err = d.ProgramExternalConnectivity("net1", "ep2", sbOptions) if err != nil { - out, err = iptable.Raw("-L", DockerChain) + out, _ = iptable.Raw("-L", DockerChain) for _, pm := range exposedPorts { regex := fmt.Sprintf("%s dpt:%d", pm.Proto.String(), pm.Port) re := regexp.MustCompile(regex) diff --git a/libnetwork/drivers/bridge/netlink_deprecated_linux.go b/libnetwork/drivers/bridge/netlink_deprecated_linux.go index c3cc6ba80a..fd25c9f376 100644 --- a/libnetwork/drivers/bridge/netlink_deprecated_linux.go +++ b/libnetwork/drivers/bridge/netlink_deprecated_linux.go @@ -2,10 +2,8 @@ package bridge import ( "fmt" - "math/rand" "net" "syscall" - "time" "unsafe" ) @@ -25,8 +23,6 @@ type ifreqHwaddr struct { IfruHwaddr syscall.RawSockaddr } -var rnd = rand.New(rand.NewSource(time.Now().UnixNano())) - // THIS CODE DOES NOT COMMUNICATE WITH KERNEL VIA RTNETLINK INTERFACE // IT IS HERE FOR BACKWARDS COMPATIBILITY WITH OLDER LINUX KERNELS // WHICH SHIP WITH OLDER NOT ENTIRELY FUNCTIONAL VERSION OF NETLINK diff --git a/libnetwork/drivers/bridge/port_mapping_test.go b/libnetwork/drivers/bridge/port_mapping_test.go index 43e4aa4631..483e91a91e 100644 --- a/libnetwork/drivers/bridge/port_mapping_test.go +++ b/libnetwork/drivers/bridge/port_mapping_test.go @@ -6,10 +6,10 @@ import ( "os" "testing" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/testutils" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/reexec" ) func TestMain(m *testing.M) { @@ -71,7 +71,7 @@ func TestPortMappingConfig(t *testing.T) { if !ok { t.Fatalf("Cannot find network %s inside driver", "dummy") } - ep, _ := network.endpoints["ep1"] + ep := network.endpoints["ep1"] if len(ep.portMapping) != 3 { t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping) } @@ -152,7 +152,7 @@ func TestPortMappingV6Config(t *testing.T) { if !ok { t.Fatalf("Cannot find network %s inside driver", "dummy") } - ep, _ := network.endpoints["ep1"] + ep := network.endpoints["ep1"] if len(ep.portMapping) != 6 { t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping) } diff --git a/libnetwork/drivers/bridge/setup_device_test.go b/libnetwork/drivers/bridge/setup_device_test.go index 16b0572f9e..55ccdb8184 100644 --- a/libnetwork/drivers/bridge/setup_device_test.go +++ b/libnetwork/drivers/bridge/setup_device_test.go @@ -90,7 +90,7 @@ func TestGenerateRandomMAC(t *testing.T) { mac1 := netutils.GenerateRandomMAC() mac2 := netutils.GenerateRandomMAC() - if bytes.Compare(mac1, mac2) == 0 { + if bytes.Equal(mac1, mac2) { t.Fatalf("Generated twice the same MAC address %v", mac1) } } diff --git a/libnetwork/drivers/bridge/setup_ip_forwarding_test.go b/libnetwork/drivers/bridge/setup_ip_forwarding_test.go index 53fd654f00..a3425b7bc5 100644 --- a/libnetwork/drivers/bridge/setup_ip_forwarding_test.go +++ b/libnetwork/drivers/bridge/setup_ip_forwarding_test.go @@ -14,7 +14,7 @@ func TestSetupIPForwarding(t *testing.T) { defer reconcileIPForwardingSetting(t, procSetting) // Disable IP Forwarding if enabled - if bytes.Compare(procSetting, []byte("1\n")) == 0 { + if bytes.Equal(procSetting, []byte("1\n")) { writeIPForwardingSetting(t, []byte{'0', '\n'}) } @@ -25,7 +25,7 @@ func TestSetupIPForwarding(t *testing.T) { // Read new setting procSetting = readCurrentIPForwardingSetting(t) - if bytes.Compare(procSetting, []byte("1\n")) != 0 { + if !bytes.Equal(procSetting, []byte("1\n")) { t.Fatal("Failed to effectively setup IP forwarding") } } @@ -47,7 +47,7 @@ func writeIPForwardingSetting(t *testing.T, chars []byte) { func reconcileIPForwardingSetting(t *testing.T, original []byte) { current := readCurrentIPForwardingSetting(t) - if bytes.Compare(original, current) != 0 { + if !bytes.Equal(original, current) { writeIPForwardingSetting(t, original) } } diff --git a/libnetwork/drivers/bridge/setup_ip_tables.go b/libnetwork/drivers/bridge/setup_ip_tables.go index 658ac0150f..b9dab9e75d 100644 --- a/libnetwork/drivers/bridge/setup_ip_tables.go +++ b/libnetwork/drivers/bridge/setup_ip_tables.go @@ -30,7 +30,7 @@ const ( func setupIPChains(config *configuration, version iptables.IPVersion) (*iptables.ChainInfo, *iptables.ChainInfo, *iptables.ChainInfo, *iptables.ChainInfo, error) { // Sanity check. - if config.EnableIPTables == false { + if !config.EnableIPTables { return nil, nil, nil, nil, errors.New("cannot create new chains, EnableIPTable is disabled") } diff --git a/libnetwork/drivers/bridge/setup_ipv6_test.go b/libnetwork/drivers/bridge/setup_ipv6_test.go index de9eb4580e..d27388f445 100644 --- a/libnetwork/drivers/bridge/setup_ipv6_test.go +++ b/libnetwork/drivers/bridge/setup_ipv6_test.go @@ -32,7 +32,7 @@ func TestSetupIPv6(t *testing.T) { t.Fatalf("Failed to read disable_ipv6 kernel setting: %v", err) } - if expected := []byte("0\n"); bytes.Compare(expected, procSetting) != 0 { + if expected := []byte("0\n"); !bytes.Equal(expected, procSetting) { t.Fatalf("Invalid kernel setting disable_ipv6: expected %q, got %q", string(expected), string(procSetting)) } diff --git a/libnetwork/drivers/bridge/setup_verify.go b/libnetwork/drivers/bridge/setup_verify.go index fd3fc10146..00baa6418c 100644 --- a/libnetwork/drivers/bridge/setup_verify.go +++ b/libnetwork/drivers/bridge/setup_verify.go @@ -40,7 +40,7 @@ func setupVerifyAndReconcile(config *networkConfiguration, i *bridgeInterface) e // Release any residual IPv6 address that might be there because of older daemon instances for _, addrv6 := range addrsv6 { if addrv6.IP.IsGlobalUnicast() && !types.CompareIPNet(addrv6.IPNet, i.bridgeIPv6) { - if err := i.nlh.AddrDel(i.Link, &addrv6); err != nil { + if err := i.nlh.AddrDel(i.Link, &addrv6); err != nil { // nolint:gosec logrus.Warnf("Failed to remove residual IPv6 address %s from bridge: %v", addrv6.IPNet, err) } } diff --git a/libnetwork/drivers/host/host.go b/libnetwork/drivers/host/host.go index ffd4f039e7..b99049ff37 100644 --- a/libnetwork/drivers/host/host.go +++ b/libnetwork/drivers/host/host.go @@ -66,7 +66,7 @@ func (d *driver) DeleteEndpoint(nid, eid string) error { } func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) { - return make(map[string]interface{}, 0), nil + return make(map[string]interface{}), nil } // Join method is invoked when a Sandbox is attached to an endpoint. diff --git a/libnetwork/drivers/ipvlan/ipvlan.go b/libnetwork/drivers/ipvlan/ipvlan.go index 17868e3783..d04ca5055d 100644 --- a/libnetwork/drivers/ipvlan/ipvlan.go +++ b/libnetwork/drivers/ipvlan/ipvlan.go @@ -9,7 +9,6 @@ import ( "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/driverapi" - "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" ) @@ -50,7 +49,6 @@ type endpoint struct { type network struct { id string - sbox osl.Sandbox endpoints endpointTable driver *driver config *configuration @@ -66,7 +64,9 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { d := &driver{ networks: networkTable{}, } - d.initStore(config) + if err := d.initStore(config); err != nil { + return err + } return dc.RegisterDriver(ipvlanType, d, c) } @@ -80,7 +80,7 @@ func (d *driver) NetworkFree(id string) error { } func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) { - return make(map[string]interface{}, 0), nil + return make(map[string]interface{}), nil } func (d *driver) Type() string { diff --git a/libnetwork/drivers/ipvlan/ipvlan_network.go b/libnetwork/drivers/ipvlan/ipvlan_network.go index 4e72395373..361a761927 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_network.go +++ b/libnetwork/drivers/ipvlan/ipvlan_network.go @@ -5,14 +5,14 @@ package ipvlan import ( "fmt" - "github.com/docker/docker/pkg/parsers/kernel" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/ns" "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/parsers/kernel" + "github.com/docker/docker/pkg/stringid" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/drivers/ipvlan/ipvlan_setup.go b/libnetwork/drivers/ipvlan/ipvlan_setup.go index 5504c6d1c1..aa930b8c14 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_setup.go +++ b/libnetwork/drivers/ipvlan/ipvlan_setup.go @@ -65,11 +65,7 @@ func setIPVlanMode(mode string) (netlink.IPVlanMode, error) { // parentExists check if the specified interface exists in the default namespace func parentExists(ifaceStr string) bool { _, err := ns.NlHandle().LinkByName(ifaceStr) - if err != nil { - return false - } - - return true + return err == nil } // createVlanLink parses sub-interfaces and vlan id for creation diff --git a/libnetwork/drivers/ipvlan/ipvlan_state.go b/libnetwork/drivers/ipvlan/ipvlan_state.go index 213977de4c..33eb2e55b1 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_state.go +++ b/libnetwork/drivers/ipvlan/ipvlan_state.go @@ -5,7 +5,6 @@ package ipvlan import ( "fmt" - "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" "github.com/sirupsen/logrus" ) @@ -89,19 +88,6 @@ func validateID(nid, eid string) error { return nil } -func (n *network) sandbox() osl.Sandbox { - n.Lock() - defer n.Unlock() - - return n.sbox -} - -func (n *network) setSandbox(sbox osl.Sandbox) { - n.Lock() - n.sbox = sbox - n.Unlock() -} - func (d *driver) getNetwork(id string) (*network, error) { d.Lock() defer d.Unlock() diff --git a/libnetwork/drivers/ipvlan/ipvlan_test.go b/libnetwork/drivers/ipvlan/ipvlan_test.go index f30e5d0451..f531fdfb21 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_test.go +++ b/libnetwork/drivers/ipvlan/ipvlan_test.go @@ -5,9 +5,9 @@ package ipvlan import ( "testing" - "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/libnetwork/driverapi" _ "github.com/docker/docker/libnetwork/testutils" + "github.com/docker/docker/pkg/plugingetter" ) const testNetworkType = "ipvlan" diff --git a/libnetwork/drivers/macvlan/macvlan.go b/libnetwork/drivers/macvlan/macvlan.go index 217314ad4c..9e5277e297 100644 --- a/libnetwork/drivers/macvlan/macvlan.go +++ b/libnetwork/drivers/macvlan/macvlan.go @@ -9,7 +9,6 @@ import ( "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/driverapi" - "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" ) @@ -52,7 +51,6 @@ type endpoint struct { type network struct { id string - sbox osl.Sandbox endpoints endpointTable driver *driver config *configuration @@ -68,7 +66,9 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { d := &driver{ networks: networkTable{}, } - d.initStore(config) + if err := d.initStore(config); err != nil { + return err + } return dc.RegisterDriver(macvlanType, d, c) } @@ -82,7 +82,7 @@ func (d *driver) NetworkFree(id string) error { } func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) { - return make(map[string]interface{}, 0), nil + return make(map[string]interface{}), nil } func (d *driver) Type() string { diff --git a/libnetwork/drivers/macvlan/macvlan_network.go b/libnetwork/drivers/macvlan/macvlan_network.go index a86d6fcb38..e4386c9faa 100644 --- a/libnetwork/drivers/macvlan/macvlan_network.go +++ b/libnetwork/drivers/macvlan/macvlan_network.go @@ -5,13 +5,13 @@ package macvlan import ( "fmt" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/ns" "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/stringid" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/drivers/macvlan/macvlan_setup.go b/libnetwork/drivers/macvlan/macvlan_setup.go index d5a80d40a9..118afb4ad7 100644 --- a/libnetwork/drivers/macvlan/macvlan_setup.go +++ b/libnetwork/drivers/macvlan/macvlan_setup.go @@ -67,11 +67,7 @@ func setMacVlanMode(mode string) (netlink.MacvlanMode, error) { // parentExists checks if the specified interface exists in the default namespace func parentExists(ifaceStr string) bool { _, err := ns.NlHandle().LinkByName(ifaceStr) - if err != nil { - return false - } - - return true + return err == nil } // createVlanLink parses sub-interfaces and vlan id for creation diff --git a/libnetwork/drivers/macvlan/macvlan_state.go b/libnetwork/drivers/macvlan/macvlan_state.go index b5654365fc..fbf05f4d63 100644 --- a/libnetwork/drivers/macvlan/macvlan_state.go +++ b/libnetwork/drivers/macvlan/macvlan_state.go @@ -5,7 +5,6 @@ package macvlan import ( "fmt" - "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/types" "github.com/sirupsen/logrus" ) @@ -88,19 +87,6 @@ func validateID(nid, eid string) error { return nil } -func (n *network) sandbox() osl.Sandbox { - n.Lock() - defer n.Unlock() - - return n.sbox -} - -func (n *network) setSandbox(sbox osl.Sandbox) { - n.Lock() - n.sbox = sbox - n.Unlock() -} - func (d *driver) getNetwork(id string) (*network, error) { d.Lock() defer d.Unlock() diff --git a/libnetwork/drivers/macvlan/macvlan_test.go b/libnetwork/drivers/macvlan/macvlan_test.go index 0f8562f427..310e2d6294 100644 --- a/libnetwork/drivers/macvlan/macvlan_test.go +++ b/libnetwork/drivers/macvlan/macvlan_test.go @@ -5,9 +5,9 @@ package macvlan import ( "testing" - "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/libnetwork/driverapi" _ "github.com/docker/docker/libnetwork/testutils" + "github.com/docker/docker/pkg/plugingetter" ) const testNetworkType = "macvlan" diff --git a/libnetwork/drivers/null/null.go b/libnetwork/drivers/null/null.go index c5728966dd..c54db37513 100644 --- a/libnetwork/drivers/null/null.go +++ b/libnetwork/drivers/null/null.go @@ -65,7 +65,7 @@ func (d *driver) DeleteEndpoint(nid, eid string) error { } func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) { - return make(map[string]interface{}, 0), nil + return make(map[string]interface{}), nil } // Join method is invoked when a Sandbox is attached to an endpoint. diff --git a/libnetwork/drivers/overlay/encryption.go b/libnetwork/drivers/overlay/encryption.go index 228f6dd5a3..5527b8f2e0 100644 --- a/libnetwork/drivers/overlay/encryption.go +++ b/libnetwork/drivers/overlay/encryption.go @@ -629,7 +629,7 @@ func clearEncryptionStates() { } for _, sp := range spList { if sp.Mark != nil && sp.Mark.Value == spMark.Value { - if err := nlh.XfrmPolicyDel(&sp); err != nil { + if err := nlh.XfrmPolicyDel(&sp); err != nil { // nolint:gosec logrus.Warnf("Failed to delete stale SP %s: %v", sp, err) continue } @@ -638,7 +638,7 @@ func clearEncryptionStates() { } for _, sa := range saList { if sa.Reqid == r { - if err := nlh.XfrmStateDel(&sa); err != nil { + if err := nlh.XfrmStateDel(&sa); err != nil { // nolint:gosec logrus.Warnf("Failed to delete stale SA %s: %v", sa, err) continue } diff --git a/libnetwork/drivers/overlay/ov_endpoint.go b/libnetwork/drivers/overlay/ov_endpoint.go index 48676f9c5e..954693c4ac 100644 --- a/libnetwork/drivers/overlay/ov_endpoint.go +++ b/libnetwork/drivers/overlay/ov_endpoint.go @@ -138,7 +138,7 @@ func (d *driver) DeleteEndpoint(nid, eid string) error { } func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) { - return make(map[string]interface{}, 0), nil + return make(map[string]interface{}), nil } func (d *driver) deleteEndpointFromStore(e *endpoint) error { diff --git a/libnetwork/drivers/overlay/ov_network.go b/libnetwork/drivers/overlay/ov_network.go index be5ec0c01d..4cd121dd62 100644 --- a/libnetwork/drivers/overlay/ov_network.go +++ b/libnetwork/drivers/overlay/ov_network.go @@ -15,7 +15,6 @@ import ( "strings" "sync" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/netlabel" @@ -24,6 +23,7 @@ import ( "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/resolvconf" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/reexec" "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" "github.com/vishvananda/netlink/nl" @@ -415,7 +415,9 @@ func (n *network) destroySandbox() { func populateVNITbl() { filepath.Walk(filepath.Dir(osl.GenerateKey("walk")), - func(path string, info os.FileInfo, err error) error { + // NOTE(cpuguy83): The linter picked up on the fact that this walk function was not using this error argument + // That seems wrong... however I'm not familiar with this code or if that error matters + func(path string, info os.FileInfo, _ error) error { _, fname := filepath.Split(path) if len(strings.Split(fname, "-")) <= 1 { diff --git a/libnetwork/drivers/overlay/ov_serf.go b/libnetwork/drivers/overlay/ov_serf.go index c6fba3ebc5..e327a4580d 100644 --- a/libnetwork/drivers/overlay/ov_serf.go +++ b/libnetwork/drivers/overlay/ov_serf.go @@ -151,7 +151,7 @@ func (d *driver) resolvePeer(nid string, peerIP net.IP) (net.HardwareAddr, net.I return nil, nil, nil, fmt.Errorf("could not resolve peer: serf instance not initialized") } - qPayload := fmt.Sprintf("%s %s", string(nid), peerIP.String()) + qPayload := fmt.Sprintf("%s %s", nid, peerIP.String()) resp, err := d.serfInstance.Query("peerlookup", []byte(qPayload), nil) if err != nil { return nil, nil, nil, fmt.Errorf("resolving peer by querying the cluster failed: %v", err) diff --git a/libnetwork/drivers/overlay/overlay_test.go b/libnetwork/drivers/overlay/overlay_test.go index 0f66384354..70facb6d12 100644 --- a/libnetwork/drivers/overlay/overlay_test.go +++ b/libnetwork/drivers/overlay/overlay_test.go @@ -13,13 +13,13 @@ import ( "golang.org/x/sys/unix" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/libkv/store/consul" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/netlabel" _ "github.com/docker/docker/libnetwork/testutils" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/libkv/store/consul" "github.com/vishvananda/netlink/nl" ) diff --git a/libnetwork/drivers/overlay/ovmanager/ovmanager.go b/libnetwork/drivers/overlay/ovmanager/ovmanager.go index cbd54e1c63..fcb0ea9cff 100644 --- a/libnetwork/drivers/overlay/ovmanager/ovmanager.go +++ b/libnetwork/drivers/overlay/ovmanager/ovmanager.go @@ -27,7 +27,6 @@ type networkTable map[string]*network type driver struct { config map[string]interface{} networks networkTable - store datastore.DataStore vxlanIdm *idm.Idm sync.Mutex } diff --git a/libnetwork/drivers/overlay/peerdb.go b/libnetwork/drivers/overlay/peerdb.go index e4ed41b9c2..6b5df0a5af 100644 --- a/libnetwork/drivers/overlay/peerdb.go +++ b/libnetwork/drivers/overlay/peerdb.go @@ -134,7 +134,7 @@ func (d *driver) peerDbNetworkWalk(nid string, f func(*peerKey, *peerEntry) bool if _, err := fmt.Sscan(pKeyStr, &pKey); err != nil { logrus.Warnf("Peer key scan on network %s failed: %v", nid, err) } - if f(&pKey, &pEntry) { + if f(&pKey, &pEntry) { // nolint:gosec return nil } } diff --git a/libnetwork/drivers/remote/driver.go b/libnetwork/drivers/remote/driver.go index d0848319c9..a21a34e6b7 100644 --- a/libnetwork/drivers/remote/driver.go +++ b/libnetwork/drivers/remote/driver.go @@ -4,13 +4,13 @@ import ( "fmt" "net" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/docker/pkg/plugins" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/drivers/remote/api" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/docker/pkg/plugins" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/drvregistry/drvregistry.go b/libnetwork/drvregistry/drvregistry.go index ab80012969..9063c472bf 100644 --- a/libnetwork/drvregistry/drvregistry.go +++ b/libnetwork/drvregistry/drvregistry.go @@ -6,10 +6,10 @@ import ( "strings" "sync" - "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/libnetwork/driverapi" "github.com/docker/docker/libnetwork/ipamapi" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/plugingetter" ) type driverData struct { diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index af88d72222..d95f9ae1c5 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/endpoint.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net" - "strings" "sync" "github.com/docker/docker/libnetwork/datastore" @@ -60,7 +59,6 @@ type endpoint struct { anonymous bool disableResolution bool generic map[string]interface{} - joinLeaveDone chan struct{} prefAddress net.IP prefAddressV6 net.IP ipamOptions map[string]string @@ -117,19 +115,25 @@ func (ep *endpoint) UnmarshalJSON(b []byte) (err error) { ep.name = epMap["name"].(string) ep.id = epMap["id"].(string) + // TODO(cpuguy83): So yeah, this isn't checking any errors anywhere. + // Seems like we should be checking errors even because of memory related issues that can arise. + // Alas it seems like given the nature of this data we could introduce problems if we start checking these errors. + // + // If anyone ever comes here and figures out one way or another if we can/should be checking these errors and it turns out we can't... then please document *why* + ib, _ := json.Marshal(epMap["ep_iface"]) - json.Unmarshal(ib, &ep.iface) + json.Unmarshal(ib, &ep.iface) // nolint:errcheck jb, _ := json.Marshal(epMap["joinInfo"]) - json.Unmarshal(jb, &ep.joinInfo) + json.Unmarshal(jb, &ep.joinInfo) // nolint:errcheck tb, _ := json.Marshal(epMap["exposed_ports"]) var tPorts []types.TransportPort - json.Unmarshal(tb, &tPorts) + json.Unmarshal(tb, &tPorts) // nolint:errcheck ep.exposedPorts = tPorts cb, _ := json.Marshal(epMap["sandbox"]) - json.Unmarshal(cb, &ep.sandboxID) + json.Unmarshal(cb, &ep.sandboxID) // nolint:errcheck if v, ok := epMap["generic"]; ok { ep.generic = v.(map[string]interface{}) @@ -208,17 +212,17 @@ func (ep *endpoint) UnmarshalJSON(b []byte) (err error) { sal, _ := json.Marshal(epMap["svcAliases"]) var svcAliases []string - json.Unmarshal(sal, &svcAliases) + json.Unmarshal(sal, &svcAliases) // nolint:errcheck ep.svcAliases = svcAliases pc, _ := json.Marshal(epMap["ingressPorts"]) var ingressPorts []*PortConfig - json.Unmarshal(pc, &ingressPorts) + json.Unmarshal(pc, &ingressPorts) // nolint:errcheck ep.ingressPorts = ingressPorts ma, _ := json.Marshal(epMap["myAliases"]) var myAliases []string - json.Unmarshal(ma, &myAliases) + json.Unmarshal(ma, &myAliases) // nolint:errcheck ep.myAliases = myAliases return nil } @@ -253,12 +257,16 @@ func (ep *endpoint) CopyTo(o datastore.KVObject) error { if ep.iface != nil { dstEp.iface = &endpointInterface{} - ep.iface.CopyTo(dstEp.iface) + if err := ep.iface.CopyTo(dstEp.iface); err != nil { + return err + } } if ep.joinInfo != nil { dstEp.joinInfo = &endpointJoinInfo{} - ep.joinInfo.CopyTo(dstEp.joinInfo) + if err := ep.joinInfo.CopyTo(dstEp.joinInfo); err != nil { + return err + } } dstEp.exposedPorts = make([]types.TransportPort, len(ep.exposedPorts)) @@ -354,17 +362,6 @@ func (ep *endpoint) KeyPrefix() []string { return []string{datastore.EndpointKeyPrefix, ep.network.id} } -func (ep *endpoint) networkIDFromKey(key string) (string, error) { - // endpoint Key structure : docker/libnetwork/endpoint/${network-id}/${endpoint-id} - // it's an invalid key if the key doesn't have all the 5 key elements above - keyElements := strings.Split(key, "/") - if !strings.HasPrefix(key, datastore.Key(datastore.EndpointKeyPrefix)) || len(keyElements) < 5 { - return "", fmt.Errorf("invalid endpoint key : %v", key) - } - // network-id is placed at index=3. pls refer to endpoint.Key() method - return strings.Split(key, "/")[3], nil -} - func (ep *endpoint) Value() []byte { b, err := json.Marshal(ep) if err != nil { @@ -650,10 +647,14 @@ func (ep *endpoint) rename(name string) error { } defer func() { if err != nil { - ep.deleteServiceInfoFromCluster(sb, true, "rename") + if err2 := ep.deleteServiceInfoFromCluster(sb, true, "rename"); err2 != nil { + logrus.WithField("main error", err).WithError(err2).Debug("Error during cleanup due deleting service info from cluster while cleaning up due to other error") + } ep.name = oldName ep.anonymous = oldAnonymous - ep.addServiceInfoToCluster(sb) + if err2 := ep.addServiceInfoToCluster(sb); err2 != nil { + logrus.WithField("main error", err).WithError(err2).Debug("Error during cleanup due adding service to from cluster while cleaning up due to other error") + } } }() } else { @@ -679,7 +680,7 @@ func (ep *endpoint) rename(name string) error { // benign error. Besides there is no meaningful recovery that // we can do. When the cluster recovers subsequent EpCnt update // will force the peers to get the correct EP name. - n.getEpCnt().updateStore() + n.getEpCnt().updateStore() // nolint:errcheck return err } @@ -1226,7 +1227,9 @@ func (c *controller) cleanupLocalEndpoints() { epCnt := n.getEpCnt().EndpointCnt() if epCnt != uint64(len(epl)) { logrus.Infof("Fixing inconsistent endpoint_cnt for network %s. Expected=%d, Actual=%d", n.name, len(epl), epCnt) - n.getEpCnt().setCnt(uint64(len(epl))) + if err := n.getEpCnt().setCnt(uint64(len(epl))); err != nil { + logrus.WithField("network", n.name).WithError(err).Warn("Error while fixing inconsistent endpoint_cnt for network") + } } } } diff --git a/libnetwork/endpoint_info.go b/libnetwork/endpoint_info.go index 40b6031844..0e20bd3362 100644 --- a/libnetwork/endpoint_info.go +++ b/libnetwork/endpoint_info.go @@ -135,7 +135,10 @@ func (epi *endpointInterface) UnmarshalJSON(b []byte) error { rb, _ := json.Marshal(epMap["routes"]) var routes []string - json.Unmarshal(rb, &routes) + + // TODO(cpuguy83): linter noticed we don't check the error here... no idea why but it seems like it could introduce problems if we start checking + json.Unmarshal(rb, &routes) // nolint:errcheck + epi.routes = make([]*net.IPNet, 0) for _, route := range routes { ip, ipr, err := net.ParseCIDR(route) @@ -436,11 +439,16 @@ func (epj *endpointJoinInfo) UnmarshalJSON(b []byte) error { if v, ok := epMap["StaticRoutes"]; ok { tb, _ := json.Marshal(v) var tStaticRoute []types.StaticRoute - json.Unmarshal(tb, &tStaticRoute) + // TODO(cpuguy83): Linter caught that we aren't checking errors here + // I don't know why we aren't other than potentially the data is not always expected to be right? + // This is why I'm not adding the error check. + // + // In any case for posterity please if you figure this out document it or check the error + json.Unmarshal(tb, &tStaticRoute) // nolint:errcheck } var StaticRoutes []*types.StaticRoute for _, r := range tStaticRoute { - StaticRoutes = append(StaticRoutes, &r) + StaticRoutes = append(StaticRoutes, &r) // nolint:gosec } epj.StaticRoutes = StaticRoutes diff --git a/libnetwork/etchosts/etchosts_test.go b/libnetwork/etchosts/etchosts_test.go index 5569ce3285..f6023b6ab6 100644 --- a/libnetwork/etchosts/etchosts_test.go +++ b/libnetwork/etchosts/etchosts_test.go @@ -5,10 +5,10 @@ import ( "fmt" "io/ioutil" "os" - "sync" "testing" _ "github.com/docker/docker/libnetwork/testutils" + "golang.org/x/sync/errgroup" ) func TestBuildDefault(t *testing.T) { @@ -431,12 +431,10 @@ func TestConcurrentWrites(t *testing.T) { t.Fatal(err) } - var wg sync.WaitGroup + group := new(errgroup.Group) for i := 0; i < 10; i++ { - wg.Add(1) - go func(i int) { - defer wg.Done() - + i := i + group.Go(func() error { rec := []Record{ { IP: fmt.Sprintf("%d.%d.%d.%d", i, i, i, i), @@ -446,17 +444,20 @@ func TestConcurrentWrites(t *testing.T) { for j := 0; j < 25; j++ { if err := Add(file.Name(), rec); err != nil { - t.Fatal(err) + return err } if err := Delete(file.Name(), rec); err != nil { - t.Fatal(err) + return err } } - }(i) + return nil + }) } - wg.Wait() + if err := group.Wait(); err != nil { + t.Fatal(err) + } content, err := ioutil.ReadFile(file.Name()) if err != nil { diff --git a/libnetwork/hostdiscovery/hostdiscovery.go b/libnetwork/hostdiscovery/hostdiscovery.go index 2512827f19..a5dea04c29 100644 --- a/libnetwork/hostdiscovery/hostdiscovery.go +++ b/libnetwork/hostdiscovery/hostdiscovery.go @@ -8,12 +8,13 @@ import ( mapset "github.com/deckarep/golang-set" "github.com/docker/docker/pkg/discovery" + // Including KV - _ "github.com/docker/docker/pkg/discovery/kv" + "github.com/docker/docker/libnetwork/types" + _ "github.com/docker/docker/pkg/discovery/kv" // register all the things with host discovery "github.com/docker/libkv/store/consul" "github.com/docker/libkv/store/etcd" "github.com/docker/libkv/store/zookeeper" - "github.com/docker/docker/libnetwork/types" ) type hostDiscovery struct { diff --git a/libnetwork/ipam/allocator.go b/libnetwork/ipam/allocator.go index db3e0d3447..d7ed917637 100644 --- a/libnetwork/ipam/allocator.go +++ b/libnetwork/ipam/allocator.go @@ -18,9 +18,6 @@ import ( const ( localAddressSpace = "LocalDefault" globalAddressSpace = "GlobalDefault" - // The biggest configurable host subnets - minNetSize = 8 - minNetSizeV6 = 64 // datastore keyes for ipam objects dsConfigKey = "ipam/" + ipamapi.DefaultIPAM + "/config" dsDataKey = "ipam/" + ipamapi.DefaultIPAM + "/data" @@ -103,11 +100,9 @@ func (a *Allocator) updateBitMasks(aSpace *addrSpace) error { aSpace.Unlock() // Add the bitmasks (data could come from datastore) - if inserterList != nil { - for _, f := range inserterList { - if err := f(); err != nil { - return err - } + for _, f := range inserterList { + if err := f(); err != nil { + return err } } @@ -572,7 +567,7 @@ func (a *Allocator) getAddress(nw *net.IPNet, bitmask *bitseq.Handle, prefAddres logrus.Debugf("Request address PoolID:%v %s Serial:%v PrefAddress:%v ", nw, bitmask.String(), serial, prefAddress) base = types.GetIPNetCopy(nw) - if bitmask.Unselected() <= 0 { + if bitmask.Unselected() == 0 { return nil, ipamapi.ErrNoAvailableIPs } if ipr == nil && prefAddress == nil { diff --git a/libnetwork/ipam/allocator_test.go b/libnetwork/ipam/allocator_test.go index d590f29ec6..ef01a8c0c1 100644 --- a/libnetwork/ipam/allocator_test.go +++ b/libnetwork/ipam/allocator_test.go @@ -21,6 +21,7 @@ import ( "github.com/docker/docker/libnetwork/types" "github.com/docker/libkv/store" "github.com/docker/libkv/store/boltdb" + "golang.org/x/sync/errgroup" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) @@ -307,7 +308,7 @@ func TestAddReleasePoolID(t *testing.T) { assert.NilError(t, err) var k0, k1 SubnetKey - aSpace, err := a.getAddrSpace(localAddressSpace) + _, err = a.getAddrSpace(localAddressSpace) if err != nil { t.Fatal(err) } @@ -320,7 +321,7 @@ func TestAddReleasePoolID(t *testing.T) { t.Fatal(err) } - aSpace, err = a.getAddrSpace(localAddressSpace) + aSpace, err := a.getAddrSpace(localAddressSpace) if err != nil { t.Fatal(err) } @@ -1070,8 +1071,6 @@ func TestRelease(t *testing.T) { t.Fatal(err) } - bm := a.addresses[SubnetKey{localAddressSpace, subnet, ""}] - // Allocate all addresses for err != ipamapi.ErrNoAvailableIPs { _, _, err = a.RequestAddress(pid, nil, nil) @@ -1108,7 +1107,7 @@ func TestRelease(t *testing.T) { for i, inp := range toRelease { ip0 := net.ParseIP(inp.address) a.ReleaseAddress(pid, ip0) - bm = a.addresses[SubnetKey{localAddressSpace, subnet, ""}] + bm := a.addresses[SubnetKey{localAddressSpace, subnet, ""}] if bm.Unselected() != 1 { t.Fatalf("Failed to update free address count after release. Expected %d, Found: %d", i+1, bm.Unselected()) } @@ -1200,13 +1199,6 @@ func benchmarkRequest(b *testing.B, a *Allocator, subnet string) { } } -func benchMarkRequest(subnet string, b *testing.B) { - a, _ := getAllocator(true) - for n := 0; n < b.N; n++ { - benchmarkRequest(b, a, subnet) - } -} - func BenchmarkRequest(b *testing.B) { subnets := []string{ @@ -1442,10 +1434,7 @@ func runParallelTests(t *testing.T, instance int) { } if instance != first { - select { - case <-start: - } - + <-start instDone := make(chan struct{}) done <- instDone defer close(instDone) @@ -1462,9 +1451,7 @@ func runParallelTests(t *testing.T, instance int) { if instance == first { for instDone := range done { - select { - case <-instDone: - } + <-instDone } // Now check each instance got a different pool for i := 0; i < numInstances; i++ { @@ -1495,7 +1482,7 @@ func TestRequestReleaseAddressDuplicate(t *testing.T) { scope: a.addrSpaces[localAddressSpace].scope, subnets: map[SubnetKey]*PoolData{}, } - var wg sync.WaitGroup + opts := map[string]string{ ipamapi.AllocSerialPrefix: "true", } @@ -1506,6 +1493,7 @@ func TestRequestReleaseAddressDuplicate(t *testing.T) { t.Fatal(err) } + group := new(errgroup.Group) for err == nil { var c *net.IPNet if c, _, err = a.RequestAddress(poolID, nil, opts); err == nil { @@ -1515,21 +1503,26 @@ func TestRequestReleaseAddressDuplicate(t *testing.T) { allocatedIPs = append(allocatedIPs, c) if len(allocatedIPs) > 500 { i := rand.Intn(len(allocatedIPs) - 1) - wg.Add(1) - go func(ip *net.IPNet) { + ip := allocatedIPs[i] + group.Go(func() error { if err = a.ReleaseAddress(poolID, ip.IP); err != nil { - t.Fatal(err) + return err } l.Lock() ips = append(ips, IP{ip, -1}) l.Unlock() - wg.Done() - }(allocatedIPs[i]) + return nil + }) + allocatedIPs = append(allocatedIPs[:i], allocatedIPs[i+1:]...) } } } - wg.Wait() + + if err := group.Wait(); err != nil { + t.Fatal(err) + } + refMap := make(map[string]int) for _, ip := range ips { refMap[ip.ip.String()] = refMap[ip.ip.String()] + ip.ref diff --git a/libnetwork/ipam/parallel_test.go b/libnetwork/ipam/parallel_test.go index 8fdff57a66..52e1f83365 100644 --- a/libnetwork/ipam/parallel_test.go +++ b/libnetwork/ipam/parallel_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/docker/docker/libnetwork/ipamapi" + "golang.org/x/sync/errgroup" "golang.org/x/sync/semaphore" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -89,11 +90,20 @@ func TestRequestPoolParallel(t *testing.T) { } var operationIndex int32 ch := make(chan *op, 240) + + group := new(errgroup.Group) + defer func() { + if err := group.Wait(); err != nil { + t.Fatal(err) + } + }() + for i := 0; i < 120; i++ { - go func(t *testing.T, a *Allocator, ch chan *op) { + group.Go(func() error { name, _, _, err := a.RequestPool("GlobalDefault", "", "", nil, false) if err != nil { - t.Fatalf("request error %v", err) + t.Log(err) // log so we can see the error in real time rather than at the end when we actually call "Wait". + return fmt.Errorf("request error %v", err) } idx := atomic.AddInt32(&operationIndex, 1) ch <- &op{idx, true, name} @@ -101,10 +111,12 @@ func TestRequestPoolParallel(t *testing.T) { idx = atomic.AddInt32(&operationIndex, 1) err = a.ReleasePool(name) if err != nil { - t.Fatalf("release error %v", err) + t.Log(err) // log so we can see the error in real time rather than at the end when we actually call "Wait". + return fmt.Errorf("release error %v", err) } ch <- &op{idx, false, name} - }(t, a, ch) + return nil + }) } // map of events @@ -258,24 +270,25 @@ func release(t *testing.T, tctx *testContext, mode releaseMode, parallel int64) var id int parallelExec := semaphore.NewWeighted(parallel) ch := make(chan *net.IPNet, len(ipIndex)) - wg := sync.WaitGroup{} + group := new(errgroup.Group) for index := range ipIndex { - wg.Add(1) - go func(id, index int) { + index := index + group.Go(func() error { parallelExec.Acquire(context.Background(), 1) - // logrus.Errorf("index %v", index) - // logrus.Errorf("list %v", tctx.ipList) err := tctx.a.ReleaseAddress(tctx.pid, tctx.ipList[index].IP) if err != nil { - t.Fatalf("routine %d got %v", id, err) + return fmt.Errorf("routine %d got %v", id, err) } ch <- tctx.ipList[index] parallelExec.Release(1) - wg.Done() - }(id, index) + return nil + }) id++ } - wg.Wait() + + if err := group.Wait(); err != nil { + t.Fatal(err) + } for i := 0; i < len(ipIndex); i++ { ip := <-ch diff --git a/libnetwork/ipam/store.go b/libnetwork/ipam/store.go index 20196f3766..e00401616b 100644 --- a/libnetwork/ipam/store.go +++ b/libnetwork/ipam/store.go @@ -116,17 +116,6 @@ func (a *Allocator) writeToStore(aSpace *addrSpace) error { return err } -func (a *Allocator) deleteFromStore(aSpace *addrSpace) error { - store := aSpace.store() - - // IPAM may not have a valid store. In such cases it is just in-memory state. - if store == nil { - return nil - } - - return store.DeleteObjectAtomic(aSpace) -} - // DataScope method returns the storage scope of the datastore func (aSpace *addrSpace) DataScope() string { aSpace.Lock() diff --git a/libnetwork/ipam/structures.go b/libnetwork/ipam/structures.go index a55e8c1700..de4130f83d 100644 --- a/libnetwork/ipam/structures.go +++ b/libnetwork/ipam/structures.go @@ -159,7 +159,7 @@ func (aSpace *addrSpace) MarshalJSON() ([]byte, error) { defer aSpace.Unlock() m := map[string]interface{}{ - "Scope": string(aSpace.scope), + "Scope": aSpace.scope, } if aSpace.subnets != nil { diff --git a/libnetwork/ipamapi/contract.go b/libnetwork/ipamapi/contract.go index 1f751990b6..2f170ccf72 100644 --- a/libnetwork/ipamapi/contract.go +++ b/libnetwork/ipamapi/contract.go @@ -4,9 +4,9 @@ package ipamapi import ( "net" - "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/plugingetter" ) /******************** diff --git a/libnetwork/ipams/remote/remote.go b/libnetwork/ipams/remote/remote.go index 09cf70d03f..4b8c7c5014 100644 --- a/libnetwork/ipams/remote/remote.go +++ b/libnetwork/ipams/remote/remote.go @@ -4,12 +4,12 @@ import ( "fmt" "net" - "github.com/docker/docker/pkg/plugingetter" - "github.com/docker/docker/pkg/plugins" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/ipamapi" "github.com/docker/docker/libnetwork/ipams/remote/api" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/plugingetter" + "github.com/docker/docker/pkg/plugins" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/iptables/conntrack.go b/libnetwork/iptables/conntrack.go index 08317c33ee..9b38097d7a 100644 --- a/libnetwork/iptables/conntrack.go +++ b/libnetwork/iptables/conntrack.go @@ -54,6 +54,8 @@ func purgeConntrackState(nlh *netlink.Handle, family netlink.InetFamily, ipAddre filter := &netlink.ConntrackFilter{} // NOTE: doing the flush using the ipAddress is safe because today there cannot be multiple networks with the same subnet // so it will not be possible to flush flows that are of other containers - filter.AddIP(netlink.ConntrackNatAnyIP, ipAddress) + if err := filter.AddIP(netlink.ConntrackNatAnyIP, ipAddress); err != nil { + return 0, err + } return nlh.ConntrackDeleteFilter(netlink.ConntrackTable, family, filter) } diff --git a/libnetwork/iptables/iptables_test.go b/libnetwork/iptables/iptables_test.go index 986bad35e8..a8bd875b84 100644 --- a/libnetwork/iptables/iptables_test.go +++ b/libnetwork/iptables/iptables_test.go @@ -5,10 +5,10 @@ import ( "os/exec" "strconv" "strings" - "sync" "testing" _ "github.com/docker/docker/libnetwork/testutils" + "golang.org/x/sync/errgroup" ) const chainName = "DOCKEREST" @@ -194,8 +194,6 @@ func TestConcurrencyNoWait(t *testing.T) { // Note that if iptables does not support the xtable lock on this // system, then allowXlock has no effect -- it will always be off. func RunConcurrencyTest(t *testing.T, allowXlock bool) { - var wg sync.WaitGroup - if !allowXlock && supportsXlock { supportsXlock = false defer func() { supportsXlock = true }() @@ -207,17 +205,15 @@ func RunConcurrencyTest(t *testing.T, allowXlock bool) { dstPort := 4321 proto := "tcp" + group := new(errgroup.Group) for i := 0; i < 10; i++ { - wg.Add(1) - go func() { - defer wg.Done() - err := natChain.Forward(Append, ip, port, proto, dstAddr, dstPort, "lo") - if err != nil { - t.Fatal(err) - } - }() + group.Go(func() error { + return natChain.Forward(Append, ip, port, proto, dstAddr, dstPort, "lo") + }) + } + if err := group.Wait(); err != nil { + t.Fatal(err) } - wg.Wait() } func TestCleanup(t *testing.T) { diff --git a/libnetwork/libnetwork_internal_test.go b/libnetwork/libnetwork_internal_test.go index e4a0047686..d9086c1a3a 100644 --- a/libnetwork/libnetwork_internal_test.go +++ b/libnetwork/libnetwork_internal_test.go @@ -156,7 +156,7 @@ func TestNetworkMarshalling(t *testing.T) { } func printIpamConf(list []*IpamConf) string { - s := fmt.Sprintf("\n[]*IpamConfig{") + s := "\n[]*IpamConfig{" for _, i := range list { s = fmt.Sprintf("%s %v,", s, i) } @@ -165,7 +165,7 @@ func printIpamConf(list []*IpamConf) string { } func printIpamInfo(list []*IpamInfo) string { - s := fmt.Sprintf("\n[]*IpamInfo{") + s := "\n[]*IpamInfo{" for _, i := range list { s = fmt.Sprintf("%s\n{\n%s\n}", s, i) } @@ -586,7 +586,9 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { if err != nil { t.Fatal(err) } - gnw.Delete() + if err := gnw.Delete(); err != nil { + t.Fatal(err) + } // Now check whether ipam release works on endpoint creation failure bd.failNetworkCreation = false @@ -594,7 +596,11 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { if err != nil { t.Fatal(err) } - defer bnw.Delete() + defer func() { + if err := bnw.Delete(); err != nil { + t.Fatal(err) + } + }() if _, err := bnw.CreateEndpoint("ep0"); err == nil { t.Fatalf("bad network driver should have failed endpoint creation") @@ -606,13 +612,17 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { if err != nil { t.Fatal(err) } - defer gnw.Delete() + defer func() { + if err := gnw.Delete(); err != nil { + t.Fatal(err) + } + }() ep, err := gnw.CreateEndpoint("ep1") if err != nil { t.Fatal(err) } - defer ep.Delete(false) + defer ep.Delete(false) // nolint:errcheck expectedIP, _ := types.ParseCIDR("10.35.0.1/16") if !types.CompareIPNet(ep.Info().Iface().Address(), expectedIP) { diff --git a/libnetwork/libnetwork_linux_test.go b/libnetwork/libnetwork_linux_test.go index a591dd863d..0141c7dfd6 100644 --- a/libnetwork/libnetwork_linux_test.go +++ b/libnetwork/libnetwork_linux_test.go @@ -14,7 +14,6 @@ import ( "strings" "testing" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork" "github.com/docker/docker/libnetwork/ipamapi" "github.com/docker/docker/libnetwork/netlabel" @@ -22,6 +21,7 @@ import ( "github.com/docker/docker/libnetwork/osl" "github.com/docker/docker/libnetwork/testutils" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/reexec" "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" "github.com/vishvananda/netns" @@ -920,7 +920,17 @@ func runParallelTests(t *testing.T, thrNumber int) { t.Fatal(err) } } - defer netns.Set(origins) + defer func() { + if err := netns.Set(origins); err != nil { + // NOTE(@cpuguy83): This... + // I touched this code because the linter found that we weren't checking the error... + // It returns an error because "origins" is a closed file handle *unless* createGlobalInstance is called. + // Which... this test is run in parallel and `createGlobalInstance` modifies `origins` without synchronization. + // I'm not sure what exactly the *intent* of this code was, but it looks very broken. + // Anyway that's why I'm only logging the error and not failing the test. + t.Log(err) + } + }() net1, err := controller.NetworkByName("testhost") if err != nil { diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 04a04aed5e..9a32632883 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -8,7 +8,6 @@ import ( "net/http/httptest" "os" "path/filepath" - "sync" "testing" "github.com/docker/docker/libnetwork" @@ -372,7 +371,7 @@ func TestNetworkConfig(t *testing.T) { } // Verify config network cannot inherit another config network - configNetwork, err := controller.NewNetwork("bridge", "config_network0", "", + _, err := controller.NewNetwork("bridge", "config_network0", "", libnetwork.NetworkOptionConfigOnly(), libnetwork.NetworkOptionConfigFrom("anotherConfigNw")) @@ -400,7 +399,7 @@ func TestNetworkConfig(t *testing.T) { libnetwork.NetworkOptionIpam("default", "", ipamV4ConfList, ipamV6ConfList, nil), } - configNetwork, err = controller.NewNetwork(bridgeNetType, "config_network0", "", netOptions...) + configNetwork, err := controller.NewNetwork(bridgeNetType, "config_network0", "", netOptions...) if err != nil { t.Fatal(err) } @@ -873,7 +872,7 @@ func TestNetworkQuery(t *testing.T) { t.Fatalf("EndpointByName() returned %v instead of %v", e, ep11) } - e, err = net1.EndpointByName("") + _, err = net1.EndpointByName("") if err == nil { t.Fatalf("EndpointByName() succeeded with invalid target name") } @@ -900,7 +899,7 @@ func TestNetworkQuery(t *testing.T) { t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12) } - e, err = net1.EndpointByID("") + _, err = net1.EndpointByID("") if err == nil { t.Fatalf("EndpointByID() succeeded with invalid target id") } @@ -1322,10 +1321,6 @@ func TestInvalidRemoteDriver(t *testing.T) { } defer server.Close() - type pluginRequest struct { - name string - } - mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintln(w, `{"Implements": ["InvalidDriver"]}`) @@ -1369,10 +1364,6 @@ func TestValidRemoteDriver(t *testing.T) { } defer server.Close() - type pluginRequest struct { - name string - } - mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") fmt.Fprintf(w, `{"Implements": ["%s"]}`, driverapi.NetworkPluginEndpointType) @@ -1420,7 +1411,6 @@ func TestValidRemoteDriver(t *testing.T) { } var ( - once sync.Once start = make(chan struct{}) done = make(chan chan struct{}, numThreads-1) origins = netns.None() @@ -1495,10 +1485,8 @@ func createGlobalInstance(t *testing.T) { } } -func debugf(format string, a ...interface{}) (int, error) { +func debugf(format string, a ...interface{}) { if debug { - return fmt.Printf(format, a...) + fmt.Printf(format, a...) } - - return 0, nil } diff --git a/libnetwork/libnetwork_unix_test.go b/libnetwork/libnetwork_unix_test.go index 9acb79add9..4366ea8b3f 100644 --- a/libnetwork/libnetwork_unix_test.go +++ b/libnetwork/libnetwork_unix_test.go @@ -2,4 +2,4 @@ package libnetwork_test -var specPath = "/etc/docker/plugins" \ No newline at end of file +var specPath = "/etc/docker/plugins" diff --git a/libnetwork/network.go b/libnetwork/network.go index 780763a004..f48bf34cce 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -8,7 +8,6 @@ import ( "sync" "time" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/libnetwork/config" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/driverapi" @@ -20,6 +19,7 @@ import ( "github.com/docker/docker/libnetwork/networkdb" "github.com/docker/docker/libnetwork/options" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/stringid" "github.com/sirupsen/logrus" ) @@ -220,7 +220,6 @@ type network struct { dbIndex uint64 dbExists bool persist bool - stopWatchCh chan struct{} drvOnce *sync.Once resolverOnce sync.Once resolver []Resolver @@ -499,25 +498,33 @@ func (n *network) CopyTo(o datastore.KVObject) error { for _, v4conf := range n.ipamV4Config { dstV4Conf := &IpamConf{} - v4conf.CopyTo(dstV4Conf) + if err := v4conf.CopyTo(dstV4Conf); err != nil { + return err + } dstN.ipamV4Config = append(dstN.ipamV4Config, dstV4Conf) } for _, v4info := range n.ipamV4Info { dstV4Info := &IpamInfo{} - v4info.CopyTo(dstV4Info) + if err := v4info.CopyTo(dstV4Info); err != nil { + return err + } dstN.ipamV4Info = append(dstN.ipamV4Info, dstV4Info) } for _, v6conf := range n.ipamV6Config { dstV6Conf := &IpamConf{} - v6conf.CopyTo(dstV6Conf) + if err := v6conf.CopyTo(dstV6Conf); err != nil { + return err + } dstN.ipamV6Config = append(dstN.ipamV6Config, dstV6Conf) } for _, v6info := range n.ipamV6Info { dstV6Info := &IpamInfo{} - v6info.CopyTo(dstV6Info) + if err := v6info.CopyTo(dstV6Info); err != nil { + return err + } dstN.ipamV6Info = append(dstN.ipamV6Info, dstV6Info) } @@ -933,16 +940,6 @@ func (n *network) resolveDriver(name string, load bool) (driverapi.Driver, *driv return d, cap, nil } -func (n *network) driverScope() string { - _, cap, err := n.resolveDriver(n.networkType, true) - if err != nil { - // If driver could not be resolved simply return an empty string - return "" - } - - return cap.DataScope -} - func (n *network) driverIsMultihost() bool { _, cap, err := n.resolveDriver(n.networkType, true) if err != nil { @@ -995,7 +992,7 @@ func (n *network) delete(force bool, rmLBEndpoint bool) error { n.Unlock() c.networkLocker.Lock(id) - defer c.networkLocker.Unlock(id) + defer c.networkLocker.Unlock(id) // nolint:errcheck n, err := c.getNetworkFromStore(id) if err != nil { @@ -1163,7 +1160,7 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi } n.ctrlr.networkLocker.Lock(n.id) - defer n.ctrlr.networkLocker.Unlock(n.id) + defer n.ctrlr.networkLocker.Unlock(n.id) // nolint:errcheck return n.createEndpoint(name, options...) @@ -1832,13 +1829,17 @@ func (n *network) IpamConfig() (string, map[string]string, []*IpamConf, []*IpamC for i, c := range n.ipamV4Config { cc := &IpamConf{} - c.CopyTo(cc) + if err := c.CopyTo(cc); err != nil { + logrus.WithError(err).Error("Error copying ipam ipv4 config") + } v4L[i] = cc } for i, c := range n.ipamV6Config { cc := &IpamConf{} - c.CopyTo(cc) + if err := c.CopyTo(cc); err != nil { + logrus.WithError(err).Debug("Error copying ipam ipv6 config") + } v6L[i] = cc } @@ -1854,13 +1855,17 @@ func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) { for i, info := range n.ipamV4Info { ic := &IpamInfo{} - info.CopyTo(ic) + if err := info.CopyTo(ic); err != nil { + logrus.WithError(err).Error("Error copying ipv4 ipam config") + } v4Info[i] = ic } for i, info := range n.ipamV6Info { ic := &IpamInfo{} - info.CopyTo(ic) + if err := info.CopyTo(ic); err != nil { + logrus.WithError(err).Error("Error copying ipv6 ipam config") + } v6Info[i] = ic } diff --git a/libnetwork/networkdb/cluster.go b/libnetwork/networkdb/cluster.go index 92c0c41bca..837ec9a18e 100644 --- a/libnetwork/networkdb/cluster.go +++ b/libnetwork/networkdb/cluster.go @@ -244,7 +244,7 @@ func (nDB *NetworkDB) clusterLeave() error { func (nDB *NetworkDB) triggerFunc(stagger time.Duration, C <-chan time.Time, f func()) { // Use a random stagger to avoid synchronizing - randStagger := time.Duration(uint64(rnd.Int63()) % uint64(stagger)) + randStagger := time.Duration(uint64(rnd.Int63()) % uint64(stagger)) // nolint:gosec select { case <-time.After(randStagger): case <-nDB.ctx.Done(): diff --git a/libnetwork/networkdb/networkdb.go b/libnetwork/networkdb/networkdb.go index 64f0e7bd8a..7655f83317 100644 --- a/libnetwork/networkdb/networkdb.go +++ b/libnetwork/networkdb/networkdb.go @@ -11,9 +11,9 @@ import ( "time" "github.com/armon/go-radix" + "github.com/docker/docker/libnetwork/types" "github.com/docker/docker/pkg/stringid" "github.com/docker/go-events" - "github.com/docker/docker/libnetwork/types" "github.com/hashicorp/memberlist" "github.com/hashicorp/serf/serf" "github.com/sirupsen/logrus" diff --git a/libnetwork/networkdb/networkdb_test.go b/libnetwork/networkdb/networkdb_test.go index 77bed9dfdc..ac05a45744 100644 --- a/libnetwork/networkdb/networkdb_test.go +++ b/libnetwork/networkdb/networkdb_test.go @@ -145,21 +145,21 @@ func testWatch(t *testing.T, ch chan events.Event, ev interface{}, tname, nid, k select { case rcvdEv := <-ch: assert.Check(t, is.Equal(fmt.Sprintf("%T", rcvdEv), fmt.Sprintf("%T", ev))) - switch rcvdEv.(type) { + switch typ := rcvdEv.(type) { case CreateEvent: - assert.Check(t, is.Equal(tname, rcvdEv.(CreateEvent).Table)) - assert.Check(t, is.Equal(nid, rcvdEv.(CreateEvent).NetworkID)) - assert.Check(t, is.Equal(key, rcvdEv.(CreateEvent).Key)) - assert.Check(t, is.Equal(value, string(rcvdEv.(CreateEvent).Value))) + assert.Check(t, is.Equal(tname, typ.Table)) + assert.Check(t, is.Equal(nid, typ.NetworkID)) + assert.Check(t, is.Equal(key, typ.Key)) + assert.Check(t, is.Equal(value, string(typ.Value))) case UpdateEvent: - assert.Check(t, is.Equal(tname, rcvdEv.(UpdateEvent).Table)) - assert.Check(t, is.Equal(nid, rcvdEv.(UpdateEvent).NetworkID)) - assert.Check(t, is.Equal(key, rcvdEv.(UpdateEvent).Key)) - assert.Check(t, is.Equal(value, string(rcvdEv.(UpdateEvent).Value))) + assert.Check(t, is.Equal(tname, typ.Table)) + assert.Check(t, is.Equal(nid, typ.NetworkID)) + assert.Check(t, is.Equal(key, typ.Key)) + assert.Check(t, is.Equal(value, string(typ.Value))) case DeleteEvent: - assert.Check(t, is.Equal(tname, rcvdEv.(DeleteEvent).Table)) - assert.Check(t, is.Equal(nid, rcvdEv.(DeleteEvent).NetworkID)) - assert.Check(t, is.Equal(key, rcvdEv.(DeleteEvent).Key)) + assert.Check(t, is.Equal(tname, typ.Table)) + assert.Check(t, is.Equal(nid, typ.NetworkID)) + assert.Check(t, is.Equal(key, typ.Key)) } case <-time.After(time.Second): t.Fail() @@ -816,7 +816,7 @@ func TestNetworkDBIslands(t *testing.T) { dbs := createNetworkDBInstances(t, 5, "node", DefaultConfig()) // Get the node IP used currently - node, _ := dbs[0].nodes[dbs[0].config.NodeID] + node := dbs[0].nodes[dbs[0].config.NodeID] baseIPStr := node.Addr.String() // Node 0,1,2 are going to be the 3 bootstrap nodes members := []string{fmt.Sprintf("%s:%d", baseIPStr, dbs[0].config.BindPort), diff --git a/libnetwork/options/options_test.go b/libnetwork/options/options_test.go index 0381f15c09..cd3f7361bf 100644 --- a/libnetwork/options/options_test.go +++ b/libnetwork/options/options_test.go @@ -86,7 +86,7 @@ func TestGenerateMissingField(t *testing.T) { } func TestFieldCannotBeSet(t *testing.T) { - type Model struct{ foo int } + type Model struct{ foo int } // nolint:structcheck _, err := GenerateFromModel(Generic{"foo": "bar"}, Model{}) if _, ok := err.(CannotSetFieldError); !ok { diff --git a/libnetwork/osl/interface_linux.go b/libnetwork/osl/interface_linux.go index 3bf9cee116..1cfe83c57c 100644 --- a/libnetwork/osl/interface_linux.go +++ b/libnetwork/osl/interface_linux.go @@ -193,12 +193,12 @@ func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) { } return &types.InterfaceStatistics{ - RxBytes: uint64(stats.RxBytes), - TxBytes: uint64(stats.TxBytes), - RxPackets: uint64(stats.RxPackets), - TxPackets: uint64(stats.TxPackets), - RxDropped: uint64(stats.RxDropped), - TxDropped: uint64(stats.TxDropped), + RxBytes: stats.RxBytes, + TxBytes: stats.TxBytes, + RxPackets: stats.RxPackets, + TxPackets: stats.TxPackets, + RxDropped: stats.RxDropped, + TxDropped: stats.TxDropped, }, nil } @@ -422,8 +422,7 @@ func setInterfaceRoutes(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err // we cannot gather the statistics from /sys/class/net//statistics/ files. Per-netns stats // are naturally found in /proc/net/dev in kernels which support netns (ifconfig relies on that). const ( - netStatsFile = "/proc/net/dev" - base = "[ ]*%s:([ ]+[0-9]+){16}" + base = "[ ]*%s:([ ]+[0-9]+){16}" ) func scanInterfaceStats(data, ifName string, i *types.InterfaceStatistics) error { diff --git a/libnetwork/osl/namespace_linux.go b/libnetwork/osl/namespace_linux.go index dba35a69af..d8cf2f896e 100644 --- a/libnetwork/osl/namespace_linux.go +++ b/libnetwork/osl/namespace_linux.go @@ -1,6 +1,7 @@ package osl import ( + "errors" "fmt" "io/ioutil" "net" @@ -14,13 +15,14 @@ import ( "syscall" "time" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork/ns" "github.com/docker/docker/libnetwork/osl/kernel" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/reexec" "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" "github.com/vishvananda/netns" + "golang.org/x/sys/unix" ) const defaultPrefix = "/var/run/docker" @@ -337,7 +339,9 @@ func createNetworkNamespace(path string, osCreate bool) error { func unmountNamespaceFile(path string) { if _, err := os.Stat(path); err == nil { - syscall.Unmount(path, syscall.MNT_DETACH) + if err := syscall.Unmount(path, syscall.MNT_DETACH); err != nil && !errors.Is(err, unix.EINVAL) { + logrus.WithError(err).Error("Error unmounting namespace file") + } } } diff --git a/libnetwork/osl/neigh_linux.go b/libnetwork/osl/neigh_linux.go index 6bf1c16dc5..7105bf6dde 100644 --- a/libnetwork/osl/neigh_linux.go +++ b/libnetwork/osl/neigh_linux.go @@ -102,7 +102,9 @@ func (n *networkNamespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, if nh.linkDst != "" { nlnh.LinkIndex = iface.Attrs().Index } - nlh.NeighDel(nlnh) + if err := nlh.NeighDel(nlnh); err != nil { + logrus.WithError(err).Warn("error while deleting neighbor entry") + } } } diff --git a/libnetwork/osl/sandbox_linux_test.go b/libnetwork/osl/sandbox_linux_test.go index 5b27007a5d..57fa22d590 100644 --- a/libnetwork/osl/sandbox_linux_test.go +++ b/libnetwork/osl/sandbox_linux_test.go @@ -147,7 +147,7 @@ func verifySandbox(t *testing.T, s Sandbox, ifaceSuffixes []string) { func verifyCleanup(t *testing.T, s Sandbox, wait bool) { if wait { - time.Sleep(time.Duration(gpmCleanupPeriod * 2)) + time.Sleep(gpmCleanupPeriod * 2) } if _, err := os.Stat(s.Key()); err == nil { @@ -202,7 +202,7 @@ func TestDisableIPv6DAD(t *testing.T) { t.Fatalf("Failed to create a new sandbox: %v", err) } runtime.LockOSThread() - defer s.Destroy() + defer destroyTest(t, s) n, ok := s.(*networkNamespace) if !ok { @@ -243,6 +243,12 @@ func TestDisableIPv6DAD(t *testing.T) { } } +func destroyTest(t *testing.T, s Sandbox) { + if err := s.Destroy(); err != nil { + t.Log(err) + } +} + func TestSetInterfaceIP(t *testing.T) { defer testutils.SetupTestOSContext(t)() @@ -256,7 +262,7 @@ func TestSetInterfaceIP(t *testing.T) { t.Fatalf("Failed to create a new sandbox: %v", err) } runtime.LockOSThread() - defer s.Destroy() + defer destroyTest(t, s) n, ok := s.(*networkNamespace) if !ok { @@ -332,7 +338,7 @@ func TestLiveRestore(t *testing.T) { t.Fatalf("Failed to create a new sandbox: %v", err) } runtime.LockOSThread() - defer s.Destroy() + defer destroyTest(t, s) n, ok := s.(*networkNamespace) if !ok { @@ -398,6 +404,7 @@ func TestLiveRestore(t *testing.T) { if err != nil { t.Fatalf("Failed to create a new sandbox: %v", err) } + defer destroyTest(t, s) // Check if the IPV4 & IPV6 entry present // If present , we should get error in below call diff --git a/libnetwork/portallocator/portallocator.go b/libnetwork/portallocator/portallocator.go index c87478c0cd..b2877d5c41 100644 --- a/libnetwork/portallocator/portallocator.go +++ b/libnetwork/portallocator/portallocator.go @@ -3,9 +3,10 @@ package portallocator import ( "errors" "fmt" - "github.com/sirupsen/logrus" "net" "sync" + + "github.com/sirupsen/logrus" ) var ( diff --git a/libnetwork/portallocator/portallocator_test.go b/libnetwork/portallocator/portallocator_test.go index 0898ceaa10..5484db6d68 100644 --- a/libnetwork/portallocator/portallocator_test.go +++ b/libnetwork/portallocator/portallocator_test.go @@ -174,7 +174,9 @@ func BenchmarkAllocatePorts(b *testing.B) { b.Fatalf("Expected port %d got %d", expected, port) } } - p.ReleaseAll() + if err := p.ReleaseAll(); err != nil { + b.Fatal(err) + } } } diff --git a/libnetwork/portmapper/mapper.go b/libnetwork/portmapper/mapper.go index 160e5c7bf9..c03fd4f614 100644 --- a/libnetwork/portmapper/mapper.go +++ b/libnetwork/portmapper/mapper.go @@ -60,7 +60,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart, allocatedHostPort int ) - switch container.(type) { + switch t := container.(type) { case *net.TCPAddr: proto = "tcp" if allocatedHostPort, err = pm.Allocator.RequestPortInRange(hostIP, proto, hostPortStart, hostPortEnd); err != nil { @@ -74,7 +74,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart, } if useProxy { - m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, container.(*net.TCPAddr).IP, container.(*net.TCPAddr).Port, pm.proxyPath) + m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, t.IP, t.Port, pm.proxyPath) if err != nil { return nil, err } @@ -97,7 +97,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart, } if useProxy { - m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, container.(*net.UDPAddr).IP, container.(*net.UDPAddr).Port, pm.proxyPath) + m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, t.IP, t.Port, pm.proxyPath) if err != nil { return nil, err } diff --git a/libnetwork/resolvconf/resolvconf.go b/libnetwork/resolvconf/resolvconf.go index b0fc2d404a..9d6391577e 100644 --- a/libnetwork/resolvconf/resolvconf.go +++ b/libnetwork/resolvconf/resolvconf.go @@ -8,9 +8,9 @@ import ( "strings" "sync" - "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/libnetwork/resolvconf/dns" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/ioutils" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/resolvconf/resolvconf_linux_test.go b/libnetwork/resolvconf/resolvconf_linux_test.go index 76f3b9d864..fb76ff7cfa 100644 --- a/libnetwork/resolvconf/resolvconf_linux_test.go +++ b/libnetwork/resolvconf/resolvconf_linux_test.go @@ -6,9 +6,9 @@ import ( "os" "testing" - "github.com/docker/docker/pkg/ioutils" _ "github.com/docker/docker/libnetwork/testutils" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/ioutils" ) func TestGet(t *testing.T) { diff --git a/libnetwork/resolver.go b/libnetwork/resolver.go index 4bdb95b1db..b35bd03301 100644 --- a/libnetwork/resolver.go +++ b/libnetwork/resolver.go @@ -157,13 +157,17 @@ func (r *resolver) Start() error { s := &dns.Server{Handler: r, PacketConn: r.conn} r.server = s go func() { - s.ActivateAndServe() + if err := s.ActivateAndServe(); err != nil { + logrus.WithError(err).Error("error starting packetconn dns server") + } }() tcpServer := &dns.Server{Handler: r, Listener: r.tcpListen} r.tcpServer = tcpServer go func() { - tcpServer.ActivateAndServe() + if err := tcpServer.ActivateAndServe(); err != nil { + logrus.WithError(err).Error("error starting tcp dns server") + } }() return nil } @@ -173,10 +177,10 @@ func (r *resolver) Stop() { defer func() { <-r.startCh }() if r.server != nil { - r.server.Shutdown() + r.server.Shutdown() // nolint:errcheck } if r.tcpServer != nil { - r.tcpServer.Shutdown() + r.tcpServer.Shutdown() // nolint:errcheck } r.conn = nil r.tcpServer = nil @@ -210,7 +214,7 @@ func setCommonFlags(msg *dns.Msg) { func shuffleAddr(addr []net.IP) []net.IP { for i := len(addr) - 1; i > 0; i-- { - r := rand.Intn(i + 1) + r := rand.Intn(i + 1) // nolint:gosec addr[i], addr[r] = addr[r], addr[i] } return addr @@ -392,7 +396,9 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) { if !r.proxyDNS { resp = new(dns.Msg) resp.SetRcode(query, dns.RcodeServerFailure) - w.WriteMsg(resp) + if err := w.WriteMsg(resp); err != nil { + logrus.WithError(err).Error("Error writing dns response") + } return } @@ -457,7 +463,9 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) { extConn.LocalAddr().String(), proto, extDNS.IPStr) // Timeout has to be set for every IO operation. - extConn.SetDeadline(time.Now().Add(extIOTimeout)) + if err := extConn.SetDeadline(time.Now().Add(extIOTimeout)); err != nil { + logrus.WithError(err).Error("Error setting conn deadline") + } co := &dns.Conn{ Conn: extConn, UDPSize: uint16(maxSize), diff --git a/libnetwork/resolver_test.go b/libnetwork/resolver_test.go index c8fce15539..076d5663b3 100644 --- a/libnetwork/resolver_test.go +++ b/libnetwork/resolver_test.go @@ -1,7 +1,6 @@ package libnetwork import ( - "bytes" "net" "runtime" "syscall" @@ -9,11 +8,11 @@ import ( "time" "github.com/miekg/dns" + "github.com/sirupsen/logrus" ) // a simple/null address type that will be used to fake a local address for unit testing type tstaddr struct { - net string } func (a *tstaddr) Network() string { return "tcp" } @@ -54,12 +53,6 @@ func checkNonNullResponse(t *testing.T, m *dns.Msg) { } } -func checkNullResponse(t *testing.T, m *dns.Msg) { - if m != nil { - t.Fatal("Non Null DNS response found. Null response msg expected.") - } -} - func checkDNSAnswersCount(t *testing.T, m *dns.Msg, expected int) { answers := len(m.Answer) if answers != expected { @@ -140,7 +133,7 @@ func TestDNSIPQuery(t *testing.T) { checkDNSAnswersCount(t, resp, 1) checkDNSRRType(t, resp.Answer[0].Header().Rrtype, dns.TypeA) if answer, ok := resp.Answer[0].(*dns.A); ok { - if !bytes.Equal(answer.A, net.ParseIP("192.168.0.1")) { + if !answer.A.Equal(net.ParseIP("192.168.0.1")) { t.Fatalf("IP response in Answer %v does not match 192.168.0.1", answer.A) } } else { @@ -182,7 +175,9 @@ func newDNSHandlerServFailOnce(requests *int) func(w dns.ResponseWriter, r *dns. m.SetRcode(r, dns.RcodeServerFailure) } *requests = *requests + 1 - w.WriteMsg(m) + if err := w.WriteMsg(m); err != nil { + logrus.WithError(err).Error("Error writing dns response") + } } } @@ -251,8 +246,16 @@ func TestDNSProxyServFail(t *testing.T) { dns.HandleFunc(".", newDNSHandlerServFailOnce(&nRequests)) // use TCP for predictable results. Connection tests (to figure out DNS server initialization) don't work with UDP server := &dns.Server{Addr: ":53", Net: "tcp"} - go server.ListenAndServe() - defer server.Shutdown() + srvErrCh := make(chan error, 1) + go func() { + srvErrCh <- server.ListenAndServe() + }() + defer func() { + server.Shutdown() // nolint:errcheck + if err := <-srvErrCh; err != nil { + t.Error(err) + } + }() waitForLocalDNSServer(t) t.Log("DNS Server can be reached") diff --git a/libnetwork/resolver_unix.go b/libnetwork/resolver_unix.go index ec46ee207c..92b1dfe8af 100644 --- a/libnetwork/resolver_unix.go +++ b/libnetwork/resolver_unix.go @@ -9,8 +9,8 @@ import ( "os/exec" "runtime" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork/iptables" + "github.com/docker/docker/pkg/reexec" "github.com/sirupsen/logrus" "github.com/vishvananda/netns" ) @@ -49,7 +49,7 @@ func reexecSetupResolver() { logrus.Errorf("failed get network namespace %q: %v", os.Args[1], err) os.Exit(2) } - defer f.Close() + defer f.Close() // nolint:gosec nsFD := f.Fd() if err = netns.Set(netns.NsHandle(nsFD)); err != nil { diff --git a/libnetwork/sandbox.go b/libnetwork/sandbox.go index 1c9fc51c3b..31de696c25 100644 --- a/libnetwork/sandbox.go +++ b/libnetwork/sandbox.go @@ -72,7 +72,6 @@ type sandbox struct { controller *controller resolver Resolver resolverOnce sync.Once - refCnt int endpoints []*endpoint epPriority map[string]int populatedEndpoints map[string]struct{} @@ -93,12 +92,13 @@ type sandbox struct { // These are the container configs used to customize container /etc/hosts file. type hostsPathConfig struct { - hostName string - domainName string - hostsPath string - originHostsPath string - extraHosts []extraHost - parentUpdates []parentUpdate + // Note(cpuguy83): The linter is drunk and says none of these fields are used while they are + hostName string // nolint:structcheck + domainName string // nolint:structcheck + hostsPath string // nolint:structcheck + originHostsPath string // nolint:structcheck + extraHosts []extraHost // nolint:structcheck + parentUpdates []parentUpdate // nolint:structcheck } type parentUpdate struct { @@ -114,12 +114,13 @@ type extraHost struct { // These are the container configs used to customize container /etc/resolv.conf file. type resolvConfPathConfig struct { - resolvConfPath string - originResolvConfPath string - resolvConfHashFile string - dnsList []string - dnsSearchList []string - dnsOptionsList []string + // Note(cpuguy83): The linter is drunk and says none of these fields are used while they are + resolvConfPath string // nolint:structcheck + originResolvConfPath string // nolint:structcheck + resolvConfHashFile string // nolint:structcheck + dnsList []string // nolint:structcheck + dnsSearchList []string // nolint:structcheck + dnsOptionsList []string // nolint:structcheck } type containerConfig struct { @@ -128,7 +129,6 @@ type containerConfig struct { generic map[string]interface{} useDefaultSandBox bool useExternalKey bool - prio int // higher the value, more the priority exposedPorts []types.TransportPort } @@ -246,7 +246,9 @@ func (sb *sandbox) delete(force bool) error { } if sb.osSbox != nil && !sb.config.useDefaultSandBox { - sb.osSbox.Destroy() + if err := sb.osSbox.Destroy(); err != nil { + logrus.WithError(err).Warn("error destroying network sandbox") + } } if err := sb.storeDelete(); err != nil { @@ -279,7 +281,9 @@ func (sb *sandbox) Rename(name string) error { defer func() { if err != nil { - lEp.rename(oldName) + if err2 := lEp.rename(oldName); err2 != nil { + logrus.WithField("old", oldName).WithField("origError", err).WithError(err2).Error("error renaming sandbox") + } } }() } @@ -408,8 +412,8 @@ func (sb *sandbox) updateGateway(ep *endpoint) error { if osSbox == nil { return nil } - osSbox.UnsetGateway() - osSbox.UnsetGatewayIPv6() + osSbox.UnsetGateway() // nolint:errcheck + osSbox.UnsetGatewayIPv6() // nolint:errcheck if ep == nil { return nil @@ -694,7 +698,9 @@ func (sb *sandbox) EnableService() (err error) { logrus.Debugf("EnableService %s START", sb.containerID) defer func() { if err != nil { - sb.DisableService() + if err2 := sb.DisableService(); err2 != nil { + logrus.WithError(err2).WithField("origError", err).Error("Error while disabling service after original error") + } } }() for _, ep := range sb.getConnectedEndpoints() { @@ -779,7 +785,9 @@ func (sb *sandbox) releaseOSSbox() { releaseOSSboxResources(osSbox, ep) } - osSbox.Destroy() + if err := osSbox.Destroy(); err != nil { + logrus.WithError(err).Error("Error destroying os sandbox") + } } func (sb *sandbox) restoreOslSandbox() error { @@ -975,7 +983,9 @@ func (sb *sandbox) clearNetworkResources(origEp *endpoint) error { sb.Unlock() if gwepAfter != nil && gwepBefore != gwepAfter { - sb.updateGateway(gwepAfter) + if err := sb.updateGateway(gwepAfter); err != nil { + return err + } } // Only update the store if we did not come here as part of @@ -989,13 +999,6 @@ func (sb *sandbox) clearNetworkResources(origEp *endpoint) error { return nil } -func (sb *sandbox) isEndpointPopulated(ep *endpoint) bool { - sb.Lock() - _, ok := sb.populatedEndpoints[ep.ID()] - sb.Unlock() - return ok -} - // joinLeaveStart waits to ensure there are no joins or leaves in progress and // marks this join/leave in progress without race func (sb *sandbox) joinLeaveStart() { @@ -1026,13 +1029,6 @@ func (sb *sandbox) joinLeaveEnd() { } } -func (sb *sandbox) hasPortConfigs() bool { - opts := sb.Labels() - _, hasExpPorts := opts[netlabel.ExposedPorts] - _, hasPortMaps := opts[netlabel.PortMap] - return hasExpPorts || hasPortMaps -} - // OptionHostname function returns an option setter for hostname option to // be passed to NewSandbox method. func OptionHostname(name string) SandboxOption { diff --git a/libnetwork/sandbox_dns_unix.go b/libnetwork/sandbox_dns_unix.go index 296462e043..bc19abae61 100644 --- a/libnetwork/sandbox_dns_unix.go +++ b/libnetwork/sandbox_dns_unix.go @@ -99,7 +99,7 @@ func (sb *sandbox) buildHostsFile() error { } func (sb *sandbox) updateHostsFile(ifaceIPs []string) error { - if ifaceIPs == nil || len(ifaceIPs) == 0 { + if len(ifaceIPs) == 0 { return nil } @@ -322,7 +322,7 @@ func (sb *sandbox) updateDNS(ipv6Enabled bool) error { if err != nil { return err } - err = ioutil.WriteFile(sb.config.resolvConfPath, newRC.Content, 0644) + err = ioutil.WriteFile(sb.config.resolvConfPath, newRC.Content, 0644) // nolint:gosec if err != nil { return err } diff --git a/libnetwork/sandbox_externalkey_unix.go b/libnetwork/sandbox_externalkey_unix.go index 50fc9f9a1e..241a092b3d 100644 --- a/libnetwork/sandbox_externalkey_unix.go +++ b/libnetwork/sandbox_externalkey_unix.go @@ -12,8 +12,8 @@ import ( "os" "path/filepath" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/stringid" "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/service_common.go b/libnetwork/service_common.go index 8819aae849..02cae58242 100644 --- a/libnetwork/service_common.go +++ b/libnetwork/service_common.go @@ -20,7 +20,9 @@ func (c *controller) addEndpointNameResolution(svcName, svcID, nID, eID, contain logrus.Debugf("addEndpointNameResolution %s %s add_service:%t sAliases:%v tAliases:%v", eID, svcName, addService, serviceAliases, taskAliases) // Add container resolution mappings - c.addContainerNameResolution(nID, eID, containerName, taskAliases, ip, method) + if err := c.addContainerNameResolution(nID, eID, containerName, taskAliases, ip, method); err != nil { + return err + } serviceID := svcID if serviceID == "" { @@ -79,7 +81,9 @@ func (c *controller) deleteEndpointNameResolution(svcName, svcID, nID, eID, cont logrus.Debugf("deleteEndpointNameResolution %s %s rm_service:%t suppress:%t sAliases:%v tAliases:%v", eID, svcName, rmService, multipleEntries, serviceAliases, taskAliases) // Delete container resolution mappings - c.delContainerNameResolution(nID, eID, containerName, taskAliases, ip, method) + if err := c.delContainerNameResolution(nID, eID, containerName, taskAliases, ip, method); err != nil { + logrus.WithError(err).Warn("Error delting container from resolver") + } serviceID := svcID if serviceID == "" { @@ -230,7 +234,7 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s // state in the c.serviceBindings map and it's sub-maps. Also, // always lock network ID before services to avoid deadlock. c.networkLocker.Lock(nID) - defer c.networkLocker.Unlock(nID) + defer c.networkLocker.Unlock(nID) // nolint:errcheck n, err := c.NetworkByID(nID) if err != nil { @@ -300,7 +304,9 @@ func (c *controller) addServiceBinding(svcName, svcID, nID, eID, containerName s n.(*network).addLBBackend(ip, lb) // Add the appropriate name resolutions - c.addEndpointNameResolution(svcName, svcID, nID, eID, containerName, vip, serviceAliases, taskAliases, ip, addService, "addServiceBinding") + if err := c.addEndpointNameResolution(svcName, svcID, nID, eID, containerName, vip, serviceAliases, taskAliases, ip, addService, "addServiceBinding"); err != nil { + return err + } logrus.Debugf("addServiceBinding from %s END for %s %s", method, svcName, eID) @@ -382,7 +388,9 @@ func (c *controller) rmServiceBinding(svcName, svcID, nID, eID, containerName st // Delete the name resolutions if deleteSvcRecords { - c.deleteEndpointNameResolution(svcName, svcID, nID, eID, containerName, vip, serviceAliases, taskAliases, ip, rmService, entries > 0, "rmServiceBinding") + if err := c.deleteEndpointNameResolution(svcName, svcID, nID, eID, containerName, vip, serviceAliases, taskAliases, ip, rmService, entries > 0, "rmServiceBinding"); err != nil { + return err + } } if len(s.loadBalancers) == 0 { diff --git a/libnetwork/service_common_test.go b/libnetwork/service_common_test.go index 30b8c42fe8..4fc36065e6 100644 --- a/libnetwork/service_common_test.go +++ b/libnetwork/service_common_test.go @@ -14,13 +14,18 @@ func TestCleanupServiceDiscovery(t *testing.T) { assert.NilError(t, err) defer c.Stop() + cleanup := func(n Network) { + if err := n.Delete(); err != nil { + t.Error(err) + } + } n1, err := c.NewNetwork("bridge", "net1", "", nil) assert.NilError(t, err) - defer n1.Delete() + defer cleanup(n1) n2, err := c.NewNetwork("bridge", "net2", "", nil) assert.NilError(t, err) - defer n2.Delete() + defer cleanup(n2) n1.(*network).addSvcRecords("N1ep1", "service_test", "serviceID1", net.ParseIP("192.168.0.1"), net.IP{}, true, "test") n1.(*network).addSvcRecords("N2ep2", "service_test", "serviceID2", net.ParseIP("192.168.0.2"), net.IP{}, true, "test") @@ -52,7 +57,14 @@ func TestDNSOptions(t *testing.T) { sb, err := c.(*controller).NewSandbox("cnt1", nil) assert.NilError(t, err) - defer sb.Delete() + + cleanup := func(s Sandbox) { + if err := s.Delete(); err != nil { + t.Error(err) + } + } + + defer cleanup(sb) sb.(*sandbox).startResolver(false) err = sb.(*sandbox).setupDNS() @@ -84,7 +96,7 @@ func TestDNSOptions(t *testing.T) { sb2, err := c.(*controller).NewSandbox("cnt2", nil) assert.NilError(t, err) - defer sb2.Delete() + defer cleanup(sb2) sb2.(*sandbox).startResolver(false) sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:0"} diff --git a/libnetwork/service_linux.go b/libnetwork/service_linux.go index 2d2cca2335..18516dd22d 100644 --- a/libnetwork/service_linux.go +++ b/libnetwork/service_linux.go @@ -14,9 +14,9 @@ import ( "sync" "syscall" - "github.com/docker/docker/pkg/reexec" "github.com/docker/docker/libnetwork/iptables" "github.com/docker/docker/libnetwork/ns" + "github.com/docker/docker/pkg/reexec" "github.com/gogo/protobuf/proto" "github.com/ishidawataru/sctp" "github.com/moby/ipvs" @@ -67,8 +67,7 @@ func (n *network) findLBEndpointSandbox() (*endpoint, *sandbox, error) { if !ok { return nil, nil, fmt.Errorf("Unable to get sandbox for %s(%s) in for %s", ep.Name(), ep.ID(), n.ID()) } - var sep *endpoint - sep = sb.getEndpoint(ep.ID()) + sep := sb.getEndpoint(ep.ID()) if sep == nil { return nil, nil, fmt.Errorf("Load balancing endpoint %s(%s) removed from %s", ep.Name(), ep.ID(), n.ID()) } @@ -379,7 +378,7 @@ func programIngress(gwIP net.IP, ingressPorts []*PortConfig, isDelete bool) erro } path := filepath.Join("/proc/sys/net/ipv4/conf", oifName, "route_localnet") - if err := ioutil.WriteFile(path, []byte{'1', '\n'}, 0644); err != nil { + if err := ioutil.WriteFile(path, []byte{'1', '\n'}, 0644); err != nil { // nolint:gosec return fmt.Errorf("could not write to %s: %v", path, err) } @@ -543,7 +542,7 @@ func writePortsToFile(ports []*PortConfig) (string, error) { if err != nil { return "", err } - defer f.Close() + defer f.Close() // nolint:gosec buf, _ := proto.Marshal(&EndpointRecord{ IngressPorts: ports, diff --git a/libnetwork/store.go b/libnetwork/store.go index a8cdad2524..65ae281fad 100644 --- a/libnetwork/store.go +++ b/libnetwork/store.go @@ -4,11 +4,11 @@ import ( "fmt" "strings" + "github.com/docker/docker/libnetwork/datastore" "github.com/docker/libkv/store/boltdb" "github.com/docker/libkv/store/consul" "github.com/docker/libkv/store/etcd" "github.com/docker/libkv/store/zookeeper" - "github.com/docker/docker/libnetwork/datastore" "github.com/sirupsen/logrus" ) diff --git a/libnetwork/store_linux_test.go b/libnetwork/store_linux_test.go index 2d929f2365..b11a0fbb60 100644 --- a/libnetwork/store_linux_test.go +++ b/libnetwork/store_linux_test.go @@ -4,8 +4,8 @@ import ( "os" "testing" - "github.com/docker/libkv/store" "github.com/docker/docker/libnetwork/datastore" + "github.com/docker/libkv/store" ) func TestBoltdbBackend(t *testing.T) { @@ -35,10 +35,10 @@ func TestNoPersist(t *testing.T) { t.Fatalf("Error creating endpoint: %v", err) } store := ctrl.(*controller).getStore(datastore.LocalScope).KVStore() - if exists, _ := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); exists { + if exists, _ := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, nw.ID())); exists { t.Fatalf("Network with persist=false should not be stored in KV Store") } - if exists, _ := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); exists { + if exists, _ := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, nw.ID(), ep.ID()}...)); exists { t.Fatalf("Endpoint in Network with persist=false should not be stored in KV Store") } store.Close() diff --git a/libnetwork/store_test.go b/libnetwork/store_test.go index a6d5a11b51..0d2d82426d 100644 --- a/libnetwork/store_test.go +++ b/libnetwork/store_test.go @@ -5,31 +5,13 @@ import ( "io/ioutil" "testing" - "github.com/docker/libkv/store" "github.com/docker/docker/libnetwork/config" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/netlabel" "github.com/docker/docker/libnetwork/options" + "github.com/docker/libkv/store" ) -func testZooKeeperBackend(t *testing.T) { - c, err := testNewController(t, "zk", "127.0.0.1:2181/custom_prefix") - if err != nil { - t.Fatal(err) - } - c.Stop() -} - -func testNewController(t *testing.T, provider, url string) (NetworkController, error) { - cfgOptions, err := OptionBoltdbWithRandomDBFile() - if err != nil { - return nil, err - } - cfgOptions = append(cfgOptions, config.OptionKVProvider(provider)) - cfgOptions = append(cfgOptions, config.OptionKVProviderURL(url)) - return New(cfgOptions...) -} - func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) { cfgOptions := []config.Option{} cfgOptions = append(cfgOptions, config.OptionLocalKVProvider(provider)) @@ -54,10 +36,10 @@ func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Con t.Fatalf("Error creating endpoint: %v", err) } store := ctrl.(*controller).getStore(datastore.LocalScope).KVStore() - if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); !exists || err != nil { + if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, nw.ID())); !exists || err != nil { t.Fatalf("Network key should have been created.") } - if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); !exists || err != nil { + if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, nw.ID(), ep.ID()}...)); !exists || err != nil { t.Fatalf("Endpoint key should have been created.") } store.Close() diff --git a/libnetwork/support/run.sh b/libnetwork/support/run.sh index 22c26e9dd0..fa460eeb3d 100755 --- a/libnetwork/support/run.sh +++ b/libnetwork/support/run.sh @@ -4,10 +4,10 @@ wget -O support.sh.new https://raw.githubusercontent.com/docker/libnetwork/master/support/support.sh if [ "$?" -eq "0" ]; then - mv support.sh.new support.sh - chmod +x support.sh + mv support.sh.new support.sh + chmod +x support.sh else - echo "issue fetching the latest support.sh, will use the container version" + echo "issue fetching the latest support.sh, will use the container version" fi echo "run the support script" diff --git a/libnetwork/support/support.sh b/libnetwork/support/support.sh index 9aa90aff77..fdd1ff6e7d 100755 --- a/libnetwork/support/support.sh +++ b/libnetwork/support/support.sh @@ -1,11 +1,11 @@ #!/usr/bin/env bash while getopts ":s" opt; do - case $opt in - s) - SSD="true" - ;; - esac + case $opt in + s) + SSD="true" + ;; + esac done SSD="${SSD:-false}" @@ -26,26 +26,26 @@ ip_overlap=0 NSDIR=/var/run/docker/netns -function die { - echo $* - exit 1 +function die() { + echo $* + exit 1 } -function echo_and_run { - echo "#" "$@" - eval $(printf '%q ' "$@") < /dev/stdout +function echo_and_run() { + echo "#" "$@" + eval $(printf '%q ' "$@") < /dev/stdout } -function check_ip_overlap { - inspect=$1 - overlap=$(echo "$inspect_output" | grep "EndpointIP\|VIP" | cut -d':' -f2 | sort | uniq -c | grep -v "1 ") - if [ ! -z "$overlap" ]; then - echo -e "\n\n*** OVERLAP on Network ${networkID} ***"; - echo -e "${overlap} \n\n" - ((ip_overlap++)) - else - echo "No overlap" - fi +function check_ip_overlap() { + inspect=$1 + overlap=$(echo "$inspect_output" | grep "EndpointIP\|VIP" | cut -d':' -f2 | sort | uniq -c | grep -v "1 ") + if [ ! -z "$overlap" ]; then + echo -e "\n\n*** OVERLAP on Network ${networkID} ***" + echo -e "${overlap} \n\n" + ((ip_overlap++)) + else + echo "No overlap" + fi } type -P ${DOCKER} > /dev/null || echo "This tool requires the docker binary" @@ -57,9 +57,9 @@ type -P ${IP} > /dev/null || echo "This tool requires ip" type -P ${JQ} > /dev/null || echo "This tool requires jq" if ${DOCKER} network inspect --help | grep -q -- --verbose; then - NETINSPECT_VERBOSE_SUPPORT="--verbose" + NETINSPECT_VERBOSE_SUPPORT="--verbose" else - NETINSPECT_VERBOSE_SUPPORT="" + NETINSPECT_VERBOSE_SUPPORT="" fi echo "Host iptables" @@ -76,64 +76,63 @@ printf "\n" echo "Overlay network configuration" for networkID in $(${DOCKER} network ls --no-trunc --filter driver=overlay -q) "ingress_sbox"; do - echo "nnn Network ${networkID}" - if [ "${networkID}" != "ingress_sbox" ]; then - nspath=($(ls ${NSDIR}/*${networkID:0:9}*)) - inspect_output=$(${DOCKER} network inspect ${NETINSPECT_VERBOSE_SUPPORT} ${networkID}) - echo "$inspect_output" - check_ip_overlap $inspect_output - else - nspath=(${NSDIR}/${networkID}) - fi + echo "nnn Network ${networkID}" + if [ "${networkID}" != "ingress_sbox" ]; then + nspath=($(ls ${NSDIR}/*${networkID:0:9}*)) + inspect_output=$(${DOCKER} network inspect ${NETINSPECT_VERBOSE_SUPPORT} ${networkID}) + echo "$inspect_output" + check_ip_overlap $inspect_output + else + nspath=(${NSDIR}/${networkID}) + fi - for i in "${nspath[@]}" - do - echo_and_run ${NSENTER} --net=${i} ${IP} -o -4 address show - echo_and_run ${NSENTER} --net=${i} ${IP} -4 route show - echo_and_run ${NSENTER} --net=${i} ${IP} -4 neigh show - bridges=$(${NSENTER} --net=${i} ${IP} -j link show type bridge | ${JQ} -r '.[].ifname') - # break string to array - bridges=(${bridges}) - for b in "${bridges[@]}" - do - if [ -z ${b} ] || [ ${b} == "null" ]; then - continue - fi - echo_and_run ${NSENTER} --net=${i} ${BRIDGE} fdb show br ${b} - done - echo_and_run ${NSENTER} --net=${i} ${IPTABLES} -w1 -n -v -L -t filter | grep -v '^$' - echo_and_run ${NSENTER} --net=${i} ${IPTABLES} -w1 -n -v -L -t nat | grep -v '^$' - echo_and_run ${NSENTER} --net=${i} ${IPTABLES} -w1 -n -v -L -t mangle | grep -v '^$' - echo_and_run ${NSENTER} --net=${i} ${IPVSADM} -l -n - printf "\n" - ((networks++)) - done + for i in "${nspath[@]}"; do + echo_and_run ${NSENTER} --net=${i} ${IP} -o -4 address show + echo_and_run ${NSENTER} --net=${i} ${IP} -4 route show + echo_and_run ${NSENTER} --net=${i} ${IP} -4 neigh show + bridges=$(${NSENTER} --net=${i} ${IP} -j link show type bridge | ${JQ} -r '.[].ifname') + # break string to array + bridges=(${bridges}) + for b in "${bridges[@]}"; do + if [ -z ${b} ] || [ ${b} == "null" ]; then + continue + fi + echo_and_run ${NSENTER} --net=${i} ${BRIDGE} fdb show br ${b} + done + echo_and_run ${NSENTER} --net=${i} ${IPTABLES} -w1 -n -v -L -t filter | grep -v '^$' + echo_and_run ${NSENTER} --net=${i} ${IPTABLES} -w1 -n -v -L -t nat | grep -v '^$' + echo_and_run ${NSENTER} --net=${i} ${IPTABLES} -w1 -n -v -L -t mangle | grep -v '^$' + echo_and_run ${NSENTER} --net=${i} ${IPVSADM} -l -n + printf "\n" + ((networks++)) + done done echo "Container network configuration" while read containerID status; do - echo "ccc Container ${containerID} state: ${status}" - ${DOCKER} container inspect ${containerID} --format 'Name:{{json .Name | printf "%s\n"}}Id:{{json .Id | printf "%s\n"}}Hostname:{{json .Config.Hostname | printf "%s\n"}}CreatedAt:{{json .Created | printf "%s\n"}}State:{{json .State|printf "%s\n"}}RestartCount:{{json .RestartCount | printf "%s\n" }}Labels:{{json .Config.Labels | printf "%s\n"}}NetworkSettings:{{json .NetworkSettings}}' | sed '/^State:/ {s/\\"/QUOTE/g; s/,"Output":"[^"]*"//g;}' - if [ ${status} = "Up" ]; then - nspath=$(docker container inspect --format {{.NetworkSettings.SandboxKey}} ${containerID}) - echo_and_run ${NSENTER} --net=${nspath[0]} ${IP} -o -4 address show - echo_and_run ${NSENTER} --net=${nspath[0]} ${IP} -4 route show - echo_and_run ${NSENTER} --net=${nspath[0]} ${IP} -4 neigh show - echo_and_run ${NSENTER} --net=${nspath[0]} ${IPTABLES} -w1 -n -v -L -t nat | grep -v '^$' - echo_and_run ${NSENTER} --net=${nspath[0]} ${IPTABLES} -w1 -n -v -L -t mangle | grep -v '^$' - echo_and_run ${NSENTER} --net=${nspath[0]} ${IPVSADM} -l -n - ((containers++)) - fi - printf "\n" -done < <(${DOCKER} container ls -a --format '{{.ID}} {{.Status}}' |cut -d' ' -f1,2) + echo "ccc Container ${containerID} state: ${status}" + ${DOCKER} container inspect ${containerID} --format 'Name:{{json .Name | printf "%s\n"}}Id:{{json .Id | printf "%s\n"}}Hostname:{{json .Config.Hostname | printf "%s\n"}}CreatedAt:{{json .Created | printf "%s\n"}}State:{{json .State|printf "%s\n"}}RestartCount:{{json .RestartCount | printf "%s\n" }}Labels:{{json .Config.Labels | printf "%s\n"}}NetworkSettings:{{json .NetworkSettings}}' | sed '/^State:/ {s/\\"/QUOTE/g; s/,"Output":"[^"]*"//g;}' + if [ ${status} = "Up" ]; then + nspath=$(docker container inspect --format {{.NetworkSettings.SandboxKey}} ${containerID}) + echo_and_run ${NSENTER} --net=${nspath[0]} ${IP} -o -4 address show + echo_and_run ${NSENTER} --net=${nspath[0]} ${IP} -4 route show + echo_and_run ${NSENTER} --net=${nspath[0]} ${IP} -4 neigh show + echo_and_run ${NSENTER} --net=${nspath[0]} ${IPTABLES} -w1 -n -v -L -t nat | grep -v '^$' + echo_and_run ${NSENTER} --net=${nspath[0]} ${IPTABLES} -w1 -n -v -L -t mangle | grep -v '^$' + echo_and_run ${NSENTER} --net=${nspath[0]} ${IPVSADM} -l -n + ((containers++)) + fi + printf "\n" +done < <(${DOCKER} container ls -a --format '{{.ID}} {{.Status}}' | cut -d' ' -f1,2) -if [ "true" == ${SSD} ] ; then - echo "" ; echo "#### SSD control-plane and datapath consistency check on a node ####" - for netName in $(docker network ls -f driver=overlay --format "{{.Name}}") ; do - echo "## $netName ##" - ${SSDBIN} $netName - echo "" - done +if [ "true" == ${SSD} ]; then + echo "" + echo "#### SSD control-plane and datapath consistency check on a node ####" + for netName in $(docker network ls -f driver=overlay --format "{{.Name}}"); do + echo "## $netName ##" + ${SSDBIN} $netName + echo "" + done fi echo -e "\n\n==SUMMARY==" diff --git a/libnetwork/test/integration/dnet/run-integration-tests.sh b/libnetwork/test/integration/dnet/run-integration-tests.sh index 87e4744b15..fd630463f2 100755 --- a/libnetwork/test/integration/dnet/run-integration-tests.sh +++ b/libnetwork/test/integration/dnet/run-integration-tests.sh @@ -10,256 +10,253 @@ declare -A cmap trap "cleanup_containers" EXIT SIGINT function cleanup_containers() { - for c in "${!cmap[@]}"; - do - docker stop $c 1>>${INTEGRATION_ROOT}/test.log 2>&1 || true - if [ -z "$CIRCLECI" ]; then - docker rm -f $c 1>>${INTEGRATION_ROOT}/test.log 2>&1 || true - fi - done + for c in "${!cmap[@]}"; do + docker stop $c 1>> ${INTEGRATION_ROOT}/test.log 2>&1 || true + if [ -z "$CIRCLECI" ]; then + docker rm -f $c 1>> ${INTEGRATION_ROOT}/test.log 2>&1 || true + fi + done - unset cmap + unset cmap } function run_bridge_tests() { - ## Setup - start_dnet 1 bridge 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-bridge]=dnet-1-bridge + ## Setup + start_dnet 1 bridge 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - bridge]=dnet-1-bridge - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/bridge.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/bridge.bats - ## Teardown - stop_dnet 1 bridge 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-bridge] + ## Teardown + stop_dnet 1 bridge 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-bridge] } function run_overlay_local_tests() { - ## Test overlay network in local scope - ## Setup - start_dnet 1 local 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-local]=dnet-1-local - start_dnet 2 local:$(dnet_container_ip 1 local) 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-local]=dnet-2-local - start_dnet 3 local:$(dnet_container_ip 1 local) 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-local]=dnet-3-local + ## Test overlay network in local scope + ## Setup + start_dnet 1 local 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - local]=dnet-1-local + start_dnet 2 local:$(dnet_container_ip 1 local) 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - local]=dnet-2-local + start_dnet 3 local:$(dnet_container_ip 1 local) 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - local]=dnet-3-local - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/overlay-local.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/overlay-local.bats - ## Teardown - stop_dnet 1 local 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-local] - stop_dnet 2 local 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-local] - stop_dnet 3 local 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-local] + ## Teardown + stop_dnet 1 local 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-local] + stop_dnet 2 local 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-local] + stop_dnet 3 local 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-local] } function run_overlay_consul_tests() { - ## Test overlay network with consul - ## Setup - start_dnet 1 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-consul]=dnet-1-consul - start_dnet 2 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-consul]=dnet-2-consul - start_dnet 3 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-consul]=dnet-3-consul + ## Test overlay network with consul + ## Setup + start_dnet 1 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - consul]=dnet-1-consul + start_dnet 2 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - consul]=dnet-2-consul + start_dnet 3 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - consul]=dnet-3-consul - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/overlay-consul.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/overlay-consul.bats - ## Teardown - stop_dnet 1 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-consul] - stop_dnet 2 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-consul] - stop_dnet 3 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-consul] + ## Teardown + stop_dnet 1 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-consul] + stop_dnet 2 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-consul] + stop_dnet 3 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-consul] } function run_overlay_consul_host_tests() { - export _OVERLAY_HOST_MODE="true" - ## Setup - start_dnet 1 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-consul]=dnet-1-consul + export _OVERLAY_HOST_MODE="true" + ## Setup + start_dnet 1 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - consul]=dnet-1-consul - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/overlay-consul-host.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/overlay-consul-host.bats - ## Teardown - stop_dnet 1 consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-consul] - unset _OVERLAY_HOST_MODE + ## Teardown + stop_dnet 1 consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-consul] + unset _OVERLAY_HOST_MODE } function run_overlay_zk_tests() { - ## Test overlay network with zookeeper - start_dnet 1 zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-zookeeper]=dnet-1-zookeeper - start_dnet 2 zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-zookeeper]=dnet-2-zookeeper - start_dnet 3 zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-zookeeper]=dnet-3-zookeeper + ## Test overlay network with zookeeper + start_dnet 1 zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - zookeeper]=dnet-1-zookeeper + start_dnet 2 zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - zookeeper]=dnet-2-zookeeper + start_dnet 3 zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - zookeeper]=dnet-3-zookeeper - ./integration-tmp/bin/bats ./test/integration/dnet/overlay-zookeeper.bats + ./integration-tmp/bin/bats ./test/integration/dnet/overlay-zookeeper.bats - stop_dnet 1 zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-zookeeper] - stop_dnet 2 zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-zookeeper] - stop_dnet 3 zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-zookeeper] + stop_dnet 1 zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-zookeeper] + stop_dnet 2 zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-zookeeper] + stop_dnet 3 zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-zookeeper] } function run_overlay_etcd_tests() { - ## Test overlay network with etcd - start_dnet 1 etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-etcd]=dnet-1-etcd - start_dnet 2 etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-etcd]=dnet-2-etcd - start_dnet 3 etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-etcd]=dnet-3-etcd + ## Test overlay network with etcd + start_dnet 1 etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - etcd]=dnet-1-etcd + start_dnet 2 etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - etcd]=dnet-2-etcd + start_dnet 3 etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - etcd]=dnet-3-etcd - ./integration-tmp/bin/bats ./test/integration/dnet/overlay-etcd.bats + ./integration-tmp/bin/bats ./test/integration/dnet/overlay-etcd.bats - stop_dnet 1 etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-etcd] - stop_dnet 2 etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-etcd] - stop_dnet 3 etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-etcd] + stop_dnet 1 etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-etcd] + stop_dnet 2 etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-etcd] + stop_dnet 3 etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-etcd] } function run_dnet_tests() { - # Test dnet configuration options - ./integration-tmp/bin/bats ./test/integration/dnet/dnet.bats + # Test dnet configuration options + ./integration-tmp/bin/bats ./test/integration/dnet/dnet.bats } function run_multi_consul_tests() { - # Test multi node configuration with a global scope test driver backed by consul + # Test multi node configuration with a global scope test driver backed by consul - ## Setup - start_dnet 1 multi_consul consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-multi_consul]=dnet-1-multi_consul - start_dnet 2 multi_consul consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-multi_consul]=dnet-2-multi_consul - start_dnet 3 multi_consul consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-multi_consul]=dnet-3-multi_consul + ## Setup + start_dnet 1 multi_consul consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - multi_consul]=dnet-1-multi_consul + start_dnet 2 multi_consul consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - multi_consul]=dnet-2-multi_consul + start_dnet 3 multi_consul consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - multi_consul]=dnet-3-multi_consul - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/multi.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/multi.bats - ## Teardown - stop_dnet 1 multi_consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-multi_consul] - stop_dnet 2 multi_consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-multi_consul] - stop_dnet 3 multi_consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-multi_consul] + ## Teardown + stop_dnet 1 multi_consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-multi_consul] + stop_dnet 2 multi_consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-multi_consul] + stop_dnet 3 multi_consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-multi_consul] } function run_multi_zk_tests() { - # Test multi node configuration with a global scope test driver backed by zookeeper + # Test multi node configuration with a global scope test driver backed by zookeeper - ## Setup - start_dnet 1 multi_zk zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-multi_zk]=dnet-1-multi_zk - start_dnet 2 multi_zk zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-multi_zk]=dnet-2-multi_zk - start_dnet 3 multi_zk zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-multi_zk]=dnet-3-multi_zk + ## Setup + start_dnet 1 multi_zk zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - multi_zk]=dnet-1-multi_zk + start_dnet 2 multi_zk zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - multi_zk]=dnet-2-multi_zk + start_dnet 3 multi_zk zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - multi_zk]=dnet-3-multi_zk - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/multi.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/multi.bats - ## Teardown - stop_dnet 1 multi_zk 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-multi_zk] - stop_dnet 2 multi_zk 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-multi_zk] - stop_dnet 3 multi_zk 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-multi_zk] + ## Teardown + stop_dnet 1 multi_zk 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-multi_zk] + stop_dnet 2 multi_zk 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-multi_zk] + stop_dnet 3 multi_zk 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-multi_zk] } function run_multi_etcd_tests() { - # Test multi node configuration with a global scope test driver backed by etcd + # Test multi node configuration with a global scope test driver backed by etcd - ## Setup - start_dnet 1 multi_etcd etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-1-multi_etcd]=dnet-1-multi_etcd - start_dnet 2 multi_etcd etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-2-multi_etcd]=dnet-2-multi_etcd - start_dnet 3 multi_etcd etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dnet-3-multi_etcd]=dnet-3-multi_etcd + ## Setup + start_dnet 1 multi_etcd etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 1 - multi_etcd]=dnet-1-multi_etcd + start_dnet 2 multi_etcd etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 2 - multi_etcd]=dnet-2-multi_etcd + start_dnet 3 multi_etcd etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dnet - 3 - multi_etcd]=dnet-3-multi_etcd - ## Run the test cases - ./integration-tmp/bin/bats ./test/integration/dnet/multi.bats + ## Run the test cases + ./integration-tmp/bin/bats ./test/integration/dnet/multi.bats - ## Teardown - stop_dnet 1 multi_etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-1-multi_etcd] - stop_dnet 2 multi_etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-2-multi_etcd] - stop_dnet 3 multi_etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - unset cmap[dnet-3-multi_etcd] + ## Teardown + stop_dnet 1 multi_etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-1-multi_etcd] + stop_dnet 2 multi_etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-2-multi_etcd] + stop_dnet 3 multi_etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + unset cmap[dnet-3-multi_etcd] } source ./test/integration/dnet/helpers.bash if [ ! -d ${INTEGRATION_ROOT} ]; then - mkdir -p ${INTEGRATION_ROOT} - git clone https://github.com/sstephenson/bats.git ${INTEGRATION_ROOT}/bats - ./integration-tmp/bats/install.sh ./integration-tmp + mkdir -p ${INTEGRATION_ROOT} + git clone https://github.com/sstephenson/bats.git ${INTEGRATION_ROOT}/bats + ./integration-tmp/bats/install.sh ./integration-tmp fi if [ ! -d ${TMPC_ROOT} ]; then - mkdir -p ${TMPC_ROOT} - docker pull busybox:ubuntu - docker export $(docker create busybox:ubuntu) > ${TMPC_ROOT}/busybox.tar - mkdir -p ${TMPC_ROOT}/rootfs - tar -C ${TMPC_ROOT}/rootfs -xf ${TMPC_ROOT}/busybox.tar + mkdir -p ${TMPC_ROOT} + docker pull busybox:ubuntu + docker export $(docker create busybox:ubuntu) > ${TMPC_ROOT}/busybox.tar + mkdir -p ${TMPC_ROOT}/rootfs + tar -C ${TMPC_ROOT}/rootfs -xf ${TMPC_ROOT}/busybox.tar fi # Suite setup if [ -z "$SUITES" ]; then - if [ -n "$CIRCLECI" ] - then - # We can only run a limited list of suites in circleci because of the - # old kernel and limited docker environment. - suites="dnet multi_consul multi_zk multi_etcd" - else - suites="dnet multi_consul multi_zk multi_etcd bridge overlay_consul overlay_consul_host overlay_zk overlay_etcd" - fi + if [ -n "$CIRCLECI" ]; then + # We can only run a limited list of suites in circleci because of the + # old kernel and limited docker environment. + suites="dnet multi_consul multi_zk multi_etcd" + else + suites="dnet multi_consul multi_zk multi_etcd bridge overlay_consul overlay_consul_host overlay_zk overlay_etcd" + fi else - suites="$SUITES" + suites="$SUITES" fi -if [[ ( "$suites" =~ .*consul.* ) || ( "$suites" =~ .*bridge.* ) ]]; then - echo "Starting consul ..." - start_consul 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[pr_consul]=pr_consul +if [[ ("$suites" =~ .*consul.*) || ("$suites" =~ .*bridge.*) ]]; then + echo "Starting consul ..." + start_consul 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[pr_consul]=pr_consul fi if [[ "$suites" =~ .*zk.* ]]; then - echo "Starting zookeeper ..." - start_zookeeper 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[zookeeper_server]=zookeeper_server + echo "Starting zookeeper ..." + start_zookeeper 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[zookeeper_server]=zookeeper_server fi if [[ "$suites" =~ .*etcd.* ]]; then - echo "Starting etcd ..." - start_etcd 1>>${INTEGRATION_ROOT}/test.log 2>&1 - cmap[dn_etcd]=dn_etcd + echo "Starting etcd ..." + start_etcd 1>> ${INTEGRATION_ROOT}/test.log 2>&1 + cmap[dn_etcd]=dn_etcd fi echo "" -for suite in ${suites}; -do - suite_func=run_${suite}_tests - echo "Running ${suite}_tests ..." - declare -F $suite_func >/dev/null && $suite_func - echo "" +for suite in ${suites}; do + suite_func=run_${suite}_tests + echo "Running ${suite}_tests ..." + declare -F $suite_func > /dev/null && $suite_func + echo "" done diff --git a/libnetwork/test/integration/helpers.bash b/libnetwork/test/integration/helpers.bash index ec18e5d4eb..8ca3a3c635 100644 --- a/libnetwork/test/integration/helpers.bash +++ b/libnetwork/test/integration/helpers.bash @@ -3,13 +3,13 @@ # Root directory of the repository. MACHINE_ROOT=/usr/bin -PLATFORM=`uname -s | tr '[:upper:]' '[:lower:]'` -ARCH=`uname -m` +PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) if [ "$ARCH" = "x86_64" ]; then - ARCH="amd64" + ARCH="amd64" else - ARCH="386" + ARCH="386" fi MACHINE_BIN_NAME=docker-machine_$PLATFORM-$ARCH BATS_LOG=/tmp/bats.log @@ -18,7 +18,7 @@ touch ${BATS_LOG} rm ${BATS_LOG} teardown() { - echo "$BATS_TEST_NAME + echo "$BATS_TEST_NAME ---------- $output ---------- @@ -31,20 +31,19 @@ EXTRA_ARGS='--tlsverify --tlscacert=/var/lib/boot2docker/ca.pem --tlskey=/var/li TMP_EXTRA_ARGS_FILE=/tmp/tmp_extra_args DAEMON_CFG_FILE=${BATS_TEST_DIRNAME}/daemon.cfg set_extra_config() { - if [ -f ${TMP_EXTRA_ARGS_FILE} ]; - then - rm ${TMP_EXTRA_ARGS_FILE} - fi - echo -n "${EXTRA_ARGS_CFG}='" > ${TMP_EXTRA_ARGS_FILE} - echo -n "$1 " >> ${TMP_EXTRA_ARGS_FILE} - echo "${EXTRA_ARGS}'" >> ${TMP_EXTRA_ARGS_FILE} + if [ -f ${TMP_EXTRA_ARGS_FILE} ]; then + rm ${TMP_EXTRA_ARGS_FILE} + fi + echo -n "${EXTRA_ARGS_CFG}='" > ${TMP_EXTRA_ARGS_FILE} + echo -n "$1 " >> ${TMP_EXTRA_ARGS_FILE} + echo "${EXTRA_ARGS}'" >> ${TMP_EXTRA_ARGS_FILE} } if [ ! -e $MACHINE_ROOT/$MACHINE_BIN_NAME ]; then - echo "${MACHINE_ROOT}/${MACHINE_BIN_NAME} not found" - exit 1 + echo "${MACHINE_ROOT}/${MACHINE_BIN_NAME} not found" + exit 1 fi function machine() { - ${MACHINE_ROOT}/$MACHINE_BIN_NAME "$@" + ${MACHINE_ROOT}/$MACHINE_BIN_NAME "$@" }