Skip to content

Commit

Permalink
Merge pull request #20008 from smarterclayton/builder
Browse files Browse the repository at this point in the history
[3.11 pre-rebase] - Prepare to move pkg/build/builder out of openshift binary
  • Loading branch information
openshift-merge-robot committed Jun 17, 2018
2 parents 29484db + 70597de commit 1ff2229
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 309 deletions.
1 change: 1 addition & 0 deletions hack/import-restrictions.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"allowedImportPackageRoots": [
"vendor/k8s.io/apimachinery",
"github.com/openshift/origin/pkg/image/apis/image/internal",
"github.com/openshift/origin/pkg/image/apis/image/reference",
"vendor/k8s.io/api",
"vendor/github.com/openshift/api",
"github.com/openshift/origin/pkg/api/apihelpers"
Expand Down
4 changes: 1 addition & 3 deletions pkg/apps/apis/apps/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/build/builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/openshift/origin/pkg/build/builder/util/dockerfile"
buildutil "github.com/openshift/origin/pkg/build/util"
"github.com/openshift/origin/pkg/git"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imagereference "github.com/openshift/origin/pkg/image/apis/image/reference"
utilglog "github.com/openshift/origin/pkg/util/glog"
)

Expand Down Expand Up @@ -363,7 +363,7 @@ func addBuildParameters(dir string, build *buildapiv1.Build, sourceInfo *git.Sou
if build.Spec.Strategy.DockerStrategy != nil && build.Spec.Strategy.DockerStrategy.From != nil && build.Spec.Strategy.DockerStrategy.From.Kind == "DockerImage" {
// Reduce the name to a minimal canonical form for the daemon
name := build.Spec.Strategy.DockerStrategy.From.Name
if ref, err := imageapi.ParseDockerImageReference(name); err == nil {
if ref, err := imagereference.Parse(name); err == nil {
name = ref.DaemonMinimal().Exact()
}
err := replaceLastFrom(node, name)
Expand Down
6 changes: 3 additions & 3 deletions pkg/build/builder/dockerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/openshift/source-to-image/pkg/tar"
s2iutil "github.com/openshift/source-to-image/pkg/util"

imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/image/apis/image/reference"
)

var (
Expand Down Expand Up @@ -114,13 +114,13 @@ func pullImage(client DockerClient, name string, authConfig docker.AuthConfigura
glog.V(0).Infof("%s", s)
}

ref, err := imageapi.ParseDockerImageReference(name)
ref, err := reference.Parse(name)
if err != nil {
return err
}
tag := ref.ID
if len(ref.ID) == 0 {
tag = imageapi.DefaultImageTag
tag = "latest"
if len(ref.Tag) != 0 {
tag = ref.Tag
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/controller/strategy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
buildapiv1 "github.com/openshift/api/build/v1"
"github.com/openshift/origin/pkg/api/apihelpers"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/image/apis/image/reference"
)

const (
Expand Down Expand Up @@ -242,7 +242,7 @@ func addOutputEnvVars(buildOutput *kapi.ObjectReference, output *[]v1.EnvVar) er
if buildOutput.Kind != "DockerImage" {
return fmt.Errorf("invalid build output kind %s, must be DockerImage", buildOutput.Kind)
}
ref, err := imageapi.ParseDockerImageReference(buildOutput.Name)
ref, err := reference.Parse(buildOutput.Name)
if err != nil {
return err
}
Expand Down
183 changes: 4 additions & 179 deletions pkg/image/apis/image/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"net"
"net/url"
"regexp"
"sort"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/golang/glog"

"github.com/openshift/origin/pkg/image/apis/image/internal/digest"
"github.com/openshift/origin/pkg/image/apis/image/reference"
)

const (
Expand Down Expand Up @@ -145,191 +145,16 @@ func ParseImageStreamTagName(istag string) (name string, tag string, err error)
return
}

// IsRegistryDockerHub returns true if the given registry name belongs to
// Docker hub.
func IsRegistryDockerHub(registry string) bool {
switch registry {
case DockerDefaultRegistry, DockerDefaultV1Registry, DockerDefaultV2Registry:
return true
default:
return false
}
}

// ParseDockerImageReference parses a Docker pull spec string into a
// DockerImageReference.
func ParseDockerImageReference(spec string) (DockerImageReference, error) {
var ref DockerImageReference

namedRef, err := parseNamedDockerImageReference(spec)
func ParseDockerImageReference(spec string) (reference.DockerImageReference, error) {
ref, err := reference.Parse(spec)
if err != nil {
return ref, err
}

ref.Registry = namedRef.Registry
ref.Namespace = namedRef.Namespace
ref.Name = namedRef.Name
ref.Tag = namedRef.Tag
ref.ID = namedRef.ID

return ref, nil
}

// Equal returns true if the other DockerImageReference is equivalent to the
// reference r. The comparison applies defaults to the Docker image reference,
// so that e.g., "foobar" equals "docker.io/library/foobar:latest".
func (r DockerImageReference) Equal(other DockerImageReference) bool {
defaultedRef := r.DockerClientDefaults()
otherDefaultedRef := other.DockerClientDefaults()
return defaultedRef == otherDefaultedRef
}

// DockerClientDefaults sets the default values used by the Docker client.
func (r DockerImageReference) DockerClientDefaults() DockerImageReference {
if len(r.Registry) == 0 {
r.Registry = DockerDefaultRegistry
}
if len(r.Namespace) == 0 && IsRegistryDockerHub(r.Registry) {
r.Namespace = DockerDefaultNamespace
}
if len(r.Tag) == 0 {
r.Tag = DefaultImageTag
}
return r
}

// Minimal reduces a DockerImageReference to its minimalist form.
func (r DockerImageReference) Minimal() DockerImageReference {
if r.Tag == DefaultImageTag {
r.Tag = ""
}
return r
}

// AsRepository returns the reference without tags or IDs.
func (r DockerImageReference) AsRepository() DockerImageReference {
r.Tag = ""
r.ID = ""
return r
}

// RepositoryName returns the registry relative name
func (r DockerImageReference) RepositoryName() string {
r.Tag = ""
r.ID = ""
r.Registry = ""
return r.Exact()
}

// RegistryHostPort returns the registry hostname and the port.
// If the port is not specified in the registry hostname we default to 443.
// This will also default to Docker client defaults if the registry hostname is empty.
func (r DockerImageReference) RegistryHostPort(insecure bool) (string, string) {
registryHost := r.AsV2().DockerClientDefaults().Registry
if strings.Contains(registryHost, ":") {
hostname, port, _ := net.SplitHostPort(registryHost)
return hostname, port
}
if insecure {
return registryHost, "80"
}
return registryHost, "443"
}

// RepositoryName returns the registry relative name
func (r DockerImageReference) RegistryURL() *url.URL {
return &url.URL{
Scheme: "https",
Host: r.AsV2().Registry,
}
}

// DaemonMinimal clears defaults that Docker assumes.
func (r DockerImageReference) DaemonMinimal() DockerImageReference {
switch r.Registry {
case DockerDefaultV1Registry, DockerDefaultV2Registry:
r.Registry = DockerDefaultRegistry
}
if IsRegistryDockerHub(r.Registry) && r.Namespace == DockerDefaultNamespace {
r.Namespace = ""
}
return r.Minimal()
}

func (r DockerImageReference) AsV2() DockerImageReference {
switch r.Registry {
case DockerDefaultV1Registry, DockerDefaultRegistry:
r.Registry = DockerDefaultV2Registry
}
return r
}

// MostSpecific returns the most specific image reference that can be constructed from the
// current ref, preferring an ID over a Tag. Allows client code dealing with both tags and IDs
// to get the most specific reference easily.
func (r DockerImageReference) MostSpecific() DockerImageReference {
if len(r.ID) == 0 {
return r
}
if _, err := digest.ParseDigest(r.ID); err == nil {
r.Tag = ""
return r
}
if len(r.Tag) == 0 {
r.Tag, r.ID = r.ID, ""
return r
}
return r
}

// NameString returns the name of the reference with its tag or ID.
func (r DockerImageReference) NameString() string {
switch {
case len(r.Name) == 0:
return ""
case len(r.Tag) > 0:
return r.Name + ":" + r.Tag
case len(r.ID) > 0:
var ref string
if _, err := digest.ParseDigest(r.ID); err == nil {
// if it parses as a digest, its v2 pull by id
ref = "@" + r.ID
} else {
// if it doesn't parse as a digest, it's presumably a v1 registry by-id tag
ref = ":" + r.ID
}
return r.Name + ref
default:
return r.Name
}
}

// Exact returns a string representation of the set fields on the DockerImageReference
func (r DockerImageReference) Exact() string {
name := r.NameString()
if len(name) == 0 {
return name
}
s := r.Registry
if len(s) > 0 {
s += "/"
}

if len(r.Namespace) != 0 {
s += r.Namespace + "/"
}
return s + name
}

// String converts a DockerImageReference to a Docker pull spec (which implies a default namespace
// according to V1 Docker registry rules). Use Exact() if you want no defaulting.
func (r DockerImageReference) String() string {
if len(r.Namespace) == 0 && IsRegistryDockerHub(r.Registry) {
r.Namespace = DockerDefaultNamespace
}
return r.Exact()
}

// SplitImageStreamTag turns the name of an ImageStreamTag into Name and Tag.
// It returns false if the tag was not properly specified in the name.
func SplitImageStreamTag(nameAndTag string) (name string, tag string, ok bool) {
Expand Down Expand Up @@ -790,7 +615,7 @@ func ResolveImageID(stream *ImageStream, imageID string) (*TagEvent, error) {
// MostAccuratePullSpec returns a docker image reference that uses the current ID if possible, the current tag otherwise, and
// returns false if the reference if the spec could not be parsed. The returned spec has all client defaults applied.
func MostAccuratePullSpec(pullSpec string, id, tag string) (string, bool) {
ref, err := ParseDockerImageReference(pullSpec)
ref, err := reference.Parse(pullSpec)
if err != nil {
return pullSpec, false
}
Expand Down
51 changes: 0 additions & 51 deletions pkg/image/apis/image/reference.go

This file was deleted.

Loading

0 comments on commit 1ff2229

Please sign in to comment.