|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent.kubernetes; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +import io.fabric8.kubernetes.api.Pluralize; |
| 6 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 7 | +import io.javaoperatorsdk.operator.processing.GroupVersionKind; |
| 8 | + |
| 9 | +/** |
| 10 | + * An extension of {@link GroupVersionKind} that also records the associated plural form which is |
| 11 | + * useful when dealing with Kubernetes RBACs. Downstream projects might leverage that information. |
| 12 | + */ |
| 13 | +public class GroupVersionKindPlural extends GroupVersionKind { |
| 14 | + private final String plural; |
| 15 | + |
| 16 | + protected GroupVersionKindPlural(String group, String version, String kind, String plural) { |
| 17 | + super(group, version, kind); |
| 18 | + this.plural = plural; |
| 19 | + } |
| 20 | + |
| 21 | + protected GroupVersionKindPlural(String apiVersion, String kind, String plural) { |
| 22 | + super(apiVersion, kind); |
| 23 | + this.plural = plural; |
| 24 | + } |
| 25 | + |
| 26 | + protected GroupVersionKindPlural(GroupVersionKind gvk, String plural) { |
| 27 | + this(gvk.getGroup(), gvk.getVersion(), gvk.getKind(), |
| 28 | + plural != null ? plural |
| 29 | + : (gvk instanceof GroupVersionKindPlural ? ((GroupVersionKindPlural) gvk).plural |
| 30 | + : null)); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new GroupVersionKindPlural from the specified {@link GroupVersionKind}. |
| 35 | + * |
| 36 | + * @param gvk a {@link GroupVersionKind} from which to create a new GroupVersionKindPlural object |
| 37 | + * @return a new GroupVersionKindPlural object matching the specified {@link GroupVersionKind} |
| 38 | + */ |
| 39 | + public static GroupVersionKindPlural from(GroupVersionKind gvk) { |
| 40 | + return gvk instanceof GroupVersionKindPlural ? ((GroupVersionKindPlural) gvk) |
| 41 | + : gvkWithPlural(gvk, null); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a new GroupVersionKindPlural based on the specified {@link GroupVersionKind} instance |
| 46 | + * but specifying a plural form to use as well. |
| 47 | + * |
| 48 | + * @param gvk the base {@link GroupVersionKind} from which to derive a new GroupVersionKindPlural |
| 49 | + * @param plural the plural form to use for the new instance or {@code null} if the default plural |
| 50 | + * form is desired. Note that the specified plural form will override any existing plural |
| 51 | + * form for the specified {@link GroupVersionKind} (in particular, if the specified |
| 52 | + * {@link GroupVersionKind} was already an instance of GroupVersionKindPlural, its plural |
| 53 | + * form will only be considered in the new instance if the specified plural form is |
| 54 | + * {@code null} |
| 55 | + * @return a new GroupVersionKindPlural derived from the specified {@link GroupVersionKind} and |
| 56 | + * plural form |
| 57 | + */ |
| 58 | + public static GroupVersionKindPlural gvkWithPlural(GroupVersionKind gvk, String plural) { |
| 59 | + return new GroupVersionKindPlural(gvk, plural); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a new GroupVersionKindPlural instance extracting the information from the specified |
| 64 | + * {@link HasMetadata} implementation |
| 65 | + * |
| 66 | + * @param resourceClass the {@link HasMetadata} from which group, version, kind and plural form |
| 67 | + * are extracted |
| 68 | + * @return a new GroupVersionKindPlural instance based on the specified {@link HasMetadata} |
| 69 | + * implementation |
| 70 | + */ |
| 71 | + public static GroupVersionKindPlural gvkFor(Class<? extends HasMetadata> resourceClass) { |
| 72 | + final var gvk = GroupVersionKind.gvkFor(resourceClass); |
| 73 | + return gvkWithPlural(gvk, HasMetadata.getPlural(resourceClass)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Retrieves the default plural form for the specified kind. |
| 78 | + * |
| 79 | + * @param kind the kind for which we want to get the default plural form |
| 80 | + * @return the default plural form for the specified kind |
| 81 | + */ |
| 82 | + public static String getDefaultPluralFor(String kind) { |
| 83 | + // todo: replace by Fabric8 version when available, see |
| 84 | + // https://github.com/fabric8io/kubernetes-client/pull/6314 |
| 85 | + return kind != null ? Pluralize.toPlural(kind.toLowerCase()) : null; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the plural form associated with the kind if it has been provided explicitly (either |
| 90 | + * manually by the user, or determined from the associated resource class definition) |
| 91 | + * |
| 92 | + * @return {@link Optional#empty()} if the plural form was not provided explicitly, or the plural |
| 93 | + * form if it was provided explicitly |
| 94 | + */ |
| 95 | + public Optional<String> getPlural() { |
| 96 | + return Optional.ofNullable(plural); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the plural form associated with the kind if it was provided or a default, computed form |
| 101 | + * via {@link #getDefaultPluralFor(String)} (which should correspond to the actual plural form in |
| 102 | + * most cases but might not always be correct, especially if the resource's creator defined an |
| 103 | + * exotic plural form via the CRD. |
| 104 | + * |
| 105 | + * @return the plural form associated with the kind if provided or a default plural form otherwise |
| 106 | + */ |
| 107 | + @SuppressWarnings("unused") |
| 108 | + public String getPluralOrDefault() { |
| 109 | + return getPlural().orElse(getDefaultPluralFor(getKind())); |
| 110 | + } |
| 111 | +} |
0 commit comments