pkg/urlutil: deprecate, and move to builder/remotecontext/urlutil

pkg/urlutil (despite its poorly chosen name) is not really intended as a generic
utility to handle URLs, and should only be used by the builder to handle (remote)
build contexts.

- IsURL() only does a very rudimentary check for http(s):// prefixes, without any
  other validation, but due to its name may give incorrect expectations.
- IsGitURL() is written specifically with docker build remote git contexts in
  mind, and has handling for backward-compatibility, where strings that are
  not URLs, but start with "github.com/" are accepted.

Because of the above, this patch:

- moves the package inside builder/remotecontext, close to where it's intended
  to be used (ideally this would be part of build/remotecontext itself, but this
  package imports many other dependencies, which would introduce those as extra
  dependencies in the CLI).
- deprecates pkg/urlutil, but adds aliases as there are some external consumers.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-04-10 16:57:45 +02:00
parent 074bc1c3ab
commit 5f89a6a78e
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
7 changed files with 37 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/remotecontext"
"github.com/docker/docker/builder/remotecontext/urlutil"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/containerfs"
"github.com/docker/docker/pkg/idtools"
@ -23,7 +24,6 @@ import (
"github.com/docker/docker/pkg/progress"
"github.com/docker/docker/pkg/streamformatter"
"github.com/docker/docker/pkg/system"
"github.com/docker/docker/pkg/urlutil"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"

View File

@ -11,9 +11,9 @@ import (
"github.com/containerd/continuity/driver"
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/remotecontext/urlutil"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/urlutil"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/pkg/errors"

View File

@ -1,6 +1,6 @@
// Package urlutil provides helper function to check urls kind.
// It supports http and git urls.
package urlutil // import "github.com/docker/docker/pkg/urlutil"
package urlutil // import "github.com/docker/docker/builder/remotecontext/urlutil"
import (
"regexp"

View File

@ -1,4 +1,4 @@
package urlutil // import "github.com/docker/docker/pkg/urlutil"
package urlutil // import "github.com/docker/docker/builder/remotecontext/urlutil"
import "testing"

View File

@ -251,6 +251,12 @@ Function Validate-PkgImports($headCommit, $upstreamCommit) {
$files=@(); $files = Invoke-Expression "git diff $upstreamCommit...$headCommit --diff-filter=ACMR --name-only -- `'pkg\*.go`'"
$badFiles=@(); $files | ForEach-Object{
$file=$_
if ($file -eq "pkg\urlutil\deprecated.go") {
# pkg/urlutil is deprecated, but has a temporary alias to help migration,
# see https://github.com/moby/moby/pull/43477
# TODO(thaJeztah) remove this exception once pkg/urlutil aliases are removed
return
}
# For the current changed file, get its list of dependencies, sorted and uniqued.
$imports = Invoke-Expression "go list -e -f `'{{ .Deps }}`' $file"
if ($LASTEXITCODE -ne 0) { Throw "Failed go list for dependencies on $file" }

View File

@ -10,6 +10,12 @@ unset IFS
badFiles=()
for f in "${files[@]}"; do
if [ "$f" = "pkg/urlutil/deprecated.go" ]; then
# pkg/urlutil is deprecated, but has a temporary alias to help migration,
# see https://github.com/moby/moby/pull/43477
# TODO(thaJeztah) remove this exception once pkg/urlutil aliases are removed
continue
fi
IFS=$'\n'
badImports=($(go list -e -f '{{ join .Deps "\n" }}' "$f" | sort -u | grep -vE '^github.com/docker/docker/pkg/' | grep -vE '^github.com/docker/docker/vendor' | grep -E '^github.com/docker/docker' || true))
unset IFS

21
pkg/urlutil/deprecated.go Normal file
View File

@ -0,0 +1,21 @@
package urlutil // import "github.com/docker/docker/pkg/urlutil"
import "github.com/docker/docker/builder/remotecontext/urlutil"
// IsURL returns true if the provided str is an HTTP(S) URL.
//
// Deprecated: use github.com/docker/docker/builder/remotecontext/urlutil.IsURL
// to detect build-context type, or use strings.HasPrefix() to check if the
// string has a https:// or http:// prefix.
func IsURL(str string) bool {
// TODO(thaJeztah) when removing this alias, remove the exception from hack/validate/pkg-imports and hack/make.ps1 (Validate-PkgImports)
return urlutil.IsURL(str)
}
// IsGitURL returns true if the provided str is a git repository URL.
//
// Deprecated: use github.com/docker/docker/builder/remotecontext/urlutil.IsGitURL
func IsGitURL(str string) bool {
// TODO(thaJeztah) when removing this alias, remove the exception from hack/validate/pkg-imports and hack/make.ps1 (Validate-PkgImports)
return urlutil.IsGitURL(str)
}