Skip to content

Commit 06aca81

Browse files
committed
Merge branch 'support-annotation-label-for-introspector' into 'main'
Support annotation label for introspector and correct reading the correct jobpod container See merge request weblogic-cloud/weblogic-kubernetes-operator!5049
2 parents f8e0e58 + 5018ce8 commit 06aca81

File tree

11 files changed

+137
-3
lines changed

11 files changed

+137
-3
lines changed

documentation/domains/Domain.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,13 @@
773773
"description": "Memory and CPU minimum requirements and limits for the Introspector Job Pod. See `kubectl explain pods.spec.containers.resources`.",
774774
"$ref": "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.28.2/_definitions.json#/definitions/io.k8s.api.core.v1.ResourceRequirements"
775775
},
776+
"annotations": {
777+
"description": "The annotations to be added to generated resources.",
778+
"additionalProperties": {
779+
"type": "string"
780+
},
781+
"$ref": "#/definitions/Map"
782+
},
776783
"env": {
777784
"description": "A list of environment variables to set in the Introspector Job Pod container. More info: https://oracle.github.io/weblogic-kubernetes-operator/userguide/managing-domains/domain-resource/#jvm-memory-and-java-option-environment-variables. See `kubectl explain pods.spec.containers.env`.",
778785
"type": "array",
@@ -793,6 +800,13 @@
793800
"items": {
794801
"$ref": "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.28.2/_definitions.json#/definitions/io.k8s.api.core.v1.Container"
795802
}
803+
},
804+
"labels": {
805+
"description": "The labels to be added to generated resources. The label names must not start with \"weblogic.\".",
806+
"additionalProperties": {
807+
"type": "string"
808+
},
809+
"$ref": "#/definitions/Map"
796810
}
797811
}
798812
},

documentation/domains/Domain.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ The current status of the operation of the WebLogic domain. Updated automaticall
278278

279279
| Name | Type | Description |
280280
| --- | --- | --- |
281+
| `annotations` | Map | The annotations to be added to generated resources. |
281282
| `env` | Array of [Env Var](k8s1.28.2.md#env-var) | A list of environment variables to set in the Introspector Job Pod container. More info: https://oracle.github.io/weblogic-kubernetes-operator/userguide/managing-domains/domain-resource/#jvm-memory-and-java-option-environment-variables. See `kubectl explain pods.spec.containers.env`. |
282283
| `envFrom` | Array of [Env From Source](k8s1.28.2.md#env-from-source) | List of sources to populate environment variables in the Introspector Job Pod container. The sources include either a config map or a secret. The operator will not expand the dependent variables in the 'envFrom' source. More details: https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#define-an-environment-variable-for-a-container. Also see: https://oracle.github.io/weblogic-kubernetes-operator/userguide/managing-domains/domain-resource/#jvm-memory-and-java-option-environment-variables. |
283284
| `initContainers` | Array of [Container](k8s1.28.2.md#container) | List of init containers for the introspector Job Pod. These containers run after the auxiliary image init container. See `kubectl explain pods.spec.initContainers`. |
285+
| `labels` | Map | The labels to be added to generated resources. The label names must not start with "weblogic.". |
284286
| `podSecurityContext` | [Pod Security Context](k8s1.28.2.md#pod-security-context) | Pod-level security attributes. See `kubectl explain pods.spec.securityContext`. If no value is specified for this field, the operator will use default content for the pod-level `securityContext`. More info: https://oracle.github.io/weblogic-kubernetes-operator/security/domain-security/pod-and-container/. |
285287
| `resources` | [Resource Requirements](k8s1.28.2.md#resource-requirements) | Memory and CPU minimum requirements and limits for the Introspector Job Pod. See `kubectl explain pods.spec.containers.resources`. |
286288

kubernetes/crd/domain-crd.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apiVersion: apiextensions.k8s.io/v1
55
kind: CustomResourceDefinition
66
metadata:
77
annotations:
8-
weblogic.sha256: 68317b3e7f2cc05c004dcf48eb7f2b83a8c6747f54d7a6929d6ba664df0594db
8+
weblogic.sha256: 6e58fa19cc5aff28cc719869a8b6172feb024bbc30d1586ee33f61e1b9fffe9a
99
name: domains.weblogic.oracle
1010
spec:
1111
group: weblogic.oracle
@@ -4413,6 +4413,11 @@ spec:
44134413
type: string
44144414
type: object
44154415
type: object
4416+
annotations:
4417+
additionalProperties:
4418+
type: string
4419+
description: The annotations to be added to generated resources.
4420+
type: object
44164421
env:
44174422
description: 'A list of environment variables to set in the
44184423
Introspector Job Pod container. More info: https://oracle.github.io/weblogic-kubernetes-operator/userguide/managing-domains/domain-resource/#jvm-memory-and-java-option-environment-variables.
@@ -5069,6 +5074,12 @@ spec:
50695074
required:
50705075
- name
50715076
type: array
5077+
labels:
5078+
additionalProperties:
5079+
type: string
5080+
description: The labels to be added to generated resources.
5081+
The label names must not start with "weblogic.".
5082+
type: object
50725083
type: object
50735084
type: object
50745085
clusters:

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,14 @@ private class ReadPodLogStep extends Step {
526526
}
527527

528528
private V1ContainerStatus getJobPodContainerStatus(V1Pod jobPod) {
529-
return Optional.ofNullable(getContainerStatuses(jobPod)).flatMap(cs -> cs.stream().findFirst()).orElse(null);
529+
String targetContainerName = getContainerName(); // Get the actual job container name
530+
531+
return Optional.ofNullable(getContainerStatuses(jobPod))
532+
.flatMap(cs -> cs.stream()
533+
.filter(status -> targetContainerName.equals(status.getName()))
534+
.findFirst())
535+
.orElse(null);
536+
530537
}
531538

532539
private List<V1ContainerStatus> getContainerStatuses(V1Pod jobPod) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.security.NoSuchAlgorithmException;
88
import java.util.ArrayList;
99
import java.util.Collections;
10+
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Map;
1213
import java.util.Optional;
@@ -199,6 +200,16 @@ private List<V1Container> getIntrospectorInitContainers() {
199200
.map(EffectiveIntrospectorJobPodSpec::getInitContainers).orElse(new ArrayList<>());
200201
}
201202

203+
private Map<String, String> getIntrospectorLabels() {
204+
return Optional.ofNullable(getDomain().getIntrospectorSpec())
205+
.map(EffectiveIntrospectorJobPodSpec::getLabels).orElse(new HashMap<>());
206+
}
207+
208+
private Map<String, String> getIntrospectorAnnotations() {
209+
return Optional.ofNullable(getDomain().getIntrospectorSpec())
210+
.map(EffectiveIntrospectorJobPodSpec::getAnnotations).orElse(new HashMap<>());
211+
}
212+
202213
private List<V1EnvVar> getAdminServerEnvVariables() {
203214
return Optional.ofNullable(getDomain().getAdminServerSpec()).map(EffectiveServerSpec::getEnvironmentVariables)
204215
.orElse(new ArrayList<>());
@@ -463,8 +474,13 @@ private List<DomainCreationImage> getDomainCreationImages() {
463474
}
464475

465476
private V1ObjectMeta createPodTemplateMetadata() {
477+
Map<String, String> labels = getIntrospectorLabels();
478+
Map<String, String> annotations = getIntrospectorAnnotations();
479+
466480
return new V1ObjectMeta()
467481
.name(getJobName())
482+
.labels(labels)
483+
.annotations(annotations)
468484
.putLabelsItem(LabelConstants.CREATEDBYOPERATOR_LABEL, "true")
469485
.putLabelsItem(LabelConstants.DOMAINUID_LABEL, getDomainUid())
470486
.putLabelsItem(LabelConstants.JOBNAME_LABEL, createJobName(getDomainUid()));

operator/src/main/java/oracle/kubernetes/operator/processing/EffectiveIntrospectorJobPodSpec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package oracle.kubernetes.operator.processing;
55

66
import java.util.List;
7+
import java.util.Map;
78

89
import io.kubernetes.client.openapi.models.V1Container;
910
import io.kubernetes.client.openapi.models.V1EnvFromSource;
@@ -31,4 +32,9 @@ public interface EffectiveIntrospectorJobPodSpec {
3132
V1PodSecurityContext getPodSecurityContext();
3233

3334
List<V1Container> getInitContainers();
35+
36+
Map<String, String> getLabels();
37+
38+
Map<String, String> getAnnotations();
39+
3440
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package oracle.kubernetes.weblogic.domain.model;
55

66
import java.util.List;
7+
import java.util.Map;
78

89
import io.kubernetes.client.openapi.models.V1Container;
910
import io.kubernetes.client.openapi.models.V1EnvFromSource;
@@ -55,4 +56,14 @@ public List<V1Container> getInitContainers() {
5556
return introspector.getInitContainers();
5657
}
5758

59+
@Override
60+
public Map<String, String> getLabels() {
61+
return introspector.getLabels();
62+
}
63+
64+
@Override
65+
public Map<String, String> getAnnotations() {
66+
return introspector.getAnnotations();
67+
}
68+
5869
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import static java.util.Collections.emptyList;
2626

27-
class IntrospectorJobPod {
27+
class IntrospectorJobPod extends KubernetesResource {
2828

2929
private static final Comparator<V1EnvVar> ENV_VAR_COMPARATOR =
3030
Comparator.comparing(V1EnvVar::getName);
@@ -159,6 +159,7 @@ void fillInFrom(IntrospectorJobPod serverPod1) {
159159
}
160160
initContainers.addAll(serverPod1.initContainers);
161161
}
162+
fillInFrom((KubernetesResource) serverPod1);
162163
copyValues(resources, serverPod1.resources);
163164
copyValues(serverPod1.podSecurityContext);
164165
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package oracle.kubernetes.weblogic.domain.model;
55

66
import java.util.List;
7+
import java.util.Map;
78
import javax.annotation.Nullable;
89

910
import io.kubernetes.client.openapi.models.V1Container;
@@ -53,6 +54,14 @@ public List<V1Container> getInitContainers() {
5354
return serverPod.getInitContainers();
5455
}
5556

57+
public Map<String, String> getLabels() {
58+
return serverPod.getLabels();
59+
}
60+
61+
public Map<String, String> getAnnotations() {
62+
return serverPod.getAnnotations();
63+
}
64+
5665
void addEnvironmentVariable(String name, String value) {
5766
serverPod.addEnvVar(new V1EnvVar().name(name).value(value));
5867
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
import static org.hamcrest.CoreMatchers.containsString;
146146
import static org.hamcrest.CoreMatchers.equalTo;
147147
import static org.hamcrest.CoreMatchers.nullValue;
148+
import static org.hamcrest.Matchers.hasEntry;
148149
import static org.hamcrest.Matchers.hasItem;
149150
import static org.hamcrest.Matchers.hasSize;
150151
import static org.hamcrest.Matchers.is;
@@ -153,6 +154,7 @@
153154
import static org.hamcrest.Matchers.stringContainsInOrder;
154155
import static org.hamcrest.core.AllOf.allOf;
155156
import static org.hamcrest.junit.MatcherAssert.assertThat;
157+
import static org.junit.Assert.assertNotNull;
156158

157159
@SuppressWarnings({"SameParameterValue"})
158160
class DomainIntrospectorJobTest extends DomainTestUtils {
@@ -505,6 +507,37 @@ void whenJobCreatedWithInitDomainOnPVWithModelEncryption_hasSecretsVolumeAndMoun
505507

506508
}
507509

510+
@Test
511+
void whenJobCreatedWithAnnotationsAndLabelsThePodHastheMetaData() {
512+
Map<String, String> annotations = new HashMap<>();
513+
annotations.put("annotation1", "value1");
514+
annotations.put("annotation2", "value2");
515+
Map<String, String> labels = new HashMap<>();
516+
labels.put("label1", "value1");
517+
labels.put("label2", "value2");
518+
519+
getConfigurator()
520+
.withIntrospectorAnnotations(annotations)
521+
.withIntrospectorLabels(labels)
522+
.withDomainHomeSourceType(DomainSourceType.FROM_MODEL)
523+
.withAuxiliaryImages(Collections.singletonList(getAuxiliaryImage("wdt-image:v1")));
524+
525+
List<V1Job> jobs = runStepsAndGetJobs();
526+
V1Job job = jobs.get(0);
527+
528+
assertNotNull(job.getSpec().getTemplate().getMetadata());
529+
assertNotNull(job.getSpec().getTemplate().getMetadata().getAnnotations());
530+
assertNotNull(job.getSpec().getTemplate().getMetadata().getLabels());
531+
532+
annotations.forEach((key, value) ->
533+
assertThat(job.getSpec().getTemplate().getMetadata().getAnnotations(), hasEntry(key, value))
534+
);
535+
labels.forEach((key, value) ->
536+
assertThat(job.getSpec().getTemplate().getMetadata().getLabels(), hasEntry(key, value))
537+
);
538+
539+
}
540+
508541

509542
private V1Secret createSecret(String name) {
510543
return new V1Secret().metadata(new V1ObjectMeta().name(name).namespace(NS));

0 commit comments

Comments
 (0)