Skip to content

Commit f5daa66

Browse files
committed
allow different startup policies for domains, servers and clusters
1 parent 8c6b9c5 commit f5daa66

File tree

10 files changed

+234
-79
lines changed

10 files changed

+234
-79
lines changed

docs/domains/Domain.json

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
"$ref": "#/definitions/ServerPod"
2323
},
2424
"serverStartPolicy": {
25-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
25+
"description": "The strategy for deciding whether to start a server. Legal values are ALWAYS, NEVER, or IF_NEEDED.",
2626
"type": "string",
2727
"enum": [
28-
"NEVER",
2928
"ALWAYS",
30-
"IF_NEEDED",
31-
"ADMIN_ONLY"
29+
"NEVER",
30+
"IF_NEEDED"
3231
]
3332
},
3433
"restartVersion": {
@@ -110,13 +109,11 @@
110109
"$ref": "#/definitions/KubernetesResource"
111110
},
112111
"serverStartPolicy": {
113-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
112+
"description": "The strategy for deciding whether to start a server. Legal values are NEVER, or IF_NEEDED.",
114113
"type": "string",
115114
"enum": [
116115
"NEVER",
117-
"ALWAYS",
118-
"IF_NEEDED",
119-
"ADMIN_ONLY"
116+
"IF_NEEDED"
120117
]
121118
},
122119
"restartVersion": {
@@ -218,11 +215,10 @@
218215
"type": "string"
219216
},
220217
"serverStartPolicy": {
221-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
218+
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, or IF_NEEDED.",
222219
"type": "string",
223220
"enum": [
224221
"NEVER",
225-
"ALWAYS",
226222
"IF_NEEDED",
227223
"ADMIN_ONLY"
228224
]
@@ -350,13 +346,12 @@
350346
"type": "string"
351347
},
352348
"serverStartPolicy": {
353-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
349+
"description": "The strategy for deciding whether to start a server. Legal values are ALWAYS, NEVER, or IF_NEEDED.",
354350
"type": "string",
355351
"enum": [
356-
"NEVER",
357352
"ALWAYS",
358-
"IF_NEEDED",
359-
"ADMIN_ONLY"
353+
"NEVER",
354+
"IF_NEEDED"
360355
]
361356
},
362357
"restartVersion": {

docs/domains/index.html

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,12 @@
942942
"$ref": "#/definitions/ServerPod"
943943
},
944944
"serverStartPolicy": {
945-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
945+
"description": "The strategy for deciding whether to start a server. Legal values are ALWAYS, NEVER, or IF_NEEDED.",
946946
"type": "string",
947947
"enum": [
948-
"NEVER",
949948
"ALWAYS",
950-
"IF_NEEDED",
951-
"ADMIN_ONLY"
949+
"NEVER",
950+
"IF_NEEDED"
952951
]
953952
},
954953
"restartVersion": {
@@ -1030,13 +1029,11 @@
10301029
"$ref": "#/definitions/KubernetesResource"
10311030
},
10321031
"serverStartPolicy": {
1033-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
1032+
"description": "The strategy for deciding whether to start a server. Legal values are NEVER, or IF_NEEDED.",
10341033
"type": "string",
10351034
"enum": [
10361035
"NEVER",
1037-
"ALWAYS",
1038-
"IF_NEEDED",
1039-
"ADMIN_ONLY"
1036+
"IF_NEEDED"
10401037
]
10411038
},
10421039
"restartVersion": {
@@ -1138,11 +1135,10 @@
11381135
"type": "string"
11391136
},
11401137
"serverStartPolicy": {
1141-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
1138+
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, or IF_NEEDED.",
11421139
"type": "string",
11431140
"enum": [
11441141
"NEVER",
1145-
"ALWAYS",
11461142
"IF_NEEDED",
11471143
"ADMIN_ONLY"
11481144
]
@@ -1270,13 +1266,12 @@
12701266
"type": "string"
12711267
},
12721268
"serverStartPolicy": {
1273-
"description": "The strategy for deciding whether to start a server. Legal values are ADMIN_ONLY, NEVER, ALWAYS, or IF_NEEDED.",
1269+
"description": "The strategy for deciding whether to start a server. Legal values are ALWAYS, NEVER, or IF_NEEDED.",
12741270
"type": "string",
12751271
"enum": [
1276-
"NEVER",
12771272
"ALWAYS",
1278-
"IF_NEEDED",
1279-
"ADMIN_ONLY"
1273+
"NEVER",
1274+
"IF_NEEDED"
12801275
]
12811276
},
12821277
"restartVersion": {

json-schema/src/main/java/oracle/kubernetes/json/EnumClass.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
@Target(FIELD)
1616
public @interface EnumClass {
1717
Class<? extends java.lang.Enum> value();
18+
19+
String qualifier() default "";
1820
}

json-schema/src/main/java/oracle/kubernetes/json/SchemaGenerator.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.IOException;
1212
import java.io.InputStreamReader;
1313
import java.lang.reflect.Field;
14+
import java.lang.reflect.InvocationTargetException;
15+
import java.lang.reflect.Method;
1416
import java.lang.reflect.Modifier;
1517
import java.net.MalformedURLException;
1618
import java.net.URL;
@@ -218,7 +220,7 @@ private String getDescription(Field field) {
218220

219221
private void addStringRestrictions(Map<String, Object> result, Field field) {
220222
Class<? extends Enum> enumClass = getEnumClass(field);
221-
if (enumClass != null) addEnumValues(result, enumClass);
223+
if (enumClass != null) addEnumValues(result, enumClass, getEnumQualifier(field));
222224

223225
String pattern = getPattern(field);
224226
if (pattern != null) result.put("pattern", pattern);
@@ -229,9 +231,14 @@ private Class<? extends java.lang.Enum> getEnumClass(Field field) {
229231
return annotation != null ? annotation.value() : null;
230232
}
231233

234+
private String getEnumQualifier(Field field) {
235+
EnumClass annotation = field.getAnnotation(EnumClass.class);
236+
return annotation != null ? annotation.qualifier() : "";
237+
}
238+
232239
private void addEnumValues(
233-
Map<String, Object> result, Class<? extends java.lang.Enum> enumClass) {
234-
result.put("enum", getEnumValues(enumClass));
240+
Map<String, Object> result, Class<? extends Enum> enumClass, String qualifier) {
241+
result.put("enum", getEnumValues(enumClass, qualifier));
235242
}
236243

237244
private String getPattern(Field field) {
@@ -254,11 +261,12 @@ private class SubSchemaGenerator {
254261
this.field = field;
255262
}
256263

264+
@SuppressWarnings("unchecked")
257265
private void generateTypeIn(Map<String, Object> result, Class<?> type) {
258266
if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) result.put("type", "boolean");
259267
else if (isNumeric(type)) result.put("type", "number");
260268
else if (isString(type)) result.put("type", "string");
261-
else if (type.isEnum()) generateEnumTypeIn(result, type);
269+
else if (type.isEnum()) generateEnumTypeIn(result, (Class<? extends Enum>) type);
262270
else if (type.isArray()) this.generateArrayTypeIn(result, type);
263271
else if (Collection.class.isAssignableFrom(type)) generateCollectionTypeIn(result);
264272
else generateObjectFieldIn(result, type);
@@ -335,21 +343,45 @@ private String getDefinitionKey(Class<?> type) {
335343
return type.getSimpleName();
336344
}
337345

338-
private void generateEnumTypeIn(Map<String, Object> result, Class<?> enumType) {
346+
private void generateEnumTypeIn(Map<String, Object> result, Class<? extends Enum> enumType) {
339347
result.put("type", "string");
340-
result.put("enum", getEnumValues(enumType));
348+
addEnumValues(result, enumType, "");
341349
}
342350

343-
private String[] getEnumValues(Class<?> enumType) {
351+
private String[] getEnumValues(Class<?> enumType, String qualifier) {
344352
List<String> values = new ArrayList<>();
353+
Method qualifierMethod = getQualifierMethod(enumType, qualifier);
345354

346355
for (Object enumConstant : enumType.getEnumConstants()) {
347-
values.add(enumConstant.toString());
356+
if (satisfiesQualifier(enumConstant, qualifierMethod)) values.add(enumConstant.toString());
348357
}
349358

350359
return values.toArray(new String[0]);
351360
}
352361

362+
private Method getQualifierMethod(Class<?> enumType, String methodName) {
363+
try {
364+
Method method = enumType.getDeclaredMethod(methodName);
365+
if (!isBooleanMethod(method)) return null;
366+
return method;
367+
} catch (NoSuchMethodException e) {
368+
return null;
369+
}
370+
}
371+
372+
private boolean isBooleanMethod(Method method) {
373+
return method.getReturnType().equals(Boolean.class)
374+
|| method.getReturnType().equals(boolean.class);
375+
}
376+
377+
private boolean satisfiesQualifier(Object enumConstant, Method qualifier) {
378+
try {
379+
return qualifier == null || (Boolean) qualifier.invoke(enumConstant);
380+
} catch (IllegalAccessException | InvocationTargetException e) {
381+
return true;
382+
}
383+
}
384+
353385
private void generateObjectTypeIn(Map<String, Object> result, Class<?> type) {
354386
Map<String, Object> properties = new HashMap<>();
355387
List<String> requiredFields = new ArrayList<>();

json-schema/src/test/java/oracle/kubernetes/json/SchemaGeneratorTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,17 @@ public void generateSchemaForEnum() throws NoSuchFieldException {
102102
@SuppressWarnings("unused")
103103
private enum TrafficLightColors {
104104
RED,
105-
YELLOW,
106-
GREEN
105+
YELLOW {
106+
@Override
107+
boolean forSmallLight() {
108+
return false;
109+
}
110+
},
111+
GREEN;
112+
113+
boolean forSmallLight() {
114+
return true;
115+
}
107116
}
108117

109118
@SuppressWarnings("unused")
@@ -123,6 +132,19 @@ public void generateSchemaForEnumAnnotatedString() throws NoSuchFieldException {
123132
@EnumClass(TrafficLightColors.class)
124133
private String colorString;
125134

135+
@Test
136+
public void generateSchemaForEnumAnnotatedStringWithQualifier() throws NoSuchFieldException {
137+
Object schema = generateForField(getClass().getDeclaredField("twoColorString"));
138+
139+
assertThat(schema, hasJsonPath("$.twoColorString.type", equalTo("string")));
140+
assertThat(
141+
schema, hasJsonPath("$.twoColorString.enum", arrayContainingInAnyOrder("RED", "GREEN")));
142+
}
143+
144+
@SuppressWarnings("unused")
145+
@EnumClass(value = TrafficLightColors.class, qualifier = "forSmallLight")
146+
private String twoColorString;
147+
126148
@Test
127149
public void whenIntegerAnnotatedWithMinimumOnly_addToSchema() throws NoSuchFieldException {
128150
Object schema = generateForField(getClass().getDeclaredField("valueWithMinimum"));

model/src/main/java/oracle/kubernetes/operator/ServerStartPolicy.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,42 @@
55
package oracle.kubernetes.operator;
66

77
public enum ServerStartPolicy {
8+
ALWAYS {
9+
@Override
10+
public boolean forDomain() {
11+
return false;
12+
}
13+
14+
@Override
15+
public boolean forCluster() {
16+
return false;
17+
}
18+
},
819
NEVER,
9-
ALWAYS,
1020
IF_NEEDED,
11-
ADMIN_ONLY;
21+
ADMIN_ONLY {
22+
@Override
23+
public boolean forCluster() {
24+
return false;
25+
}
26+
27+
@Override
28+
public boolean forServer() {
29+
return false;
30+
}
31+
};
32+
33+
public boolean forDomain() {
34+
return true;
35+
}
36+
37+
public boolean forCluster() {
38+
return true;
39+
}
40+
41+
public boolean forServer() {
42+
return true;
43+
}
1244

1345
public static ServerStartPolicy getDefaultPolicy() {
1446
return IF_NEEDED;

0 commit comments

Comments
 (0)