Search in sources :

Example 11 with StackGresClusterContext

use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.

the class OwnerReferenceDecorator method decorate.

@Override
public void decorate(StackGresClusterContext context, Iterable<? extends HasMetadata> resources) {
    List<OwnerReference> ownerReferences = List.of(ResourceUtil.getOwnerReference(context.getSource()));
    Seq.seq(resources).filter(resource -> Objects.equals(resource.getMetadata().getNamespace(), context.getSource().getMetadata().getNamespace())).filter(resource -> resource.getMetadata().getOwnerReferences().isEmpty()).forEach(resource -> {
        resource.getMetadata().setOwnerReferences(ownerReferences);
        if (resource.getKind().equals("StatefulSet")) {
            StatefulSet sts = (StatefulSet) resource;
            sts.getSpec().getVolumeClaimTemplates().forEach(vct -> vct.getMetadata().setOwnerReferences(ownerReferences));
        }
    });
}
Also used : Objects(java.util.Objects) List(java.util.List) ResourceUtil(io.stackgres.common.resource.ResourceUtil) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Seq(org.jooq.lambda.Seq) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Decorator(io.stackgres.operator.conciliation.factory.Decorator) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet)

Example 12 with StackGresClusterContext

use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.

the class ContextUtil method toPostgresContext.

public static PostgresContainerContext toPostgresContext(StackGresClusterContainerContext context) {
    final StackGresClusterContext clusterContext = context.getClusterContext();
    final StackGresCluster cluster = clusterContext.getSource();
    ImmutablePostgresContainerContext.Builder contextBuilder = Optional.of(clusterContext.getSource()).map(StackGresCluster::getStatus).map(StackGresClusterStatus::getDbOps).map(StackGresClusterDbOpsStatus::getMajorVersionUpgrade).map(majorVersionUpgradeStatus -> {
        String targetVersion = majorVersionUpgradeStatus.getTargetPostgresVersion();
        String sourceVersion = majorVersionUpgradeStatus.getSourcePostgresVersion();
        String sourceMajorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(sourceVersion);
        return ImmutablePostgresContainerContext.builder().from(context).postgresMajorVersion(StackGresComponent.POSTGRESQL.findMajorVersion(targetVersion)).oldMajorVersion(sourceMajorVersion).imageBuildMajorVersion(StackGresComponent.POSTGRESQL.findBuildMajorVersion(targetVersion)).oldImageBuildMajorVersion(StackGresComponent.POSTGRESQL.findBuildMajorVersion(sourceVersion)).postgresVersion(targetVersion).oldPostgresVersion(sourceVersion);
    }).orElseGet(() -> {
        final String postgresVersion = cluster.getSpec().getPostgres().getVersion();
        final String majorVersion = StackGresComponent.POSTGRESQL.findMajorVersion(postgresVersion);
        final String buildMajorVersion = StackGresComponent.POSTGRESQL.findBuildMajorVersion(postgresVersion);
        return ImmutablePostgresContainerContext.builder().from(context).postgresVersion(postgresVersion).imageBuildMajorVersion(buildMajorVersion).postgresMajorVersion(majorVersion);
    });
    final List<StackGresClusterInstalledExtension> installedExtensions = Optional.ofNullable(cluster.getSpec()).map(StackGresClusterSpec::getToInstallPostgresExtensions).stream().flatMap(Collection::stream).collect(Collectors.toUnmodifiableList());
    contextBuilder.addAllInstalledExtensions(installedExtensions);
    return contextBuilder.build();
}
Also used : StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) StackGresClusterContainerContext(io.stackgres.operator.conciliation.factory.cluster.StackGresClusterContainerContext) StackGresClusterDbOpsStatus(io.stackgres.common.crd.sgcluster.StackGresClusterDbOpsStatus) StackGresDistributedLogsUtil(io.stackgres.common.StackGresDistributedLogsUtil) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) Collection(java.util.Collection) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresDistributedLogsSpec(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogsSpec) DistributedLogsContainerContext(io.stackgres.operator.conciliation.factory.distributedlogs.DistributedLogsContainerContext) Collectors(java.util.stream.Collectors) StackGresComponent(io.stackgres.common.StackGresComponent) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension) Optional(java.util.Optional) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterDbOpsStatus(io.stackgres.common.crd.sgcluster.StackGresClusterDbOpsStatus) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) StackGresClusterInstalledExtension(io.stackgres.common.crd.sgcluster.StackGresClusterInstalledExtension)

Example 13 with StackGresClusterContext

use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.

the class PatroniScriptsConfigMap method buildVolume.

@NotNull
public Stream<Volume> buildVolume(StackGresClusterContext context) {
    var indexedScripts = context.getIndexedScripts();
    var inlineScripts = indexedScripts.stream().filter(t -> t.v1.getScript() != null).map(t -> new VolumeBuilder().withName(name(context, t)).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(name(context, t)).withOptional(false).build()).build()).collect(Collectors.toUnmodifiableList());
    var configMapScripts = indexedScripts.stream().filter(t -> t.v1.getScriptFrom() != null).filter(t -> t.v1.getScriptFrom().getConfigMapKeyRef() != null).map(t -> new VolumeBuilder().withName(name(context, t)).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(t.v1.getScriptFrom().getConfigMapKeyRef().getName()).withOptional(false).build()).build()).collect(Collectors.toUnmodifiableList());
    var secretScripts = indexedScripts.stream().filter(t -> t.v1.getScriptFrom() != null).filter(t -> t.v1.getScriptFrom().getSecretKeyRef() != null).map(t -> new VolumeBuilder().withName(name(context, t)).withSecret(new SecretVolumeSourceBuilder().withSecretName(t.v1.getScriptFrom().getSecretKeyRef().getName()).withOptional(false).build()).build()).collect(Collectors.toUnmodifiableList());
    return ImmutableList.<Volume>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) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with StackGresClusterContext

use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.

the class OwnerReferenceDecorator method decorate.

@Override
public void decorate(StackGresClusterContext context, Iterable<? extends HasMetadata> resources) {
    List<OwnerReference> ownerReferences = getOwnerReferences(context.getSource());
    resources.forEach(resource -> {
        if (!resource.getMetadata().getOwnerReferences().isEmpty()) {
            resource.getMetadata().getOwnerReferences().stream().filter(ownerReference -> ownerReference.getApiVersion().equals(CommonDefinition.GROUP + "/" + CommonDefinition.VERSION)).forEach(ownerReference -> ownerReference.setApiVersion(CommonDefinition.GROUP + "/v1beta1"));
        } else {
            resource.getMetadata().setOwnerReferences(ownerReferences);
            if (resource.getKind().equals("StatefulSet")) {
                StatefulSet sts = (StatefulSet) resource;
                sts.getSpec().getVolumeClaimTemplates().forEach(vct -> vct.getMetadata().setOwnerReferences(ownerReferences));
            }
        }
    });
}
Also used : List(java.util.List) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) CommonDefinition(io.stackgres.common.crd.CommonDefinition) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Decorator(io.stackgres.operator.conciliation.factory.Decorator) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet)

Example 15 with StackGresClusterContext

use of io.stackgres.operator.conciliation.cluster.StackGresClusterContext in project stackgres by ongres.

the class InitRelocateBinaries method getContainer.

@Override
public Container getContainer(StackGresClusterContainerContext context) {
    final StackGresClusterContext clusterContext = context.getClusterContext();
    final String patroniImageName = StackGresComponent.PATRONI.findImageName(StackGresComponent.LATEST, ImmutableMap.of(StackGresComponent.POSTGRESQL, clusterContext.getCluster().getSpec().getPostgres().getVersion()));
    return new ContainerBuilder().withName("relocate-binaries").withImage(patroniImageName).withImagePullPolicy("IfNotPresent").withCommand("/bin/sh", "-ex", ClusterStatefulSetPath.TEMPLATES_PATH.path() + "/" + ClusterStatefulSetPath.LOCAL_BIN_RELOCATE_BINARIES_SH_PATH.filename()).withEnv(postgresExtensionsMounts.getDerivedEnvVars(ContextUtil.toPostgresContext(context))).withVolumeMounts(templateMounts.getVolumeMounts(context)).addToVolumeMounts(new VolumeMountBuilder().withName(context.getDataVolumeName()).withMountPath(ClusterStatefulSetPath.PG_BASE_PATH.path()).build()).build();
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder)

Aggregations

StackGresClusterContext (io.stackgres.operator.conciliation.cluster.StackGresClusterContext)16 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)13 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)8 StackGresVersion (io.stackgres.operator.common.StackGresVersion)8 OperatorVersionBinder (io.stackgres.operator.conciliation.OperatorVersionBinder)8 Singleton (javax.inject.Singleton)8 ImmutableList (com.google.common.collect.ImmutableList)7 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)7 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)7 List (java.util.List)7 Stream (java.util.stream.Stream)7 Inject (javax.inject.Inject)7 Optional (java.util.Optional)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)4 Volume (io.fabric8.kubernetes.api.model.Volume)4 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)4