Skip to content

Commit

Permalink
Fix fabric8io#2316: Cannot load resource from stream without apiVersion
Browse files Browse the repository at this point in the history
In case of duplicate Kubernetes resources found, when no apiVersion
is provided return first item found instead of null
  • Loading branch information
rohanKanojia committed Jul 28, 2020
1 parent a0a4516 commit 492d18e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,12 +2,13 @@

### 4.10-SNAPSHOT
#### Bugs
* Fix #2316: Cannot load resource from stream without apiVersion

#### Improvements

#### Dependency Upgrade
* Fix #2355: bump jandex from 2.1.3.Final to 2.2.0.Final
* Fix #2353: bump workflow action-setup-* versions + kubernetes to 1.18.6
* Fix #2353: bump workflow action-setup- versions + kubernetes to 1.18.6

#### New Features
* Fix #2287: Add support for V1 and V1Beta1 CustomResourceDefinition
Expand Down
Expand Up @@ -248,16 +248,20 @@ private Class<? extends KubernetesResource> getInternalTypeForName(String key) {
// If only one class found, return it
if (possibleResults.size() == 1) {
return possibleResults.get(0);
}

// Compare with apiVersions being compared for set of classes found
for (Class<? extends KubernetesResource> result : possibleResults) {
} else if (possibleResults.size() > 1) {
// Compare with apiVersions being compared for set of classes found
for (Class<? extends KubernetesResource> result : possibleResults) {
String defaultKeyFromClass = getKeyFromClass(result);
if (key.equals(defaultKeyFromClass)) {
return result;
return result;
}
}
// Not able to compare with apiVersions, perhaps it was not provided;
// return first item found
return possibleResults.get(0);
} else {
return null;
}
return null;
}

private String getKeyFromClass(Class<? extends KubernetesResource> clazz) {
Expand Down
Expand Up @@ -41,6 +41,7 @@
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;

import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.kubernetes.model.util.Helper;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Rule;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -772,4 +773,20 @@ private DeploymentBuilder getDeploymentBuilder() {
.endSpec();
}

@Test
void testDeploymentLoadWithoutApiVersion() {
// Given
KubernetesClient client = server.getClient();

// When
List<HasMetadata> list = client.load(getClass().getResourceAsStream("/valid-deployment-without-apiversion.json")).get();
Deployment deployment = (Deployment) list.get(0);

// Then
assertNotNull(deployment);
assertEquals("test", deployment.getMetadata().getName());
assertEquals(1, deployment.getSpec().getReplicas());
assertEquals(1, deployment.getSpec().getTemplate().getSpec().getContainers().size());
}

}
@@ -0,0 +1,39 @@
{
"kind": "Deployment",
"metadata": {
"name": "test",
"labels": {
"app": "test"
}
},
"spec": {
"selector": {
"matchLabels": {
"app": "test"
}
},
"replicas": 1,
"template": {
"metadata": {
"labels": {
"app": "test"
}
},
"spec": {
"containers": [
{
"name": "test",
"image": "busybox:latest",
"command": [
"/bin/sh",
"-c"
],
"args": [
"sleep 60"
]
}
]
}
}
}
}

0 comments on commit 492d18e

Please sign in to comment.