use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.
the class PodTestUtil method buildJobPod.
public Pod buildJobPod(StackGresCluster cluster, int index) {
String namespace = cluster.getMetadata().getNamespace();
String clusterName = cluster.getMetadata().getName();
StackGresDbOps dbOps = new StackGresDbOps();
dbOps.setMetadata(cluster.getMetadata());
final Map<String, String> labels = dbOpsLabelFactory.dbOpsPodLabels(dbOps);
return new PodBuilder().withNewMetadata().withName(String.format(JOB_NAME_FORMAT, clusterName, clusterName, index, StringUtils.getRandomString(5))).withNamespace(namespace).withLabels(labels).endMetadata().build();
}
use of io.stackgres.common.crd.sgdbops.StackGresDbOps 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.common.crd.sgdbops.StackGresDbOps 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.common.crd.sgdbops.StackGresDbOps 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.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.
the class DbOpsRestartJob method createJob.
@Override
public Job createJob(StackGresDbOpsContext context) {
StackGresDbOps dbOps = context.getSource();
String namespace = dbOps.getMetadata().getNamespace();
final Map<String, String> labels = dbOpsLabelFactory.dbOpsPodLabels(context.getSource());
return new JobBuilder().withNewMetadata().withNamespace(namespace).withName(jobName(dbOps, "restart")).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)).withContainers(new ContainerBuilder().withName("restart").withImagePullPolicy("IfNotPresent").withImage(String.format(IMAGE_NAME, StackGresProperty.OPERATOR_IMAGE_VERSION.getString())).addToEnv(new EnvVarBuilder().withName(OperatorProperty.OPERATOR_NAME.getEnvironmentVariableName()).withValue(OperatorProperty.OPERATOR_NAME.getString()).build(), new EnvVarBuilder().withName(OperatorProperty.OPERATOR_NAMESPACE.getEnvironmentVariableName()).withValue(OperatorProperty.OPERATOR_NAMESPACE.getString()).build(), new EnvVarBuilder().withName("JOB_NAMESPACE").withValue(namespace).build(), new EnvVarBuilder().withName(StackGresProperty.OPERATOR_VERSION.getEnvironmentVariableName()).withValue(StackGresProperty.OPERATOR_VERSION.getString()).build(), new EnvVarBuilder().withName("CRD_UPGRADE").withValue(Boolean.FALSE.toString()).build(), new EnvVarBuilder().withName("CONVERSION_WEBHOOKS").withValue(Boolean.FALSE.toString()).build(), new EnvVarBuilder().withName("DATABASE_OPERATION_JOB").withValue(Boolean.TRUE.toString()).build(), new EnvVarBuilder().withName("DATABASE_OPERATION_CR_NAME").withValue(dbOps.getMetadata().getName()).build(), new EnvVarBuilder().withName("SERVICE_ACCOUNT").withNewValueFrom().withNewFieldRef().withFieldPath("spec.serviceAccountName").endFieldRef().endValueFrom().build(), new EnvVarBuilder().withName("POD_NAME").withNewValueFrom().withNewFieldRef().withFieldPath("metadata.name").endFieldRef().endValueFrom().build(), new EnvVarBuilder().withName("APP_OPTS").withValue(System.getenv("APP_OPTS")).build(), new EnvVarBuilder().withName("JAVA_OPTS").withValue(System.getenv("JAVA_OPTS")).build(), new EnvVarBuilder().withName("DEBUG_JOBS").withValue(System.getenv("DEBUG_OPERATOR")).build(), new EnvVarBuilder().withName("DEBUG_JOBS_SUSPEND").withValue(System.getenv("DEBUG_OPERATOR_SUSPEND")).build(), new EnvVarBuilder().withName("DBOPS_LOCK_TIMEOUT").withValue(OperatorProperty.LOCK_TIMEOUT.getString()).build(), new EnvVarBuilder().withName("DBOPS_LOCK_POLL_INTERVAL").withValue(OperatorProperty.LOCK_POLL_INTERVAL.getString()).build()).build()).endSpec().endTemplate().endSpec().build();
}
Aggregations