Skip to content

Commit 2f3ca66

Browse files
committed
gvk support
Signed-off-by: Attila Mészáros <[email protected]>
1 parent a88fa5e commit 2f3ca66

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/GroupVersionKind.java

+22
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ public static GroupVersionKind gvkFor(Class<? extends HasMetadata> resourceClass
3232
HasMetadata.getVersion(resourceClass), HasMetadata.getKind(resourceClass));
3333
}
3434

35+
/**
36+
* Parse GVK from a String representation. Expected format is: [group]/[version]/[kind]
37+
* <p/>
38+
* Sample: "apps/v1/Deployment"
39+
* <p/>
40+
* or: [version]/[kind]
41+
* <p/>
42+
* Sample: v1/ConfigMap
43+
**/
44+
public static GroupVersionKind fromString(String gvk) {
45+
String[] parts = gvk.split("/");
46+
if (parts.length == 3) {
47+
return new GroupVersionKind(parts[0], parts[1], parts[2]);
48+
} else if (parts.length == 2) {
49+
return new GroupVersionKind(null, parts[0], parts[1]);
50+
} else {
51+
throw new IllegalArgumentException(
52+
"Cannot parse gvk: " + gvk + ". Needs to be in form [group]/[version]/[kind]");
53+
}
54+
}
55+
3556
public String getGroup() {
3657
return group;
3758
}
@@ -72,4 +93,5 @@ public String toString() {
7293
", kind='" + kind + '\'' +
7394
'}';
7495
}
96+
7597
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/InformerConfig.java

+12
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,16 @@
100100
*/
101101
boolean followControllerNamespacesOnChange() default DEFAULT_FOLLOW_CONTROLLER_NAMESPACES_ON_CHANGE;
102102

103+
/**
104+
* The {@link io.javaoperatorsdk.operator.processing.GroupVersionKind} of the secondary resource
105+
* of this dependent resource. Used only if dependent resource is based on
106+
* {@link io.fabric8.kubernetes.api.model.GenericKubernetesResource}
107+
*
108+
* It should be in format: [group]/[version]/[kind]. Thus: apps/v1/Deployment.
109+
* <p/>
110+
* Note that using this dynamic approach only makes sense if there is no class representation for
111+
* the target resource.
112+
**/
113+
String groupVersionKind() default NO_VALUE_SET;
114+
103115
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentConverter.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.javaoperatorsdk.operator.api.config.dependent.ConfigurationConverter;
1010
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
1111
import io.javaoperatorsdk.operator.api.reconciler.Constants;
12+
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
1213
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
1314
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
1415
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
@@ -47,15 +48,28 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
4748

4849
}
4950

50-
@SuppressWarnings("unchecked")
51+
@SuppressWarnings({"unchecked", "rawtypes"})
5152
private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent configAnnotation,
5253
ControllerConfiguration<?> controllerConfig,
5354
Class<KubernetesDependentResource<R, P>> originatingClass) {
5455
try {
5556
var dependentInstance = originatingClass.getConstructor().newInstance();
5657
var resourceType = dependentInstance.resourceType();
57-
var informerConfig =
58-
InformerConfiguration.from(resourceType, controllerConfig.getResourceClass());
58+
59+
InformerConfiguration.InformerConfigurationBuilder informerConfig;
60+
if (resourceType.isAssignableFrom(GenericKubernetesDependentResource.class)) {
61+
if (configAnnotation.informerConfig() == null ||
62+
Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().groupVersionKind())) {
63+
throw new IllegalStateException(
64+
"Set groupVersionKind should be set when using GenericKubernetesResource");
65+
}
66+
informerConfig = InformerConfiguration.from(
67+
GroupVersionKind.fromString(configAnnotation.informerConfig().groupVersionKind()),
68+
controllerConfig.getResourceClass());
69+
} else {
70+
informerConfig =
71+
InformerConfiguration.from(resourceType, controllerConfig.getResourceClass());
72+
}
5973

6074
informerConfig.withSecondaryToPrimaryMapper(
6175
dependentInstance.getSecondaryToPrimaryMapper().orElse(null));
@@ -113,6 +127,7 @@ private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent
113127
if (secondaryToPrimaryMapper != null) {
114128
informerConfig.withSecondaryToPrimaryMapper(secondaryToPrimaryMapper);
115129
}
130+
116131
}
117132

118133
return informerConfig.build();

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/GroupVersionKindTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,21 @@ void testInitFromApiVersion() {
1818
assertThat(gvk.getVersion()).isEqualTo("v1");
1919
}
2020

21+
@Test
22+
void parseGVK() {
23+
var gvk = GroupVersionKind.fromString("apps/v1/Deployment");
24+
assertThat(gvk.getGroup()).isEqualTo("apps");
25+
assertThat(gvk.getVersion()).isEqualTo("v1");
26+
assertThat(gvk.getKind()).isEqualTo("Deployment");
27+
28+
29+
gvk = GroupVersionKind.fromString("v1/ConfigMap");
30+
assertThat(gvk.getGroup()).isNull();
31+
assertThat(gvk.getVersion()).isEqualTo("v1");
32+
assertThat(gvk.getKind()).isEqualTo("ConfigMap");
33+
34+
assertThrows(IllegalArgumentException.class, () -> GroupVersionKind.fromString("v1#ConfigMap"));
35+
assertThrows(IllegalArgumentException.class,
36+
() -> GroupVersionKind.fromString("api/beta/v1/ConfigMap"));
37+
}
2138
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/generickubernetesresource/generickubernetesdependentresourcemanaged/ConfigMapGenericKubernetesDependent.java

+6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
import io.javaoperatorsdk.operator.processing.dependent.Creator;
1212
import io.javaoperatorsdk.operator.processing.dependent.Updater;
1313
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.GenericKubernetesDependentResource;
14+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.InformerConfig;
15+
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
1416

17+
import static io.javaoperatorsdk.operator.sample.generickubernetesresource.generickubernetesdependentresourcemanaged.ConfigMapGenericKubernetesDependent.KIND;
18+
import static io.javaoperatorsdk.operator.sample.generickubernetesresource.generickubernetesdependentresourcemanaged.ConfigMapGenericKubernetesDependent.VERSION;
19+
20+
@KubernetesDependent(informerConfig = @InformerConfig(groupVersionKind = VERSION + "/" + KIND))
1521
public class ConfigMapGenericKubernetesDependent extends
1622
GenericKubernetesDependentResource<GenericKubernetesDependentManagedCustomResource>
1723
implements

0 commit comments

Comments
 (0)