Skip to content

Commit 2da2b27

Browse files
authored
Merge pull request #506 from oracle/OWLS-68700
OWLS-68700: Add support for pod labels and annotations
2 parents 76ff080 + d36daa4 commit 2da2b27

File tree

13 files changed

+498
-16
lines changed

13 files changed

+498
-16
lines changed

model/src/main/java/oracle/kubernetes/weblogic/domain/ClusterConfigurator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ ClusterConfigurator withLivenessProbeSettings(
3939
ClusterConfigurator withAdditionalVolume(String name, String path);
4040

4141
ClusterConfigurator withAdditionalVolumeMount(String name, String path);
42+
43+
ClusterConfigurator withPodLabel(String name, String value);
44+
45+
ClusterConfigurator withPodAnnotation(String name, String value);
4246
}

model/src/main/java/oracle/kubernetes/weblogic/domain/DomainConfigurator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ protected String getAsName() {
232232

233233
public abstract DomainConfigurator withAdditionalVolumeMount(String name, String path);
234234

235+
public abstract DomainConfigurator withPodLabel(String name, String value);
236+
237+
public abstract DomainConfigurator withPodAnnotation(String name, String value);
238+
235239
/**
236240
* Adds a default server configuration to the domain, if not already present.
237241
*

model/src/main/java/oracle/kubernetes/weblogic/domain/ServerConfigurator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ ServerConfigurator withReadinessProbeSettings(
3737
ServerConfigurator withAdditionalVolume(String name, String path);
3838

3939
ServerConfigurator withAdditionalVolumeMount(String name, String path);
40+
41+
ServerConfigurator withPodLabel(String name, String value);
42+
43+
ServerConfigurator withPodAnnotation(String name, String value);
4044
}

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/BaseConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ void addAdditionalVolumeMount(String name, String path) {
178178
serverPod.addAdditionalVolumeMount(name, path);
179179
}
180180

181+
public Map<String, String> getPodLabels() {
182+
return serverPod.getPodLabels();
183+
}
184+
185+
void addPodLabels(String name, String value) {
186+
serverPod.addPodLabel(name, value);
187+
}
188+
189+
public Map<String, String> getPodAnnotations() {
190+
return serverPod.getPodAnnotations();
191+
}
192+
193+
void addPodAnnotations(String name, String value) {
194+
serverPod.addPodAnnotations(name, value);
195+
}
196+
181197
@Override
182198
public String toString() {
183199
return new ToStringBuilder(this)

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/DomainV2Configurator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ public DomainConfigurator withConfigOverrideSecrets(String... secretNames) {
103103
return this;
104104
}
105105

106+
@Override
107+
public DomainConfigurator withPodLabel(String name, String value) {
108+
((BaseConfiguration) getDomainSpec()).addPodLabels(name, value);
109+
return this;
110+
}
111+
112+
@Override
113+
public DomainConfigurator withPodAnnotation(String name, String value) {
114+
((BaseConfiguration) getDomainSpec()).addPodAnnotations(name, value);
115+
return this;
116+
}
117+
106118
class AdminServerConfiguratorImpl extends ServerConfiguratorImpl
107119
implements AdminServerConfigurator {
108120
private AdminServer adminServer;
@@ -280,6 +292,18 @@ public ServerConfigurator withAdditionalVolumeMount(String name, String path) {
280292
server.addAdditionalVolumeMount(name, path);
281293
return this;
282294
}
295+
296+
@Override
297+
public ServerConfigurator withPodLabel(String name, String value) {
298+
server.addPodLabels(name, value);
299+
return this;
300+
}
301+
302+
@Override
303+
public ServerConfigurator withPodAnnotation(String name, String value) {
304+
server.addPodAnnotations(name, value);
305+
return this;
306+
}
283307
}
284308

285309
@Override
@@ -400,5 +424,17 @@ public ClusterConfigurator withAdditionalVolumeMount(String name, String path) {
400424
cluster.addAdditionalVolumeMount(name, path);
401425
return this;
402426
}
427+
428+
@Override
429+
public ClusterConfigurator withPodLabel(String name, String value) {
430+
cluster.addPodLabels(name, value);
431+
return this;
432+
}
433+
434+
@Override
435+
public ClusterConfigurator withPodAnnotation(String name, String value) {
436+
cluster.addPodAnnotations(name, value);
437+
return this;
438+
}
403439
}
404440
}

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/ServerPod.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ class ServerPod {
135135
@Description("Additional volume mounts")
136136
private List<V1VolumeMount> additionalVolumeMounts = new ArrayList<>();
137137

138+
/**
139+
* The labels to be attached to pods.
140+
*
141+
* @since 2.0
142+
*/
143+
@SerializedName("podLabels")
144+
@Expose
145+
@Description("Labels applied to pods")
146+
private Map<String, String> podLabels = new HashMap<String, String>();
147+
148+
/**
149+
* The annotations to be attached to pods.
150+
*
151+
* @since 2.0
152+
*/
153+
@SerializedName("podAnnotations")
154+
@Expose
155+
@Description("Annotations applied to pods")
156+
private Map<String, String> podAnnotations = new HashMap<String, String>();
157+
138158
ProbeTuning getReadinessProbeTuning() {
139159
return this.readinessProbeTuning;
140160
}
@@ -163,6 +183,8 @@ void fillInFrom(ServerPod serverPod1) {
163183
readinessProbeTuning.copyValues(serverPod1.readinessProbeTuning);
164184
for (V1Volume var : serverPod1.getAdditionalVolumes()) addIfMissing(var);
165185
for (V1VolumeMount var : serverPod1.getAdditionalVolumeMounts()) addIfMissing(var);
186+
serverPod1.getPodLabels().forEach((k, v) -> addPodLabelIfMissing(k, v));
187+
serverPod1.getPodAnnotations().forEach((k, v) -> addPodAnnotationIfMissing(k, v));
166188
serverPod1.nodeSelectorMap.forEach(nodeSelectorMap::putIfAbsent);
167189
copyValues(resourceRequirements, serverPod1.resourceRequirements);
168190
copyValues(podSecurityContext, serverPod1.podSecurityContext);
@@ -263,6 +285,14 @@ private void addIfMissing(V1VolumeMount var) {
263285
if (!hasVolumeMountName(var.getName())) addAdditionalVolumeMount(var);
264286
}
265287

288+
private void addPodLabelIfMissing(String name, String value) {
289+
if (!podLabels.containsKey(name)) podLabels.put(name, value);
290+
}
291+
292+
private void addPodAnnotationIfMissing(String name, String value) {
293+
if (!podAnnotations.containsKey(name)) podAnnotations.put(name, value);
294+
}
295+
266296
List<V1EnvVar> getEnv() {
267297
return this.env;
268298
}
@@ -345,12 +375,32 @@ public List<V1VolumeMount> getAdditionalVolumeMounts() {
345375
return additionalVolumeMounts;
346376
}
347377

378+
void addPodLabel(String name, String value) {
379+
podLabels.put(name, value);
380+
}
381+
382+
void addPodAnnotations(String name, String value) {
383+
podAnnotations.put(name, value);
384+
}
385+
386+
public Map<String, String> getPodLabels() {
387+
return podLabels;
388+
}
389+
390+
public Map<String, String> getPodAnnotations() {
391+
return podAnnotations;
392+
}
393+
348394
@Override
349395
public String toString() {
350396
return new ToStringBuilder(this)
351397
.append("env", env)
352398
.append("livenessProbe", livenessProbeTuning)
353399
.append("readinessProbe", readinessProbeTuning)
400+
.append("additionalVolumes", additionalVolumes)
401+
.append("additionalVolumeMounts", additionalVolumeMounts)
402+
.append("podLabels", podLabels)
403+
.append("podAnnotations", podAnnotations)
354404
.append("nodeSelector", nodeSelectorMap)
355405
.append("resourceRequirements", resourceRequirements)
356406
.append("podSecurityContext", podSecurityContext)
@@ -370,6 +420,10 @@ public boolean equals(Object o) {
370420
.append(env, that.env)
371421
.append(livenessProbeTuning, that.livenessProbeTuning)
372422
.append(readinessProbeTuning, that.readinessProbeTuning)
423+
.append(additionalVolumes, that.additionalVolumes)
424+
.append(additionalVolumeMounts, that.additionalVolumeMounts)
425+
.append(podLabels, that.podLabels)
426+
.append(podAnnotations, that.podAnnotations)
373427
.append(nodeSelectorMap, that.nodeSelectorMap)
374428
.append(resourceRequirements, that.resourceRequirements)
375429
.append(podSecurityContext, that.podSecurityContext)
@@ -383,6 +437,10 @@ public int hashCode() {
383437
.append(env)
384438
.append(livenessProbeTuning)
385439
.append(readinessProbeTuning)
440+
.append(additionalVolumes)
441+
.append(additionalVolumeMounts)
442+
.append(podLabels)
443+
.append(podAnnotations)
386444
.append(nodeSelectorMap)
387445
.append(resourceRequirements)
388446
.append(podSecurityContext)

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/ServerSpec.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,21 @@ public ProbeTuning getReadinessProbe() {
163163
return new ProbeTuning();
164164
}
165165

166+
/**
167+
* Returns the labels applied to the pod.
168+
*
169+
* @return a map of labels
170+
*/
166171
@Nonnull
167-
public Map<String, String> getPodLabels() {
168-
return Collections.emptyMap();
169-
}
172+
public abstract Map<String, String> getPodLabels();
170173

174+
/**
175+
* Returns the annotations applied to the pod.
176+
*
177+
* @return a map of annotations
178+
*/
171179
@Nonnull
172-
public Map<String, String> getPodAnnotations() {
173-
return Collections.emptyMap();
174-
}
180+
public abstract Map<String, String> getPodAnnotations();
175181

176182
@Nonnull
177183
public Map<String, String> getListenAddressServiceLabels() {

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/ServerSpecV2Impl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public List<V1VolumeMount> getAdditionalVolumeMounts() {
6363
return server.getAdditionalVolumeMounts();
6464
}
6565

66+
@Override
67+
public Map<String, String> getPodLabels() {
68+
return server.getPodLabels();
69+
}
70+
71+
@Override
72+
public Map<String, String> getPodAnnotations() {
73+
return server.getPodAnnotations();
74+
}
75+
6676
@Override
6777
public String getDesiredState() {
6878
return Optional.ofNullable(getConfiguredDesiredState()).orElse("RUNNING");

model/src/test/java/oracle/kubernetes/weblogic/domain/v2/DomainV2Test.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,103 @@ public void domainHomeTest_customHome1() {
12421242
assertThat(domain.getDomainHome(), equalTo("/custom/domain/home"));
12431243
}
12441244

1245+
@Test
1246+
public void whenPodLabelsAppliedOnMultipleLevels_useCombination() {
1247+
configureDomain(domain)
1248+
.withPodLabel("label1", "domain-label-value1")
1249+
.withPodLabel("label2", "domain-label-value2");
1250+
configureCluster("cluster1")
1251+
.withPodLabel("label3", "cluster-label-value1")
1252+
.withPodLabel("label4", "cluster-label-value2");
1253+
configureServer("server1").withPodLabel("label5", "server-label-value1");
1254+
1255+
assertThat(
1256+
domain.getServer("server1", "cluster1").getPodLabels(),
1257+
hasEntry("label1", "domain-label-value1"));
1258+
assertThat(
1259+
domain.getServer("server1", "cluster1").getPodLabels(),
1260+
hasEntry("label2", "domain-label-value2"));
1261+
assertThat(
1262+
domain.getServer("server1", "cluster1").getPodLabels(),
1263+
hasEntry("label3", "cluster-label-value1"));
1264+
assertThat(
1265+
domain.getServer("server1", "cluster1").getPodLabels(),
1266+
hasEntry("label4", "cluster-label-value2"));
1267+
assertThat(
1268+
domain.getServer("server1", "cluster1").getPodLabels(),
1269+
hasEntry("label5", "server-label-value1"));
1270+
}
1271+
1272+
@Test
1273+
public void whenPodAnnotationsAppliedOnMultipleLevels_useCombination() {
1274+
configureDomain(domain)
1275+
.withPodAnnotation("annotation1", "domain-annotation-value1")
1276+
.withPodAnnotation("annotation2", "domain-annotation-value2");
1277+
configureCluster("cluster1")
1278+
.withPodAnnotation("annotation3", "cluster-annotation-value1")
1279+
.withPodAnnotation("annotation4", "cluster-annotation-value2");
1280+
configureServer("server1").withPodAnnotation("annotation5", "server-annotation-value1");
1281+
1282+
assertThat(
1283+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1284+
hasEntry("annotation1", "domain-annotation-value1"));
1285+
assertThat(
1286+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1287+
hasEntry("annotation2", "domain-annotation-value2"));
1288+
assertThat(
1289+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1290+
hasEntry("annotation3", "cluster-annotation-value1"));
1291+
assertThat(
1292+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1293+
hasEntry("annotation4", "cluster-annotation-value2"));
1294+
assertThat(
1295+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1296+
hasEntry("annotation5", "server-annotation-value1"));
1297+
}
1298+
1299+
@Test
1300+
public void whenDuplicatePodLabelsConfiguredOnMultipleLevels_useCombination() {
1301+
configureDomain(domain)
1302+
.withPodLabel("label1", "domain-label-value1")
1303+
.withPodLabel("label2", "domain-label-value2");
1304+
configureCluster("cluster1")
1305+
.withPodLabel("label2", "cluster-label-value1")
1306+
.withPodLabel("label3", "cluster-label-value2");
1307+
;
1308+
configureServer("server1").withPodLabel("label3", "server-label-value1");
1309+
1310+
assertThat(
1311+
domain.getServer("server1", "cluster1").getPodLabels(),
1312+
hasEntry("label1", "domain-label-value1"));
1313+
assertThat(
1314+
domain.getServer("server1", "cluster1").getPodLabels(),
1315+
hasEntry("label2", "cluster-label-value1"));
1316+
assertThat(
1317+
domain.getServer("server1", "cluster1").getPodLabels(),
1318+
hasEntry("label3", "server-label-value1"));
1319+
}
1320+
1321+
@Test
1322+
public void whenDuplicatePodAnnotationsConfiguredOnMultipleLevels_useCombination() {
1323+
configureDomain(domain)
1324+
.withPodAnnotation("annotation1", "domain-annotation-value1")
1325+
.withPodAnnotation("annotation2", "domain-annotation-value2");
1326+
configureCluster("cluster1")
1327+
.withPodAnnotation("annotation2", "cluster-annotation-value1")
1328+
.withPodAnnotation("annotation3", "cluster-annotation-value2");
1329+
configureServer("server1").withPodAnnotation("annotation3", "server-annotation-value1");
1330+
1331+
assertThat(
1332+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1333+
hasEntry("annotation1", "domain-annotation-value1"));
1334+
assertThat(
1335+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1336+
hasEntry("annotation2", "cluster-annotation-value1"));
1337+
assertThat(
1338+
domain.getServer("server1", "cluster1").getPodAnnotations(),
1339+
hasEntry("annotation3", "server-annotation-value1"));
1340+
}
1341+
12451342
private V1Volume volume(String name, String path) {
12461343
return new V1Volume().name(name).hostPath(new V1HostPathVolumeSource().path(path));
12471344
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ protected List<V1VolumeMount> getAdditionalVolumeMounts() {
124124
return getServerSpec().getAdditionalVolumeMounts();
125125
}
126126

127+
@Override
128+
protected Map<String, String> getPodLabels() {
129+
return getServerSpec().getPodLabels();
130+
}
131+
132+
@Override
133+
protected Map<String, String> getPodAnnotations() {
134+
return getServerSpec().getPodAnnotations();
135+
}
136+
127137
private String getInternalOperatorCertFile(TuningParameters tuningParameters) {
128138
return tuningParameters.get(INTERNAL_OPERATOR_CERT_FILE);
129139
}
@@ -195,6 +205,16 @@ ServerSpec getServerSpec() {
195205
return getDomain().getServer(getServerName(), getClusterName());
196206
}
197207

208+
@Override
209+
protected Map<String, String> getPodLabels() {
210+
return getServerSpec().getPodLabels();
211+
}
212+
213+
@Override
214+
protected Map<String, String> getPodAnnotations() {
215+
return getServerSpec().getPodAnnotations();
216+
}
217+
198218
@Override
199219
Integer getPort() {
200220
return scan.getListenPort();

0 commit comments

Comments
 (0)