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

refactor: findTagSha method in ImageStreamService for improved readability #2547

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,59 +180,81 @@ private TagReference extractTag(ImageStream is) {
return tag;
}

// Method to find the SHA of the latest tag in an ImageStream
private String findTagSha(OpenShiftClient client, String imageStreamName, String namespace) {
ImageStream currentImageStream = null;

for (int i = 0; i < imageStreamTagRetries; i++) {
if (i > 0) {
log.info("Retrying to find tag on ImageStream %s", imageStreamName);
try {
Thread.sleep(imageStreamTagRetryTimeoutInMillis);
} catch (InterruptedException e) {
log.debug("interrupted", e);
Thread.currentThread().interrupt();
}
}
currentImageStream = client.imageStreams().inNamespace(namespace).withName(imageStreamName).get();
if (currentImageStream == null) {
continue;
}
ImageStreamStatus status = currentImageStream.getStatus();
if (status == null) {
continue;
}
List<NamedTagEventList> tags = status.getTags();
if (tags == null || tags.isEmpty()) {
continue;
sleepThread();
}

// Iterate all imagestream tags and get the latest one by 'created' attribute
TagEvent latestTag = null;
currentImageStream = client.imageStreams().inNamespace(namespace).withName(imageStreamName).get();
if (currentImageStream != null) {
TagEvent latestTag = findLatestTag(currentImageStream);

TAG_EVENT_LIST:
for (NamedTagEventList list : tags) {
List<TagEvent> items = list.getItems();
if (items == null || items.isEmpty()) {
continue TAG_EVENT_LIST;
if (latestTag != null && StringUtils.isNotBlank(latestTag.getImage())) {
String image = latestTag.getImage();
log.info("Found tag on ImageStream " + imageStreamName + " tag: " + image);
return image;
}
}
}
// Handle the case when no image is found after all retries:
throw new IllegalStateException(generateImageStreamErrorMessage(currentImageStream, imageStreamName));
}

for (TagEvent tag : items) {
latestTag = latestTag == null ? tag : newerTag(tag, latestTag);
}
// Method to sleep the current thread for a given amount of time
private void sleepThread() {
try {
Thread.sleep(imageStreamTagRetryTimeoutInMillis);
} catch (InterruptedException e) {
log.debug("interrupted", e);
Thread.currentThread().interrupt();
}
}

// Method to find the latest TagEvent from an ImageStream
private TagEvent findLatestTag(ImageStream currentImageStream) {
ImageStreamStatus status = currentImageStream.getStatus();
if (status != null) {
List<NamedTagEventList> tags = status.getTags();
if (tags != null && !tags.isEmpty()) {
return getLatestTagFromEventLists(tags);
}
}
return null;
}

if (latestTag != null && StringUtils.isNotBlank(latestTag.getImage())) {
String image = latestTag.getImage();
log.info("Found tag on ImageStream " + imageStreamName + " tag: " + image);
return image;
// Method to iterate over NamedTagEventList and find the latest TagEvent
private TagEvent getLatestTagFromEventLists(List<NamedTagEventList> tags) {
TagEvent latestTag = null;
for (NamedTagEventList list : tags) {
latestTag = updateLatestTagFromList(latestTag, list);
}
return latestTag;
}

// Method to find the latest TagEvent from a NamedTagEventList
private TagEvent updateLatestTagFromList(TagEvent latestTag, NamedTagEventList list) {
List<TagEvent> items = list.getItems();
if (items != null && !items.isEmpty()) {
for (TagEvent tag : items) {
// Update the latest tag
latestTag = latestTag == null ? tag : newerTag(tag, latestTag);
}
}
//
return latestTag;
}

// No image found, even after several retries:
// Method to handle cases where no image is found
private String generateImageStreamErrorMessage(ImageStream currentImageStream, String imageStreamName) {
if (currentImageStream == null) {
throw new IllegalStateException("Could not find a current ImageStream with name " + imageStreamName + " in namespace " + namespace);
return "Could not find a current ImageStream with name " + imageStreamName + " in namespace " + namespace;
} else {
throw new IllegalStateException("Could not find a tag in the ImageStream " + imageStreamName);
return "Could not find a tag in the ImageStream " + imageStreamName;
}
}

Expand Down