Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix parsing docker image when the full image name contains registry port #349

Closed
wants to merge 12 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class DockerImageNameHelper {
/**
* Docker image names are by convention separated by slashes. The last portion is the image's name.
* This is followed by a colon and a version number. e.g. airbyte/scheduler:v1 or
* gcr.io/my-project/my-project:v2.
* gcr.io/my-project/image-name:v2. Registry name may also include port number,
* e.g. registry.internal:1234/my-project/image-name:v2
*
* @param fullImagePath the image name with repository and version ex
* gcr.io/my-project/image-name:v2
Expand All @@ -43,7 +44,7 @@ public static String extractShortImageName(final String fullImagePath) {
* Extracts the image name without the version tag.
*
* @param fullImagePath the docker image name
* @return anything before ":"
* @return anything before last ":"
*/
public static String extractImageNameWithoutVersion(final String fullImagePath) {
return extractPartFromFullPath(fullImagePath, NAME_PARTS_INDEX);
Expand All @@ -53,7 +54,7 @@ public static String extractImageNameWithoutVersion(final String fullImagePath)
* Extracts the image version label as a string.
*
* @param fullImagePath the docker image name
* @return anything after ":"
* @return anything after last ":"
*/
public static String extractImageVersionString(final String fullImagePath) {
return extractPartFromFullPath(fullImagePath, VERSION_PART_INDEX);
Expand All @@ -78,8 +79,15 @@ public static Optional<Version> extractImageVersion(final String fullImagePath)
}

private static String extractPartFromFullPath(final String fullImagePath, final int partIndex) {
final String[] parts = fullImagePath.split(VERSION_DELIMITER);
return parts.length > partIndex ? parts[partIndex] : null;
final int delimeterIndex = fullImagePath.lastIndexOf(VERSION_DELIMITER);
if (partIndex == NAME_PARTS_INDEX) {
return delimeterIndex >= 0 ? fullImagePath.substring(0, delimeterIndex) : fullImagePath;
} else if (partIndex == VERSION_PART_INDEX) {
return delimeterIndex >= 0 ? fullImagePath.substring(delimeterIndex + 1) : null;
} else {
LOGGER.warn("Invalid part index: {}", partIndex);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,38 @@ Map<String, String> getEnv() {
/**
* Extracts the image name from the provided image string, if the image string uses the following
* format: {@code <image name>:<image version>}.
* Image name may include registry and port number, which is also delimited by ":"
*
* @param image The image.
* @return The name extracted from the image, or the originally provided string if blank.
*/
public static String getImageName(final String image) {
if (StringUtils.isNotEmpty(image)) {
return image.split(IMAGE_DELIMITER)[0];
final int delimeterIndex = image.lastIndexOf(IMAGE_DELIMITER);
if (delimeterIndex >= 0) {
return image.substring(0, delimeterIndex);
}
}

// If image is null, empty, or does not contain a delimiter, return the original string.
return image;
}

/**
* Extracts the image version from the provided image string, if the image string uses the following
* format: {@code <image name>:<image version>}.
* Image name may include registry and port number, which is also delimited by ":"
*
* @param image The image.
* @return The version extracted from the image, or the originally provided string if blank.
*/
public static String getImageVersion(final String image) {
if (StringUtils.isNotEmpty(image)) {
return image.split(IMAGE_DELIMITER)[1];
final int delimeterIndex = image.lastIndexOf(IMAGE_DELIMITER);
if (delimeterIndex >= 0 && image.length() > delimeterIndex + 1) {
return image.substring(delimeterIndex + 1);
}
}

// If image is null, empty, or does not contain a delimiter, return the original string.
return image;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ static String getShortImageName(final String fullImagePath) {
/**
* Docker image names are by convention separated by slashes. The last portion is the image's name.
* This is followed by a colon and a version number. e.g. airbyte/scheduler:v1 or
* gcr.io/my-project/image-name:v2.
* gcr.io/my-project/image-name:v2. Registry name may also include port number,
* e.g. registry.internal:1234/my-project/image-name:v2
*
* Get the image version by returning the substring following a colon.
*/
static String getImageVersion(final String fullImagePath) {
if (fullImagePath == null || fullImagePath.length() < 1) {
return null;
}
int colonIndex = fullImagePath.indexOf(":");
// Use the last colon to find the image tag
int colonIndex = fullImagePath.lastIndexOf(":");
if (colonIndex != -1 && fullImagePath.length() > colonIndex + 1) {
return fullImagePath.substring(colonIndex + 1);
}
Expand Down
Loading