Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #3620: throw a meaningful exception if no kind/plural, default plural #3624

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is tricky because the target class might use a different plural version than the one computed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the context is constructed from a class it will use the annotation to populate this field. This will only occur in case where someone is manually constructing the context. Every method the specifies both a context and a class has been deprecated - it's not expected that a user will manually construct the context when a class is used.

}
}

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() {
shawkins marked this conversation as resolved.
Show resolved Hide resolved
ResourceDefinitionContext.Builder builder = new ResourceDefinitionContext.Builder();
assertThrows(IllegalArgumentException.class, () -> builder.build());
builder.withKind("Kind");
ResourceDefinitionContext context = builder.build();
assertEquals("kinds", context.getPlural());
}

}