6
6
import io .fabric8 .kubernetes .api .model .HasMetadata ;
7
7
import io .javaoperatorsdk .operator .processing .GroupVersionKind ;
8
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
+ */
9
13
public class GroupVersionKindPlural extends GroupVersionKind {
10
14
private final String plural ;
11
15
12
- public GroupVersionKindPlural (String group , String version , String kind , String plural ) {
16
+ protected GroupVersionKindPlural (String group , String version , String kind , String plural ) {
13
17
super (group , version , kind );
14
18
this .plural = plural ;
15
19
}
16
20
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 ) {
18
27
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 ));
20
31
}
21
32
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 );
24
42
}
25
43
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 );
28
60
}
29
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
+ */
30
71
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 ;
34
86
}
35
87
36
88
/**
@@ -46,13 +98,14 @@ public Optional<String> getPlural() {
46
98
47
99
/**
48
100
* 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
50
102
* most cases but might not always be correct, especially if the resource's creator defined an
51
103
* exotic plural form via the CRD.
52
104
*
53
105
* @return the plural form associated with the kind if provided or a default plural form otherwise
54
106
*/
107
+ @ SuppressWarnings ("unused" )
55
108
public String getPluralOrDefault () {
56
- return getPlural ().orElse (Pluralize . toPlural (getKind ()));
109
+ return getPlural ().orElse (getDefaultPluralFor (getKind ()));
57
110
}
58
111
}
0 commit comments