Skip to content

Commit a8cf40d

Browse files
authored
Include monitoring exporter sidecar and Prometheus annotations for admin server (#2365)
1 parent e9accfc commit a8cf40d

File tree

7 files changed

+278
-295
lines changed

7 files changed

+278
-295
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
import oracle.kubernetes.operator.steps.DeleteDomainStep;
6262
import oracle.kubernetes.operator.steps.DomainPresenceStep;
6363
import oracle.kubernetes.operator.steps.ManagedServersUpStep;
64-
import oracle.kubernetes.operator.steps.MonitorExporterSteps;
64+
import oracle.kubernetes.operator.steps.MonitoringExporterSteps;
6565
import oracle.kubernetes.operator.steps.WatchPodReadyAdminStep;
6666
import oracle.kubernetes.operator.work.Component;
6767
import oracle.kubernetes.operator.work.Fiber;
@@ -1154,7 +1154,7 @@ Step createDomainUpPlan(DomainPresenceInfo info) {
11541154
bringManagedServersUp(null),
11551155
DomainStatusUpdater.createEndProgressingStep(null),
11561156
EventHelper.createEventStep(EventItem.DOMAIN_PROCESSING_COMPLETED),
1157-
MonitorExporterSteps.updateExporterSidecars(),
1157+
MonitoringExporterSteps.updateExporterSidecars(),
11581158
new TailStep());
11591159

11601160
Step domainUpStrategy =

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

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
import java.util.Collections;
88
import java.util.List;
99
import java.util.Map;
10-
import java.util.Objects;
1110
import java.util.Optional;
1211
import javax.annotation.Nonnull;
1312
import javax.annotation.Nullable;
1413

15-
import io.kubernetes.client.openapi.models.V1Container;
16-
import io.kubernetes.client.openapi.models.V1ContainerPort;
1714
import io.kubernetes.client.openapi.models.V1DeleteOptions;
1815
import io.kubernetes.client.openapi.models.V1EnvVar;
1916
import io.kubernetes.client.openapi.models.V1ObjectMeta;
@@ -38,12 +35,9 @@
3835
import oracle.kubernetes.operator.work.NextAction;
3936
import oracle.kubernetes.operator.work.Packet;
4037
import oracle.kubernetes.operator.work.Step;
41-
import oracle.kubernetes.weblogic.domain.model.MonitoringExporterSpecification;
4238
import oracle.kubernetes.weblogic.domain.model.ServerSpec;
4339
import oracle.kubernetes.weblogic.domain.model.Shutdown;
4440

45-
import static oracle.kubernetes.operator.KubernetesConstants.DEFAULT_EXPORTER_SIDECAR_PORT;
46-
import static oracle.kubernetes.operator.KubernetesConstants.EXPORTER_CONTAINER_NAME;
4741
import static oracle.kubernetes.operator.LabelConstants.CLUSTERNAME_LABEL;
4842
import static oracle.kubernetes.operator.LabelConstants.SERVERNAME_LABEL;
4943
import static oracle.kubernetes.operator.ProcessingConstants.SERVERS_TO_ROLL;
@@ -405,26 +399,15 @@ static class ManagedPodStepContext extends PodStepContext {
405399

406400
private final String clusterName;
407401
private final Packet packet;
408-
private final ExporterContext exporterContext;
409402

410403
ManagedPodStepContext(Step conflictStep, Packet packet) {
411404
super(conflictStep, packet);
412405
this.packet = packet;
413406
clusterName = (String) packet.get(ProcessingConstants.CLUSTER_NAME);
414-
exporterContext = createExporterContext();
415407

416408
init();
417409
}
418410

419-
ExporterContext createExporterContext() {
420-
return useSidecar() ? new SidecarExporterContext() : new WebAppExporterContext();
421-
}
422-
423-
// Use the monitoring exporter sidecar if an exporter configuration is part of the domain.
424-
private boolean useSidecar() {
425-
return getDomain().getMonitoringExporterConfiguration() != null;
426-
}
427-
428411
@Override
429412
ServerSpec getServerSpec() {
430413
return getDomain().getServer(getServerName(), getClusterName());
@@ -513,21 +496,6 @@ protected String getPodReplacedMessageKey() {
513496
return MessageKeys.MANAGED_POD_REPLACED;
514497
}
515498

516-
@Override
517-
V1Pod withNonHashedElements(V1Pod pod) {
518-
V1Pod v1Pod = super.withNonHashedElements(pod);
519-
520-
// Add prometheus annotations. This will overwrite any custom annotations with same name.
521-
// Prometheus does not support "prometheus.io/scheme". The scheme(http/https) can be set
522-
// in the Prometheus Chart values yaml under the "extraScrapeConfigs:" section.
523-
if (exporterContext.isEnabled()) {
524-
final V1ObjectMeta metadata = v1Pod.getMetadata();
525-
AnnotationHelper.annotateForPrometheus(metadata, exporterContext.getBasePath(), exporterContext.getPort());
526-
}
527-
528-
return v1Pod;
529-
}
530-
531499
@Override
532500
protected V1ObjectMeta createMetadata() {
533501
V1ObjectMeta metadata = super.createMetadata();
@@ -548,13 +516,6 @@ protected List<String> getContainerCommand() {
548516
return new ArrayList<>(super.getContainerCommand());
549517
}
550518

551-
@Override
552-
protected List<V1Container> getContainers() {
553-
List<V1Container> containers = new ArrayList<>(super.getContainers());
554-
exporterContext.addContainer(containers);
555-
return containers;
556-
}
557-
558519
@Override
559520
@SuppressWarnings("unchecked")
560521
List<V1EnvVar> getConfiguredEnvVars(TuningParameters tuningParameters) {
@@ -564,106 +525,6 @@ List<V1EnvVar> getConfiguredEnvVars(TuningParameters tuningParameters) {
564525
addStartupEnvVars(vars);
565526
return vars;
566527
}
567-
568-
abstract class ExporterContext {
569-
int getWebLogicRestPort() {
570-
return scan.getLocalAdminProtocolChannelPort();
571-
}
572-
573-
boolean isWebLogicSecure() {
574-
return !Objects.equals(getWebLogicRestPort(), getListenPort());
575-
}
576-
577-
abstract boolean isEnabled();
578-
579-
abstract int getPort();
580-
581-
abstract String getBasePath();
582-
583-
abstract void addContainer(List<V1Container> containers);
584-
}
585-
586-
class WebAppExporterContext extends ExporterContext {
587-
588-
@Override
589-
boolean isEnabled() {
590-
return getListenPort() != null;
591-
}
592-
593-
@Override
594-
int getPort() {
595-
return getListenPort();
596-
}
597-
598-
@Override
599-
String getBasePath() {
600-
return "/wls-exporter";
601-
}
602-
603-
@Override
604-
void addContainer(List<V1Container> containers) {
605-
// do nothing
606-
}
607-
}
608-
609-
class SidecarExporterContext extends ExporterContext {
610-
private static final int DEBUG_PORT = 30055;
611-
private final int metricsPort;
612-
613-
public SidecarExporterContext() {
614-
metricsPort = MonitoringExporterSpecification.getRestPort(scan);
615-
}
616-
617-
@Override
618-
boolean isEnabled() {
619-
return true;
620-
}
621-
622-
@Override
623-
int getPort() {
624-
return metricsPort;
625-
}
626-
627-
@Override
628-
String getBasePath() {
629-
return "";
630-
}
631-
632-
@Override
633-
void addContainer(List<V1Container> containers) {
634-
containers.add(createMonitoringExporterContainer());
635-
}
636-
637-
private V1Container createMonitoringExporterContainer() {
638-
return new V1Container()
639-
.name(EXPORTER_CONTAINER_NAME)
640-
.image(getDomain().getMonitoringExporterImage())
641-
.imagePullPolicy(getDomain().getMonitoringExporterImagePullPolicy())
642-
.addEnvItem(new V1EnvVar().name("JAVA_OPTS").value(createJavaOptions()))
643-
.addPortsItem(new V1ContainerPort().name("metrics").protocol("TCP").containerPort(getPort()))
644-
.addPortsItem(new V1ContainerPort().name("debugger").protocol("TCP").containerPort(DEBUG_PORT));
645-
}
646-
647-
private String createJavaOptions() {
648-
final List<String> args = new ArrayList<>();
649-
args.add("-DDOMAIN=" + getDomainUid());
650-
args.add("-DWLS_PORT=" + getWebLogicRestPort());
651-
if (isWebLogicSecure()) {
652-
args.add("-DWLS_SECURE=true");
653-
}
654-
if (metricsPort != DEFAULT_EXPORTER_SIDECAR_PORT) {
655-
args.add("-DEXPORTER_PORT=" + metricsPort);
656-
}
657-
args.add(getDebugOption());
658-
659-
return String.join(" ", args);
660-
}
661-
662-
private String getDebugOption() {
663-
return "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:" + DEBUG_PORT;
664-
}
665-
}
666-
667528
}
668529

669530
static class ManagedPodStep extends Step {

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

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@
6262
import oracle.kubernetes.weblogic.domain.model.Domain;
6363
import oracle.kubernetes.weblogic.domain.model.DomainStatus;
6464
import oracle.kubernetes.weblogic.domain.model.IntrospectorJobEnvVars;
65+
import oracle.kubernetes.weblogic.domain.model.MonitoringExporterSpecification;
6566
import oracle.kubernetes.weblogic.domain.model.ServerEnvVars;
6667
import oracle.kubernetes.weblogic.domain.model.ServerSpec;
6768
import oracle.kubernetes.weblogic.domain.model.Shutdown;
6869
import org.apache.commons.lang3.builder.EqualsBuilder;
6970

7071
import static oracle.kubernetes.operator.IntrospectorConfigMapConstants.NUM_CONFIG_MAPS;
72+
import static oracle.kubernetes.operator.KubernetesConstants.DEFAULT_EXPORTER_SIDECAR_PORT;
73+
import static oracle.kubernetes.operator.KubernetesConstants.EXPORTER_CONTAINER_NAME;
7174
import static oracle.kubernetes.operator.LabelConstants.INTROSPECTION_STATE_LABEL;
7275
import static oracle.kubernetes.operator.LabelConstants.MII_UPDATED_RESTART_REQUIRED_LABEL;
7376
import static oracle.kubernetes.operator.LabelConstants.MODEL_IN_IMAGE_DOMAINZIP_HASH;
@@ -86,6 +89,7 @@ public abstract class PodStepContext extends BasePodStepContext {
8689

8790
private static final String READINESS_PATH = "/weblogic/ready";
8891
private static String productVersion;
92+
protected final ExporterContext exporterContext;
8993

9094
final WlsServerConfig scan;
9195
@Nonnull
@@ -108,6 +112,7 @@ public abstract class PodStepContext extends BasePodStepContext {
108112
domainRestartVersion = (String)packet.get(IntrospectorConfigMapConstants.DOMAIN_RESTART_VERSION);
109113
scan = (WlsServerConfig) packet.get(ProcessingConstants.SERVER_SCAN);
110114
this.packet = packet;
115+
exporterContext = createExporterContext();
111116
}
112117

113118
private static boolean isPatchableItem(Map.Entry<String, String> entry) {
@@ -137,6 +142,15 @@ private Step getConflictStep() {
137142
return new ConflictStep();
138143
}
139144

145+
ExporterContext createExporterContext() {
146+
return useSidecar() ? new SidecarExporterContext() : new WebAppExporterContext();
147+
}
148+
149+
// Use the monitoring exporter sidecar if an exporter configuration is part of the domain.
150+
private boolean useSidecar() {
151+
return getDomain().getMonitoringExporterConfiguration() != null;
152+
}
153+
140154
abstract Map<String, String> getPodLabels();
141155

142156
abstract Map<String, String> getPodAnnotations();
@@ -617,6 +631,14 @@ V1Pod withNonHashedElements(V1Pod pod) {
617631
getContainer(pod).map(V1Container::getEnv).ifPresent(this::updateEnv);
618632

619633
updateForOwnerReference(metadata);
634+
635+
// Add prometheus annotations. This will overwrite any custom annotations with same name.
636+
// Prometheus does not support "prometheus.io/scheme". The scheme(http/https) can be set
637+
// in the Prometheus Chart values yaml under the "extraScrapeConfigs:" section.
638+
if (exporterContext.isEnabled()) {
639+
AnnotationHelper.annotateForPrometheus(metadata, exporterContext.getBasePath(), exporterContext.getPort());
640+
}
641+
620642
return updateForDeepSubstitution(pod.getSpec(), pod);
621643
}
622644

@@ -784,7 +806,9 @@ protected List<String> getContainerCommand() {
784806
}
785807

786808
protected List<V1Container> getContainers() {
787-
return getServerSpec().getContainers();
809+
List<V1Container> containers = new ArrayList<>(getServerSpec().getContainers());
810+
exporterContext.addContainer(containers);
811+
return containers;
788812
}
789813

790814
private List<V1VolumeMount> getVolumeMounts() {
@@ -1132,4 +1156,103 @@ protected V1Pod processResponse(CallResponse<V1Pod> callResponse) {
11321156
return newPod;
11331157
}
11341158
}
1159+
1160+
abstract class ExporterContext {
1161+
int getWebLogicRestPort() {
1162+
return scan.getLocalAdminProtocolChannelPort();
1163+
}
1164+
1165+
boolean isWebLogicSecure() {
1166+
return !Objects.equals(getWebLogicRestPort(), getListenPort());
1167+
}
1168+
1169+
abstract boolean isEnabled();
1170+
1171+
abstract int getPort();
1172+
1173+
abstract String getBasePath();
1174+
1175+
abstract void addContainer(List<V1Container> containers);
1176+
}
1177+
1178+
class WebAppExporterContext extends ExporterContext {
1179+
1180+
@Override
1181+
boolean isEnabled() {
1182+
return getListenPort() != null;
1183+
}
1184+
1185+
@Override
1186+
int getPort() {
1187+
return getListenPort();
1188+
}
1189+
1190+
@Override
1191+
String getBasePath() {
1192+
return "/wls-exporter";
1193+
}
1194+
1195+
@Override
1196+
void addContainer(List<V1Container> containers) {
1197+
// do nothing
1198+
}
1199+
}
1200+
1201+
class SidecarExporterContext extends ExporterContext {
1202+
private static final int DEBUG_PORT = 30055;
1203+
private final int metricsPort;
1204+
1205+
public SidecarExporterContext() {
1206+
metricsPort = MonitoringExporterSpecification.getRestPort(scan);
1207+
}
1208+
1209+
@Override
1210+
boolean isEnabled() {
1211+
return true;
1212+
}
1213+
1214+
@Override
1215+
int getPort() {
1216+
return metricsPort;
1217+
}
1218+
1219+
@Override
1220+
String getBasePath() {
1221+
return "";
1222+
}
1223+
1224+
@Override
1225+
void addContainer(List<V1Container> containers) {
1226+
containers.add(createMonitoringExporterContainer());
1227+
}
1228+
1229+
private V1Container createMonitoringExporterContainer() {
1230+
return new V1Container()
1231+
.name(EXPORTER_CONTAINER_NAME)
1232+
.image(getDomain().getMonitoringExporterImage())
1233+
.imagePullPolicy(getDomain().getMonitoringExporterImagePullPolicy())
1234+
.addEnvItem(new V1EnvVar().name("JAVA_OPTS").value(createJavaOptions()))
1235+
.addPortsItem(new V1ContainerPort().name("metrics").protocol("TCP").containerPort(getPort()))
1236+
.addPortsItem(new V1ContainerPort().name("debugger").protocol("TCP").containerPort(DEBUG_PORT));
1237+
}
1238+
1239+
private String createJavaOptions() {
1240+
final List<String> args = new ArrayList<>();
1241+
args.add("-DDOMAIN=" + getDomainUid());
1242+
args.add("-DWLS_PORT=" + getWebLogicRestPort());
1243+
if (isWebLogicSecure()) {
1244+
args.add("-DWLS_SECURE=true");
1245+
}
1246+
if (metricsPort != DEFAULT_EXPORTER_SIDECAR_PORT) {
1247+
args.add("-DEXPORTER_PORT=" + metricsPort);
1248+
}
1249+
args.add(getDebugOption());
1250+
1251+
return String.join(" ", args);
1252+
}
1253+
1254+
private String getDebugOption() {
1255+
return "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:" + DEBUG_PORT;
1256+
}
1257+
}
11351258
}

0 commit comments

Comments
 (0)