Skip to content

Commit

Permalink
refactor(provider/kubernetes): v2 include api version in resource id (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander authored Sep 16, 2017
1 parent 537efa7 commit 4807a59
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -130,19 +130,20 @@ public Optional<Map<String, String>> getSpecTemplateAnnotations() {

@JsonIgnore
public String getFullResourceName() {
return getKind() + "/" + getName();
return String.join("|", getApiVersion().toString(), getKind().toString(), getName());
}

public static Pair<KubernetesKind, String> fromFullResourceName(String fullResourceName) {
String[] split = fullResourceName.split("/");
if (split.length != 2) {
throw new IllegalArgumentException("Expected a full resource name of the form <kind>/<name>");
public static Triple<KubernetesApiVersion, KubernetesKind, String> fromFullResourceName(String fullResourceName) {
String[] split = fullResourceName.split("\\|");
if (split.length != 3) {
throw new IllegalArgumentException("Expected a full resource name of the form <version>|<kind>|<name>");
}

KubernetesKind kind = KubernetesKind.fromString(split[0]);
String name = split[1];
KubernetesApiVersion apiVersion = KubernetesApiVersion.fromString(split[0]);
KubernetesKind kind = KubernetesKind.fromString(split[1]);
String name = split[2];

return new ImmutablePair<>(kind, name);
return new ImmutableTriple<>(apiVersion, kind, name);
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ spec:
@Unroll
void "correctly parses a fully qualified resource name #kind/#name"() {
expect:
def pair = KubernetesManifest.fromFullResourceName(fullResourceName)
pair.getLeft() == kind
pair.getRight() == name
def triple = KubernetesManifest.fromFullResourceName(fullResourceName)
triple.getRight() == name
triple.getLeft() == apiVersion
triple.getMiddle() == kind

where:
fullResourceName || kind | name
"replicaSet/abc" || KubernetesKind.REPLICA_SET | "abc"
"service/abc" || KubernetesKind.SERVICE | "abc"
"SERVICE/abc" || KubernetesKind.SERVICE | "abc"
"ingress/abc" || KubernetesKind.INGRESS | "abc"
fullResourceName || apiVersion | kind | name
"extensions/v1beta1|replicaSet|abc" || KubernetesApiVersion.EXTENSIONS_V1BETA1 | KubernetesKind.REPLICA_SET | "abc"
"v1|service|abc" || KubernetesApiVersion.V1 | KubernetesKind.SERVICE | "abc"
"V1|SERVICE|abc" || KubernetesApiVersion.V1 | KubernetesKind.SERVICE | "abc"
"apps/v1beta1|ingress|abc" || KubernetesApiVersion.APPS_V1BETA1 | KubernetesKind.INGRESS | "abc"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ metadata:

then:
1 * credentialsMock.createReplicaSet(_) >> null
result.serverGroupNames == ["$NAMESPACE:$KIND/$NAME"]
result.serverGroupNames == ["$NAMESPACE:$API_VERSION|$KIND|$NAME"]
}

void "replica set deployer uses backup namespace"() {
Expand All @@ -111,6 +111,6 @@ metadata:

then:
1 * credentialsMock.createReplicaSet(_) >> null
result.serverGroupNames == ["$BACKUP_NAMESPACE:$KIND/$NAME"]
result.serverGroupNames == ["$BACKUP_NAMESPACE:$API_VERSION|$KIND|$NAME"]
}
}

0 comments on commit 4807a59

Please sign in to comment.