Skip to content

Commit

Permalink
fix(kubernetes): replace getCreatedTime in KubernetesResource interfa…
Browse files Browse the repository at this point in the history
…ce (#4791)

We [removed](#4769) getCreatedTime from the shared interface extended by Kubernetes resource types and pushed it down to KubernetesServerGroup only, but it turns out that Deck expects all resources to have this field.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
maggieneterval and mergify[bot] committed Aug 11, 2020
1 parent 07d719d commit 68748e1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface KubernetesResource {
KubernetesApiVersion getApiVersion();

String getNamespace();

Long getCreatedTime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class KubernetesV2Instance implements Instance, KubernetesResource
private final KubernetesKind kind;
private final Map<String, String> labels;
private final Moniker moniker;
private final Long createdTime;

@Null
@Override
Expand All @@ -73,6 +74,7 @@ private KubernetesV2Instance(KubernetesManifest manifest, String key, Moniker mo
this.kind = manifest.getKind();
this.labels = ImmutableMap.copyOf(manifest.getLabels());
this.moniker = moniker;
this.createdTime = manifest.getFormattedCreationTimestamp();

this.health = new ArrayList<>();
V1PodStatus status =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class KubernetesV2LoadBalancer
private final KubernetesKind kind;
private final Map<String, String> labels;
private final Moniker moniker;
private final Long createdTime;

private KubernetesV2LoadBalancer(
KubernetesManifest manifest,
Expand All @@ -64,6 +65,7 @@ private KubernetesV2LoadBalancer(
this.labels = ImmutableMap.copyOf(manifest.getLabels());
this.moniker = moniker;
this.serverGroups = serverGroups;
this.createdTime = manifest.getFormattedCreationTimestamp();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public final class KubernetesV2SecurityGroup implements KubernetesResource, Secu
private final KubernetesKind kind;
private final Map<String, String> labels;
private final Moniker moniker;
private final Long createdTime;

private final Set<Rule> inboundRules;
private final Set<Rule> outboundRules;
Expand Down Expand Up @@ -104,6 +105,7 @@ private KubernetesV2SecurityGroup(
this.namespace = manifest.getNamespace();
this.labels = ImmutableMap.copyOf(manifest.getLabels());
this.moniker = moniker;
this.createdTime = manifest.getFormattedCreationTimestamp();

this.inboundRules = inboundRules;
this.outboundRules = outboundRules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.common.collect.ImmutableList.toImmutableList;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand All @@ -43,8 +42,6 @@
import com.netflix.spinnaker.clouddriver.model.ServerGroupManager.ServerGroupManagerSummary;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.moniker.Moniker;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -135,7 +132,7 @@ private KubernetesV2ServerGroup(
this.displayName = manifest.getName();
this.labels = ImmutableMap.copyOf(manifest.getLabels());
this.moniker = moniker;
this.createdTime = getCreatedTime(manifest);
this.createdTime = manifest.getFormattedCreationTimestamp();
this.buildInfo =
ImmutableMap.of(
"images",
Expand Down Expand Up @@ -269,21 +266,6 @@ public ImagesSummary getImagesSummary() {
.build());
}

private static Long getCreatedTime(KubernetesManifest manifest) {
Map<String, String> metadata =
(Map<String, String>) manifest.getOrDefault("metadata", new HashMap<>());
String timestamp = metadata.get("creationTimestamp");
try {
if (!Strings.isNullOrEmpty(timestamp)) {
return (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(timestamp)).getTime();
}
} catch (ParseException e) {
log.warn("Failed to parse timestamp: ", e);
}

return null;
}

@Override
public String getType() {
return KubernetesCloudProvider.ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class KubernetesV2ServerGroupManager
private final KubernetesKind kind;
private final Map<String, String> labels;
private final Moniker moniker;
private final Long createdTime;

private KubernetesV2ServerGroupManager(
KubernetesManifest manifest,
Expand All @@ -64,6 +65,7 @@ private KubernetesV2ServerGroupManager(
this.labels = ImmutableMap.copyOf(manifest.getLabels());
this.moniker = moniker;
this.serverGroups = serverGroups;
this.createdTime = manifest.getFormattedCreationTimestamp();
}

public static KubernetesV2ServerGroupManager fromCacheData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

@Slf4j
public class KubernetesManifest extends HashMap<String, Object> {
private static final ObjectMapper mapper = new ObjectMapper();

Expand Down Expand Up @@ -151,6 +155,21 @@ public String getCreationTimestamp() {
: "";
}

@JsonIgnore
@Nullable
public Long getFormattedCreationTimestamp() {
String timestamp = getCreationTimestamp();
try {
if (!Strings.isNullOrEmpty(timestamp)) {
return (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(timestamp)).getTime();
}
} catch (ParseException e) {
log.warn("Failed to parse timestamp: ", e);
}

return null;
}

@JsonIgnore
@Nonnull
public List<OwnerReference> getOwnerReferences() {
Expand Down

0 comments on commit 68748e1

Please sign in to comment.