use of io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext in project stackgres by ongres.
the class DbOpsJob method createJob.
@Override
public Job createJob(StackGresDbOpsContext context) {
final StackGresDbOps dbOps = context.getSource();
final String retries = String.valueOf(DbOpsUtil.getCurrentRetry(dbOps));
List<EnvVar> runEnvVars = getRunEnvVars(context);
List<EnvVar> setResultEnvVars = getSetResultEnvVars(context);
final String namespace = dbOps.getMetadata().getNamespace();
final String name = dbOps.getMetadata().getName();
final Map<String, String> labels = dbOpsLabelFactory.dbOpsPodLabels(context.getSource());
final String timeout = DbOpsUtil.getTimeout(dbOps);
return new JobBuilder().withNewMetadata().withNamespace(namespace).withName(jobName(dbOps)).withLabels(labels).endMetadata().withNewSpec().withBackoffLimit(0).withCompletions(1).withParallelism(1).withNewTemplate().withNewMetadata().withNamespace(namespace).withName(jobName(dbOps)).withLabels(labels).endMetadata().withNewSpec().withSecurityContext(podSecurityFactory.createResource(context)).withRestartPolicy("Never").withServiceAccountName(DbOpsRole.roleName(context)).withInitContainers(new ContainerBuilder().withName("set-dbops-running").withImage(getSetResultImage()).withImagePullPolicy("IfNotPresent").withEnv(ImmutableList.<EnvVar>builder().addAll(clusterEnvironmentVariables.listResources(context)).add(new EnvVarBuilder().withName("OP_NAME").withValue(dbOps.getSpec().getOp()).build(), new EnvVarBuilder().withName("NORMALIZED_OP_NAME").withValue(UPPERCASE_PATTERN.matcher(dbOps.getSpec().getOp()).replaceAll(result -> " " + result.group(1).toLowerCase(Locale.US))).build(), new EnvVarBuilder().withName("KEBAB_OP_NAME").withValue(UPPERCASE_PATTERN.matcher(dbOps.getSpec().getOp()).replaceAll(result -> "-" + result.group(1).toLowerCase(Locale.US))).build(), new EnvVarBuilder().withName("CLUSTER_NAMESPACE").withValue(namespace).build(), new EnvVarBuilder().withName("DB_OPS_NAME").withValue(name).build(), new EnvVarBuilder().withName("DB_OPS_CRD_NAME").withValue(CustomResource.getCRDName(StackGresDbOps.class)).build(), new EnvVarBuilder().withName("CURRENT_RETRY").withValue(retries).build()).addAll(Seq.of(DbOpsStatusCondition.values()).map(c -> new EnvVarBuilder().withName("CONDITION_" + c.name()).withValue(conditions.get(c)).build()).toList()).build()).withCommand("/bin/sh", "-ex", ClusterStatefulSetPath.LOCAL_BIN_SET_DBOPS_RUNNING_SH_PATH.path()).withVolumeMounts(ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_SET_DBOPS_RUNNING_SH_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_SET_DBOPS_RUNNING_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()).withContainers(new ContainerBuilder().withName("run-dbops").withImage(getRunImage(context)).withImagePullPolicy("IfNotPresent").withEnv(ImmutableList.<EnvVar>builder().addAll(clusterEnvironmentVariables.listResources(context)).add(new EnvVarBuilder().withName("OP_NAME").withValue(dbOps.getSpec().getOp()).build(), new EnvVarBuilder().withName("EXCLUSIVE_OP").withValue(String.valueOf(isExclusiveOp())).build(), new EnvVarBuilder().withName("NORMALIZED_OP_NAME").withValue(UPPERCASE_PATTERN.matcher(dbOps.getSpec().getOp()).replaceAll(result -> " " + result.group(1).toLowerCase(Locale.US))).build(), new EnvVarBuilder().withName("KEBAB_OP_NAME").withValue(UPPERCASE_PATTERN.matcher(dbOps.getSpec().getOp()).replaceAll(result -> "-" + result.group(1).toLowerCase(Locale.US))).build(), new EnvVarBuilder().withName("RUN_SCRIPT_PATH").withValue(Optional.ofNullable(getRunScript()).map(ClusterStatefulSetPath::path).orElse("")).build(), new EnvVarBuilder().withName("TIMEOUT").withValue(timeout).build()).addAll(runEnvVars).build()).withCommand("/bin/sh", "-ex", ClusterStatefulSetPath.LOCAL_BIN_RUN_DBOPS_SH_PATH.path()).withVolumeMounts(ClusterStatefulSetVolumeConfig.SHARED.volumeMount(context), ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_RUN_DBOPS_SH_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_RUN_DBOPS_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)), ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(getRunScript().filename()).withMountPath(getRunScript().path()).withReadOnly(true))).build(), new ContainerBuilder().withName("set-dbops-result").withImage(StackGresComponent.KUBECTL.findLatestImageName()).withImagePullPolicy("IfNotPresent").withEnv(ImmutableList.<EnvVar>builder().addAll(clusterEnvironmentVariables.listResources(context)).add(new EnvVarBuilder().withName("OP_NAME").withValue(dbOps.getSpec().getOp()).build(), new EnvVarBuilder().withName("NORMALIZED_OP_NAME").withValue(UPPERCASE_PATTERN.matcher(dbOps.getSpec().getOp()).replaceAll(result -> " " + result.group(1).toLowerCase(Locale.US))).build(), new EnvVarBuilder().withName("KEBAB_OP_NAME").withValue(UPPERCASE_PATTERN.matcher(dbOps.getSpec().getOp()).replaceAll(result -> "-" + result.group(1).toLowerCase(Locale.US))).build(), new EnvVarBuilder().withName("SET_RESULT_SCRIPT_PATH").withValue(Optional.ofNullable(getSetResultScript()).map(ClusterStatefulSetPath::path).orElse("")).build(), new EnvVarBuilder().withName("CLUSTER_NAMESPACE").withValue(namespace).build(), new EnvVarBuilder().withName("DB_OPS_NAME").withValue(name).build(), new EnvVarBuilder().withName("DB_OPS_CRD_NAME").withValue(CustomResource.getCRDName(StackGresDbOps.class)).build(), new EnvVarBuilder().withName("JOB_POD_LABELS").withValue(Seq.seq(labels).append(Tuple.tuple("job-name", jobName(dbOps))).map(t -> t.v1 + "=" + t.v2).toString(",")).build()).addAll(Seq.of(DbOpsStatusCondition.values()).map(c -> new EnvVarBuilder().withName("CONDITION_" + c.name()).withValue(conditions.get(c)).build()).toList()).addAll(setResultEnvVars).build()).withCommand("/bin/sh", "-ex", ClusterStatefulSetPath.LOCAL_BIN_SET_DBOPS_RESULT_SH_PATH.path()).withVolumeMounts(ClusterStatefulSetVolumeConfig.SHARED.volumeMount(context), ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(ClusterStatefulSetPath.LOCAL_BIN_SET_DBOPS_RESULT_SH_PATH.filename()).withMountPath(ClusterStatefulSetPath.LOCAL_BIN_SET_DBOPS_RESULT_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))).addAllToVolumeMounts(Optional.ofNullable(getSetResultScript()).map(script -> ClusterStatefulSetVolumeConfig.TEMPLATES.volumeMount(context, volumeMountBuilder -> volumeMountBuilder.withSubPath(script.filename()).withMountPath(script.path()).withReadOnly(true))).stream().collect(Collectors.toList())).build()).withVolumes(ClusterStatefulSetVolumeConfig.SHARED.volume(context), new VolumeBuilder(ClusterStatefulSetVolumeConfig.TEMPLATES.volume(context)).editConfigMap().withDefaultMode(// NOPMD
0555).endConfigMap().build()).endSpec().endTemplate().endSpec().build();
}
use of io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext in project stackgres by ongres.
the class DbOpsJobsGenerator method generateResource.
@Override
public Stream<HasMetadata> generateResource(StackGresDbOpsContext config) {
Instant now = Instant.now();
Map<String, JobFactory> factories = jobsDiscoverer.discoverFactories(config);
return Seq.of(config.getSource()).filter(dbOp -> !isToRunAfter(dbOp, now)).map(dbOp -> {
JobFactory jobFactory = factories.get(dbOp.getSpec().getOp());
if (jobFactory == null) {
throw new UnsupportedOperationException("DbOps " + dbOp.getSpec().getOp() + " not implemented!");
}
return jobFactory.createJob(config);
});
}
use of io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext in project stackgres by ongres.
the class DbOpsMajorVersionUpgradeJob method getRunEnvVars.
@Override
protected List<EnvVar> getRunEnvVars(StackGresDbOpsContext context) {
StackGresDbOps dbOps = context.getSource();
StackGresDbOpsMajorVersionUpgrade majorVersionUpgrade = dbOps.getSpec().getMajorVersionUpgrade();
return ImmutableList.<EnvVar>builder().add(new EnvVarBuilder().withName("LINK").withValue(Optional.ofNullable(majorVersionUpgrade).map(StackGresDbOpsMajorVersionUpgrade::getLink).map(String::valueOf).orElse("false")).build(), new EnvVarBuilder().withName("CLONE").withValue(Optional.ofNullable(majorVersionUpgrade).map(StackGresDbOpsMajorVersionUpgrade::getClone).map(String::valueOf).orElse("false")).build(), new EnvVarBuilder().withName("CHECK").withValue(Optional.ofNullable(majorVersionUpgrade).map(StackGresDbOpsMajorVersionUpgrade::getCheck).map(String::valueOf).orElse("false")).build(), new EnvVarBuilder().withName("CRD_GROUP").withValue(CommonDefinition.GROUP).build(), new EnvVarBuilder().withName("CLUSTER_CRD_NAME").withValue(HasMetadata.getPlural(StackGresCluster.class)).build(), new EnvVarBuilder().withName("CLUSTER_NAMESPACE").withValue(context.getSource().getMetadata().getNamespace()).build(), new EnvVarBuilder().withName("CLUSTER_NAME").withValue(context.getSource().getSpec().getSgCluster()).build(), new EnvVarBuilder().withName("SERVICE_ACCOUNT").withValueFrom(new EnvVarSourceBuilder().withFieldRef(new ObjectFieldSelector("v1", "spec.serviceAccountName")).build()).build(), new EnvVarBuilder().withName("POD_NAME").withValueFrom(new EnvVarSourceBuilder().withFieldRef(new ObjectFieldSelector("v1", "metadata.name")).build()).build(), new EnvVarBuilder().withName("DB_OPS_CRD_NAME").withValue(CustomResource.getCRDName(StackGresDbOps.class)).build(), new EnvVarBuilder().withName("DB_OPS_NAME").withValue(dbOps.getMetadata().getName()).build(), new EnvVarBuilder().withName("CLUSTER_POD_LABELS").withValue(labelFactory.patroniClusterLabels(context.getCluster()).entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","))).build(), new EnvVarBuilder().withName("CLUSTER_PRIMARY_POD_LABELS").withValue(labelFactory.patroniPrimaryLabels(context.getCluster()).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("MAJOR_VERSION_UPGRADE_CONTAINER_NAME").withValue(StackgresClusterContainers.MAJOR_VERSION_UPGRADE).build(), new EnvVarBuilder().withName("LOCK_TIMEOUT").withValue(OperatorProperty.LOCK_TIMEOUT.getString()).build(), new EnvVarBuilder().withName("LOCK_SLEEP").withValue(OperatorProperty.LOCK_POLL_INTERVAL.getString()).build()).build();
}
use of io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext in project stackgres by ongres.
the class DbOpsRepackJob method getRunEnvVars.
@Override
protected List<EnvVar> getRunEnvVars(StackGresDbOpsContext context) {
StackGresDbOps dbOps = context.getSource();
StackGresDbOpsRepack repack = dbOps.getSpec().getRepack();
List<EnvVar> runEnvVars = ImmutableList.<EnvVar>builder().add(new EnvVarBuilder().withName("CLUSTER_NAMESPACE").withValue(context.getSource().getMetadata().getNamespace()).build(), new EnvVarBuilder().withName("CLUSTER_NAME").withValue(context.getSource().getMetadata().getName()).build(), new EnvVarBuilder().withName("CLUSTER_PRIMARY_POD_LABELS").withValue(labelFactory.patroniPrimaryLabels(context.getCluster()).entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","))).build(), new EnvVarBuilder().withName("PATRONI_CONTAINER_NAME").withValue(StackgresClusterContainers.PATRONI).build()).addAll(getRepackConfigEnvVar(repack)).add(new EnvVarBuilder().withName("DATABASES").withValue(Seq.seq(Optional.ofNullable(repack).map(StackGresDbOpsRepack::getDatabases).stream()).flatMap(List::stream).map(database -> Seq.seq(getRepackConfigEnvVar(repack)).map(envVar -> envVar.getName() + "=" + envVar.getValue()).toString(";") + " " + database.getName()).toString("\n")).build()).build();
return runEnvVars;
}
use of io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext in project stackgres by ongres.
the class DbOpsVacuumJob method getRunEnvVars.
@Override
protected List<EnvVar> getRunEnvVars(StackGresDbOpsContext context) {
StackGresDbOps dbOps = context.getSource();
StackGresDbOpsVacuum vacuum = dbOps.getSpec().getVacuum();
final String primaryServiceDns = PatroniServices.readWriteName(context);
List<EnvVar> runEnvVars = ImmutableList.<EnvVar>builder().add(new EnvVarBuilder().withName("PGHOST").withValue(primaryServiceDns).build(), new EnvVarBuilder().withName("PGUSER").withValue("postgres").build(), new EnvVarBuilder().withName("PGPASSWORD").withNewValueFrom().withNewSecretKeyRef().withName(PatroniSecret.name(context.getCluster())).withKey(PatroniSecret.SUPERUSER_PASSWORD_KEY).endSecretKeyRef().endValueFrom().build()).addAll(getVacuumConfigEnvVar(vacuum)).add(new EnvVarBuilder().withName("DATABASES").withValue(Seq.seq(Optional.ofNullable(vacuum).map(StackGresDbOpsVacuum::getDatabases).stream()).flatMap(List::stream).map(database -> Seq.seq(getVacuumConfigEnvVar(vacuum)).map(envVar -> envVar.getName() + "=" + envVar.getValue()).toString(";") + " " + database.getName()).toString("\n")).build()).build();
return runEnvVars;
}
Aggregations