diff --git a/CHANGELOG.md b/CHANGELOG.md index fb2b2a6590d..7ef432e5d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * Fix #1156 : Watcher does not have correct authentication information in Openshift environment. + * Fix #1125 : ConfigMap labels are ignored when using mock KubernetesServer + * Fix #1144 : Get Request with OpenShift Mock Server Not Working * Fix #1147: Cluster context was being ignored when loading the Config from a kubeconfig file 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 40b1c710272..388aa84d2e1 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 @@ -21,6 +21,8 @@ import io.fabric8.mockwebserver.crud.AttributeExtractor; import io.fabric8.mockwebserver.crud.AttributeSet; import io.fabric8.zjsonpatch.internal.guava.Strings; +import java.util.Map; +import okhttp3.HttpUrl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,9 +36,11 @@ public class KubernetesAttributesExtractor implements AttributeExtractor label : o.getMetadata().getLabels().entrySet()) { + attributes = attributes.add(new Attribute(LABEL_KEY_PREFIX + label.getKey(), label.getValue())); + } + } return attributes; } @@ -165,6 +188,22 @@ else if (kind.endsWith("s")) { return attributes; } + private static AttributeSet extractQueryParameters(HttpUrl url) { + AttributeSet attributes = new AttributeSet(); + String labelSelector = url.queryParameter("labelSelector"); + if (labelSelector != null) { + for (String requirement : labelSelector.split(",")) { + Matcher m = LABEL_REQUIREMENT_EQUALITY.matcher(requirement); + if (m.matches()) { + attributes = attributes.add(new Attribute(LABEL_KEY_PREFIX + m.group(KEY), m.group(VALUE))); + } else { + LOGGER.warn("Ignoring unsupported label requirement: {}", requirement); + } + } + } + return attributes; + } + private static HasMetadata toKubernetesResource(String s) { try (InputStream stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8.name()))) { return Serialization.unmarshal(stream); 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 aaf087a6acf..64f99695ebd 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 @@ -19,6 +19,8 @@ import io.fabric8.kubernetes.api.model.PodBuilder; import io.fabric8.mockwebserver.crud.Attribute; import io.fabric8.mockwebserver.crud.AttributeSet; +import java.util.HashMap; +import java.util.Map; import org.junit.Assert; import org.junit.Test; @@ -84,6 +86,20 @@ public void shouldHandleResource() { } + @Test + public void shouldHandleResourceWithLabel() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + Map labels = new HashMap<>(); + labels.put("name", "myname"); + Pod pod = new PodBuilder().withNewMetadata().withLabels(labels).endMetadata().build(); + + AttributeSet attributes = extractor.extract(pod); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("labels:name", "myname")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + @Test public void shouldHandleKindWithoutVersion() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); @@ -151,4 +167,45 @@ public void shouldHandleCrds() { Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); } + @Test + public void shouldHandleLabelSelectorsWithOneLabel() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("labels:name", "myname")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + + @Test + public void shouldHandleLabelSelectorsWithDoubleEquals() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3D%3Dmyname"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("labels:name", "myname")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + + @Test + public void shouldHandleLabelSelectorsWithTwoLabels() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod?labelSelector=name%3Dmyname,age%3D37"); + + AttributeSet expected = new AttributeSet(); + expected = expected.add(new Attribute("labels:name", "myname")); + expected = expected.add(new Attribute("labels:age", "37")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + + @Test + public void shouldHandleLabelSelectorsWithADomain() { + KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); + AttributeSet attributes = extractor.extract("/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")); + Assert.assertTrue("Expected " + attributes + " to match " + expected, attributes.matches(expected)); + } + } diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ConfigMapCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ConfigMapCrudTest.java index 73838faffdb..94dd224bcc4 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ConfigMapCrudTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ConfigMapCrudTest.java @@ -67,6 +67,10 @@ public void testCrud() { assertNotNull(aConfigMapList); assertEquals(3, aConfigMapList.getItems().size()); + aConfigMapList = client.configMaps().inAnyNamespace().withLabels(Collections.singletonMap("foo", "bar")).list(); + assertNotNull(aConfigMapList); + assertEquals(2, aConfigMapList.getItems().size()); + aConfigMapList = client.configMaps().inNamespace("ns1").list(); assertNotNull(aConfigMapList); assertEquals(2, aConfigMapList.getItems().size()); @@ -76,10 +80,6 @@ public void testCrud() { assertNotNull(aConfigMapList); assertEquals(1, aConfigMapList.getItems().size()); - aConfigMapList = client.configMaps().inAnyNamespace().withLabels(Collections.singletonMap("foo", "bar")).list(); - assertNotNull(aConfigMapList); - assertEquals(2, aConfigMapList.getItems().size()); - configmap2 = client.configMaps().inNamespace("ns1").withName("configmap2").edit().addToData("II", "TWO").done(); assertNotNull(configmap2); assertEquals("TWO", configmap2.getData().get("II")); 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 9da4b0b56bd..3edbba3fea2 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 @@ -51,6 +51,11 @@ public void testCrud() { assertNotNull(podList); assertEquals(0, podList.getItems().size()); + // test listing with labels + podList = client.pods().inAnyNamespace().withLabels(Collections.singletonMap("testKey", "testValue")).list(); + assertNotNull(podList); + assertEquals(2, podList.getItems().size()); + podList = client.pods().inNamespace("ns1").list(); assertNotNull(podList); assertEquals(2, podList.getItems().size()); @@ -68,11 +73,6 @@ public void testCrud() { assertNotNull(podList); assertEquals(2, podList.getItems().size()); - // test listing with labels - podList = client.pods().inAnyNamespace().withLabels(Collections.singletonMap("testKey", "testValue")).list(); - assertNotNull(podList); - assertEquals(2, podList.getItems().size()); - // test update pod2 = client.pods().inNamespace("ns1").withName("pod2").edit() .editMetadata().addToLabels("key1", "value1").endMetadata().done();