Skip to content

Commit

Permalink
Refactor findTagSha method for clarity
Browse files Browse the repository at this point in the history
Improve readability and maintainability of findTagSha in ImageStreamService

- Flattened nested loop structures for better readability.
- Extracted complex logic into private helper methods.
- Replaced continue statements with conditional logic.
- Enhanced error handling for clearer exception messages.

This refactor is in line with our goal to simplify complex methods and
improve the overall code quality in the ImageStreamService class.

Resolves #2531
  • Loading branch information
724thomas committed Jan 11, 2024
1 parent 7956212 commit 857c9d6
Showing 1 changed file with 57 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,59 +172,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 < IMAGE_STREAM_TAG_RETRIES; i++) {
if (i > 0) {
log.info("Retrying to find tag on ImageStream %s", imageStreamName);
try {
Thread.sleep(IMAGE_STREAM_TAG_RETRY_TIMEOUT_IN_MILLIS);
} 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 handleNoImageFoundException(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(IMAGE_STREAM_TAG_RETRY_TIMEOUT_IN_MILLIS);
} 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 IllegalStateException handleNoImageFoundException(ImageStream currentImageStream, String imageStreamName) {
if (currentImageStream == null) {
throw new IllegalStateException("Could not find a current ImageStream with name " + imageStreamName + " in namespace " + namespace);
return new IllegalStateException("Could not find ImageStream with name " + imageStreamName + "in namespace " + namespace);
} else {
throw new IllegalStateException("Could not find a tag in the ImageStream " + imageStreamName);
return new IllegalStateException("Could not find a tag in the ImageStream " + imageStreamName);
}
}

Expand Down

0 comments on commit 857c9d6

Please sign in to comment.