66import io .fabric8 .kubernetes .api .model .HasMetadata ;
77import io .javaoperatorsdk .operator .processing .GroupVersionKind ;
88
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+ */
913public class GroupVersionKindPlural extends GroupVersionKind {
1014 private final String plural ;
1115
12- public GroupVersionKindPlural (String group , String version , String kind , String plural ) {
16+ protected GroupVersionKindPlural (String group , String version , String kind , String plural ) {
1317 super (group , version , kind );
1418 this .plural = plural ;
1519 }
1620
17- public GroupVersionKindPlural (GroupVersionKind gvk ) {
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 ) {
1827 this (gvk .getGroup (), gvk .getVersion (), gvk .getKind (),
19- gvk instanceof GroupVersionKindPlural ? ((GroupVersionKindPlural ) gvk ).plural : null );
28+ plural != null ? plural
29+ : (gvk instanceof GroupVersionKindPlural ? ((GroupVersionKindPlural ) gvk ).plural
30+ : null ));
2031 }
2132
22- public static GroupVersionKind gvkFor (String group , String version , String kind ) {
23- return new GroupVersionKind (group , version , kind );
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 );
2442 }
2543
26- public static GroupVersionKind gvkFor (String apiVersion , String kind ) {
27- return new GroupVersionKind (apiVersion , kind );
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 );
2860 }
2961
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+ */
3071 public static GroupVersionKindPlural gvkFor (Class <? extends HasMetadata > resourceClass ) {
31- return new GroupVersionKindPlural (HasMetadata .getGroup (resourceClass ),
32- HasMetadata .getVersion (resourceClass ), HasMetadata .getKind (resourceClass ),
33- HasMetadata .getPlural (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 ;
3486 }
3587
3688 /**
@@ -46,13 +98,14 @@ public Optional<String> getPlural() {
4698
4799 /**
48100 * Returns the plural form associated with the kind if it was provided or a default, computed form
49- * via {@link Pluralize#toPlural (String)} (which should correspond to the actual plural form in
101+ * via {@link #getDefaultPluralFor (String)} (which should correspond to the actual plural form in
50102 * most cases but might not always be correct, especially if the resource's creator defined an
51103 * exotic plural form via the CRD.
52104 *
53105 * @return the plural form associated with the kind if provided or a default plural form otherwise
54106 */
107+ @ SuppressWarnings ("unused" )
55108 public String getPluralOrDefault () {
56- return getPlural ().orElse (Pluralize . toPlural (getKind ()));
109+ return getPlural ().orElse (getDefaultPluralFor (getKind ()));
57110 }
58111}
0 commit comments