From 324fac4e182fed8414dd9a0e6138c8406d5c4129 Mon Sep 17 00:00:00 2001 From: shawkins Date: Tue, 8 Jun 2021 13:55:18 -0400 Subject: [PATCH] switching logic to use generickubernetes resource, rather than jsonnode --- .../api/model/GenericKubernetesResource.java | 2 +- .../model/GenericKubernetesResourceTest.java | 22 +++ .../mock/KubernetesAttributesExtractor.java | 7 +- .../server/mock/KubernetesCrudDispatcher.java | 130 +++++++++--------- .../KubernetesAttributesExtractorTest.java | 36 ++--- ...KubernetesCrudAttributesExtractorTest.java | 32 ++--- .../kubernetes/client/mock/PodCrudTest.java | 3 +- 7 files changed, 127 insertions(+), 105 deletions(-) diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java index 187b534a414..dba0bb4f540 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java @@ -51,7 +51,7 @@ public class GenericKubernetesResource implements HasMetadata { @JsonProperty("metadata") private ObjectMeta metadata; @JsonIgnore - private transient Map additionalProperties = new LinkedHashMap<>(); + private Map additionalProperties = new LinkedHashMap<>(); @JsonAnyGetter public Map getAdditionalProperties() { diff --git a/kubernetes-model-generator/kubernetes-model-core/src/test/java/io/fabric8/kubernetes/api/model/GenericKubernetesResourceTest.java b/kubernetes-model-generator/kubernetes-model-core/src/test/java/io/fabric8/kubernetes/api/model/GenericKubernetesResourceTest.java index 1e8e193bc03..610ac486462 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/test/java/io/fabric8/kubernetes/api/model/GenericKubernetesResourceTest.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/test/java/io/fabric8/kubernetes/api/model/GenericKubernetesResourceTest.java @@ -94,6 +94,28 @@ void serializeWithCustomResourceShouldSerialize() { .hasFieldOrPropertyWithValue("spec.field", "value"); } + @Test + @DisplayName("equality should look at all fields") + void equality() { + // Given + final GenericKubernetesResource gkr = new GenericKubernetesResource(); + gkr.setApiVersion("the-cr.example.com/v1"); + gkr.setKind("SomeCustomResource"); + gkr.setMetadata(new ObjectMetaBuilder().withName("custom-resource-example").build()); + gkr.setAdditionalProperties(Collections.singletonMap("spec", Collections.singletonMap("field", "value"))); + + // clone + final GenericKubernetesResource gkr1 = objectMapper.convertValue(gkr, GenericKubernetesResource.class); + // Then + assertThat(gkr) + .isEqualTo(gkr1); + + gkr1.getAdditionalProperties().put("key", "value"); + + assertThat(gkr) + .isNotEqualTo(gkr1); + } + private static InputStream load(String resource) { return GenericKubernetesResource.class.getResourceAsStream("/generic-kubernetes-resource/" + resource); } diff --git a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java index d7c341c2356..0a053185ab9 100644 --- a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java +++ b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractor.java @@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -291,10 +292,10 @@ private static Attribute parseLabel(String label) { } static GenericKubernetesResource toKubernetesResource(String s) { - try (InputStream stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8.name()))) { + try (InputStream stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8))) { return Serialization.unmarshal(stream, GenericKubernetesResource.class); - } catch (Exception e) { - return null; + } catch (IOException e) { + throw new RuntimeException(e); // unexpected } } diff --git a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java index 874bc723b32..b451caa0c3c 100644 --- a/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java +++ b/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudDispatcher.java @@ -18,14 +18,17 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; - +import io.fabric8.kubernetes.api.model.GenericKubernetesResource; import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.Status; import io.fabric8.kubernetes.api.model.StatusBuilder; import io.fabric8.kubernetes.api.model.StatusCause; import io.fabric8.kubernetes.api.model.StatusCauseBuilder; +import io.fabric8.kubernetes.client.KubernetesClientException; import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; import io.fabric8.kubernetes.client.dsl.base.OperationSupport; +import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; import io.fabric8.kubernetes.client.utils.Serialization; import io.fabric8.kubernetes.client.utils.Utils; import io.fabric8.mockwebserver.Context; @@ -33,7 +36,6 @@ import io.fabric8.mockwebserver.crud.AttributeSet; import io.fabric8.mockwebserver.crud.CrudDispatcher; import io.fabric8.mockwebserver.crud.ResponseComposer; -import io.fabric8.zjsonpatch.JsonDiff; import io.fabric8.zjsonpatch.JsonPatch; import okhttp3.MediaType; import okhttp3.mockwebserver.MockResponse; @@ -53,7 +55,7 @@ import java.util.UUID; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Supplier; +import java.util.function.Function; import java.util.stream.Collectors; import okhttp3.mockwebserver.RecordedRequest; @@ -124,7 +126,7 @@ public synchronized MockResponse dispatch(RecordedRequest request) { */ @Override public MockResponse handleCreate(String path, String s) { - return validateRequestBodyAndHandleRequest(s, () -> doCreateOrModify(path, s, ADDED)); + return validateRequestBodyAndHandleRequest(s, (g) -> doCreateOrModify(path, g, ADDED)); } /** @@ -134,7 +136,7 @@ public MockResponse handleCreate(String path, String s) { * @return The {@link MockResponse} */ public MockResponse handleReplace(String path, String s) { - return validateRequestBodyAndHandleRequest(s, () -> doCreateOrModify(path, s, MODIFIED)); + return validateRequestBodyAndHandleRequest(s, (g) -> doCreateOrModify(path, g, MODIFIED)); } /** @@ -352,7 +354,6 @@ private String fetchResource(String path) { } } - private int doDelete(String path, String event) { List items = findItems(attributeExtractor.fromPath(path)); @@ -377,72 +378,68 @@ private List findItems(AttributeSet query) { .collect(Collectors.toList()); } - private MockResponse doCreateOrModify(String path, String initial, String event) { + private MockResponse doCreateOrModify(String path, GenericKubernetesResource value, String event) { MockResponse mockResponse = new MockResponse(); // workaround for mockserver https://github.com/fabric8io/mockwebserver/pull/59 Map pathValues = kubernetesAttributesExtractor.fromKubernetesPath(path); AttributeSet attributes = attributeExtractor.fromPath(path); try { - JsonNode source = context.getMapper().readTree(initial); int responseCode = HttpURLConnection.HTTP_OK; - if (ADDED.equals(event)) { - HasMetadata h = toKubernetesResource(initial); - if (h != null && h.getMetadata() != null && h.getMetadata().getName() != null) { - attributes = AttributeSet.merge(attributes, new AttributeSet(new Attribute(KubernetesAttributesExtractor.NAME, h.getMetadata().getName()))); - } + if (ADDED.equals(event) && value.getMetadata() != null && value.getMetadata().getName() != null) { + attributes = AttributeSet.merge(attributes, new AttributeSet(new Attribute(KubernetesAttributesExtractor.NAME, value.getMetadata().getName()))); } boolean statusSubresource = crdProcessor.isStatusSubresource(pathValues.get(KubernetesAttributesExtractor.KIND)); + GenericKubernetesResource updated = Serialization.clone(value); + List items = findItems(attributes); if (items.isEmpty()) { if (MODIFIED.equals(event)) { responseCode = HttpURLConnection.HTTP_NOT_FOUND; } else { if (statusSubresource) { - removeStatus(source); + updated.getAdditionalProperties().remove(STATUS); } - setDefaultMetadata(source, pathValues, null); + setDefaultMetadata(updated, pathValues, null); } } else if (ADDED.equals(event)) { responseCode = HttpURLConnection.HTTP_CONFLICT; } else if (MODIFIED.equals(event)) { String existing = map.remove(items.get(0)); - JsonNode existingNode = context.getMapper().readTree(existing); - JsonNode status = null; + GenericKubernetesResource existingResource = toKubernetesResource(existing); + Object status = null; if (isStatusPath(path)) { - status = removeStatus(source); + status = updated.getAdditionalProperties().remove(STATUS); // set the status on the existing node - source = existingNode; + updated = Serialization.clone(existingResource); } else { // preserve status and generated fields if (statusSubresource) { - status = removeStatus(existingNode); + status = existingResource.getAdditionalProperties().remove(STATUS); } - setDefaultMetadata(source, pathValues, existingNode.findValue("metadata")); + setDefaultMetadata(updated, pathValues, existingResource.getMetadata()); } if (statusSubresource || isStatusPath(path)) { if (status != null) { - ((ObjectNode) source).set(STATUS, status); + updated.getAdditionalProperties().put(STATUS, status); } else { - ((ObjectNode) source).remove(STATUS); + updated.getAdditionalProperties().remove(STATUS); } } - // re-read without modifications - existingNode = context.getMapper().readTree(existing); - if (JsonDiff.asJson(source, existingNode).isEmpty()) { + if (existingResource.equals(updated)) { event = null; // no change } } if (responseCode == HttpURLConnection.HTTP_OK) { - String s = context.getMapper().writeValueAsString(source); - AttributeSet features = AttributeSet.merge(attributes, attributeExtractor.fromResource(s)); + String s = context.getMapper().writeValueAsString(updated); + AttributeSet features = AttributeSet.merge(attributes, kubernetesAttributesExtractor.extract(updated)); map.put(features, s); // always add back as it was proactively removed if (event != null && !event.isEmpty()) { - crdProcessor.process(path, initial, false); + crdProcessor.process(path, s, false); final String response = s; final String finalEvent = event; watchEventListeners.stream() @@ -462,27 +459,31 @@ private static boolean isStatusPath(String path) { return path.endsWith("/" + STATUS); } - private void setDefaultMetadata(JsonNode source, Map pathValues, JsonNode exitingMetadata) { - ObjectNode metadata = (ObjectNode)source.findValue("metadata"); - UUID uuid = UUID.randomUUID(); - if (metadata.get("name") == null) { - metadata.put("name", metadata.get("generateName").asText() + "-" + uuid.toString()); - } - if (metadata.get("namespace") == null) { - metadata.put("namespace", pathValues.get(KubernetesAttributesExtractor.NAMESPACE)); - } - metadata.put("uid", getOrDefault(exitingMetadata, "uid", uuid.toString())); - // resourceVersion is not yet handled appropriately - metadata.put("resourceVersion", "1"); - metadata.put("generation", 1); - metadata.put("creationTimestamp", getOrDefault(exitingMetadata, "creationTimestamp", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT))); + private void setDefaultMetadata(GenericKubernetesResource source, Map pathValues, ObjectMeta exitingMetadata) { + ObjectMeta metadata = source.getMetadata(); + if (metadata == null) { + metadata = new ObjectMeta(); + source.setMetadata(metadata); + } + UUID uuid = UUID.randomUUID(); + if (metadata.getName() == null) { + metadata.setName(metadata.getGenerateName() + "-" + uuid.toString()); + } + if (metadata.getNamespace() == null) { + metadata.setNamespace(pathValues.get(KubernetesAttributesExtractor.NAMESPACE)); + } + metadata.setUid(getOrDefault(exitingMetadata, ObjectMeta::getUid, uuid.toString())); + // resourceVersion is not yet handled appropriately + metadata.setResourceVersion("1"); + metadata.setGeneration(1l); + metadata.setCreationTimestamp(getOrDefault(exitingMetadata, ObjectMeta::getCreationTimestamp, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT))); } - private String getOrDefault(JsonNode node, String name, String defaultValue) { - if (node != null) { - JsonNode field = node.get(name); - if (field != null) { - return field.asText(); + private String getOrDefault(ObjectMeta metadata, Function extractor, String defaultValue) { + if (metadata != null) { + String result = extractor.apply(metadata); + if (result != null) { + return result; } } return defaultValue; @@ -492,34 +493,33 @@ private JsonNode removeStatus(JsonNode source) { return ((ObjectNode)source).remove(STATUS); } - private MockResponse validateRequestBodyAndHandleRequest(String s, Supplier mockResponseSupplier) { - HasMetadata h = toKubernetesResource(s); - if (h != null) { - try { - validateResource(h); - } catch (IllegalArgumentException illegalArgumentException) { - return getUnprocessableEntityMockResponse(s, h, illegalArgumentException); - } + private MockResponse validateRequestBodyAndHandleRequest(String s, Function mockResponseFunction) { + GenericKubernetesResource g = null; + try { + g = toKubernetesResource(s); + validateResource(g); + return mockResponseFunction.apply(g); + } catch (IllegalArgumentException | KubernetesClientException e) { + return getUnprocessableEntityMockResponse(s, g, e); } - return mockResponseSupplier.get(); } - private MockResponse getUnprocessableEntityMockResponse(String s, HasMetadata h, IllegalArgumentException illegalArgumentException) { - String statusBody = getStatusBody(h, HTTP_UNPROCESSABLE_ENTITY, illegalArgumentException); + private MockResponse getUnprocessableEntityMockResponse(String s, HasMetadata h, Exception ex) { + String statusBody = getStatusBody(h, HTTP_UNPROCESSABLE_ENTITY, ex); if (statusBody == null) { statusBody = s; } return new MockResponse().setResponseCode(HTTP_UNPROCESSABLE_ENTITY).setBody(statusBody); } - private String getStatusBody(HasMetadata h, int code, IllegalArgumentException illegalArgumentException) { - String kind = Utils.getNonNullOrElse(h.getKind(), "Unknown"); + private String getStatusBody(HasMetadata h, int code, Exception ex) { + String kind = Utils.getNonNullOrElse(KubernetesResourceUtil.getKind(h), "Unknown"); Status status = new StatusBuilder().withStatus("Failure") .withReason("Invalid") .withMessage(kind + " is invalid") .withNewDetails() - .withKind(h.getKind()) - .withCauses(getFailureStatusCause(illegalArgumentException)) + .withKind(kind) + .withCauses(getFailureStatusCause(ex)) .endDetails() .withCode(code) .build(); @@ -530,9 +530,9 @@ private String getStatusBody(HasMetadata h, int code, IllegalArgumentException i } } - private StatusCause getFailureStatusCause(IllegalArgumentException illegalArgumentException) { + private StatusCause getFailureStatusCause(Exception ex) { return new StatusCauseBuilder() - .withMessage(illegalArgumentException.getMessage()) + .withMessage(ex.getMessage()) .withReason("ValueRequired") .build(); } diff --git a/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java b/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java index 5aaec33d946..98c87a5a404 100644 --- a/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java +++ b/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesAttributesExtractorTest.java @@ -40,7 +40,7 @@ public class KubernetesAttributesExtractorTest { @Test public void shouldHandleNamespacedPathWithResource() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/pods/mypod"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -52,7 +52,7 @@ public void shouldHandleNamespacedPathWithResource() { @Test public void shouldHandleNamespacedPath() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -63,7 +63,7 @@ public void shouldHandleNamespacedPath() { @Test public void shouldHandleNonNamespacedPath() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/nodes/mynode"); + AttributeSet attributes = extractor.fromPath("/api/v1/nodes/mynode"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "node")); @@ -74,7 +74,7 @@ public void shouldHandleNonNamespacedPath() { @Test public void shouldHandlePathWithParameters() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/pods?labelSelector=testKey%3DtestValue"); + AttributeSet attributes = extractor.fromPath("/api/v1/pods?labelSelector=testKey%3DtestValue"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -126,7 +126,7 @@ public void shouldHandleResourceWithLabel() { @Test public void shouldHandleKindWithoutVersion() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/pods"); + AttributeSet attributes = extractor.fromPath("/api/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -136,7 +136,7 @@ public void shouldHandleKindWithoutVersion() { @Test public void shouldHandleExtensions() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/apps/v1/deployments"); + AttributeSet attributes = extractor.fromPath("/apis/apps/v1/deployments"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "deployment")); @@ -146,7 +146,7 @@ public void shouldHandleExtensions() { @Test public void shouldHandleIngress() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress"); + AttributeSet attributes = extractor.fromPath("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "ingress")); @@ -158,7 +158,7 @@ public void shouldHandleIngress() { @Test public void shouldHandleEndpoints() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/endpoints"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/endpoints"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "endpoints")); @@ -169,7 +169,7 @@ public void shouldHandleEndpoints() { @Test public void shouldHandleIngresses() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses"); + AttributeSet attributes = extractor.fromPath("/apis/extensions/v1beta1/namespaces/myns/ingresses"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "ingress")); @@ -181,7 +181,7 @@ public void shouldHandleIngresses() { public void shouldHandleApiGroups() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor - .extract("/apis/autoscaling/v1/namespaces/myns/horizontalpodautoscalers/myhpa"); + .fromPath("/apis/autoscaling/v1/namespaces/myns/horizontalpodautoscalers/myhpa"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "horizontalpodautoscaler")); @@ -193,7 +193,7 @@ public void shouldHandleApiGroups() { @Test public void shouldHandleCrds() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/test.com/v1/namespaces/myns/crds/mycrd"); + AttributeSet attributes = extractor.fromPath("/apis/test.com/v1/namespaces/myns/crds/mycrd"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "crd")); @@ -209,7 +209,7 @@ void shouldHandleCrdSubresources() { String basePath = "/apis/test.com/v1/namespaces/myns/crds/mycrd/"; for (String subresource : subresources) { - AttributeSet attributes = extractor.extract(basePath + subresource); + AttributeSet attributes = extractor.fromPath(basePath + subresource); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "crd")); @@ -219,7 +219,7 @@ void shouldHandleCrdSubresources() { "extracted attributes match for " + subresource + " expectation: " + expected); } - AttributeSet attributes = extractor.extract(basePath + "somethingRandom"); + AttributeSet attributes = extractor.fromPath(basePath + "somethingRandom"); assertTrue(attributes.matches(new AttributeSet()), "should extract nothing from an unsupported crd subresource"); } @@ -227,7 +227,7 @@ void shouldHandleCrdSubresources() { @Test public void shouldHandleLabelSelectorsWithOneLabel() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); @@ -238,7 +238,7 @@ public void shouldHandleLabelSelectorsWithOneLabel() { public void shouldHandleLabelSelectorsWithDoubleEquals() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor - .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3D%3Dmyname"); + .fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3D%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); @@ -249,7 +249,7 @@ public void shouldHandleLabelSelectorsWithDoubleEquals() { public void shouldHandleLabelSelectorsWithTwoLabels() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor - .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname,age%3D37"); + .fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname,age%3D37"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); @@ -261,7 +261,7 @@ public void shouldHandleLabelSelectorsWithTwoLabels() { public void shouldHandleLabelSelectorsWithADomain() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor - .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=example.com/name%3Dmyname"); + .fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=example.com/name%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:example.com/name", "myname")); @@ -407,7 +407,7 @@ void testCustomResourceAttributesExtraction() { // When AttributeSet attributes = extractor - .extract("/apis/demo.fabric8.io/v1alpha1/namespaces/ns1/customdatabases"); + .fromPath("/apis/demo.fabric8.io/v1alpha1/namespaces/ns1/customdatabases"); // Then AttributeSet expected = new AttributeSet(); diff --git a/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudAttributesExtractorTest.java b/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudAttributesExtractorTest.java index 0ff6394e682..9650c493a1b 100644 --- a/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudAttributesExtractorTest.java +++ b/kubernetes-server-mock/src/test/java/io/fabric8/kubernetes/client/server/mock/KubernetesCrudAttributesExtractorTest.java @@ -53,7 +53,7 @@ public class KubernetesCrudAttributesExtractorTest { @Test public void shouldHandleNamespacedPathWithResource() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/pods/mypod"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -65,7 +65,7 @@ public void shouldHandleNamespacedPathWithResource() { @Test public void shouldHandleNamespacedPath() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -76,7 +76,7 @@ public void shouldHandleNamespacedPath() { @Test public void shouldHandleNonNamespacedPath() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/nodes/mynode"); + AttributeSet attributes = extractor.fromPath("/api/v1/nodes/mynode"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "node")); @@ -87,7 +87,7 @@ public void shouldHandleNonNamespacedPath() { @Test public void shouldHandlePathWithParameters() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/pods?labelSelector=testKey%3DtestValue"); + AttributeSet attributes = extractor.fromPath("/api/v1/pods?labelSelector=testKey%3DtestValue"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -130,7 +130,7 @@ public void shouldHandleResourceWithLabel() { @Test public void shouldHandleKindWithoutVersion() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/pods"); + AttributeSet attributes = extractor.fromPath("/api/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); @@ -140,7 +140,7 @@ public void shouldHandleKindWithoutVersion() { @Test public void shouldHandleExtensions() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/apps/v1/deployments"); + AttributeSet attributes = extractor.fromPath("/apis/apps/v1/deployments"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "deployment")); @@ -150,7 +150,7 @@ public void shouldHandleExtensions() { @Test public void shouldHandleIngress() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress"); + AttributeSet attributes = extractor.fromPath("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "ingress")); @@ -162,7 +162,7 @@ public void shouldHandleIngress() { @Test public void shouldHandleEndpoints() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/endpoints"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/endpoints"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "endpoints")); @@ -173,7 +173,7 @@ public void shouldHandleEndpoints() { @Test public void shouldHandleIngresses() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses"); + AttributeSet attributes = extractor.fromPath("/apis/extensions/v1beta1/namespaces/myns/ingresses"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "ingress")); @@ -185,7 +185,7 @@ public void shouldHandleIngresses() { public void shouldHandleApiGroups() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor - .extract("/apis/autoscaling/v1/namespaces/myns/horizontalpodautoscalers/myhpa"); + .fromPath("/apis/autoscaling/v1/namespaces/myns/horizontalpodautoscalers/myhpa"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "horizontalpodautoscaler")); @@ -197,7 +197,7 @@ public void shouldHandleApiGroups() { @Test public void shouldHandleCrds() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/apis/test.com/v1/namespaces/myns/crds/mycrd"); + AttributeSet attributes = extractor.fromPath("/apis/test.com/v1/namespaces/myns/crds/mycrd"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "crd")); @@ -209,7 +209,7 @@ public void shouldHandleCrds() { @Test public void shouldHandleLabelSelectorsWithOneLabel() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); - AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname"); + AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); @@ -220,7 +220,7 @@ public void shouldHandleLabelSelectorsWithOneLabel() { public void shouldHandleLabelSelectorsWithDoubleEquals() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor - .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3D%3Dmyname"); + .fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3D%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); @@ -231,7 +231,7 @@ public void shouldHandleLabelSelectorsWithDoubleEquals() { public void shouldHandleLabelSelectorsWithTwoLabels() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor - .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname,age%3D37"); + .fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname,age%3D37"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:name", "myname")); @@ -243,7 +243,7 @@ public void shouldHandleLabelSelectorsWithTwoLabels() { public void shouldHandleLabelSelectorsWithADomain() { KubernetesCrudAttributesExtractor extractor = new KubernetesCrudAttributesExtractor(); AttributeSet attributes = extractor - .extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=example.com/name%3Dmyname"); + .fromPath("/api/v1/namespaces/myns/pods/mypod?labelSelector=example.com/name%3Dmyname"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("labels:example.com/name", "myname")); @@ -317,7 +317,7 @@ void nonSubresourceStatusHandling() { assertNotNull(result.getStatus()); // should be a no-op - assertNotNull(kubernetesClient.pods().updateStatus(pod).getStatus()); + assertNotNull(kubernetesClient.pods().replaceStatus(pod).getStatus()); labels.put("other", "label"); pod.getStatus().setHostIP("y"); diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java index ffe1c322802..3efc336e566 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodCrudTest.java @@ -30,7 +30,6 @@ import org.junit.jupiter.api.Test; import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -164,7 +163,7 @@ void testPodWatchOnLabels() throws InterruptedException { client.pods().inNamespace("ns1").create(pod1); Watch watch = client.pods().inNamespace("ns1") - .withLabels(new HashMap() {{ put("test", "watch");}}) + .withLabels(Collections.singletonMap("test", "watch")) .watch(lw); Map m = pod1.getMetadata().getLabels();