Skip to content

Commit

Permalink
fix fabric8io#3620: throw a meaningful exception if no kind/plural, d…
Browse files Browse the repository at this point in the history
…efault

plural
  • Loading branch information
shawkins committed Nov 29, 2021
1 parent 9ada97f commit 25128d6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public Builder withStatusSubresource(boolean statusSubresource) {
}

public CustomResourceDefinitionContext build() {
this.customResourceDefinitionContext.validate();
return this.customResourceDefinitionContext;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends KubernetesResource> resource) {
return new Builder()
Expand Down Expand Up @@ -89,6 +98,7 @@ public Builder withKind(String kind) {
}

public ResourceDefinitionContext build() {
this.resourceDefinitionContext.validate();
return this.resourceDefinitionContext;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 25128d6

Please sign in to comment.