Skip to content

Commit 6fa7078

Browse files
authored
Backport selected changes to CRD helper and introspector job/pod helper from PR2816 to 3.4 branch (#2842)
* Backport selected changes from PR2816 to 3.4 branch to allow 3.4 and 4.0 operators to coexist.
1 parent 906b5d9 commit 6fa7078

File tree

9 files changed

+104
-21
lines changed

9 files changed

+104
-21
lines changed

operator/src/main/java/oracle/kubernetes/operator/DomainResourcesValidation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void addPodDisruptionBudget(V1beta1PodDisruptionBudget pdb) {
123123
}
124124

125125
private void addDomainList(DomainList list) {
126-
list.getItems().forEach(this::addDomain);
126+
Optional.ofNullable(list).ifPresent(l -> l.getItems().forEach(this::addDomain));
127127
}
128128

129129
private void addDomain(Domain domain) {

operator/src/main/java/oracle/kubernetes/operator/ProcessingConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,8 @@ public interface ProcessingConstants {
6767
String FATAL_ERROR_DOMAIN_STATUS_MESSAGE = "Introspection encountered a fatal error and will not retry automatically."
6868
+ " Please resolve the error and then update 'domain.spec.introspectVersion' to force a retry.";
6969

70+
String WEBHOOK = "Webhook";
71+
72+
String COMPATIBILITY_MODE = "compatibility-mode-";
7073

7174
}

operator/src/main/java/oracle/kubernetes/operator/helpers/CrdHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import oracle.kubernetes.weblogic.domain.model.DomainStatus;
5050
import org.yaml.snakeyaml.constructor.SafeConstructor;
5151

52+
import static oracle.kubernetes.operator.ProcessingConstants.WEBHOOK;
5253
import static oracle.kubernetes.weblogic.domain.model.CrdSchemaGenerator.createCrdSchemaGenerator;
5354

5455
/** Helper class to ensure Domain CRD is created. */
@@ -293,6 +294,11 @@ ResponseStep<V1CustomResourceDefinition> createCreateResponseStep(Step next) {
293294
return new CreateResponseStep(next);
294295
}
295296

297+
private boolean existingCrdContainsConversionWebhook(V1CustomResourceDefinition existingCrd) {
298+
return existingCrd.getSpec().getConversion() != null
299+
&& existingCrd.getSpec().getConversion().getStrategy().equalsIgnoreCase(WEBHOOK);
300+
}
301+
296302
private boolean isOutdatedCrd(V1CustomResourceDefinition existingCrd) {
297303
return COMPARATOR.isOutdatedCrd(mainDelegate.getProductVersion(), existingCrd, this.model);
298304
}
@@ -353,6 +359,8 @@ public NextAction onSuccess(
353359

354360
if (existingCrd == null) {
355361
return doNext(createCrd(getNext()), packet);
362+
} else if (existingCrdContainsConversionWebhook(existingCrd)) {
363+
return doNext(packet);
356364
} else if (isOutdatedCrd(existingCrd)) {
357365
return doNext(updateCrd(getNext(), existingCrd), packet);
358366
} else if (!existingCrdContainsVersion(existingCrd)) {

operator/src/main/java/oracle/kubernetes/operator/helpers/JobHelper.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import static oracle.kubernetes.operator.DomainStatusUpdater.recordLastIntrospectJobProcessedUid;
6363
import static oracle.kubernetes.operator.LabelConstants.INTROSPECTION_DOMAIN_SPEC_GENERATION;
6464
import static oracle.kubernetes.operator.LabelConstants.INTROSPECTION_STATE_LABEL;
65+
import static oracle.kubernetes.operator.ProcessingConstants.COMPATIBILITY_MODE;
6566
import static oracle.kubernetes.operator.ProcessingConstants.DOMAIN_INTROSPECT_REQUESTED;
6667
import static oracle.kubernetes.operator.logging.MessageKeys.INTROSPECTOR_JOB_FAILED;
6768
import static oracle.kubernetes.operator.logging.MessageKeys.INTROSPECTOR_JOB_FAILED_DETAIL;
@@ -279,12 +280,29 @@ String getJobName() {
279280

280281
@Override
281282
protected List<V1Volume> getAdditionalVolumes() {
282-
return getDomain().getSpec().getAdditionalVolumes();
283+
List<V1Volume> volumes = getDomain().getSpec().getAdditionalVolumes();
284+
getServerSpec().getAdditionalVolumes().stream().forEach(volume -> addVolumeIfMissing(volume, volumes));
285+
return volumes;
286+
}
287+
288+
private void addVolumeIfMissing(V1Volume volume, List<V1Volume> volumes) {
289+
if (!volumes.contains(volume) && volume.getName().startsWith(COMPATIBILITY_MODE)) {
290+
volumes.add(volume);
291+
}
283292
}
284293

285294
@Override
286295
protected List<V1VolumeMount> getAdditionalVolumeMounts() {
287-
return getDomain().getSpec().getAdditionalVolumeMounts();
296+
List<V1VolumeMount> volumeMounts = getDomain().getSpec().getAdditionalVolumeMounts();
297+
getServerSpec().getAdditionalVolumeMounts().stream()
298+
.forEach(mount -> addVolumeMountIfMissing(mount, volumeMounts));
299+
return volumeMounts;
300+
}
301+
302+
private void addVolumeMountIfMissing(V1VolumeMount mount, List<V1VolumeMount> volumeMounts) {
303+
if (!volumeMounts.contains(mount) && mount.getName().startsWith(COMPATIBILITY_MODE)) {
304+
volumeMounts.add(mount);
305+
}
288306
}
289307

290308
private String getAsName() {

operator/src/main/java/oracle/kubernetes/operator/helpers/JobStepContext.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import java.util.List;
1010
import java.util.Map;
1111
import java.util.Optional;
12+
import java.util.stream.Collectors;
1213
import java.util.stream.IntStream;
1314

1415
import io.kubernetes.client.openapi.models.V1ConfigMapVolumeSource;
1516
import io.kubernetes.client.openapi.models.V1Container;
17+
import io.kubernetes.client.openapi.models.V1EnvVar;
1618
import io.kubernetes.client.openapi.models.V1Job;
1719
import io.kubernetes.client.openapi.models.V1JobSpec;
1820
import io.kubernetes.client.openapi.models.V1ObjectMeta;
@@ -42,6 +44,7 @@
4244
import oracle.kubernetes.weblogic.domain.model.DomainSpec;
4345
import oracle.kubernetes.weblogic.domain.model.ServerSpec;
4446

47+
import static oracle.kubernetes.operator.ProcessingConstants.COMPATIBILITY_MODE;
4548
import static oracle.kubernetes.utils.OperatorUtils.emptyToNull;
4649

4750
public abstract class JobStepContext extends BasePodStepContext {
@@ -294,8 +297,7 @@ private V1PodTemplateSpec createPodTemplateSpec(TuningParameters tuningParameter
294297
V1PodTemplateSpec podTemplateSpec = new V1PodTemplateSpec()
295298
.metadata(createPodTemplateMetadata())
296299
.spec(createPodSpec(tuningParameters));
297-
Optional.ofNullable(getServerSpec().getAuxiliaryImages()).ifPresent(auxiliaryImages ->
298-
addAuxiliaryImageInitContainers(podTemplateSpec.getSpec(), auxiliaryImages));
300+
addInitContainers(podTemplateSpec.getSpec(), tuningParameters);
299301
addEmptyDirVolume(podTemplateSpec.getSpec(), info.getDomain().getAuxiliaryImageVolumes());
300302

301303
return updateForDeepSubstitution(podTemplateSpec.getSpec(), podTemplateSpec);
@@ -314,15 +316,33 @@ private V1ObjectMeta createPodTemplateMetadata() {
314316
return metadata;
315317
}
316318

317-
protected V1PodSpec addAuxiliaryImageInitContainers(V1PodSpec podSpec, List<AuxiliaryImage> auxiliaryImages) {
318-
Optional.ofNullable(auxiliaryImages).ifPresent(cl -> addInitContainers(podSpec, cl));
319+
private V1PodSpec addInitContainers(V1PodSpec podSpec, TuningParameters tuningParameters) {
320+
List<V1Container> initContainers = new ArrayList<>();
321+
Optional.ofNullable(getAuxiliaryImages()).ifPresent(auxImages -> addInitContainers(initContainers, auxImages));
322+
initContainers.addAll(getAdditionalInitContainers().stream()
323+
.filter(container -> container.getName().startsWith(COMPATIBILITY_MODE))
324+
.map(c -> c.env(createEnv(c, tuningParameters))).collect(Collectors.toList()));
325+
podSpec.initContainers(initContainers);
319326
return podSpec;
320327
}
321328

322-
private V1PodSpec addInitContainers(V1PodSpec podSpec, List<AuxiliaryImage> auxiliaryImages) {
329+
private void addInitContainers(List<V1Container> initContainers, List<AuxiliaryImage> auxiliaryImages) {
323330
IntStream.range(0, auxiliaryImages.size()).forEach(idx ->
324-
podSpec.addInitContainersItem(createInitContainerForAuxiliaryImage(auxiliaryImages.get(idx), idx)));
325-
return podSpec;
331+
initContainers.add(createInitContainerForAuxiliaryImage(auxiliaryImages.get(idx), idx)));
332+
}
333+
334+
protected List<V1EnvVar> createEnv(V1Container c, TuningParameters tuningParameters) {
335+
List<V1EnvVar> initContainerEnvVars = new ArrayList<>();
336+
Optional.ofNullable(c.getEnv()).ifPresent(initContainerEnvVars::addAll);
337+
if (!c.getName().startsWith(COMPATIBILITY_MODE)) {
338+
getEnvironmentVariables(tuningParameters).stream()
339+
.forEach(var -> addIfMissing(initContainerEnvVars, var.getName(), var.getValue(), var.getValueFrom()));
340+
}
341+
return initContainerEnvVars;
342+
}
343+
344+
private List<V1Container> getAdditionalInitContainers() {
345+
return getServerSpec().getInitContainers();
326346
}
327347

328348
protected V1PodSpec createPodSpec(TuningParameters tuningParameters) {

operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import static oracle.kubernetes.operator.LabelConstants.MII_UPDATED_RESTART_REQUIRED_LABEL;
8585
import static oracle.kubernetes.operator.LabelConstants.MODEL_IN_IMAGE_DOMAINZIP_HASH;
8686
import static oracle.kubernetes.operator.LabelConstants.OPERATOR_VERSION;
87+
import static oracle.kubernetes.operator.ProcessingConstants.COMPATIBILITY_MODE;
8788
import static oracle.kubernetes.operator.ProcessingConstants.DOMAIN_ROLL_START_EVENT_GENERATED;
8889
import static oracle.kubernetes.operator.ProcessingConstants.MII_DYNAMIC_UPDATE;
8990
import static oracle.kubernetes.operator.ProcessingConstants.MII_DYNAMIC_UPDATE_SUCCESS;
@@ -93,6 +94,7 @@
9394
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.DOMAIN_ROLL_STARTING;
9495
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.POD_CYCLE_STARTING;
9596
import static oracle.kubernetes.operator.helpers.LegalNames.LEGAL_CONTAINER_PORT_NAME_MAX_LENGTH;
97+
import static oracle.kubernetes.weblogic.domain.model.Model.DEFAULT_WDT_INSTALL_HOME;
9698

9799
public abstract class PodStepContext extends BasePodStepContext {
98100

@@ -861,8 +863,10 @@ protected void getAuxiliaryImageInitContainers(List<AuxiliaryImage> auxiliaryIma
861863
private List<V1EnvVar> createEnv(V1Container c, TuningParameters tuningParameters) {
862864
List<V1EnvVar> initContainerEnvVars = new ArrayList<>();
863865
Optional.ofNullable(c.getEnv()).ifPresent(initContainerEnvVars::addAll);
864-
getEnvironmentVariables(tuningParameters).forEach(envVar ->
865-
addIfMissing(initContainerEnvVars, envVar.getName(), envVar.getValue(), envVar.getValueFrom()));
866+
if (!c.getName().startsWith(COMPATIBILITY_MODE)) {
867+
getEnvironmentVariables(tuningParameters).forEach(envVar ->
868+
addIfMissing(initContainerEnvVars, envVar.getName(), envVar.getValue(), envVar.getValueFrom()));
869+
}
866870
return initContainerEnvVars;
867871
}
868872

@@ -958,6 +962,10 @@ void addStartupEnvVars(List<V1EnvVar> vars, TuningParameters tuningParameters) {
958962
addEnvVar(vars, ServerEnvVars.SERVICE_NAME, LegalNames.toServerServiceName(getDomainUid(), getServerName()));
959963
addEnvVar(vars, ServerEnvVars.AS_SERVICE_NAME, LegalNames.toServerServiceName(getDomainUid(), getAsName()));
960964
Optional.ofNullable(getDataHome()).ifPresent(v -> addEnvVar(vars, ServerEnvVars.DATA_HOME, v));
965+
String wdtInstallHome = getWdtInstallHome();
966+
if (wdtInstallHome != null && !wdtInstallHome.isEmpty() && !wdtInstallHome.equals(DEFAULT_WDT_INSTALL_HOME)) {
967+
addEnvVar(vars, IntrospectorJobEnvVars.WDT_INSTALL_HOME, wdtInstallHome);
968+
}
961969
Optional.ofNullable(getServerSpec().getAuxiliaryImages()).ifPresent(cm -> addAuxiliaryImageEnv(cm, vars));
962970
addEnvVarIfTrue(mockWls(), vars, "MOCK_WLS");
963971
Optional.ofNullable(getKubernetesPlatform(tuningParameters)).ifPresent(v ->
@@ -966,8 +974,6 @@ void addStartupEnvVars(List<V1EnvVar> vars, TuningParameters tuningParameters) {
966974

967975
protected void addAuxiliaryImageEnv(List<AuxiliaryImage> auxiliaryImageList, List<V1EnvVar> vars) {
968976
Optional.ofNullable(auxiliaryImageList).ifPresent(auxiliaryImages -> {
969-
addEnvVar(vars, IntrospectorJobEnvVars.WDT_INSTALL_HOME, getWdtInstallHome());
970-
addEnvVar(vars, IntrospectorJobEnvVars.WDT_MODEL_HOME, getModelHome());
971977
Optional.ofNullable(getAuxiliaryImagePaths(auxiliaryImageList, getDomain().getAuxiliaryImageVolumes()))
972978
.ifPresent(c -> addEnvVar(vars, AuxiliaryImageEnvVars.AUXILIARY_IMAGE_PATHS, c));
973979
});

operator/src/main/java/oracle/kubernetes/weblogic/domain/model/Model.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import org.apache.commons.lang3.builder.ToStringBuilder;
1515

1616
public class Model {
17-
static final String DEFAULT_WDT_MODEL_HOME = "/u01/wdt/models";
18-
static final String DEFAULT_WDT_INSTALL_HOME = "/u01/wdt/weblogic-deploy";
17+
public static final String DEFAULT_WDT_MODEL_HOME = "/u01/wdt/models";
18+
public static final String DEFAULT_WDT_INSTALL_HOME = "/u01/wdt/weblogic-deploy";
1919

2020
@EnumClass(value = ModelInImageDomainType.class)
2121
@Description("WebLogic Deploy Tooling domain type. Legal values: WLS, RestrictedJRF, JRF. Defaults to WLS.")

operator/src/test/java/oracle/kubernetes/operator/helpers/CrdHelperTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.meterware.simplestub.Memento;
2020
import com.meterware.simplestub.StaticStubSupport;
21+
import io.kubernetes.client.openapi.models.V1CustomResourceConversion;
2122
import io.kubernetes.client.openapi.models.V1CustomResourceDefinition;
2223
import io.kubernetes.client.openapi.models.V1CustomResourceDefinitionNames;
2324
import io.kubernetes.client.openapi.models.V1CustomResourceDefinitionSpec;
@@ -199,6 +200,19 @@ private SemanticVersion getProductVersionFromMetadata(V1ObjectMeta metadata) {
199200
.orElse(null);
200201
}
201202

203+
@Test
204+
void whenExistingCrdHasFutureVersionWithConversionWebhook_dontReplaceIt() {
205+
MainDelegateStub delegate = createStrictStub(MainDelegateStub.class, KUBERNETES_VERSION_16, PRODUCT_VERSION_FUTURE);
206+
207+
V1CustomResourceDefinition existing = defineCrd(PRODUCT_VERSION_FUTURE);
208+
existing.getSpec().addVersionsItem(
209+
new V1CustomResourceDefinitionVersion().served(true).name(KubernetesConstants.DOMAIN_VERSION))
210+
.conversion(new V1CustomResourceConversion().strategy("Webhook"));
211+
testSupport.defineResources(existing);
212+
213+
testSupport.runSteps(CrdHelper.createDomainCrdStep(delegate));
214+
}
215+
202216
@Test
203217
void whenExistingCrdHasFutureVersion_dontReplaceIt() {
204218
V1CustomResourceDefinition existing = defineCrd(PRODUCT_VERSION_FUTURE);

operator/src/test/java/oracle/kubernetes/operator/helpers/JobHelperTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static oracle.kubernetes.operator.DomainProcessorTestSetup.NS;
6262
import static oracle.kubernetes.operator.DomainProcessorTestSetup.UID;
6363
import static oracle.kubernetes.operator.DomainProcessorTestSetup.createTestDomain;
64+
import static oracle.kubernetes.operator.ProcessingConstants.COMPATIBILITY_MODE;
6465
import static oracle.kubernetes.operator.ProcessingConstants.DOMAIN_TOPOLOGY;
6566
import static oracle.kubernetes.operator.ProcessingConstants.JOBWATCHER_COMPONENT_NAME;
6667
import static oracle.kubernetes.operator.helpers.BasePodStepContext.KUBERNETES_PLATFORM_HELM_VARIABLE;
@@ -754,15 +755,28 @@ void introspectorPodSpec_createdWithoutConfiguredReadinessGates() {
754755
@Test
755756
void introspectorPodSpec_createdWithoutConfiguredInitContainers() {
756757
configureDomain()
757-
.withInitContainer(
758-
createContainer(
759-
"container1", "busybox", "sh", "-c", "echo managed server && sleep 120"));
758+
.withInitContainer(
759+
createContainer(
760+
"container1", "busybox", "sh", "-c", "echo managed server && sleep 120"));
760761

761762
V1JobSpec jobSpec = createJobSpec();
762763

763764
assertThat(
764-
getPodSpec(jobSpec).getInitContainers(),
765-
nullValue());
765+
getPodSpec(jobSpec).getInitContainers().size(), equalTo(0));
766+
}
767+
768+
@Test
769+
void introspectorPodSpec_createdWithCompatibilityAuxImageInitContainers() {
770+
configureDomain()
771+
.withInitContainer(
772+
createContainer(
773+
COMPATIBILITY_MODE + "aux-image-container", "busybox", "sh", "-c",
774+
"echo managed server && sleep 120"));
775+
776+
V1JobSpec jobSpec = createJobSpec();
777+
778+
assertThat(
779+
getPodSpec(jobSpec).getInitContainers().size(), equalTo(1));
766780
}
767781

768782
@Test

0 commit comments

Comments
 (0)