Skip to content

Commit

Permalink
fix: ensures kind is set for generic resource lists
Browse files Browse the repository at this point in the history
closes fabric8io#5729

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
  • Loading branch information
shawkins committed Jan 29, 2024
1 parent 2c4d01d commit c9bd241
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 6.11-SNAPSHOT

#### Bugs
* Fix #5729: ensure that kind is set for generic resource lists
* Fix #3032: JUnit5 Kubernetes Extension works with Nested tests

#### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.fabric8.kubernetes.api.builder.Visitor;
import io.fabric8.kubernetes.api.model.DefaultKubernetesResourceList;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
Expand Down Expand Up @@ -438,7 +439,7 @@ public Type getType() {
}
};
CompletableFuture<L> futureAnswer = handleResponse(httpClient, requestBuilder, listTypeReference);
return futureAnswer.thenApply(updateApiVersion());
return futureAnswer.thenApply(this::updateListItems);
} catch (IOException e) {
throw KubernetesClientException.launderThrowable(forOperationType("list"), e);
}
Expand Down Expand Up @@ -873,16 +874,22 @@ protected Class<? extends Config> getConfigType() {

/**
* Updates the list items if they have missing or default apiGroupVersion values and the resource is currently
* using API Groups with custom version strings
* using API Groups with custom version strings, or if they are generic and lack a kind
*/
protected UnaryOperator<L> updateApiVersion() {
return list -> {
String version = apiVersion;
if (list != null && version != null && version.length() > 0 && list.getItems() != null) {
list.getItems().forEach(this::updateApiVersion);
protected L updateListItems(L list) {
if (list != null && list.getItems() != null) {
boolean updateApiVersion = Utils.isNotNullOrEmpty(apiVersion);
boolean updateKind = GenericKubernetesResource.class.isAssignableFrom(getType());
if (updateApiVersion || updateKind) {
for (T item : list.getItems()) {
updateApiVersion(item);
if (updateKind && item != null && item.getKind() == null) {
((GenericKubernetesResource) item).setKind(getKind());
}
}
}
return list;
};
}
return list;
}

/**
Expand All @@ -893,11 +900,11 @@ protected UnaryOperator<L> updateApiVersion() {
*/
protected void updateApiVersion(HasMetadata hasMetadata) {
String version = apiVersion;
if (hasMetadata != null && version != null && version.length() > 0) {
if (hasMetadata != null && Utils.isNotNullOrEmpty(version)) {
String current = hasMetadata.getApiVersion();
// lets overwrite the api version if its currently missing, the resource uses an API Group with '/'
// or we have the default of 'v1' when using an API group version like 'build.openshift.io/v1'
if (current == null || "v1".equals(current) || current.indexOf('/') < 0 && version.indexOf('/') > 0) {
if (current == null || "v1".equals(current) || (current.indexOf('/') < 0 && version.indexOf('/') > 0)) {
hasMetadata.setApiVersion(version);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ public boolean isResourceNamespaced() {
return rdc.isNamespaceScoped();
}

@Override
public OperationContext getOperationContext() {
return this.context;
}

@Override
public String getKind() {
return rdc.getKind();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ void testGenericBuiltinResourceWithoutMetadata() {

MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> resources = client
.genericKubernetesResources("v1", "ConfigMap");
assertTrue(!resources.list().getItems().isEmpty());
List<GenericKubernetesResource> items = resources.list().getItems();
assertTrue(!items.isEmpty());
assertTrue(items.stream().allMatch(g -> g.getKind().equals("ConfigMap")));
}

@Test
Expand Down

0 comments on commit c9bd241

Please sign in to comment.