diff options
Diffstat (limited to 'config-provisioning/src/main/java/com/yahoo/config/provision/DockerImage.java')
-rw-r--r-- | config-provisioning/src/main/java/com/yahoo/config/provision/DockerImage.java | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/DockerImage.java b/config-provisioning/src/main/java/com/yahoo/config/provision/DockerImage.java index bbf65c1cd47..8350badc1fe 100644 --- a/config-provisioning/src/main/java/com/yahoo/config/provision/DockerImage.java +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/DockerImage.java @@ -7,42 +7,57 @@ import java.util.Objects; import java.util.Optional; /** - * A Docker image. + * A container image. * * @author mpolden */ public class DockerImage { - public static final DockerImage EMPTY = new DockerImage("", Optional.empty()); + public static final DockerImage EMPTY = new DockerImage("", "", Optional.empty()); + private final String registry; private final String repository; private final Optional<String> tag; - private DockerImage(String repository, Optional<String> tag) { + DockerImage(String registry, String repository, Optional<String> tag) { + this.registry = Objects.requireNonNull(registry, "registry must be non-null"); this.repository = Objects.requireNonNull(repository, "repository must be non-null"); this.tag = Objects.requireNonNull(tag, "tag must be non-null"); } + /** Returns the registry-part of this, i.e. the host/port of the registry. */ + public String registry() { + return registry; + } + + /** Returns the repository-part of this */ public String repository() { return repository; } + /** Returns the registry and repository for this image, excluding its tag */ + public String untagged() { + return new DockerImage(registry, repository, Optional.empty()).asString(); + } + + /** Returns this image's tag, if any */ public Optional<String> tag() { return tag; } - /** Returns the tag as Version, {@link Version#emptyVersion} if tag is not set */ + /** Returns the tag as a {@link Version}, {@link Version#emptyVersion} if tag is not set */ public Version tagAsVersion() { return tag.map(Version::new).orElse(Version.emptyVersion); } /** Returns the Docker image tagged with the given version */ public DockerImage withTag(Version version) { - return new DockerImage(repository, Optional.of(version.toFullString())); + return new DockerImage(registry, repository, Optional.of(version.toFullString())); } public String asString() { - return repository + tag.map(t -> ':' + t).orElse(""); + if (equals(EMPTY)) return ""; + return registry + "/" + repository + tag.map(t -> ':' + t).orElse(""); } @Override @@ -55,25 +70,32 @@ public class DockerImage { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DockerImage that = (DockerImage) o; - return repository.equals(that.repository) && - tag.equals(that.tag); + return registry.equals(that.registry) && + repository.equals(that.repository) && + tag.equals(that.tag); } @Override public int hashCode() { - return Objects.hash(repository, tag); + return Objects.hash(registry, repository, tag); } - public static DockerImage fromString(String name) { - if (name.isEmpty()) return EMPTY; + public static DockerImage fromString(String s) { + if (s.isEmpty()) return EMPTY; - int n = name.lastIndexOf(':'); - if (n < 0) return new DockerImage(name, Optional.empty()); + int firstPathSeparator = s.indexOf('/'); + if (firstPathSeparator < 0) throw new IllegalArgumentException("Missing path separator in '" + s + "'"); - String tag = name.substring(n + 1); - if (!tag.contains("/")) { - return new DockerImage(name.substring(0, n), Optional.of(tag)); - } - return new DockerImage(name, Optional.empty()); + String registry = s.substring(0, firstPathSeparator); + String repository = s.substring(firstPathSeparator + 1); + if (repository.isEmpty()) throw new IllegalArgumentException("Repository must be non-empty in '" + s + "'"); + + int tagStart = repository.indexOf(':'); + if (tagStart < 0) return new DockerImage(registry, repository, Optional.empty()); + + String tag = repository.substring(tagStart + 1); + repository = repository.substring(0, tagStart); + return new DockerImage(registry, repository, Optional.of(tag)); } + } |