diff --git a/CHANGELOG.md b/CHANGELOG.md index 33d4758c8b6..5ad6eaf3ed8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Fix #3535: ensure clientKeyAlgo is set properly when loading config YAML from `fromKubeconfig` * Fix #3598: applying cancel to the correct future for waitUntilCondition and waitUntilReady * Fix #3609: adding locking to prevent long running Watcher methods from causing reconnects with concurrent processing +* Fix #3620: throw a meaningful exception if no kind/plural is on a ResourceDefinitionContext, default plural if possible #### Improvements diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContext.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContext.java index 97daea513bb..ad47c65e91c 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContext.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContext.java @@ -223,6 +223,7 @@ public Builder withStatusSubresource(boolean statusSubresource) { } public CustomResourceDefinitionContext build() { + this.customResourceDefinitionContext.validate(); return this.customResourceDefinitionContext; } } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContext.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContext.java index 9db1b4bb3f4..f5aad38fe01 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContext.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContext.java @@ -45,6 +45,15 @@ public String getKind() { public boolean isNamespaceScoped() { return namespaced; } + + protected void validate() { + if (plural == null) { + if (kind == null) { + throw new IllegalArgumentException("Neither kind nor plural was set, at least one is required"); + } + plural = Utils.getPluralFromKind(kind); + } + } public static ResourceDefinitionContext fromResourceType(Class resource) { return new Builder() @@ -89,6 +98,7 @@ public Builder withKind(String kind) { } public ResourceDefinitionContext build() { + this.resourceDefinitionContext.validate(); return this.resourceDefinitionContext; } } diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContextTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContextTest.java index 7c2b9a6e2c4..e027ef95cd8 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContextTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/CustomResourceDefinitionContextTest.java @@ -142,8 +142,8 @@ void fromCrdV1beta1OldVersionStyle() { @Test void isNamespaceScoped() { // Given - CustomResourceDefinitionContext crdc1 = new CustomResourceDefinitionContext.Builder().withScope("Namespaced").build(); - CustomResourceDefinitionContext crdc2 = new CustomResourceDefinitionContext.Builder().withScope("Cluster").build(); + CustomResourceDefinitionContext crdc1 = new CustomResourceDefinitionContext.Builder().withPlural("values").withScope("Namespaced").build(); + CustomResourceDefinitionContext crdc2 = new CustomResourceDefinitionContext.Builder().withPlural("values").withScope("Cluster").build(); // When + Then assertThat(crdc1.isNamespaceScoped()).isTrue(); diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContextTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContextTest.java new file mode 100644 index 00000000000..7a89db887fc --- /dev/null +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/base/ResourceDefinitionContextTest.java @@ -0,0 +1,35 @@ +/** + * 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.dsl.base; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ResourceDefinitionContextTest { + + @Test + void testMissing() { + ResourceDefinitionContext.Builder builder = new ResourceDefinitionContext.Builder(); + assertThrows(IllegalArgumentException.class, () -> builder.build()); + builder.withKind("Kind"); + ResourceDefinitionContext context = builder.build(); + assertEquals("kinds", context.getPlural()); + } + +}