Skip to content

Commit

Permalink
feat(provider/kubernetes): Update deployable's patch behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Wander committed Oct 17, 2017
1 parent 99b3497 commit 71a3fe6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,16 @@ private String getVersion(String type, String name, String location) {
.collect(Collectors.toList());

taken.sort(Integer::compareTo);

int attempt = 0;
for (Integer exists : taken) {
if (attempt == exists) {
attempt++;
} else {
break;
}
int sequence = 0;
if (!taken.isEmpty()) {
sequence = taken.get(taken.size() - 1) + 1;
}

// omit 1000 artifact restriction because it's silly (vNNN)
return String.format("v%d", attempt);
// Match vNNN pattern until impossible
if (sequence < 1000) {
return String.format("v%03d", sequence);
} else {
return String.format("v%d", sequence);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public Class<V1beta1Ingress> getDeployedClass() {

@Override
void deploy(KubernetesV2Credentials credentials, V1beta1Ingress resource) {
credentials.createIngress(resource);
String namespace = resource.getMetadata().getNamespace();
String name = resource.getMetadata().getName();
V1beta1Ingress current = credentials.readIngress(namespace, name);
if (current != null) {
credentials.patchIngress(current, resource);
} else {
credentials.createIngress(resource);
}
}

@Override
Expand All @@ -64,6 +71,6 @@ public Class<V1DeleteOptions> getDeleteOptionsClass() {

@Override
public void delete(KubernetesV2Credentials credentials, String namespace, String name, V1DeleteOptions deleteOptions) {
credentials.deleteDeployment(namespace, name, deleteOptions);
credentials.deleteIngress(namespace, name, deleteOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public Class<V1DeleteOptions> getDeleteOptionsClass() {

@Override
void deploy(KubernetesV2Credentials credentials, V1beta1ReplicaSet resource) {
credentials.createReplicaSet(resource);
String namespace = resource.getMetadata().getNamespace();
String name = resource.getMetadata().getName();
V1beta1ReplicaSet current = credentials.readReplicaSet(namespace, name);
if (current != null) {
credentials.patchReplicaSet(current, resource);
} else {
credentials.createReplicaSet(resource);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ public Class<V1Service> getDeployedClass() {

@Override
void deploy(KubernetesV2Credentials credentials, V1Service resource) {
credentials.createService(resource);
String namespace = resource.getMetadata().getNamespace();
String name = resource.getMetadata().getName();
V1Service current = credentials.readService(namespace, name);
if (current != null) {
credentials.patchService(current, resource);
} else {
credentials.createService(resource);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,43 @@ public V1Status deleteIngress(String namespace, String name, V1DeleteOptions del
});
}

public void patchIngress(String namespace, String name, V1beta1Ingress desired) {
V1beta1Ingress current = readIngress(namespace, name);
patchIngress(current, desired);
}

public void patchIngress(V1beta1Ingress current, V1beta1Ingress desired) {
final String methodName = "ingresses.patch";
final String namespace = current.getMetadata().getNamespace();
final String name = current.getMetadata().getName();
final Map[] jsonPatch = determineJsonPatch(current, desired);
runAndRecordMetrics(methodName, namespace, () -> {
try {
return extensionsV1beta1Api.patchNamespacedIngress(name, namespace, jsonPatch, null);
} catch (ApiException e) {
throw new KubernetesApiException(methodName, e);
}
});
}

public V1beta1Ingress readIngress(String namespace, String name) {
final String methodName = "ingresses.read";
final KubernetesApiVersion apiVersion = KubernetesApiVersion.APPS_V1BETA1;
final KubernetesKind kind = KubernetesKind.DEPLOYMENT;
return runAndRecordMetrics(methodName, namespace, () -> {
try {
V1beta1Ingress result = extensionsV1beta1Api.readNamespacedIngress(name, namespace, PRETTY, EXACT, EXPORT);
return annotateMissingFields(result, V1beta1Ingress.class, apiVersion, kind);
} catch (ApiException e) {
if (notFound(e)) {
return null;
}

throw new KubernetesApiException(methodName, e);
}
});
}

public void createNetworkPolicy(V1beta1NetworkPolicy networkPolicy) {
final String methodName = "networkPolicies.create";
final String namespace = networkPolicy.getMetadata().getNamespace();
Expand Down Expand Up @@ -507,6 +544,43 @@ public V1Status deleteService(String namespace, String name) {
});
}

public void patchService(String namespace, String name, V1Service desired) {
V1Service current = readService(namespace, name);
patchService(current, desired);
}

public void patchService(V1Service current, V1Service desired) {
final String methodName = "services.patch";
final String namespace = current.getMetadata().getNamespace();
final String name = current.getMetadata().getName();
final Map[] jsonPatch = determineJsonPatch(current, desired);
runAndRecordMetrics(methodName, namespace, () -> {
try {
return coreV1Api.patchNamespacedService(name, namespace, jsonPatch, null);
} catch (ApiException e) {
throw new KubernetesApiException(methodName, e);
}
});
}

public V1Service readService(String namespace, String name) {
final String methodName = "services.read";
final KubernetesApiVersion apiVersion = KubernetesApiVersion.EXTENSIONS_V1BETA1;
final KubernetesKind kind = KubernetesKind.REPLICA_SET;
return runAndRecordMetrics(methodName, namespace, () -> {
try {
V1Service result = coreV1Api.readNamespacedService(name, namespace, PRETTY, EXACT, EXPORT);
return annotateMissingFields(result, V1Service.class, apiVersion, kind);
} catch (ApiException e) {
if (notFound(e)) {
return null;
}

throw new KubernetesApiException(methodName, e);
}
});
}

private <T> T runAndRecordMetrics(String methodName, String namespace, Supplier<T> op) {
T result = null;
Throwable failure = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ class KubernetesVersionedArtifactConverterSpec extends Specification {

where:
versions | expected
[0, 1, 2] | "v3"
[0] | "v1"
[] | "v0"
[1] | "v0"
[1, 2, 3] | "v0"
[0, 2, 3] | "v1"
[2, 0, 1] | "v3"
[0, 1, 3] | "v2"
[1, 0, 3] | "v2"
[0, 1, 2] | "v003"
[0] | "v001"
[] | "v000"
[1] | "v002"
[1, 2, 3] | "v004"
[0, 2, 3] | "v004"
[2, 0, 1] | "v003"
[0, 1, 3] | "v004"
[1, 0, 3] | "v004"
[1000] | "v1001"
}
}

0 comments on commit 71a3fe6

Please sign in to comment.