Skip to content

Commit

Permalink
fix (kubernetes-client) : Throw exception when HasMetadata is used …
Browse files Browse the repository at this point in the history
…in `resources(...)` API (#3990)

Access to `resources(...)` DSL method with generic HasMetadata interface
shouldn't be allowed.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed May 18, 2022
1 parent 5e683d7 commit aa89c52
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#### Bugs
* Fix #3832 #1883: simplifying the isHttpsAvailable check
* Fix #3745: the client will throw better exceptions when a namespace is not discernible for an operation
* Fix #3990: Throw exception when `HasMetadata` is used in `resources(...)` API
* Fix #4106: removed listing from projectrequests
* Fix #4140: changed StatefulSet rolling pause / resume to unsupported. Also relying on default rolling logic to Deployments and StatefulSets
* Fix #4081: moving Versionable.withResourceVersion to a method on WatchAndWaitable and removing Waitable from the return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public <T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Re
if (GenericKubernetesResource.class.equals(resourceType)) {
throw new KubernetesClientException("resources cannot be called with a generic type");
}
if (resourceType.isInterface()) {
throw new IllegalArgumentException("resources cannot be called with an interface");
}
try {
// TODO: check the Resource class type
return handlers.getOperation(resourceType, listClass, this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.mock;

import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@EnableKubernetesMockClient
class TypedResourcesApiTest {
KubernetesClient client;

@Test
void resources_whenUsedWithGenericKubernetesResource_shouldThrowException() {
// Given + When
KubernetesClientException kubernetesClientException = assertThrows(KubernetesClientException.class,
() -> client.resources(GenericKubernetesResource.class));

// Then
assertThat(kubernetesClientException)
.hasMessage("resources cannot be called with a generic type");
}

@Test
void resources_whenUsedWithHasMetadata_shouldThrowException() {
// Given + When
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class,
() -> client.resources(HasMetadata.class));

// Then
assertThat(illegalArgumentException)
.hasMessage("resources cannot be called with an interface");
}
}

0 comments on commit aa89c52

Please sign in to comment.