Search in sources :

Example 46 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class BackupCronJob method createCronJob.

private CronJob createCronJob(StackGresClusterContext context, StackGresBackupConfig backupConfig) {
    String namespace = context.getSource().getMetadata().getNamespace();
    String name = context.getSource().getMetadata().getName();
    final StackGresCluster cluster = context.getSource();
    Map<String, String> labels = labelFactory.scheduledBackupPodLabels(cluster);
    return new CronJobBuilder().withNewMetadata().withNamespace(namespace).withName(backupName(context)).withLabels(labels).endMetadata().withNewSpec().withConcurrencyPolicy("Allow").withFailedJobsHistoryLimit(10).withStartingDeadlineSeconds(5 * 60L).withSchedule(Optional.of(backupConfig).map(StackGresBackupConfig::getSpec).map(StackGresBackupConfigSpec::getBaseBackups).map(StackGresBaseBackupConfig::getCronSchedule).orElse("0 5 * * *")).withJobTemplate(new JobTemplateSpecBuilder().withNewMetadata().withNamespace(namespace).withName(backupName(context)).withLabels(labels).endMetadata().withNewSpec().withBackoffLimit(3).withCompletions(1).withParallelism(1).withNewTemplate().withNewMetadata().withNamespace(namespace).withName(backupName(context)).withLabels(labels).endMetadata().withNewSpec().withSecurityContext(podSecurityFactory.createResource(context)).withRestartPolicy("OnFailure").withServiceAccountName(PatroniRoleGenerator.roleName(context)).withContainers(new ContainerBuilder().withName("create-backup").withImage(StackGresComponent.KUBECTL.findLatestImageName()).withImagePullPolicy("IfNotPresent").withEnv(ImmutableList.<EnvVar>builder().addAll(getClusterEnvVars(context)).add(new EnvVarBuilder().withName("CLUSTER_NAMESPACE").withValue(namespace).build(), new EnvVarBuilder().withName("CLUSTER_NAME").withValue(name).build(), new EnvVarBuilder().withName("CRONJOB_NAME").withValue(backupName(context)).build(), new EnvVarBuilder().withName("BACKUP_CONFIG_CRD_NAME").withValue(CustomResource.getCRDName(StackGresBackupConfig.class)).build(), new EnvVarBuilder().withName("BACKUP_CONFIG").withValue(backupConfig.getMetadata().getName()).build(), new EnvVarBuilder().withName("BACKUP_CRD_KIND").withValue(HasMetadata.getKind(StackGresBackup.class)).build(), new EnvVarBuilder().withName("BACKUP_CRD_NAME").withValue(CustomResource.getCRDName(StackGresBackup.class)).build(), new EnvVarBuilder().withName("BACKUP_CRD_APIVERSION").withValue(HasMetadata.getApiVersion(StackGresBackup.class)).build(), new EnvVarBuilder().withName("BACKUP_PHASE_RUNNING").withValue(BackupPhase.RUNNING.label()).build(), new EnvVarBuilder().withName("BACKUP_PHASE_COMPLETED").withValue(BackupPhase.COMPLETED.label()).build(), new EnvVarBuilder().withName("BACKUP_PHASE_FAILED").withValue(BackupPhase.FAILED.label()).build(), new EnvVarBuilder().withName("PATRONI_ROLE_KEY").withValue(StackGresContext.ROLE_KEY).build(), new EnvVarBuilder().withName("PATRONI_PRIMARY_ROLE").withValue(StackGresContext.PRIMARY_ROLE).build(), new EnvVarBuilder().withName("PATRONI_REPLICA_ROLE").withValue(StackGresContext.REPLICA_ROLE).build(), new EnvVarBuilder().withName("SCHEDULED_BACKUP_KEY").withValue(StackGresContext.SCHEDULED_BACKUP_KEY).build(), new EnvVarBuilder().withName("RIGHT_VALUE").withValue(StackGresContext.RIGHT_VALUE).build(), new EnvVarBuilder().withName("PATRONI_CLUSTER_LABELS").withValue(labelFactory.patroniClusterLabels(cluster).entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","))).build(), new EnvVarBuilder().withName("PATRONI_CONTAINER_NAME").withValue(StackgresClusterContainers.PATRONI).build(), new EnvVarBuilder().withName("SERVICE_ACCOUNT").withValueFrom(new EnvVarSourceBuilder().withFieldRef(new ObjectFieldSelectorBuilder().withFieldPath("spec.serviceAccountName").build()).build()).build(), new EnvVarBuilder().withName("POD_NAME").withValueFrom(new EnvVarSourceBuilder().withFieldRef(new ObjectFieldSelectorBuilder().withFieldPath("metadata.name").build()).build()).build(), new EnvVarBuilder().withName("RETAIN").withValue(Optional.of(backupConfig).map(StackGresBackupConfig::getSpec).map(StackGresBackupConfigSpec::getBaseBackups).map(StackGresBaseBackupConfig::getRetention).map(String::valueOf).orElse("5")).build(), new EnvVarBuilder().withName("WINDOW").withValue("3600").build()).build()).withCommand("/bin/bash", "-e" + (BACKUP_LOGGER.isTraceEnabled() ? "x" : ""), ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.path()).withVolumeMounts(ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_CREATE_BACKUP_SH_PATH.path()).withReadOnly(true)), ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_SHELL_UTILS_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_SHELL_UTILS_PATH.path()).withReadOnly(true))).build()).withVolumes(new VolumeBuilder(ClusterStatefulSetVolumeConfig.TEMPLATES.volume(context)).editConfigMap().withDefaultMode(// NOPMD
    0555).endConfigMap().build()).endSpec().endTemplate().endSpec().build()).endSpec().build();
}
Also used : StackGresContext(io.stackgres.common.StackGresContext) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) StackGresBackupConfigSpec(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) StackgresClusterContainers(io.stackgres.common.StackgresClusterContainers) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ClusterStatefulSetPath(io.stackgres.common.ClusterStatefulSetPath) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) Map(java.util.Map) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) ObjectFieldSelectorBuilder(io.fabric8.kubernetes.api.model.ObjectFieldSelectorBuilder) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) JobTemplateSpecBuilder(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpecBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) StackGresBaseBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBaseBackupConfig) ResourceFactory(io.stackgres.operator.conciliation.factory.ResourceFactory) ClusterContext(io.stackgres.common.ClusterContext) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) Logger(org.slf4j.Logger) PatroniRoleGenerator(io.stackgres.operator.conciliation.factory.cluster.patroni.PatroniRoleGenerator) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) StackGresUtil(io.stackgres.common.StackGresUtil) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) BackupPhase(io.stackgres.common.crd.sgbackup.BackupPhase) CronJobBuilder(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJobBuilder) StackGresComponent(io.stackgres.common.StackGresComponent) List(java.util.List) Stream(java.util.stream.Stream) ClusterStatefulSetVolumeConfig(io.stackgres.operator.conciliation.factory.cluster.patroni.ClusterStatefulSetVolumeConfig) ClusterEnvironmentVariablesFactoryDiscoverer(io.stackgres.operator.conciliation.factory.cluster.patroni.ClusterEnvironmentVariablesFactoryDiscoverer) Optional(java.util.Optional) ClusterEnvironmentVariablesFactory(io.stackgres.operator.conciliation.factory.cluster.patroni.ClusterEnvironmentVariablesFactory) CustomResource(io.fabric8.kubernetes.client.CustomResource) ObjectFieldSelectorBuilder(io.fabric8.kubernetes.api.model.ObjectFieldSelectorBuilder) CronJobBuilder(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJobBuilder) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) StackGresBackupConfigSpec(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfigSpec) JobTemplateSpecBuilder(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpecBuilder) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 47 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class Patroni method postInitName.

public String postInitName(StackGresClusterContext clusterContext) {
    final StackGresCluster cluster = clusterContext.getSource();
    final String clusterName = labelFactory.resourceName(cluster);
    return clusterName + POST_INIT_SUFFIX;
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) IntOrString(io.fabric8.kubernetes.api.model.IntOrString)

Example 48 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster 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";
    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().withName(PATRONI_RESTAPI_PORT_NAME).withProtocol("TCP").withContainerPort(EnvoyUtil.PATRONI_ENTRY_PORT).build()).withVolumeMounts(getVolumeMounts(context)).withEnvFrom(new EnvFromSourceBuilder().withConfigMapRef(new ConfigMapEnvSourceBuilder().withName(PatroniConfigMap.name(clusterContext)).build()).build()).withEnv(ImmutableList.<EnvVar>builder().addAll(getClusterEnvVars(clusterContext)).addAll(patroniEnvironmentVariables.createResource(clusterContext)).build()).withLivenessProbe(new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/cluster").withPort(new IntOrString(8008)).withScheme("HTTP").build()).withInitialDelaySeconds(15).withPeriodSeconds(20).withFailureThreshold(6).build()).withReadinessProbe(new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/read-only").withPort(new IntOrString(8008)).withScheme("HTTP").build()).withInitialDelaySeconds(5).withPeriodSeconds(10).build()).withResources(podResources).build();
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) HTTPGetActionBuilder(io.fabric8.kubernetes.api.model.HTTPGetActionBuilder) ProbeBuilder(io.fabric8.kubernetes.api.model.ProbeBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) EnvFromSourceBuilder(io.fabric8.kubernetes.api.model.EnvFromSourceBuilder) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) ConfigMapEnvSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapEnvSourceBuilder)

Example 49 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class Patroni method postInitName.

public String postInitName(StackGresClusterContext clusterContext) {
    final StackGresCluster cluster = clusterContext.getSource();
    final String clusterName = labelFactory.resourceName(cluster);
    return clusterName + POST_INIT_SUFFIX;
}
Also used : StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) IntOrString(io.fabric8.kubernetes.api.model.IntOrString)

Example 50 with StackGresCluster

use of io.stackgres.common.crd.sgcluster.StackGresCluster in project stackgres by ongres.

the class ClusterStatefulSet method generateResource.

@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
    final StackGresCluster cluster = context.getSource();
    final ObjectMeta metadata = cluster.getMetadata();
    final String name = metadata.getName();
    final String namespace = metadata.getNamespace();
    final StackGresPodPersistentVolume persistentVolume = cluster.getSpec().getPod().getPersistentVolume();
    StorageConfig dataStorageConfig = ImmutableStorageConfig.builder().size(persistentVolume.getSize()).storageClass(Optional.ofNullable(persistentVolume.getStorageClass()).orElse(null)).build();
    final Map<String, String> labels = labelFactory.clusterLabels(cluster);
    final Map<String, String> podLabels = labelFactory.statefulSetPodLabels(cluster);
    Map<String, VolumePair> availableVolumesPairs = volumeDiscoverer.discoverVolumes(context);
    final PersistentVolumeClaimSpecBuilder volumeClaimSpec = new PersistentVolumeClaimSpecBuilder().withAccessModes("ReadWriteOnce").withResources(dataStorageConfig.getResourceRequirements()).withStorageClassName(dataStorageConfig.getStorageClass());
    Map<String, Volume> availableVolumes = availableVolumesPairs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, vp -> vp.getValue().getVolume()));
    final PodTemplateResult podTemplateSpec = podTemplateSpecFactory.getPodTemplateSpec(ImmutableStackGresClusterContainerContext.builder().clusterContext(context).availableVolumes(availableVolumes).dataVolumeName(dataName(context)).build());
    StatefulSet clusterStatefulSet = new StatefulSetBuilder().withNewMetadata().withNamespace(namespace).withName(name).withLabels(labels).endMetadata().withNewSpec().withReplicas(cluster.getSpec().getInstances()).withSelector(new LabelSelectorBuilder().addToMatchLabels(podLabels).build()).withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withServiceName(name).withTemplate(podTemplateSpec.getSpec()).withVolumeClaimTemplates(new PersistentVolumeClaimBuilder().withNewMetadata().withNamespace(namespace).withName(dataName(context)).withLabels(labels).endMetadata().withSpec(volumeClaimSpec.build()).build()).endSpec().build();
    var volumeDependencies = podTemplateSpec.claimedVolumes().stream().map(availableVolumesPairs::get).filter(Objects::nonNull).map(VolumePair::getSource).filter(Optional::isPresent).map(Optional::get);
    return Stream.concat(Stream.of(clusterStatefulSet), volumeDependencies);
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) ImmutableStorageConfig(io.stackgres.common.ImmutableStorageConfig) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) PersistentVolumeClaimSpecBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) VolumeDiscoverer(io.stackgres.operator.conciliation.factory.VolumeDiscoverer) Map(java.util.Map) Volume(io.fabric8.kubernetes.api.model.Volume) ClusterContext(io.stackgres.common.ClusterContext) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) PodTemplateFactory(io.stackgres.operator.conciliation.factory.PodTemplateFactory) StackGresUtil(io.stackgres.common.StackGresUtil) StorageConfig(io.stackgres.common.StorageConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Objects(java.util.Objects) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Stream(java.util.stream.Stream) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) Optional(java.util.Optional) ImmutableStorageConfig(io.stackgres.common.ImmutableStorageConfig) StorageConfig(io.stackgres.common.StorageConfig) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) VolumePair(io.stackgres.operator.conciliation.factory.VolumePair) PodTemplateResult(io.stackgres.operator.conciliation.factory.PodTemplateResult) Volume(io.fabric8.kubernetes.api.model.Volume) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) StackGresPodPersistentVolume(io.stackgres.common.crd.sgcluster.StackGresPodPersistentVolume) PersistentVolumeClaimSpecBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpecBuilder) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Map(java.util.Map)

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