Skip to content

Commit

Permalink
fix: KubernetesHelper is compatible with MockWebServer timestamps
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jan 12, 2024
1 parent 14ce5a4 commit 911fd4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -94,7 +93,7 @@


public class KubernetesHelper {
protected static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
protected static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.n]X");
private static final String FILENAME_PATTERN_REGEX = "^(?<name>.*?)(-(?<type>[^-]+))?\\.(?<ext>yaml|yml|json)$";
private static final String PROFILES_PATTERN_REGEX = "^profiles?\\.ya?ml$";
public static final Pattern FILENAME_PATTERN = Pattern.compile(FILENAME_PATTERN_REGEX, Pattern.CASE_INSENSITIVE);
Expand Down Expand Up @@ -544,32 +543,28 @@ private static LabelSelector toLabelSelector(Map<String, String> matchLabels) {
}

public static boolean isNewerResource(HasMetadata newer, HasMetadata older) {
Date t1 = getCreationTimestamp(newer);
Date t2 = getCreationTimestamp(older);
Instant t1 = getCreationTimestamp(newer);
Instant t2 = getCreationTimestamp(older);
return t1 != null && (t2 == null || t1.compareTo(t2) > 0);
}

public static Date getCreationTimestamp(HasMetadata hasMetadata) {
public static Instant getCreationTimestamp(HasMetadata hasMetadata) {
ObjectMeta metadata = hasMetadata.getMetadata();
if (metadata != null) {
return parseTimestamp(metadata.getCreationTimestamp());
}
return null;
}

private static Date parseTimestamp(String text) {
private static Instant parseTimestamp(String text) {
if (StringUtils.isBlank(text)) {
return null;
}
return parseDate(text);
}

public static Date parseDate(String text) {
try {
return new SimpleDateFormat(DATE_TIME_FORMAT).parse(text);
} catch (ParseException e) {
return null;
}
public static Instant parseDate(String text) {
return Instant.from(DATE_TIME_FORMATTER.parse(text));
}

public static Pod getNewestPod(Collection<Pod> pods) {
Expand All @@ -578,8 +573,8 @@ public static Pod getNewestPod(Collection<Pod> pods) {
}
List<Pod> sortedPods = new ArrayList<>(pods);
sortedPods.sort((p1, p2) -> {
Date t1 = getCreationTimestamp(p1);
Date t2 = getCreationTimestamp(p2);
Instant t1 = getCreationTimestamp(p1);
Instant t2 = getCreationTimestamp(p2);
if (t1 != null) {
if (t2 == null) {
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReview;
Expand Down Expand Up @@ -482,6 +486,24 @@ static Stream<Arguments> getKindTestCases() {
);
}

@ParameterizedTest(name = "{index}: {0} returns {1}")
@MethodSource("getCreationTimestampTestCases")
void getCreationTimestamp(String timestamp, LocalDateTime expectedDate) {
final ConfigMap cm = new ConfigMapBuilder().withNewMetadata().withCreationTimestamp(timestamp).endMetadata()
.build();
assertThat(KubernetesHelper.getCreationTimestamp(cm))
.isEqualTo(expectedDate.atZone(ZoneOffset.UTC).toInstant());
}

static Stream<Arguments> getCreationTimestampTestCases() {
return Stream.of(
Arguments.of("1955-11-12T06:38:00Z", LocalDateTime.of(1955, 11, 12, 6, 38)),
Arguments.of("1955-11-12T06:38:00+01", LocalDateTime.of(1955, 11, 12, 5, 38)),
Arguments.of("1955-11-12T06:38:00.123456789Z", LocalDateTime.of(1955, 11, 12, 6, 38, 0, 123456789)),
Arguments.of("1955-11-12T06:38:00.625Z", LocalDateTime.of(1955, 11, 12, 6, 38, 0, 625))
);
}

private void assertLocalFragments(File[] fragments, int expectedSize) {
assertThat(fragments).hasSize(expectedSize);
assertThat(Arrays.stream(fragments).anyMatch( f -> f.getName().equals("deployment.yml"))).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.util.Collection;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -145,8 +146,8 @@ private void waitAndLogPods(final NamespacedKubernetesClient kc, LabelSelector s
if (KubernetesHelper.isPodRunning(pod) || KubernetesHelper.isPodWaiting(pod)) {
if (latestPod == null || KubernetesHelper.isNewerResource(pod, latestPod)) {
if (ignorePodsOlderThan != null) {
Date podCreateTime = KubernetesHelper.getCreationTimestamp(pod);
if (podCreateTime != null && podCreateTime.compareTo(ignorePodsOlderThan) > 0) {
Instant podCreateTime = KubernetesHelper.getCreationTimestamp(pod);
if (podCreateTime != null && Date.from(podCreateTime).compareTo(ignorePodsOlderThan) > 0) {
latestPod = pod;
}
} else {
Expand Down

0 comments on commit 911fd4f

Please sign in to comment.