Skip to content

Commit

Permalink
removing kubernetescrudattributesextractor by refining kind/plural logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed Jun 11, 2021
1 parent 324fac4 commit 031ca7e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;
Expand All @@ -36,6 +37,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -82,6 +84,7 @@ public class KubernetesAttributesExtractor implements AttributeExtractor<HasMeta
private static final String SCHEME = "http";
private static final String HOST = "localhost";
private Map<String, CustomResourceDefinitionContext> crdContexts;
private Map<String, String> pluralToKind = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

public KubernetesAttributesExtractor() {
this(Collections.emptyList());
Expand Down Expand Up @@ -165,29 +168,30 @@ public AttributeSet extract(String s) {
}

@Override
public AttributeSet extract(HasMetadata o) {
AttributeSet attributes = extractMetadataAttributes(o);
if (!Utils.isNullOrEmpty(o.getKind())) {
attributes = attributes.add(new Attribute(KIND, o.getKind().toLowerCase(Locale.ROOT)));
public AttributeSet extract(HasMetadata hasMetadata) {
AttributeSet metadataAttributes = new AttributeSet();
if (!Utils.isNullOrEmpty(hasMetadata.getMetadata().getName())) {
metadataAttributes = metadataAttributes.add(new Attribute(NAME, hasMetadata.getMetadata().getName()));
}
return attributes;
}

protected AttributeSet extractMetadataAttributes(HasMetadata hasMetadata) {
AttributeSet metadataAttributes = new AttributeSet();
if (!Utils.isNullOrEmpty(hasMetadata.getMetadata().getName())) {
metadataAttributes = metadataAttributes.add(new Attribute(NAME, hasMetadata.getMetadata().getName()));
}

if (!Utils.isNullOrEmpty(hasMetadata.getMetadata().getNamespace())) {
metadataAttributes = metadataAttributes.add(new Attribute(NAMESPACE, hasMetadata.getMetadata().getNamespace()));
}
if (!Utils.isNullOrEmpty(hasMetadata.getMetadata().getNamespace())) {
metadataAttributes = metadataAttributes.add(new Attribute(NAMESPACE, hasMetadata.getMetadata().getNamespace()));
}

if (hasMetadata.getMetadata().getLabels() != null) {
for (Map.Entry<String, String> label : hasMetadata.getMetadata().getLabels().entrySet()) {
metadataAttributes = metadataAttributes.add(new Attribute(LABEL_KEY_PREFIX + label.getKey(), label.getValue()));
}
}
if (!Utils.isNullOrEmpty(hasMetadata.getKind())) {
String kind = hasMetadata.getKind().toLowerCase(Locale.ROOT);
metadataAttributes = metadataAttributes.add(new Attribute(KIND, kind));
if (hasMetadata instanceof GenericKubernetesResource) {
pluralToKind.put(getPluralForKind(hasMetadata.getKind(), hasMetadata.getApiVersion()), kind);
} else {
pluralToKind.put(hasMetadata.getPlural(), kind);
}
}
return metadataAttributes;
}

Expand Down Expand Up @@ -217,37 +221,29 @@ private Map<String, String> extract(Matcher m) {
return attributes;
}

private String resolveKindFromPlural(String kind) {
String result = getCustomResourceKindFromPlural(kind);
private String resolveKindFromPlural(String plural) {
String result = getCustomResourceKindFromPlural(plural);
if (result != null) {
return result;
}
return getKindFromPluralForKubernetesTypes(kind);
return pluralToKind.getOrDefault(plural, plural.substring(0, plural.length() - 1));
}

private static String getKindFromPluralForKubernetesTypes(String kind) {
//Poor mans to singular.
//Special Case for PodSecurityPolicies and NetworkPolicies because
//we need to return PodSecurityPolicy and NetworkPolicy respectively
//because it is returning PodSecurityPolicie and NetworkPolicie now
//Right now not adding generalised case of "ies" because it may break other resource not sure

if (kind.endsWith("ses")) {
kind = kind.substring(0, kind.length() - 2);
}
else if (kind.equalsIgnoreCase("PodSecurityPolicies") ||
kind.equalsIgnoreCase("NetworkPolicies")){
kind = kind.substring(0, kind.length() - 3) + "y";
} else if (kind.equalsIgnoreCase("securityContextConstraints") ||
kind.equalsIgnoreCase("endpoints")){
// do nothing
// because its a case which is ending with s but its name is
// like that, it is not plural
}
else if (kind.endsWith("s")) {
kind = kind.substring(0, kind.length() - 1);
/**
* Find the plural for standard types by consulting the deserializer
*/
private static String getPluralForKind(String kind, String apiVersion) {
GenericKubernetesResource gkr = new GenericKubernetesResource();
gkr.setApiVersion(apiVersion);
gkr.setKind(kind);
try {
HasMetadata result = Serialization.unmarshal(new ByteArrayInputStream(Serialization.asJson(gkr).getBytes(StandardCharsets.UTF_8)));
if (result != null) {
return result.getPlural();
}
} catch (KubernetesClientException e) {
}
return kind;
return kind + "s";
}

private static AttributeSet extractQueryParameters(HttpUrl url) {
Expand Down Expand Up @@ -301,7 +297,7 @@ static GenericKubernetesResource toKubernetesResource(String s) {

private String getCustomResourceKindFromPlural(String plural) {
CustomResourceDefinitionContext crdContext = crdContexts.get(plural);
return crdContext != null && crdContext.getKind() != null ? crdContext.getKind().toLowerCase() : null;
return crdContext != null && crdContext.getKind() != null ? crdContext.getKind().toLowerCase(Locale.ROOT) : null;
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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;
Expand Down Expand Up @@ -88,10 +87,10 @@ public KubernetesCrudDispatcher() {
}

public KubernetesCrudDispatcher(List<CustomResourceDefinitionContext> crdContexts) {
this(new KubernetesCrudAttributesExtractor(crdContexts), new KubernetesResponseComposer());
this(new KubernetesAttributesExtractor(crdContexts), new KubernetesResponseComposer());
}

public KubernetesCrudDispatcher(KubernetesCrudAttributesExtractor attributeExtractor, ResponseComposer responseComposer) {
public KubernetesCrudDispatcher(KubernetesAttributesExtractor attributeExtractor, ResponseComposer responseComposer) {
super(new Context(Serialization.jsonMapper()), attributeExtractor, responseComposer);
this.kubernetesAttributesExtractor = attributeExtractor;
crdProcessor = new CustomResourceDefinitionProcessor(kubernetesAttributesExtractor);
Expand Down Expand Up @@ -513,7 +512,10 @@ private MockResponse getUnprocessableEntityMockResponse(String s, HasMetadata h,
}

private String getStatusBody(HasMetadata h, int code, Exception ex) {
String kind = Utils.getNonNullOrElse(KubernetesResourceUtil.getKind(h), "Unknown");
String kind = "Unknown";
if (h != null && Utils.isNotNullOrEmpty(h.getKind())) {
kind = h.getKind();
}
Status status = new StatusBuilder().withStatus("Failure")
.withReason("Invalid")
.withMessage(kind + " is invalid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.EndpointsBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.extensions.IngressBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.mockwebserver.crud.Attribute;
import io.fabric8.mockwebserver.crud.AttributeSet;
Expand Down Expand Up @@ -146,6 +147,7 @@ public void shouldHandleExtensions() {
@Test
public void shouldHandleIngress() {
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor();
extractor.extract(new IngressBuilder().withNewMetadata().endMetadata().build());
AttributeSet attributes = extractor.fromPath("/apis/extensions/v1beta1/namespaces/myns/ingresses/myingress");

AttributeSet expected = new AttributeSet();
Expand All @@ -158,6 +160,7 @@ public void shouldHandleIngress() {
@Test
public void shouldHandleEndpoints() {
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor();
extractor.extract(new EndpointsBuilder().withNewMetadata().endMetadata().build());
AttributeSet attributes = extractor.fromPath("/api/v1/namespaces/myns/endpoints");

AttributeSet expected = new AttributeSet();
Expand All @@ -169,6 +172,7 @@ public void shouldHandleEndpoints() {
@Test
public void shouldHandleIngresses() {
KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor();
extractor.extract(new IngressBuilder().withNewMetadata().endMetadata().build());
AttributeSet attributes = extractor.fromPath("/apis/extensions/v1beta1/namespaces/myns/ingresses");

AttributeSet expected = new AttributeSet();
Expand Down
Loading

0 comments on commit 031ca7e

Please sign in to comment.