Search in sources :

Example 36 with StackGresCluster

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();
}
Also used : Logger(org.slf4j.Logger) AddOperation(com.github.fge.jsonpatch.AddOperation) Operation(io.stackgres.operatorframework.admissionwebhook.Operation) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) JsonPointer(com.github.fge.jackson.jsonpointer.JsonPointer) ReplaceOperation(com.github.fge.jsonpatch.ReplaceOperation) Seq(org.jooq.lambda.Seq) AdmissionReview(io.stackgres.operatorframework.admissionwebhook.AdmissionReview) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) StackGresClusterExtension(io.stackgres.common.crd.sgcluster.StackGresClusterExtension) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresExtensionMetadata(io.stackgres.common.extension.StackGresExtensionMetadata) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) ExtensionUtil(io.stackgres.common.extension.ExtensionUtil) JsonPatchMutator(io.stackgres.operatorframework.admissionwebhook.mutating.JsonPatchMutator) Optional(java.util.Optional) AdmissionRequest(io.stackgres.operatorframework.admissionwebhook.AdmissionRequest) CustomResource(io.fabric8.kubernetes.client.CustomResource) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) StackGresClusterExtension(io.stackgres.common.crd.sgcluster.StackGresClusterExtension) AddOperation(com.github.fge.jsonpatch.AddOperation) JsonPatchOperation(com.github.fge.jsonpatch.JsonPatchOperation) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ReplaceOperation(com.github.fge.jsonpatch.ReplaceOperation) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 37 with StackGresCluster

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();
}
Also used : VolumeFactory(io.stackgres.operator.conciliation.factory.VolumeFactory) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) ImmutableVolumePair(io.stackgres.operator.conciliation.factory.ImmutableVolumePair) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) ImmutableList(com.google.common.collect.ImmutableList) Tuple4(org.jooq.lambda.tuple.Tuple4) Locale(java.util.Locale) Volume(io.fabric8.kubernetes.api.model.Volume) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) ResourceUtil(io.stackgres.operatorframework.resource.ResourceUtil) Collectors(java.util.stream.Collectors) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) List(java.util.List) Stream(java.util.stream.Stream) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) NotNull(org.jetbrains.annotations.NotNull) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ImmutableVolumePair(io.stackgres.operator.conciliation.factory.ImmutableVolumePair) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with StackGresCluster

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());
}
Also used : SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) HashMap(java.util.HashMap)

Example 39 with StackGresCluster

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;
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresPostgresService(io.stackgres.common.crd.postgres.service.StackGresPostgresService) Service(io.fabric8.kubernetes.api.model.Service) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) StackGresClusterPostgresServices(io.stackgres.common.crd.sgcluster.StackGresClusterPostgresServices)

Example 40 with StackGresCluster

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();
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Aggregations

StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)127 Test (org.junit.jupiter.api.Test)48 Optional (java.util.Optional)32 List (java.util.List)31 StackGresClusterSpec (io.stackgres.common.crd.sgcluster.StackGresClusterSpec)27 StackGresClusterInstalledExtension (io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)25 ImmutableList (com.google.common.collect.ImmutableList)24 Inject (javax.inject.Inject)22 ArrayList (java.util.ArrayList)19 StackGresClusterStatus (io.stackgres.common.crd.sgcluster.StackGresClusterStatus)18 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)15 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 StackGresComponent (io.stackgres.common.StackGresComponent)15 StackGresClusterPodStatus (io.stackgres.common.crd.sgcluster.StackGresClusterPodStatus)14 ExtensionInstaller (io.stackgres.common.extension.ExtensionManager.ExtensionInstaller)14 StackGresClusterReview (io.stackgres.operator.common.StackGresClusterReview)13 Collectors (java.util.stream.Collectors)13 BeforeEach (org.junit.jupiter.api.BeforeEach)13 ExtensionUninstaller (io.stackgres.common.extension.ExtensionManager.ExtensionUninstaller)12 JsonUtil (io.stackgres.testutil.JsonUtil)12