Skip to content

Commit e3ae206

Browse files
committed
work in progress
1 parent fc3c277 commit e3ae206

File tree

6 files changed

+77
-40
lines changed

6 files changed

+77
-40
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,10 @@ private static V1VolumeMount volumeMount(String volumeName, String mountPath) {
5555

5656
void init() {
5757
jobModel = createJobModel();
58-
createSubstitutionMap();
5958
}
6059

6160
// ------------------------ data methods ----------------------------
6261

63-
private void createSubstitutionMap() {
64-
substitutionVariables.put("DOMAIN_HOME", getDomainHome());
65-
}
66-
6762
private V1Job getJobModel() {
6863
return jobModel;
6964
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ protected V1ObjectMeta createMetadata() {
388388
return metadata;
389389
}
390390

391-
private String getClusterName() {
391+
@Override
392+
protected String getClusterName() {
392393
return clusterName;
393394
}
394395

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,9 @@ private static boolean isCustomerItem(Map.Entry<String, String> entry) {
8686
}
8787

8888
void init() {
89-
createSubstitutionMap();
9089
podModel = createPodModel();
9190
}
9291

93-
private void createSubstitutionMap() {
94-
substitutionVariables.put("DOMAIN_NAME", getDomainName());
95-
substitutionVariables.put("DOMAIN_HOME", getDomainHome());
96-
substitutionVariables.put("SERVER_NAME", getServerName());
97-
substitutionVariables.put("ADMIN_NAME", getAsName());
98-
substitutionVariables.put("ADMIN_PORT", getAsPort().toString());
99-
}
100-
10192
V1Pod getPodModel() {
10293
return podModel;
10394
}
@@ -221,6 +212,10 @@ private List<V1ContainerPort> getContainerPorts() {
221212

222213
abstract String getServerName();
223214

215+
String getClusterName() {
216+
return null;
217+
}
218+
224219
// Prevent the watcher from recreating pod with old spec
225220
private void markBeingDeleted() {
226221
info.setServerPodBeingDeleted(getServerName(), Boolean.TRUE);
@@ -406,12 +401,32 @@ V1Pod withNonHashedElements(V1Pod pod) {
406401
.filter(PodStepContext::isCustomerItem)
407402
.forEach(e -> metadata.putAnnotationsItem(e.getKey(), e.getValue()));
408403

409-
updateForStarupMode(pod);
404+
updateForStartupMode(pod);
410405
updateForShutdown(pod);
406+
updateForDeepSubstitution(pod);
407+
411408
return pod;
412409
}
413410

414-
final void updateForStarupMode(V1Pod pod) {
411+
final void updateForDeepSubstitution(V1Pod pod) {
412+
getContainer(pod)
413+
.ifPresent(
414+
c -> {
415+
doDeepSubstitution(deepSubVars(c.getEnv()), pod.getSpec());
416+
}
417+
);
418+
}
419+
420+
final Map<String, String> deepSubVars(List<V1EnvVar> envVars) {
421+
Map<String, String> vars = varsToSubVariables(envVars);
422+
String clusterName = getClusterName();
423+
if (clusterName != null) {
424+
vars.put("CLUSTER_NAME", clusterName);
425+
}
426+
return vars;
427+
}
428+
429+
final void updateForStartupMode(V1Pod pod) {
415430
ServerSpec serverSpec = getServerSpec();
416431
if (serverSpec != null) {
417432
String desiredState = serverSpec.getDesiredState();
@@ -496,13 +511,14 @@ LabelConstants.CLUSTERRESTARTVERSION_LABEL, getServerSpec().getClusterRestartVer
496511
}
497512

498513
protected V1PodSpec createSpec(TuningParameters tuningParameters) {
514+
V1Container container =
515+
createContainer(tuningParameters)
516+
.resources(getServerSpec().getResources())
517+
.securityContext(getServerSpec().getContainerSecurityContext());
499518
V1PodSpec podSpec =
500519
new V1PodSpec()
501520
.containers(getServerSpec().getContainers())
502-
.addContainersItem(
503-
createContainer(tuningParameters)
504-
.resources(getServerSpec().getResources())
505-
.securityContext(getServerSpec().getContainerSecurityContext()))
521+
.addContainersItem(container)
506522
.nodeSelector(getServerSpec().getNodeSelectors())
507523
.securityContext(getServerSpec().getPodSecurityContext())
508524
.initContainers(getServerSpec().getInitContainers());
@@ -513,8 +529,6 @@ protected V1PodSpec createSpec(TuningParameters tuningParameters) {
513529
podSpec.addVolumesItem(additionalVolume);
514530
}
515531

516-
doDeepSubstitution(podSpec);
517-
518532
return podSpec;
519533
}
520534

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@
1313
import java.util.Map;
1414

1515
import io.kubernetes.client.models.V1EnvVar;
16+
import io.kubernetes.client.models.V1Pod;
1617
import oracle.kubernetes.operator.Pair;
1718
import oracle.kubernetes.operator.TuningParameters;
1819
import oracle.kubernetes.operator.logging.LoggingFacade;
1920
import oracle.kubernetes.operator.logging.LoggingFactory;
2021
import oracle.kubernetes.operator.logging.MessageKeys;
22+
import oracle.kubernetes.weblogic.domain.model.Domain;
2123

2224
public abstract class StepContextBase implements StepContextConstants {
2325
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
2426

25-
// Map of <token, substitution string> to be used in the translate method
26-
// Subclass should populate this map prior to calling translate().
27-
protected Map<String, String> substitutionVariables = new HashMap<>();
28-
2927
/**
3028
* Abstract method to be implemented by subclasses to return a list of configured and additional
3129
* environment variables to be set up in the pod.
@@ -52,30 +50,41 @@ final List<V1EnvVar> getEnvironmentVariables(TuningParameters tuningParameters)
5250
vars, "USER_MEM_ARGS", "-XX:+UseContainerSupport -Djava.security.egd=file:/dev/./urandom");
5351

5452
hideAdminUserCredentials(vars);
55-
doSubstitution(vars);
53+
doSubstitution(varsToSubVariables(vars), vars);
5654

5755
return vars;
5856
}
5957

60-
protected void doSubstitution(List<V1EnvVar> vars) {
58+
protected Map<String, String> varsToSubVariables(List<V1EnvVar> vars) {
59+
Map<String, String> substitutionVariables = new HashMap<>();
60+
if (vars != null) {
61+
for (V1EnvVar envVar : vars) {
62+
substitutionVariables.put(envVar.getName(), envVar.getValue());
63+
}
64+
}
65+
66+
return substitutionVariables;
67+
}
68+
69+
protected void doSubstitution(final Map<String, String> substitutionVariables, List<V1EnvVar> vars) {
6170
for (V1EnvVar var : vars) {
62-
var.setValue(translate(var.getValue()));
71+
var.setValue(translate(substitutionVariables, var.getValue()));
6372
}
6473
}
6574

66-
protected void doDeepSubstitution(Object obj) {
75+
protected void doDeepSubstitution(final Map<String, String> substitutionVariables, Object obj) {
6776
if (obj != null) {
6877
if (obj instanceof List) {
6978
ListIterator<Object> it = ((List) obj).listIterator();
7079
while (it.hasNext()) {
7180
Object member = it.next();
7281
if (member instanceof String) {
73-
String trans = translate((String) member);
82+
String trans = translate(substitutionVariables, (String) member);
7483
if (!member.equals(trans)) {
7584
it.set(trans);
7685
}
7786
} else if (member != null && isModelClass(member.getClass())) {
78-
doDeepSubstitution(member);
87+
doDeepSubstitution(substitutionVariables, member);
7988
}
8089
}
8190
} else {
@@ -84,12 +93,12 @@ protected void doDeepSubstitution(Object obj) {
8493
if (isModelClass(cls)) {
8594
List<Method> modelOrListBeans = modelOrListBeans(cls);
8695
for (Method item : modelOrListBeans) {
87-
doDeepSubstitution(item.invoke(obj));
96+
doDeepSubstitution(substitutionVariables, item.invoke(obj));
8897
}
8998

9099
List<Pair<Method, Method>> stringBeans = stringBeans(cls);
91100
for (Pair<Method, Method> item : stringBeans) {
92-
item.getRight().invoke(obj, translate((String) item.getLeft().invoke(obj)));
101+
item.getRight().invoke(obj, translate(substitutionVariables, (String) item.getLeft().invoke(obj)));
93102
}
94103
}
95104
} catch (IllegalAccessException | InvocationTargetException e) {
@@ -103,9 +112,12 @@ private boolean isModelOrListClass(Class cls) {
103112
return isModelClass(cls) || List.class.isAssignableFrom(cls);
104113
}
105114

115+
private static final String MODELS_PACKAGE = V1Pod.class.getPackageName();
116+
private static final String DOMAIN_MODEL_PACKAGE = Domain.class.getPackageName();
117+
106118
private boolean isModelClass(Class cls) {
107-
return cls.getPackageName().startsWith("io.kubernetes.client.models")
108-
|| cls.getPackageName().startsWith("oracle.kubernetes.weblogic.domain.model");
119+
return cls.getPackageName().startsWith(MODELS_PACKAGE)
120+
|| cls.getPackageName().startsWith(DOMAIN_MODEL_PACKAGE);
109121
}
110122

111123
private List<Method> modelOrListBeans(Class cls) {
@@ -145,7 +157,7 @@ private List<Pair<Method, Method>> stringBeans(Class cls) {
145157
return results;
146158
}
147159

148-
private String translate(String rawValue) {
160+
private String translate(final Map<String, String> substitutionVariables, String rawValue) {
149161
String result = rawValue;
150162
for (Map.Entry<String, String> entry : substitutionVariables.entrySet()) {
151163
if (result != null && entry.getValue() != null) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ public void whenClusterHasAdditionalVolumesWithVariables_createManagedPodWithSub
172172
hasVolume("volume2", "/source-domain1")));
173173
}
174174

175+
@Test
176+
public void whenClusterHasLabelsWithVariables_createManagedPodWithSubstitutions() {
177+
testSupport.addToPacket(ProcessingConstants.CLUSTER_NAME, CLUSTER_NAME);
178+
getConfigurator()
179+
.configureCluster(CLUSTER_NAME)
180+
.withPodLabel("myCluster", "my-$(CLUSTER_NAME)")
181+
.withPodLabel("logHome", "$(LOG_HOME)");
182+
183+
assertThat(
184+
getCreatedPod().getMetadata().getLabels(),
185+
allOf(
186+
hasEntry("myCluster", "my" + CLUSTER_NAME),
187+
hasEntry("logHome", "/shared/logs/" + UID)));
188+
}
189+
175190
@Test
176191
public void createManagedPodStartupWithNullAdminUsernamePasswordEnvVarsValues() {
177192
testSupport.addToPacket(ProcessingConstants.ENVVARS, Arrays.asList());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public abstract class PodHelperTestBase {
105105
static final String NS = "namespace";
106106
static final String ADMIN_SERVER = "ADMIN_SERVER";
107107
static final Integer ADMIN_PORT = 7001;
108-
private static final String DOMAIN_NAME = "domain1";
109-
private static final String UID = "uid1";
108+
protected static final String DOMAIN_NAME = "domain1";
109+
protected static final String UID = "uid1";
110110
private static final boolean INCLUDE_SERVER_OUT_IN_POD_LOG = true;
111111

112112
private static final String CREDENTIALS_SECRET_NAME = "webLogicCredentialsSecretName";

0 commit comments

Comments
 (0)