use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class AbstractExtensionsMutator method mutateExtensions.
private ImmutableList<JsonPatchOperation> mutateExtensions(T customResource) {
final ImmutableList.Builder<JsonPatchOperation> operations = ImmutableList.builder();
final StackGresCluster cluster = getCluster(customResource);
List<StackGresClusterExtension> extensions = getExtensions(customResource);
List<StackGresClusterInstalledExtension> missingDefaultExtensions = Seq.seq(getDefaultExtensions(getCluster(customResource))).filter(defaultExtension -> extensions.stream().noneMatch(extension -> extension.getName().equals(defaultExtension.getName()))).collect(ImmutableList.toImmutableList());
final List<StackGresClusterInstalledExtension> toInstallExtensions = Seq.seq(extensions).map(extension -> getToInstallExtension(cluster, extension)).filter(Optional::isPresent).map(Optional::get).append(missingDefaultExtensions).collect(ImmutableList.toImmutableList());
final ArrayNode toInstallExtensionsNode = getObjectMapper().valueToTree(toInstallExtensions);
if (getToInstallExtensions(customResource).orElse(null) == null) {
operations.add(new AddOperation(TO_INSTALL_EXTENSIONS_POINTER, toInstallExtensionsNode));
} else if (getToInstallExtensions(customResource).map(previousToInstallExtensions -> toInstallExtensionsHasChanged(toInstallExtensions, previousToInstallExtensions)).orElse(true)) {
operations.add(new ReplaceOperation(TO_INSTALL_EXTENSIONS_POINTER, toInstallExtensionsNode));
}
Seq.seq(extensions).zipWithIndex().forEach(extension -> toInstallExtensions.stream().filter(toInstallExtension -> toInstallExtension.getName().equals(extension.v1.getName())).findFirst().ifPresent(installedExtension -> onExtensionToInstall(operations, extension.v1, extension.v2.intValue(), installedExtension)));
return operations.build();
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster 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();
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class PatroniSecret method generateResource.
/**
* Create the Secret for patroni associated to the cluster.
*/
@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
final StackGresCluster cluster = context.getSource();
final String name = cluster.getMetadata().getName();
final String namespace = cluster.getMetadata().getNamespace();
final Map<String, String> labels = factoryFactory.clusterLabels(cluster);
Map<String, String> generatedPasswords = context.getDatabaseCredentials().map(Secret::getData).orElse(Map.of());
Map<String, String> data = new HashMap<>();
data.put(SUPERUSER_PASSWORD_KEY, generatedPasswords.getOrDefault(SUPERUSER_PASSWORD_KEY, generatePassword()));
data.put(REPLICATION_PASSWORD_KEY, generatedPasswords.getOrDefault(REPLICATION_PASSWORD_KEY, generatePassword()));
data.put(AUTHENTICATOR_PASSWORD_KEY, generatedPasswords.getOrDefault(AUTHENTICATOR_PASSWORD_KEY, generatePassword()));
data.put(RESTAPI_PASSWORD_KEY, generatedPasswords.getOrDefault(RESTAPI_PASSWORD_KEY, generatePassword()));
return Stream.of(new SecretBuilder().withNewMetadata().withNamespace(namespace).withName(name).withLabels(labels).endMetadata().withType("Opaque").withData(data).build());
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class PatroniServices method generateResource.
/**
* Create the Services associated with the cluster.
*/
@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
final StackGresCluster cluster = context.getSource();
final String namespace = cluster.getMetadata().getNamespace();
final Map<String, String> clusterLabels = labelFactory.clusterLabels(cluster);
Service config = createConfigService(namespace, configName(context), clusterLabels);
Service rest = createPatroniRestService(context);
Seq<HasMetadata> services = Seq.of(config, rest);
boolean isPrimaryServiceEnabled = Optional.of(cluster).map(StackGresCluster::getSpec).map(StackGresClusterSpec::getPostgresServices).map(StackGresClusterPostgresServices::getPrimary).map(StackGresPostgresService::getEnabled).orElse(true);
if (isPrimaryServiceEnabled) {
Service patroni = createPatroniService(context);
services = services.append(patroni);
Service primary = createPrimaryService(context);
services = services.append(primary);
}
boolean isReplicaServiceEnabled = Optional.of(cluster).map(StackGresCluster::getSpec).map(StackGresClusterSpec::getPostgresServices).map(StackGresClusterPostgresServices::getReplicas).map(StackGresPostgresService::getEnabled).orElse(true);
if (isReplicaServiceEnabled) {
Service replicas = createReplicaService(context);
services = services.append(replicas);
}
return services;
}
use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.
the class PatroniServices method createPrimaryService.
private Service createPrimaryService(StackGresClusterContext context) {
StackGresCluster cluster = context.getSource();
final Map<String, String> clusterLabels = labelFactory.clusterLabels(cluster);
return new ServiceBuilder().withNewMetadata().withNamespace(cluster.getMetadata().getNamespace()).withName(readWriteName(context)).withLabels(clusterLabels).endMetadata().withNewSpec().withType("ExternalName").withExternalName(name(context) + "." + cluster.getMetadata().getNamespace() + ".svc.cluster.local").endSpec().build();
}
Aggregations