-
Notifications
You must be signed in to change notification settings - Fork 219
feat: add getPlural method on GroupVersionKind #2515
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
18e337d
feat: add getPlural method on GroupVersionKind
metacosm 313a5f0
fix: make getPlural return Optional to show whether plural is known
metacosm 240fdba
refactor: add GroupVersionKindPlural class, used by GenericKubernetes…
metacosm 4756be3
refactor: clean up, cache already resolved GVKs
metacosm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...a/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GroupVersionKindPlural.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.javaoperatorsdk.operator.processing.dependent.kubernetes; | ||
|
||
import java.util.Optional; | ||
|
||
import io.fabric8.kubernetes.api.Pluralize; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.processing.GroupVersionKind; | ||
|
||
/** | ||
* An extension of {@link GroupVersionKind} that also records the associated plural form which is | ||
* useful when dealing with Kubernetes RBACs. Downstream projects might leverage that information. | ||
*/ | ||
public class GroupVersionKindPlural extends GroupVersionKind { | ||
private final String plural; | ||
|
||
protected GroupVersionKindPlural(String group, String version, String kind, String plural) { | ||
super(group, version, kind); | ||
this.plural = plural; | ||
} | ||
|
||
protected GroupVersionKindPlural(String apiVersion, String kind, String plural) { | ||
super(apiVersion, kind); | ||
this.plural = plural; | ||
} | ||
|
||
protected GroupVersionKindPlural(GroupVersionKind gvk, String plural) { | ||
this(gvk.getGroup(), gvk.getVersion(), gvk.getKind(), | ||
plural != null ? plural | ||
: (gvk instanceof GroupVersionKindPlural ? ((GroupVersionKindPlural) gvk).plural | ||
: null)); | ||
} | ||
|
||
/** | ||
* Creates a new GroupVersionKindPlural from the specified {@link GroupVersionKind}. | ||
* | ||
* @param gvk a {@link GroupVersionKind} from which to create a new GroupVersionKindPlural object | ||
* @return a new GroupVersionKindPlural object matching the specified {@link GroupVersionKind} | ||
*/ | ||
public static GroupVersionKindPlural from(GroupVersionKind gvk) { | ||
return gvk instanceof GroupVersionKindPlural ? ((GroupVersionKindPlural) gvk) | ||
: gvkWithPlural(gvk, null); | ||
} | ||
|
||
/** | ||
* Creates a new GroupVersionKindPlural based on the specified {@link GroupVersionKind} instance | ||
* but specifying a plural form to use as well. | ||
* | ||
* @param gvk the base {@link GroupVersionKind} from which to derive a new GroupVersionKindPlural | ||
* @param plural the plural form to use for the new instance or {@code null} if the default plural | ||
* form is desired. Note that the specified plural form will override any existing plural | ||
* form for the specified {@link GroupVersionKind} (in particular, if the specified | ||
* {@link GroupVersionKind} was already an instance of GroupVersionKindPlural, its plural | ||
* form will only be considered in the new instance if the specified plural form is | ||
* {@code null} | ||
* @return a new GroupVersionKindPlural derived from the specified {@link GroupVersionKind} and | ||
* plural form | ||
*/ | ||
public static GroupVersionKindPlural gvkWithPlural(GroupVersionKind gvk, String plural) { | ||
return new GroupVersionKindPlural(gvk, plural); | ||
} | ||
|
||
/** | ||
* Creates a new GroupVersionKindPlural instance extracting the information from the specified | ||
* {@link HasMetadata} implementation | ||
* | ||
* @param resourceClass the {@link HasMetadata} from which group, version, kind and plural form | ||
* are extracted | ||
* @return a new GroupVersionKindPlural instance based on the specified {@link HasMetadata} | ||
* implementation | ||
*/ | ||
public static GroupVersionKindPlural gvkFor(Class<? extends HasMetadata> resourceClass) { | ||
final var gvk = GroupVersionKind.gvkFor(resourceClass); | ||
return gvkWithPlural(gvk, HasMetadata.getPlural(resourceClass)); | ||
} | ||
|
||
/** | ||
* Retrieves the default plural form for the specified kind. | ||
* | ||
* @param kind the kind for which we want to get the default plural form | ||
* @return the default plural form for the specified kind | ||
*/ | ||
public static String getDefaultPluralFor(String kind) { | ||
// todo: replace by Fabric8 version when available, see | ||
// https://github.com/fabric8io/kubernetes-client/pull/6314 | ||
return kind != null ? Pluralize.toPlural(kind.toLowerCase()) : null; | ||
} | ||
|
||
/** | ||
* Returns the plural form associated with the kind if it has been provided explicitly (either | ||
* manually by the user, or determined from the associated resource class definition) | ||
* | ||
* @return {@link Optional#empty()} if the plural form was not provided explicitly, or the plural | ||
* form if it was provided explicitly | ||
*/ | ||
public Optional<String> getPlural() { | ||
return Optional.ofNullable(plural); | ||
} | ||
|
||
/** | ||
* Returns the plural form associated with the kind if it was provided or a default, computed form | ||
* via {@link #getDefaultPluralFor(String)} (which should correspond to the actual plural form in | ||
* most cases but might not always be correct, especially if the resource's creator defined an | ||
* exotic plural form via the CRD. | ||
* | ||
* @return the plural form associated with the kind if provided or a default plural form otherwise | ||
*/ | ||
@SuppressWarnings("unused") | ||
public String getPluralOrDefault() { | ||
return getPlural().orElse(getDefaultPluralFor(getKind())); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.