Skip to content

Commit

Permalink
Update ImageName.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Devashishbasu committed Feb 6, 2024
1 parent 5db6f9b commit acf4f7d
Showing 1 changed file with 32 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,28 @@ public ImageName(String fullName, String givenTag) {
fullName = digestParts[0];
}

// check for tag
Pattern tagPattern = Pattern.compile("^(.+?)(?::([^:/]+))?$");
Matcher matcher = tagPattern.matcher(fullName);
if (!matcher.matches()) {
throw new IllegalArgumentException(fullName + " is not a proper image name ([registry/][repo][:port]");
}
// Extract tag if it exists, but ignore it if digest is also provided
if (digest == null) {
tag = givenTag != null ? givenTag : matcher.group(2);
// check for tag and digest
Pattern tagDigestPattern = Pattern.compile("^(.+?)(?::([^:/]+))?@(.+)$");
Matcher tagDigestMatcher = tagDigestPattern.matcher(fullName);
if (tagDigestMatcher.matches()) {
// extract digest and ignore tag
digest = tagDigestMatcher.group(3);
tag = null;
fullName = tagDigestMatcher.group(1);
} else {
// check for tag
Pattern tagPattern = Pattern.compile("^(.+?)(?::([^:/]+))?$");
Matcher tagMatcher = tagPattern.matcher(fullName);
if (!tagMatcher.matches()) {
throw new IllegalArgumentException(fullName + " is not a proper image name ([registry/][repo][:port]");
}
// extract tag if it exists
tag = givenTag != null ? givenTag : tagMatcher.group(2);
fullName = tagMatcher.group(1);
}
String rest = matcher.group(1);

// extract registry, repository, user
parseComponentsBeforeTag(rest);
parseComponentsBeforeTag(fullName);

/*
* set tag to latest if tag AND digest are null
Expand Down Expand Up @@ -195,10 +203,6 @@ public String getNameWithoutTag(String optionalRegistry) {
ret.append(registry != null ? registry : optionalRegistry).append("/");
}
ret.append(repository);
// Include digest if it exists
if (digest != null) {
ret.append("@").append(digest);
}
return ret.toString();
}

Expand All @@ -223,29 +227,16 @@ public String getFullName() {
* @return full name with original registry (if set) or optional registry (if not <code>null</code>).
*/
public String getFullName(String optionalRegistry) {
StringBuilder ret = new StringBuilder();

if (!isFullyQualifiedName() && isRegistryValidPathComponent() &&
StringUtils.isNotBlank(optionalRegistry) && !optionalRegistry.equals(registry)) {
ret.append(optionalRegistry).append("/").append(registry).append("/");
} else if (registry != null || optionalRegistry != null) {
ret.append(registry != null ? registry : optionalRegistry).append("/");
String fullName = getNameWithoutTag(optionalRegistry);
if (tag != null) {
fullName = fullName + ":" + tag;
}
ret.append(repository);

if (digest != null) {
ret.append("@").append(digest);
} else if (tag != null) {
ret.append(":").append(tag);
} else {
ret.append(":latest"); // If both tag and digest are null, set the tag to "latest"
if(digest != null) {
fullName = fullName + "@" + digest;
}
return ret.toString();
return fullName;
}




/**
* Try to infer the user (or "project") part of the image name. This is usually the part after the registry
* and before the image name.
Expand Down Expand Up @@ -294,11 +285,11 @@ private void doValidate() {
String user = inferUser();
String image = user != null ? repository.substring(user.length() + 1) : repository;
Object[] checks = new Object[] {
"registry", DOMAIN_REGEXP, registry,
"image", IMAGE_NAME_REGEXP, image,
"user", NAME_COMP_REGEXP, user,
"tag", TAG_REGEXP, tag,
"digest", DIGEST_REGEXP, digest
"registry", DOMAIN_REGEXP, registry,
"image", IMAGE_NAME_REGEXP, image,
"user", NAME_COMP_REGEXP, user,
"tag", TAG_REGEXP, tag,
"digest", DIGEST_REGEXP, digest
};

if (repository.length() > REPO_NAME_MAX_LENGTH) {
Expand All @@ -309,9 +300,9 @@ private void doValidate() {
String value = (String) checks[i + 2];
Pattern checkPattern = (Pattern) checks[i + 1];
if (value != null &&
!checkPattern.matcher(value).matches()) {
!checkPattern.matcher(value).matches()) {
errors.add(String.format("%s part '%s' doesn't match allowed pattern '%s'",
checks[i], value, checkPattern.pattern()));
checks[i], value, checkPattern.pattern()));
}
}
if (!errors.isEmpty()) {
Expand Down

0 comments on commit acf4f7d

Please sign in to comment.