Skip to content

Commit e324f14

Browse files
committed
No longer modify original and resolve errors in copying
1 parent 8623245 commit e324f14

File tree

5 files changed

+66
-107
lines changed

5 files changed

+66
-107
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
import oracle.kubernetes.operator.KubernetesConstants;
1616

1717
public abstract class BasePodStepContext extends StepContextBase {
18-
final void updateForDeepSubstitution(V1PodSpec podSpec, Object target) {
19-
getContainer(podSpec)
20-
.ifPresent(
18+
final <T> T updateForDeepSubstitution(V1PodSpec podSpec, T target) {
19+
return getContainer(podSpec)
20+
.map(
2121
c -> {
22-
doDeepSubstitution(augmentSubVars(deepSubVars(c.getEnv())), target);
23-
});
22+
return doDeepSubstitution(augmentSubVars(deepSubVars(c.getEnv())), target);
23+
})
24+
.orElse(target);
2425
}
2526

2627
final Map<String, String> deepSubVars(List<V1EnvVar> envVars) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ private V1PodTemplateSpec createPodTemplateSpec(TuningParameters tuningParameter
211211
.metadata(createPodTemplateMetadata())
212212
.spec(createPodSpec(tuningParameters));
213213

214-
updateForDeepSubstitution(podTemplateSpec.getSpec(), podTemplateSpec);
215-
return podTemplateSpec;
214+
return updateForDeepSubstitution(podTemplateSpec.getSpec(), podTemplateSpec);
216215
}
217216

218217
private V1ObjectMeta createPodTemplateMetadata() {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,7 @@ V1Pod withNonHashedElements(V1Pod pod) {
398398

399399
updateForStartupMode(pod);
400400
updateForShutdown(pod);
401-
updateForDeepSubstitution(pod.getSpec(), pod);
402-
403-
return pod;
401+
return updateForDeepSubstitution(pod.getSpec(), pod);
404402
}
405403

406404
@Override

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

Lines changed: 47 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44

55
package oracle.kubernetes.operator.helpers;
66

7+
import java.lang.reflect.Constructor;
78
import java.lang.reflect.InvocationTargetException;
89
import java.lang.reflect.Method;
910
import java.util.ArrayList;
1011
import java.util.HashMap;
11-
import java.util.Iterator;
1212
import java.util.List;
13-
import java.util.ListIterator;
1413
import java.util.Map;
15-
import java.util.Set;
1614

1715
import io.kubernetes.client.models.V1EnvVar;
1816
import io.kubernetes.client.models.V1Pod;
1917
import oracle.kubernetes.operator.Pair;
2018
import oracle.kubernetes.operator.TuningParameters;
2119
import oracle.kubernetes.operator.logging.LoggingFacade;
2220
import oracle.kubernetes.operator.logging.LoggingFactory;
23-
import oracle.kubernetes.operator.logging.MessageKeys;
2421
import oracle.kubernetes.weblogic.domain.model.Domain;
2522

2623
public abstract class StepContextBase implements StepContextConstants {
@@ -74,49 +71,46 @@ protected void doSubstitution(final Map<String, String> substitutionVariables, L
7471
}
7572
}
7673

77-
protected void doDeepSubstitution(final Map<String, String> substitutionVariables, Object obj) {
78-
if (obj != null) {
79-
if (obj instanceof List) {
80-
ListIterator<Object> it = ((List) obj).listIterator();
81-
while (it.hasNext()) {
82-
Object member = it.next();
83-
if (member instanceof String) {
84-
String trans = translate(substitutionVariables, (String) member);
85-
if (!member.equals(trans)) {
86-
it.set(trans);
87-
}
88-
} else if (member != null && isModelClass(member.getClass())) {
89-
doDeepSubstitution(substitutionVariables, member);
90-
}
91-
}
92-
} else {
74+
protected <T> T doDeepSubstitution(final Map<String, String> substitutionVariables, T obj) {
75+
if (obj instanceof String) {
76+
return (T) translate(substitutionVariables, (String) obj);
77+
} else if (obj instanceof List) {
78+
List<Object> result = new ArrayList<>();
79+
for (Object o : (List) obj) {
80+
result.add(doDeepSubstitution(substitutionVariables, o));
81+
}
82+
return (T) result;
83+
} else if (obj instanceof Map) {
84+
Map<String, Object> result = new HashMap<>();
85+
for (Map.Entry<String, Object> entry : ((Map<String, Object>) obj).entrySet()) {
86+
result.put(
87+
translate(substitutionVariables, entry.getKey()),
88+
doDeepSubstitution(substitutionVariables, entry.getValue()));
89+
}
90+
return (T) result;
91+
} else if (obj != null) {
92+
Class<T> cls = (Class<T>) obj.getClass();
93+
if (isModelClass(cls)) {
9394
try {
94-
Class cls = obj.getClass();
95-
if (isModelClass(cls)) {
96-
List<Method> modelOrListBeans = modelOrListBeans(cls);
97-
for (Method item : modelOrListBeans) {
98-
doDeepSubstitution(substitutionVariables, item.invoke(obj));
99-
}
100-
101-
List<Pair<Method, Method>> stringBeans = stringBeans(cls);
102-
for (Pair<Method, Method> item : stringBeans) {
103-
item.getRight().invoke(obj, translate(substitutionVariables, (String) item.getLeft().invoke(obj)));
104-
}
105-
106-
List<Pair<Method, Method>> mapBeans = mapBeans(cls);
107-
for (Pair<Method, Method> item : mapBeans) {
108-
item.getRight().invoke(obj, translate(substitutionVariables, (Map) item.getLeft().invoke(obj)));
109-
}
95+
Constructor<T> constructor = cls.getConstructor();
96+
T subObj = constructor.newInstance();
97+
98+
List<Pair<Method, Method>> typeBeans = typeBeans(cls);
99+
for (Pair<Method, Method> item : typeBeans) {
100+
item.getRight()
101+
.invoke(
102+
subObj, doDeepSubstitution(substitutionVariables, item.getLeft().invoke(obj)));
110103
}
111-
} catch (IllegalAccessException | InvocationTargetException e) {
112-
LOGGER.severe(MessageKeys.EXCEPTION, e);
104+
return subObj;
105+
} catch (NoSuchMethodException
106+
| InstantiationException
107+
| IllegalAccessException
108+
| InvocationTargetException e) {
109+
throw new RuntimeException(e);
113110
}
114111
}
115112
}
116-
}
117-
118-
private boolean isModelOrListClass(Class cls) {
119-
return isModelClass(cls) || List.class.isAssignableFrom(cls);
113+
return obj;
120114
}
121115

122116
private static final String MODELS_PACKAGE = V1Pod.class.getPackageName();
@@ -127,39 +121,20 @@ private boolean isModelClass(Class cls) {
127121
|| cls.getPackageName().startsWith(DOMAIN_MODEL_PACKAGE);
128122
}
129123

130-
private List<Method> modelOrListBeans(Class cls) {
131-
List<Method> results = new ArrayList<>();
132-
Method[] methods = cls.getMethods();
133-
if (methods != null) {
134-
for (Method m : methods) {
135-
if (m.getName().startsWith("get")
136-
&& isModelOrListClass(m.getReturnType())
137-
&& m.getParameterCount() == 0) {
138-
results.add(m);
139-
}
140-
}
141-
}
142-
return results;
143-
}
144-
145-
private List<Pair<Method, Method>> stringBeans(Class cls) {
146-
return typeBeans(cls, String.class);
147-
}
148-
149-
private List<Pair<Method, Method>> mapBeans(Class cls) {
150-
return typeBeans(cls, Map.class);
151-
}
152-
153-
private List<Pair<Method, Method>> typeBeans(Class cls, Class type) {
124+
private List<Pair<Method, Method>> typeBeans(Class cls) {
154125
List<Pair<Method, Method>> results = new ArrayList<>();
155126
Method[] methods = cls.getMethods();
156-
if (methods != null) {
157-
for (Method m : methods) {
158-
if (m.getName().startsWith("get")
159-
&& m.getReturnType().equals(type)
160-
&& m.getParameterCount() == 0) {
127+
for (Method m : methods) {
128+
if (m.getParameterCount() == 0) {
129+
String beanName = null;
130+
if (m.getName().startsWith("get")) {
131+
beanName = m.getName().substring(3);
132+
} else if (m.getName().startsWith("is")) {
133+
beanName = m.getName().substring(2);
134+
}
135+
if (beanName != null) {
161136
try {
162-
Method set = cls.getMethod("set" + m.getName().substring(3), type);
137+
Method set = cls.getMethod("set" + beanName, m.getReturnType());
163138
if (set != null) {
164139
results.add(new Pair<>(m, set));
165140
}
@@ -182,26 +157,6 @@ private String translate(final Map<String, String> substitutionVariables, String
182157
return result;
183158
}
184159

185-
private Map<String, Object> translate(final Map<String, String> substitutionVariables, Map<String, Object> rawValue) {
186-
if (rawValue == null)
187-
return null;
188-
189-
Map<String, Object> trans = new HashMap<>();
190-
for (Map.Entry<String, ?> entry : rawValue.entrySet()) {
191-
Object value = entry.getValue();
192-
if (value instanceof String) {
193-
value = translate(substitutionVariables, (String) value);
194-
} else if (value instanceof Map) {
195-
value = translate(substitutionVariables, (Map) value);
196-
} else {
197-
doDeepSubstitution(substitutionVariables, value);
198-
}
199-
trans.put(translate(substitutionVariables, entry.getKey()), value);
200-
}
201-
202-
return trans;
203-
}
204-
205160
protected void addEnvVar(List<V1EnvVar> vars, String name, String value) {
206161
vars.add(new V1EnvVar().name(name).value(value));
207162
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,16 @@ public void whenClusterHasLabelsWithVariables_createManagedPodWithSubstitutions(
180180
V1EnvVar envVar = toEnvVar("TEST_ENV", "test-value");
181181
testSupport.addToPacket(ProcessingConstants.ENVVARS, Arrays.asList(envVar));
182182

183+
V1Container container = new V1Container()
184+
.name("test")
185+
.addCommandItem("/bin/bash")
186+
.addArgsItem("echo")
187+
.addArgsItem("This server is $(SERVER_NAME) and has $(TEST_ENV)");
188+
183189
testSupport.addToPacket(ProcessingConstants.CLUSTER_NAME, CLUSTER_NAME);
184190
getConfigurator()
185191
.withLogHomeEnabled(true)
186-
.withContainer(new V1Container()
187-
.name("test")
188-
.addCommandItem("/bin/bash")
189-
.addArgsItem("echo")
190-
.addArgsItem("This server is $(SERVER_NAME) and has $(TEST_ENV)"))
192+
.withContainer(container)
191193
.configureCluster(CLUSTER_NAME)
192194
.withPodLabel("myCluster", "my-$(CLUSTER_NAME)")
193195
.withPodLabel("logHome", "$(LOG_HOME)");
@@ -204,6 +206,10 @@ public void whenClusterHasLabelsWithVariables_createManagedPodWithSubstitutions(
204206
o.orElseThrow().getArgs(),
205207
allOf(
206208
hasItem("This server is " + SERVER_NAME + " and has test-value")));
209+
assertThat(
210+
container.getArgs(),
211+
allOf(hasItem("This server is $(SERVER_NAME) and has $(TEST_ENV)"))
212+
);
207213
}
208214

209215
@Test

0 commit comments

Comments
 (0)