KubernetesAttributesExtractor implements AttributeExtractor<HasMetadata> { @Override public AttributeSet extract(String s) { if (s == null || s.isEmpty()) { return new AttributeSet(); } HasMetadata h = toKubernetesResource(s); if (h != null) { return extract(h); } return fromPath(s); } KubernetesAttributesExtractor();  KubernetesAttributesExtractor(List<CustomResourceDefinitionContext> crdContexts); @Override AttributeSet fromPath(String s); @Override AttributeSet fromResource(String s); @Override AttributeSet extract(String s); @Override AttributeSet extract(HasMetadata o); static final String KEY; static final String KIND; static final String NAME; static final String NAMESPACE; static final String VALUE; }
@Test public void shouldHandleNamespacedPathWithResource() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods/mypod"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "mypod")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleNamespacedPath() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); expected = expected.add(new Attribute("namespace", "myns")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleNonNamespacedPath() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/nodes/mynode"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "node")); expected = expected.add(new Attribute("name", "mynode")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandlePathWithParameters() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/pods?labelSelector=testKey%3DtestValue"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleResource() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); Pod pod = new PodBuilder().withNewMetadata().withName("mypod").withNamespace("myns").endMetadata().build(); AttributeSet attributes = extractor.extract(pod); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "mypod")); assertTrue(attributes.matches(expected)); }
@Test void shouldHandleRawResource() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); String resource = "{\"metadata\":{\"name\":\"myresource\",\"namespace\":\"myns\"}, \"kind\":\"raw\", \"apiVersion\":\"v1\"}"; AttributeSet attributes = extractor.extract(resource); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "raw")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "myresource")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleResourceWithLabel() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); Map<String, String> 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")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleKindWithoutVersion() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/pods"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "pod")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleExtensions() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/apis/apps/v1/deployments"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "deployment")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleIngress() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "ingress")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "myingress")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleEndpoints() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/api/v1/namespaces/myns/endpoints"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "endpoints")); expected = expected.add(new Attribute("namespace", "myns")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleIngresses() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/apis/extensions/v1beta1/namespaces/myns/ingresses"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "ingress")); expected = expected.add(new Attribute("namespace", "myns")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleApiGroups() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor .extract("/apis/autoscaling/v1/namespaces/myns/horizontalpodautoscalers/myhpa"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "horizontalpodautoscaler")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "myhpa")); assertTrue(attributes.matches(expected)); }
@Test public void shouldHandleCrds() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); AttributeSet attributes = extractor.extract("/apis/test.com/v1/namespaces/myns/crds/mycrd"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "crd")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "mycrd")); assertTrue(attributes.matches(expected)); }
@Test void shouldHandleCrdSubresources() { KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(); String[] subresources = new String[]{"status", "scale"}; String basePath = "/apis/test.com/v1/namespaces/myns/crds/mycrd/"; for (String subresource : subresources) { AttributeSet attributes = extractor.extract(basePath + subresource); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("kind", "crd")); expected = expected.add(new Attribute("namespace", "myns")); expected = expected.add(new Attribute("name", "mycrd")); assertTrue(attributes.matches(expected), "extracted attributes match for " + subresource + " expectation: " + expected); } AttributeSet attributes = extractor.extract(basePath + "somethingRandom"); assertTrue(attributes.matches(new AttributeSet()), "should extract nothing from an unsupported crd subresource"); }
@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")); assertTrue(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")); assertTrue(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")); assertTrue(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")); assertTrue(attributes.matches(expected)); }
@Test void testCustomResourceAttributesExtraction() { CustomResourceDefinitionContext crdContext = new CustomResourceDefinitionContext.Builder() .withScope("Namespaced") .withPlural("customdatabases") .withVersion("v1alpha1") .withGroup("demo.fabric8.io") .withKind("CustomDatabase") .build(); KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor(Collections.singletonList(crdContext)); AttributeSet attributes = extractor .extract("/apis/demo.fabric8.io/v1alpha1/namespaces/ns1/customdatabases"); AttributeSet expected = new AttributeSet(); expected = expected.add(new Attribute("namespace", "ns1")); expected = expected.add(new Attribute("kind", "customdatabase")); assertTrue(attributes.matches(expected)); }