Skip to content

Commit fc3c054

Browse files
authored
Correct logHomeLayout conversion (#3763)
1 parent b047c47 commit fc3c054

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

common/src/main/java/oracle/kubernetes/common/utils/SchemaConversionUtils.java

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public class SchemaConversionUtils {
4949
private static final String CLUSTERS = "clusters";
5050
private static final String CLUSTER_NAME = "clusterName";
5151
private static final String ACPFE = "adminChannelPortForwardingEnabled";
52-
private static final String PRESERVED = "weblogic.v8.preserved";
52+
private static final String LHL = "logHomeLayout";
53+
private static final String PRESERVED_V9 = "weblogic.v9.preserved";
54+
private static final String PRESERVED_V8 = "weblogic.v8.preserved";
5355
private static final String PRESERVED_AUX = "weblogic.v8.preserved.aux";
5456
private static final String ADDED_ACPFE = "weblogic.v8.adminChannelPortForwardingEnabled";
5557
private static final String FAILED_REASON = "weblogic.v8.failed.reason";
@@ -134,24 +136,36 @@ public Resources convertDomainSchema(Map<String, Object> domain, ResourceLookup
134136
convertDomainStatusTargetV8(domain);
135137
constantsToCamelCase(spec);
136138

137-
restore(PRESERVED, domain);
139+
try {
140+
Map<String, Object> toBePreserved = new TreeMap<>();
141+
removeAndPreserveLogHomeLayout(spec, toBePreserved);
142+
143+
preserveV9(PRESERVED_V9, domain, toBePreserved, apiVersion);
144+
} catch (IOException io) {
145+
throw new RuntimeException(io); // need to use unchecked exception because of stream processing
146+
}
147+
148+
restore(PRESERVED_V8, domain);
138149
restore(PRESERVED_AUX, domain, this::validateRestoreLegacyAuxilaryImages);
139150
removeAddedAdminChannelPortForwardingEnabled(domain);
140151
} else {
152+
restore(PRESERVED_V9, domain);
153+
141154
adjustAdminPortForwardingDefault(domain, spec, apiVersion);
142155
convertDomainStatusTargetV9(domain);
143156
convertDomainHomeInImageToDomainHomeSourceType(domain);
144157
moveConfigOverrides(domain);
145158
moveConfigOverrideSecrets(domain);
146159
constantsToCamelCase(spec);
147160
adjustReplicasDefault(spec, apiVersion);
161+
adjustLogHomeLayoutDefault(spec, apiVersion);
148162
removeWebLogicCredentialsSecretNamespace(spec, apiVersion);
149163

150164
try {
151165
Map<String, Object> toBePreserved = new TreeMap<>();
152166
removeAndPreserveLegacyAuxiliaryImages(spec, toBePreserved);
153167

154-
preserve(PRESERVED_AUX, domain, toBePreserved, apiVersion);
168+
preserveV8(PRESERVED_AUX, domain, toBePreserved, apiVersion);
155169
} catch (IOException io) {
156170
throw new RuntimeException(io); // need to use unchecked exception because of stream processing
157171
}
@@ -163,7 +177,7 @@ public Resources convertDomainSchema(Map<String, Object> domain, ResourceLookup
163177
removeAndPreserveServerStartState(spec, toBePreserved);
164178
removeAndPreserveIstio(spec, toBePreserved);
165179

166-
preserve(PRESERVED, domain, toBePreserved, apiVersion);
180+
preserveV8(PRESERVED_V8, domain, toBePreserved, apiVersion);
167181
} catch (IOException io) {
168182
throw new RuntimeException(io); // need to use unchecked exception because of stream processing
169183
}
@@ -456,8 +470,6 @@ private void constantsToCamelCase(Map<String, Object> spec) {
456470
convertServerStartPolicy((Map<String, Object>) managedServer)));
457471

458472
Optional.ofNullable(getConfiguration(spec)).ifPresent(this::convertOverrideDistributionStrategy);
459-
460-
convertLogHomeLayout(spec);
461473
}
462474

463475
private void convertServerStartPolicy(Map<String, Object> spec) {
@@ -509,19 +521,6 @@ private Object overrideDistributionStrategyCamelCase(String key, Object value) {
509521
return convertWithMap(select(overrideDistributionStrategyMap, invertOverrideDistributionStrategyMap), value);
510522
}
511523

512-
private void convertLogHomeLayout(Map<String, Object> configuration) {
513-
configuration.computeIfPresent("logHomeLayout", this::logHomeLayoutCamelCase);
514-
}
515-
516-
private static final Map<String, String> logHomeLayoutMap = Map.of(
517-
"FLAT", "Flat", "BY_SERVERS", "ByServers");
518-
519-
private static final Map<String, String> invertLogHomeLayoutMap = invertMapUsingMapper(logHomeLayoutMap);
520-
521-
private Object logHomeLayoutCamelCase(String key, Object value) {
522-
return convertWithMap(select(logHomeLayoutMap, invertLogHomeLayoutMap), value);
523-
}
524-
525524
private Map<String, Object> getConfiguration(Map<String, Object> spec) {
526525
return (Map<String, Object>) spec.get("configuration");
527526
}
@@ -768,6 +767,19 @@ private void adjustReplicasDefault(Map<String, Object> spec, String apiVersion)
768767
}
769768
}
770769

770+
private void adjustLogHomeLayoutDefault(Map<String, Object> spec, String apiVersion) {
771+
if (CommonConstants.API_VERSION_V8.equals(apiVersion)) {
772+
spec.putIfAbsent(LHL, "Flat");
773+
}
774+
}
775+
776+
private void removeAndPreserveLogHomeLayout(Map<String, Object> spec, Map<String, Object> toBePreserved) {
777+
Object existing = Optional.ofNullable(spec.remove(LHL)).orElse("ByServers");
778+
if (!"Flat".equals(existing)) {
779+
preserve(toBePreserved, "$.spec", Map.of(LHL, existing));
780+
}
781+
}
782+
771783
private void removeAndPreserveIstio(Map<String, Object> spec, Map<String, Object> toBePreserved) {
772784
Optional.ofNullable(getConfiguration(spec)).ifPresent(configuration -> {
773785
Object existing = configuration.remove("istio");
@@ -854,16 +866,27 @@ private void preserve(Map<String, Object> toBePreserved, String key, Map<String,
854866
}
855867

856868
private void preserve(String annoName, Map<String, Object> domain,
857-
Map<String, Object> toBePreserved, String apiVersion)
869+
Map<String, Object> toBePreserved, String apiVersion, boolean targetV8)
858870
throws IOException {
859-
if (!toBePreserved.isEmpty() && API_VERSION_V8.equals(apiVersion)) {
871+
if (!toBePreserved.isEmpty() && (API_VERSION_V8.equals(apiVersion) == targetV8)) {
860872
Map<String, Object> meta = getMetadata(domain);
861873
Map<String, Object> annotations = (Map<String, Object>) meta.computeIfAbsent(
862874
ANNOTATIONS, k -> new LinkedHashMap<>());
863875
annotations.put(annoName, new ObjectMapper().writeValueAsString(toBePreserved));
864876
}
865877
}
866878

879+
private void preserveV8(String annoName, Map<String, Object> domain,
880+
Map<String, Object> toBePreserved, String apiVersion) throws IOException {
881+
preserve(annoName, domain, toBePreserved, apiVersion, true);
882+
}
883+
884+
private void preserveV9(String annoName, Map<String, Object> domain,
885+
Map<String, Object> toBePreserved, String apiVersion)
886+
throws IOException {
887+
preserve(annoName, domain, toBePreserved, apiVersion, false);
888+
}
889+
867890
private void removeAddedAdminChannelPortForwardingEnabled(Map<String, Object> domain) {
868891
withAnnotation(ADDED_ACPFE, domain, labelValue -> {
869892
if ("true".equals(labelValue)) {

common/src/test/java/oracle/kubernetes/common/utils/SchemaConversionUtilsTest.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,59 @@ void testV8DomainWithOverrideDistributionStrategy_changeToCamelCase() {
467467
}
468468

469469
@Test
470-
void testV8DomainWithLogHomeLayout_changeToCamelCase() {
471-
getMapAtPath(v8Domain, "spec").put("logHomeLayout", "BY_SERVERS");
470+
void testV8Domain_addLogHomeLayout() {
471+
converter.convert(v8Domain);
472+
473+
assertThat(converter.getDomain(),
474+
hasJsonPath("$.spec.logHomeLayout", equalTo("Flat")));
475+
}
472476

477+
@Test
478+
void testV8DomainWithPreservedLogHomeLayout_restoreLogHomeLayout() {
479+
Map<String, Object> annotations = new HashMap<>();
480+
getMapAtPath(v8Domain, "metadata").put("annotations", annotations);
481+
annotations.put("weblogic.v9.preserved", "{\"$.spec\":{\"logHomeLayout\":\"ByServers\"}}");
473482
converter.convert(v8Domain);
474483

475484
assertThat(converter.getDomain(),
476-
hasJsonPath("$.spec.logHomeLayout", equalTo("ByServers")));
485+
hasJsonPath("$.spec.logHomeLayout", equalTo("ByServers")));
486+
}
487+
488+
@Test
489+
void testV9DomainWithLogHomeLayoutFlat_dropIt() throws IOException {
490+
Map<String, Object> v9Domain = readAsYaml(DOMAIN_V9_CONVERTED_LEGACY_AUX_IMAGE_YAML);
491+
getMapAtPath(v9Domain, "spec")
492+
.put("logHomeLayout", "Flat");
493+
494+
converterv8.convert(v9Domain);
495+
496+
assertThat(converterv8.getDomain(), hasNoJsonPath("$.metadata.annotations.['weblogic.v9.preserved']"));
497+
}
498+
499+
@Test
500+
void testV9DomainWithLogHomeLayoutByServers_preserveIt() throws IOException {
501+
Map<String, Object> v9Domain = readAsYaml(DOMAIN_V9_CONVERTED_LEGACY_AUX_IMAGE_YAML);
502+
getMapAtPath(v9Domain, "spec")
503+
.put("logHomeLayout", "ByServers");
504+
505+
converterv8.convert(v9Domain);
506+
507+
assertThat(converterv8.getDomain(), hasNoJsonPath("$.spec.logHomeLayout"));
508+
assertThat(converterv8.getDomain(), hasJsonPath("$.metadata.annotations.['weblogic.v9.preserved']",
509+
equalTo("{\"$.spec\":{\"logHomeLayout\":\"ByServers\"}}")));
510+
}
511+
512+
@Test
513+
void testV9DomainWithNoLogHomeLayout_preserveItAsByServers() throws IOException {
514+
Map<String, Object> v9Domain = readAsYaml(DOMAIN_V9_CONVERTED_LEGACY_AUX_IMAGE_YAML);
515+
getMapAtPath(v9Domain, "spec")
516+
.remove("logHomeLayout");
517+
518+
converterv8.convert(v9Domain);
519+
520+
assertThat(converterv8.getDomain(), hasNoJsonPath("$.spec.logHomeLayout"));
521+
assertThat(converterv8.getDomain(), hasJsonPath("$.metadata.annotations.['weblogic.v9.preserved']",
522+
equalTo("{\"$.spec\":{\"logHomeLayout\":\"ByServers\"}}")));
477523
}
478524

479525
@Test

common/src/test/resources/oracle/kubernetes/common/utils/converted-domain-sample-2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ spec:
7575
modelHome: /auxiliary/models
7676
wdtInstallHome: /auxiliary/weblogic-deploy
7777
runtimeEncryptionSecret: sample-domain1-runtime-encryption-secret
78+
logHomeLayout: Flat
7879
clusters:
7980
- name: sample-domain1-cluster-1
8081

common/src/test/resources/oracle/kubernetes/common/utils/converted-domain-sample.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ spec:
7373
modelHome: /auxiliary/models
7474
wdtInstallHome: /auxiliary/weblogic-deploy
7575
runtimeEncryptionSecret: sample-domain1-runtime-encryption-secret
76+
logHomeLayout: Flat
7677
clusters:
7778
- name: sample-domain1-cluster-1
7879

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"kind":"ConversionReview","apiVersion":"apiextensions.k8s.io/v1","response":{"uid":"177923b2-bb51-4496-9bf1-b793fdc81a88","convertedObjects":[{"apiVersion":"weblogic.oracle/v9","kind":"Domain","metadata":{"name":"sample-domain1","namespace":"sample-domain1-ns","uid":"304cd5f3-04bb-4c51-a67e-6ebcce46d936","annotations":{"weblogic.v8.adminChannelPortForwardingEnabled":"true","weblogic.v8.preserved.aux":"{\"$.spec\":{\"auxiliaryImageVolumes\":[{\"mountPath\":\"/auxiliary\",\"name\":\"auxiliaryImageVolume1\"}]},\"$.spec.serverPod\":{\"auxiliaryImages\":[{\"image\":\"model-in-image:WLS-AI-v1\",\"imagePullPolicy\":\"IfNotPresent\",\"volume\":\"auxiliaryImageVolume1\"}]}}","weblogic.v8.preserved":"{\"$.spec.adminServer\":{\"serverStartState\":\"RUNNING\"},\"$.spec.clusters[?(@.clusterName\u003d\u003d\u0027cluster-1\u0027)]\":{\"serverStartState\":\"RUNNING\"}}"}},"spec":{"adminServer":{"adminChannelPortForwardingEnabled":false},"configuration":{"model":{"domainType":"WLS","modelHome":"/auxiliary/models","runtimeEncryptionSecret":"sample-domain1-runtime-encryption-secret","wdtInstallHome":"/auxiliary/weblogic-deploy"}},"domainHome":"/u01/domains/sample-domain1","domainHomeSourceType":"FromModel","image":"container-registry.oracle.com/middleware/weblogic:12.2.1.4","imagePullPolicy":"IfNotPresent","includeServerOutInPodLog":true,"introspectVersion":"1","replicas":1,"restartVersion":"1","serverPod":{"env":[{"name":"CUSTOM_DOMAIN_NAME","value":"domain1"},{"name":"JAVA_OPTIONS","value":"-Dweblogic.StdoutDebugEnabled\u003dfalse"},{"name":"USER_MEM_ARGS","value":"-Djava.security.egd\u003dfile:/dev/./urandom -Xms256m -Xmx512m "},{"name":"AUXILIARY_IMAGE_PATHS","value":"/auxiliary"}],"resources":{"requests":{"cpu":"250m","memory":"768Mi"}},"volumes":[{"name":"compat-ai-vol-auxiliaryimagevolume1","emptyDir":{}}],"initContainers":[{"name":"compat-operator-aux-container1","image":"model-in-image:WLS-AI-v1","command":["/weblogic-operator/scripts/auxImage.sh"],"imagePullPolicy":"IfNotPresent","env":[{"name":"AUXILIARY_IMAGE_PATH","value":"/auxiliary"},{"name":"AUXILIARY_IMAGE_TARGET_PATH","value":"/tmpAuxiliaryImage"},{"name":"AUXILIARY_IMAGE_COMMAND","value":"cp -R $AUXILIARY_IMAGE_PATH/* $AUXILIARY_IMAGE_TARGET_PATH"},{"name":"AUXILIARY_IMAGE_CONTAINER_IMAGE","value":"model-in-image:WLS-AI-v1"},{"name":"AUXILIARY_IMAGE_CONTAINER_NAME","value":"compat-operator-aux-container1"}],"volumeMounts":[{"name":"compat-ai-vol-auxiliaryimagevolume1","mountPath":"/tmpAuxiliaryImage"},{"name":"weblogic-scripts-cm-volume","mountPath":"/weblogic-operator/scripts"}]}],"volumeMounts":[{"name":"compat-ai-vol-auxiliaryimagevolume1","mountPath":"/auxiliary"}]},"serverStartPolicy":"IfNeeded","webLogicCredentialsSecret":{"name":"sample-domain1-weblogic-credentials"},"clusters":[{"name":"sample-domain1-cluster-1"}]},"status":{"conditions":[{"lastTransitionTime":"2022-03-11T19:41:08.794468Z","status":"False","type":"Completed"}]}}],"result":{"status":"Success"}}}
1+
{"kind":"ConversionReview","apiVersion":"apiextensions.k8s.io/v1","response":{"uid":"177923b2-bb51-4496-9bf1-b793fdc81a88","convertedObjects":[{"apiVersion":"weblogic.oracle/v9","kind":"Domain","metadata":{"name":"sample-domain1","namespace":"sample-domain1-ns","uid":"304cd5f3-04bb-4c51-a67e-6ebcce46d936","annotations":{"weblogic.v8.adminChannelPortForwardingEnabled":"true","weblogic.v8.preserved.aux":"{\"$.spec\":{\"auxiliaryImageVolumes\":[{\"mountPath\":\"/auxiliary\",\"name\":\"auxiliaryImageVolume1\"}]},\"$.spec.serverPod\":{\"auxiliaryImages\":[{\"image\":\"model-in-image:WLS-AI-v1\",\"imagePullPolicy\":\"IfNotPresent\",\"volume\":\"auxiliaryImageVolume1\"}]}}","weblogic.v8.preserved":"{\"$.spec.adminServer\":{\"serverStartState\":\"RUNNING\"},\"$.spec.clusters[?(@.clusterName\u003d\u003d\u0027cluster-1\u0027)]\":{\"serverStartState\":\"RUNNING\"}}"}},"spec":{"adminServer":{"adminChannelPortForwardingEnabled":false},"configuration":{"model":{"domainType":"WLS","modelHome":"/auxiliary/models","runtimeEncryptionSecret":"sample-domain1-runtime-encryption-secret","wdtInstallHome":"/auxiliary/weblogic-deploy"}},"domainHome":"/u01/domains/sample-domain1","domainHomeSourceType":"FromModel","image":"container-registry.oracle.com/middleware/weblogic:12.2.1.4","imagePullPolicy":"IfNotPresent","includeServerOutInPodLog":true,"introspectVersion":"1","replicas":1,"restartVersion":"1","serverPod":{"env":[{"name":"CUSTOM_DOMAIN_NAME","value":"domain1"},{"name":"JAVA_OPTIONS","value":"-Dweblogic.StdoutDebugEnabled\u003dfalse"},{"name":"USER_MEM_ARGS","value":"-Djava.security.egd\u003dfile:/dev/./urandom -Xms256m -Xmx512m "},{"name":"AUXILIARY_IMAGE_PATHS","value":"/auxiliary"}],"resources":{"requests":{"cpu":"250m","memory":"768Mi"}},"volumes":[{"name":"compat-ai-vol-auxiliaryimagevolume1","emptyDir":{}}],"initContainers":[{"name":"compat-operator-aux-container1","image":"model-in-image:WLS-AI-v1","command":["/weblogic-operator/scripts/auxImage.sh"],"imagePullPolicy":"IfNotPresent","env":[{"name":"AUXILIARY_IMAGE_PATH","value":"/auxiliary"},{"name":"AUXILIARY_IMAGE_TARGET_PATH","value":"/tmpAuxiliaryImage"},{"name":"AUXILIARY_IMAGE_COMMAND","value":"cp -R $AUXILIARY_IMAGE_PATH/* $AUXILIARY_IMAGE_TARGET_PATH"},{"name":"AUXILIARY_IMAGE_CONTAINER_IMAGE","value":"model-in-image:WLS-AI-v1"},{"name":"AUXILIARY_IMAGE_CONTAINER_NAME","value":"compat-operator-aux-container1"}],"volumeMounts":[{"name":"compat-ai-vol-auxiliaryimagevolume1","mountPath":"/tmpAuxiliaryImage"},{"name":"weblogic-scripts-cm-volume","mountPath":"/weblogic-operator/scripts"}]}],"volumeMounts":[{"name":"compat-ai-vol-auxiliaryimagevolume1","mountPath":"/auxiliary"}]},"serverStartPolicy":"IfNeeded","webLogicCredentialsSecret":{"name":"sample-domain1-weblogic-credentials"},"logHomeLayout":"Flat","clusters":[{"name":"sample-domain1-cluster-1"}]},"status":{"conditions":[{"lastTransitionTime":"2022-03-11T19:41:08.794468Z","status":"False","type":"Completed"}]}}],"result":{"status":"Success"}}}

0 commit comments

Comments
 (0)