From b948b07e2d72e59cbb5a3ff4e0379b4382fd9525 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 25 May 2024 16:12:10 +0200 Subject: [PATCH] remove uses of github.com/docker/docker/builder/remotecontext package This package is part of the classic builder, and was currently only used for the IsURL utility, which is a very rudimentary check for a string having a "https://" or "http://" scheme. This patch copies the code as non-exported functions where they're used to remove the dependency. Signed-off-by: Sebastiaan van Stijn --- build/opt.go | 3 +- build/utils.go | 10 ++- controller/pb/path.go | 12 ++- .../builder/remotecontext/urlutil/urlutil.go | 86 ------------------- vendor/modules.txt | 1 - 5 files changed, 18 insertions(+), 94 deletions(-) delete mode 100644 vendor/github.com/docker/docker/builder/remotecontext/urlutil/urlutil.go diff --git a/build/opt.go b/build/opt.go index 5d05e25a..38361a08 100644 --- a/build/opt.go +++ b/build/opt.go @@ -20,7 +20,6 @@ import ( "github.com/docker/buildx/util/dockerutil" "github.com/docker/buildx/util/osutil" "github.com/docker/buildx/util/progress" - "github.com/docker/docker/builder/remotecontext/urlutil" "github.com/moby/buildkit/client" "github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/ociindex" @@ -452,7 +451,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, addVCSL dockerfileName = "Dockerfile" target.FrontendAttrs["dockerfilekey"] = "dockerfile" } - if urlutil.IsURL(inp.DockerfilePath) { + if isHTTPURL(inp.DockerfilePath) { dockerfileDir, err = createTempDockerfileFromURL(ctx, d, inp.DockerfilePath, pw) if err != nil { return nil, err diff --git a/build/utils.go b/build/utils.go index 8894b592..3d095ae6 100644 --- a/build/utils.go +++ b/build/utils.go @@ -11,7 +11,6 @@ import ( "github.com/docker/buildx/driver" "github.com/docker/cli/opts" - "github.com/docker/docker/builder/remotecontext/urlutil" "github.com/moby/buildkit/util/gitutil" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -26,8 +25,15 @@ const ( mobyHostGatewayName = "host-gateway" ) +// isHTTPURL returns true if the provided str is an HTTP(S) URL by checking if it +// has a http:// or https:// scheme. No validation is performed to verify if the +// URL is well-formed. +func isHTTPURL(str string) bool { + return strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://") +} + func IsRemoteURL(c string) bool { - if urlutil.IsURL(c) { + if isHTTPURL(c) { return true } if _, err := gitutil.ParseGitRef(c); err == nil { diff --git a/controller/pb/path.go b/controller/pb/path.go index b2bbe0c7..b04b355d 100644 --- a/controller/pb/path.go +++ b/controller/pb/path.go @@ -4,7 +4,6 @@ import ( "path/filepath" "strings" - "github.com/docker/docker/builder/remotecontext/urlutil" "github.com/moby/buildkit/util/gitutil" ) @@ -22,7 +21,7 @@ func ResolveOptionPaths(options *BuildOptions) (_ *BuildOptions, err error) { } } if options.DockerfileName != "" && options.DockerfileName != "-" { - if localContext && !urlutil.IsURL(options.DockerfileName) { + if localContext && !isHTTPURL(options.DockerfileName) { options.DockerfileName, err = filepath.Abs(options.DockerfileName) if err != nil { return nil, err @@ -164,8 +163,15 @@ func ResolveOptionPaths(options *BuildOptions) (_ *BuildOptions, err error) { return options, nil } +// isHTTPURL returns true if the provided str is an HTTP(S) URL by checking if it +// has a http:// or https:// scheme. No validation is performed to verify if the +// URL is well-formed. +func isHTTPURL(str string) bool { + return strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://") +} + func isRemoteURL(c string) bool { - if urlutil.IsURL(c) { + if isHTTPURL(c) { return true } if _, err := gitutil.ParseGitRef(c); err == nil { diff --git a/vendor/github.com/docker/docker/builder/remotecontext/urlutil/urlutil.go b/vendor/github.com/docker/docker/builder/remotecontext/urlutil/urlutil.go deleted file mode 100644 index e8459cc8..00000000 --- a/vendor/github.com/docker/docker/builder/remotecontext/urlutil/urlutil.go +++ /dev/null @@ -1,86 +0,0 @@ -// Package urlutil provides helper function to check if a given build-context -// location should be considered a URL or a remote Git repository. -// -// This package is specifically written for use with docker build contexts, and -// should not be used as a general-purpose utility. -package urlutil // import "github.com/docker/docker/builder/remotecontext/urlutil" - -import ( - "regexp" - "strings" -) - -// urlPathWithFragmentSuffix matches fragments to use as Git reference and build -// context from the Git repository. See IsGitURL for details. -var urlPathWithFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`) - -// IsURL returns true if the provided str is an HTTP(S) URL by checking if it -// has a http:// or https:// scheme. No validation is performed to verify if the -// URL is well-formed. -func IsURL(str string) bool { - return strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://") -} - -// IsGitURL returns true if the provided str is a remote git repository "URL". -// -// This function only performs a rudimentary check (no validation is performed -// to ensure the URL is well-formed), and is written specifically for use with -// docker build, with some logic for backward compatibility with older versions -// of docker: do not use this function as a general-purpose utility. -// -// The following patterns are considered to be a Git URL: -// -// - https://(.*).git(?:#.+)?$ git repository URL with optional fragment, as known to be used by GitHub and GitLab. -// - http://(.*).git(?:#.+)?$ same, but non-TLS -// - git://(.*) URLs using git:// scheme -// - git@(.*) -// - github.com/ see description below -// -// The github.com/ prefix is a special case used to treat context-paths -// starting with "github.com/" as a git URL if the given path does not -// exist locally. The "github.com/" prefix is kept for backward compatibility, -// and is a legacy feature. -// -// Going forward, no additional prefixes should be added, and users should -// be encouraged to use explicit URLs (https://github.com/user/repo.git) instead. -// -// Note that IsGitURL does not check if "github.com/" prefixes exist as a local -// path. Code using this function should check if the path exists locally before -// using it as a URL. -// -// # Fragments -// -// Git URLs accept context configuration in their fragment section, separated by -// a colon (`:`). The first part represents the reference to check out, and can -// be either a branch, a tag, or a remote reference. The second part represents -// a subdirectory inside the repository to use as the build context. -// -// For example,the following URL uses a directory named "docker" in the branch -// "container" in the https://github.com/myorg/my-repo.git repository: -// -// https://github.com/myorg/my-repo.git#container:docker -// -// The following table represents all the valid suffixes with their build -// contexts: -// -// | Build Syntax Suffix | Git reference used | Build Context Used | -// |--------------------------------|----------------------|--------------------| -// | my-repo.git | refs/heads/master | / | -// | my-repo.git#mytag | refs/tags/my-tag | / | -// | my-repo.git#mybranch | refs/heads/my-branch | / | -// | my-repo.git#pull/42/head | refs/pull/42/head | / | -// | my-repo.git#:directory | refs/heads/master | /directory | -// | my-repo.git#master:directory | refs/heads/master | /directory | -// | my-repo.git#mytag:directory | refs/tags/my-tag | /directory | -// | my-repo.git#mybranch:directory | refs/heads/my-branch | /directory | -func IsGitURL(str string) bool { - if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) { - return true - } - for _, prefix := range []string{"git://", "github.com/", "git@"} { - if strings.HasPrefix(str, prefix) { - return true - } - } - return false -} diff --git a/vendor/modules.txt b/vendor/modules.txt index c0f8e281..4d4856e2 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -289,7 +289,6 @@ github.com/docker/docker/api/types/system github.com/docker/docker/api/types/time github.com/docker/docker/api/types/versions github.com/docker/docker/api/types/volume -github.com/docker/docker/builder/remotecontext/urlutil github.com/docker/docker/client github.com/docker/docker/errdefs github.com/docker/docker/internal/multierror