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();
}
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;
}
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();
}
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;
}
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);
}
Aggregations