Skip to content

Commit

Permalink
Updating Kubernetes CRUD mock logic to use GenericKubernetesResource (#…
Browse files Browse the repository at this point in the history
…3229)

* removing fallbackhasmetadata to rely on generic

needs better handling for non-conforming parsing

* switching logic to use generickubernetes resource, rather than jsonnode

* removing kubernetescrudattributesextractor by refining kind/plural logic

* making the event logic more uniform

* fix #3216: further refining the mock logic

added awareness of apiVersion
switched to using plural rather than kind

* switching to action enum and trying to remove complexity

* clearing up code smells
  • Loading branch information
shawkins authored Jun 11, 2021
1 parent 876b1bf commit 1093d5b
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 1,073 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Fix #3144: walking back the assumption that resource/status should be a subresource for the crud mock server, now it will be only if a registered crd indicates that it should be
* Fix #3194: the mock server will now infer the namespace from the path
* Fix #3076: the MetadataObject for CustomResource is now seen as Buildable
* Fix #3216: made the mock server aware of apiVersions

#### Improvements
* Fix #3078: adding javadocs to further clarify patch, edit, replace, etc. and note the possibility of items being modified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ public static <T> T unmarshal(InputStream is, ObjectMapper mapper, Map<String, S
throw KubernetesClientException.launderThrowable(e);
}
}

/**
* Unmarshals a {@link String}
* @param str The {@link String}.
* @param <T> template argument denoting type
* @return returns de-serialized object
*/
public static<T> T unmarshal(String str) {
try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
return unmarshal(is);
} catch (IOException e) {
throw KubernetesClientException.launderThrowable(e);
}
}

/**
* Unmarshals a {@link String}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GenericKubernetesResource implements HasMetadata {
@JsonProperty("metadata")
private ObjectMeta metadata;
@JsonIgnore
private transient Map<String, Object> additionalProperties = new LinkedHashMap<>();
private Map<String, Object> additionalProperties = new LinkedHashMap<>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.utils.Serialization;

import java.util.Optional;
import java.util.Map;

/**
* Holds state related to crds by manipulating the crds known to the attributes extractor
Expand Down Expand Up @@ -49,23 +49,19 @@ public void process(String path, String crdString, boolean delete) {
return;
}
if (delete) {
extractor.getCrdContexts().remove(context.getPlural());
extractor.removeCrdContext(context);
} else {
extractor.getCrdContexts().put(context.getPlural(), context);
extractor.addCrdContext(context);
}
}

public boolean isStatusSubresource(String kind) {
if (kind == null) {
public boolean isStatusSubresource(Map<String, String> pathValues) {
CustomResourceDefinitionContext context = extractor.getCrdContext(pathValues.get(KubernetesAttributesExtractor.API),
pathValues.get(KubernetesAttributesExtractor.VERSION), pathValues.get(KubernetesAttributesExtractor.PLURAL));
if (context == null) {
return false;
}
// we are only holding by plural, lookup now by kind
Optional<CustomResourceDefinitionContext> context =
extractor.getCrdContexts().values().stream().filter(c -> kind.equalsIgnoreCase(c.getKind())).findFirst();
if (!context.isPresent()) {
return false;
}
return context.get().isStatusSubresource();
return context.isStatusSubresource();
}

}

This file was deleted.

Loading

0 comments on commit 1093d5b

Please sign in to comment.