use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.
the class AbstractEnvoy method buildSource.
@NotNull
public HasMetadata buildSource(StackGresClusterContext context) {
final StackGresCluster stackGresCluster = context.getSource();
boolean disablePgBouncer = Optional.ofNullable(stackGresCluster.getSpec()).map(StackGresClusterSpec::getPod).map(StackGresClusterPod::getDisableConnectionPooling).orElse(false);
final String envoyConfPath = getEnvoyConfigPath(stackGresCluster, disablePgBouncer);
YAMLMapper yamlMapper = yamlMapperProvider.yamlMapper();
final ObjectNode envoyConfig;
try {
envoyConfig = (ObjectNode) yamlMapper.readTree(Envoy.class.getResource(envoyConfPath));
} catch (Exception ex) {
throw new IllegalStateException("couldn't read envoy config file", ex);
}
Seq.seq(envoyConfig.get("static_resources").get("listeners")).map(listener -> listener.get("address").get("socket_address")).cast(ObjectNode.class).forEach(socketAddress -> socketAddress.put("port_value", LISTEN_SOCKET_ADDRESS_PORT_MAPPING.get(socketAddress.get("port_value").asText())));
Seq.seq(envoyConfig.get("static_resources").get("clusters")).flatMap(cluster -> Seq.seq(cluster.get("load_assignment").get("endpoints").elements())).flatMap(endpoint -> Seq.seq(endpoint.get("lb_endpoints").elements())).map(endpoint -> endpoint.get("endpoint").get("address").get("socket_address")).cast(ObjectNode.class).forEach(socketAddress -> socketAddress.put("port_value", CLUSTER_SOCKET_ADDRESS_PORT_MAPPING.get(socketAddress.get("port_value").asText())));
final Map<String, String> data;
try {
data = ImmutableMap.of("default_envoy.yaml", yamlMapper.writeValueAsString(envoyConfig));
} catch (Exception ex) {
throw new IllegalStateException("couldn't parse envoy config file", ex);
}
String namespace = stackGresCluster.getMetadata().getNamespace();
String configMapName = AbstractEnvoy.configName(context);
return new ConfigMapBuilder().withNewMetadata().withNamespace(namespace).withName(configMapName).withLabels(labelFactory.clusterLabels(stackGresCluster)).endMetadata().withData(data).build();
}
use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.
the class PrometheusIntegration method generateResource.
@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
final StackGresCluster stackGresCluster = context.getSource();
final Map<String, String> defaultLabels = labelFactory.clusterLabels(stackGresCluster);
Map<String, String> crossNamespaceLabels = labelFactory.clusterCrossNamespaceLabels(stackGresCluster);
Seq<HasMetadata> resources = Seq.of(new ServiceBuilder().withNewMetadata().withNamespace(stackGresCluster.getMetadata().getNamespace()).withName(AbstractEnvoy.serviceName(context)).withLabels(ImmutableMap.<String, String>builder().putAll(crossNamespaceLabels).put("container", AbstractEnvoy.NAME).build()).endMetadata().withSpec(new ServiceSpecBuilder().withSelector(defaultLabels).withPorts(new ServicePortBuilder().withProtocol("TCP").withName(AbstractEnvoy.NAME).withPort(8001).build()).build()).build());
Optional<Stream<HasMetadata>> serviceMonitors = context.getPrometheus().filter(c -> Optional.ofNullable(c.getCreateServiceMonitor()).orElse(false)).map(c -> getServiceMonitors(context, crossNamespaceLabels, c));
return serviceMonitors.map(hasMetadataStream -> Stream.concat(resources, hasMetadataStream)).orElse(resources);
}
use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.
the class PrometheusIntegration method generateResource.
@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
final StackGresCluster cluster = context.getSource();
final Map<String, String> defaultLabels = labelFactory.clusterLabels(cluster);
Map<String, String> labels = new ImmutableMap.Builder<String, String>().putAll(labelFactory.clusterCrossNamespaceLabels(cluster)).build();
final String clusterNamespace = cluster.getMetadata().getNamespace();
final Stream<HasMetadata> resources = Stream.of(new ServiceBuilder().withNewMetadata().withNamespace(clusterNamespace).withName(serviceName(context)).withLabels(ImmutableMap.<String, String>builder().putAll(labels).put("container", POSTGRES_EXPORTER_CONTAINER_NAME).build()).endMetadata().withSpec(new ServiceSpecBuilder().withSelector(defaultLabels).withPorts(new ServicePortBuilder().withName(POSTGRES_EXPORTER_CONTAINER_NAME).withProtocol("TCP").withPort(9187).build()).build()).build());
Optional<Stream<HasMetadata>> serviceMonitors = context.getPrometheus().filter(c -> Optional.ofNullable(c.getCreateServiceMonitor()).orElse(false)).map(c -> c.getPrometheusInstallations().stream().map(pi -> {
ServiceMonitor serviceMonitor = new ServiceMonitor();
serviceMonitor.setMetadata(new ObjectMetaBuilder().withNamespace(pi.getNamespace()).withName(serviceMonitorName(context)).withLabels(ImmutableMap.<String, String>builder().putAll(pi.getMatchLabels()).putAll(labels).build()).build());
ServiceMonitorSpec spec = new ServiceMonitorSpec();
serviceMonitor.setSpec(spec);
LabelSelector selector = new LabelSelector();
spec.setSelector(selector);
NamespaceSelector namespaceSelector = new NamespaceSelector();
namespaceSelector.setMatchNames(ImmutableList.of(clusterNamespace));
spec.setNamespaceSelector(namespaceSelector);
selector.setMatchLabels(labels);
Endpoint endpoint = new Endpoint();
endpoint.setPort(POSTGRES_EXPORTER_CONTAINER_NAME);
spec.setEndpoints(Collections.singletonList(endpoint));
return serviceMonitor;
}));
return serviceMonitors.map(hasMetadataStream -> Stream.concat(resources, hasMetadataStream)).orElse(resources);
}
use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.
the class Patroni method getContainer.
@Override
public Container getContainer(StackGresClusterContainerContext context) {
final StackGresClusterContext clusterContext = context.getClusterContext();
final StackGresCluster cluster = clusterContext.getSource();
final String patroniImageName = String.format(IMAGE_NAME, cluster.getSpec().getPostgres().getVersion());
ResourceRequirements podResources = requirementsFactory.createResource(clusterContext);
final String startScript = clusterContext.getRestoreBackup().isPresent() ? "/start-patroni-with-restore.sh" : "/start-patroni.sh";
final PostgresContainerContext postgresContext = ContextUtil.toPostgresContext(context);
ImmutableList.Builder<VolumeMount> volumeMounts = ImmutableList.<VolumeMount>builder().addAll(postgresSocket.getVolumeMounts(context)).add(new VolumeMountBuilder().withName(PatroniStaticVolume.DSHM.getVolumeName()).withMountPath(ClusterStatefulSetPath.SHARED_MEMORY_PATH.path()).build()).add(new VolumeMountBuilder().withName(PatroniStaticVolume.LOG.getVolumeName()).withMountPath(ClusterStatefulSetPath.PG_LOG_PATH.path()).build()).addAll(localBinMounts.getVolumeMounts(context)).add(new VolumeMountBuilder().withName(StatefulSetDynamicVolumes.PATRONI_ENV.getVolumeName()).withMountPath("/etc/env/patroni").build(), new VolumeMountBuilder().withName(PatroniStaticVolume.PATRONI_CONFIG.getVolumeName()).withMountPath("/etc/patroni").build()).addAll(backupMounts.getVolumeMounts(context)).addAll(postgresExtensions.getVolumeMounts(postgresContext));
clusterContext.getRestoreBackup().ifPresent(ignore -> volumeMounts.addAll(restoreMounts.getVolumeMounts(context)));
return new ContainerBuilder().withName(StackgresClusterContainers.PATRONI).withImage(patroniImageName).withCommand("/bin/sh", "-ex", ClusterStatefulSetPath.LOCAL_BIN_PATH.path() + startScript).withImagePullPolicy("IfNotPresent").withPorts(new ContainerPortBuilder().withProtocol("TCP").withName(PatroniConfigMap.POSTGRES_PORT_NAME).withContainerPort(EnvoyUtil.PG_ENTRY_PORT).build(), new ContainerPortBuilder().withProtocol("TCP").withName(PatroniConfigMap.POSTGRES_REPLICATION_PORT_NAME).withContainerPort(EnvoyUtil.PG_REPL_ENTRY_PORT).build(), new ContainerPortBuilder().withProtocol("TCP").withName(PATRONI_RESTAPI_PORT_NAME).withProtocol("TCP").withContainerPort(EnvoyUtil.PATRONI_ENTRY_PORT).build()).withVolumeMounts(volumeMounts.build()).addToVolumeMounts(clusterContext.getIndexedScripts().stream().map(t -> new VolumeMountBuilder().withName(PatroniScriptsConfigMap.name(clusterContext, t)).withMountPath("/etc/patroni/init-script.d/" + PatroniScriptsConfigMap.scriptName(t)).withSubPath(t.v1.getScript() != null ? PatroniScriptsConfigMap.scriptName(t) : t.v1.getScriptFrom().getConfigMapKeyRef() != null ? t.v1.getScriptFrom().getConfigMapKeyRef().getKey() : t.v1.getScriptFrom().getSecretKeyRef().getKey()).withReadOnly(true).build()).toArray(VolumeMount[]::new)).withEnvFrom(new EnvFromSourceBuilder().withConfigMapRef(new ConfigMapEnvSourceBuilder().withName(PatroniConfigMap.name(clusterContext)).build()).build()).withEnv(getEnvVars(context)).withLivenessProbe(new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/cluster").withPort(new IntOrString(EnvoyUtil.PATRONI_ENTRY_PORT)).withScheme("HTTP").build()).withInitialDelaySeconds(15).withPeriodSeconds(20).withFailureThreshold(6).build()).withReadinessProbe(new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/read-only").withPort(new IntOrString(EnvoyUtil.PATRONI_ENTRY_PORT)).withScheme("HTTP").build()).withInitialDelaySeconds(5).withPeriodSeconds(10).build()).withResources(podResources).build();
}
use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.
the class PatroniScriptsConfigMap method buildVolumes.
@Override
@NotNull
public Stream<VolumePair> buildVolumes(StackGresClusterContext context) {
StackGresCluster cluster = context.getSource();
var indexedScripts = context.getIndexedScripts();
List<VolumePair> inlineScripts = indexedScripts.stream().filter(t -> t.v1.getScript() != null).map(t -> ImmutableVolumePair.builder().volume(new VolumeBuilder().withName(name(context, t)).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(name(context, t)).withOptional(false).build()).build()).source(new ConfigMapBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(name(context, t)).withLabels(labelFactory.patroniClusterLabels(cluster)).endMetadata().withData(ImmutableMap.of(scriptName(t), t.v1.getScript())).build()).build()).collect(Collectors.toUnmodifiableList());
List<VolumePair> configMapScripts = indexedScripts.stream().filter(t -> t.v1.getScriptFrom() != null).filter(t -> t.v1.getScriptFrom().getConfigMapKeyRef() != null).map(t -> ImmutableVolumePair.builder().volume(new VolumeBuilder().withName(name(context, t)).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(t.v1.getScriptFrom().getConfigMapKeyRef().getName()).withOptional(false).build()).build()).build()).collect(Collectors.toUnmodifiableList());
List<VolumePair> secretScripts = indexedScripts.stream().filter(t -> t.v1.getScriptFrom() != null).filter(t -> t.v1.getScriptFrom().getSecretKeyRef() != null).map(t -> ImmutableVolumePair.builder().volume(new VolumeBuilder().withName(name(context, t)).withSecret(new SecretVolumeSourceBuilder().withSecretName(t.v1.getScriptFrom().getSecretKeyRef().getName()).withOptional(false).build()).build()).build()).collect(Collectors.toUnmodifiableList());
return ImmutableList.<VolumePair>builder().addAll(inlineScripts).addAll(configMapScripts).addAll(secretScripts).build().stream();
}
Aggregations