Search in sources :

Example 6 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DbOpsRole method createRoleBinding.

/**
 * Create the RoleBinding for Job associated to the dbOps.
 */
private RoleBinding createRoleBinding(StackGresDbOpsContext context) {
    final StackGresDbOps dbOps = context.getSource();
    final Map<String, String> labels = labelFactory.clusterLabels(context.getCluster());
    return new RoleBindingBuilder().withNewMetadata().withName(roleName(context)).withNamespace(dbOps.getMetadata().getNamespace()).withLabels(labels).endMetadata().withSubjects(new SubjectBuilder().withKind("ServiceAccount").withName(roleName(context)).withNamespace(dbOps.getMetadata().getNamespace()).build()).withRoleRef(new RoleRefBuilder().withKind("Role").withName(roleName(context)).withApiGroup("rbac.authorization.k8s.io").build()).build();
}
Also used : RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) SubjectBuilder(io.fabric8.kubernetes.api.model.rbac.SubjectBuilder) RoleRefBuilder(io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder)

Example 7 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DbOpsRole method createServiceAccount.

/**
 * Create the ServiceAccount for Job associated to the dbOps.
 */
private ServiceAccount createServiceAccount(StackGresDbOpsContext context) {
    final StackGresDbOps dbOps = context.getSource();
    final Map<String, String> labels = labelFactory.clusterLabels(context.getCluster());
    final String serviceAccountName = roleName(context);
    final String serviceAccountNamespace = dbOps.getMetadata().getNamespace();
    return new ServiceAccountBuilder().withNewMetadata().withName(serviceAccountName).withNamespace(serviceAccountNamespace).withLabels(labels).endMetadata().build();
}
Also used : ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps)

Example 8 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DbOpsRole method createRole.

/**
 * Create the Role for Job associated to the dbOps.
 */
private Role createRole(StackGresDbOpsContext context) {
    final StackGresDbOps dbOps = context.getSource();
    final Map<String, String> labels = labelFactory.clusterLabels(context.getCluster());
    return new RoleBuilder().withNewMetadata().withName(roleName(context)).withNamespace(dbOps.getMetadata().getNamespace()).withLabels(labels).endMetadata().addToRules(new PolicyRuleBuilder().withApiGroups("").withResources("pods").withVerbs("get", "list", "watch", "delete").build()).addToRules(new PolicyRuleBuilder().withApiGroups("").withResources("pods/log").withVerbs("get").build()).addToRules(new PolicyRuleBuilder().withApiGroups("").withResources("pods/exec").withVerbs("create").build()).addToRules(new PolicyRuleBuilder().withApiGroups("").withResources("services", "secrets").withVerbs("get", "list").build()).addToRules(new PolicyRuleBuilder().withApiGroups("apps").withResources("statefulsets").withVerbs("get", "delete").build()).addToRules(new PolicyRuleBuilder().withApiGroups("").withResources("events").withVerbs("get", "list", "create", "patch", "update").build()).addToRules(new PolicyRuleBuilder().withApiGroups(CommonDefinition.GROUP).withResources(HasMetadata.getPlural(StackGresDbOps.class)).withVerbs("get", "list", "watch", "patch", "update").build()).addToRules(new PolicyRuleBuilder().withApiGroups(CommonDefinition.GROUP).withResources(HasMetadata.getPlural(StackGresCluster.class)).withVerbs("get", "list", "watch", "patch", "update").build()).addToRules(new PolicyRuleBuilder().withApiGroups(CommonDefinition.GROUP).withResources(HasMetadata.getPlural(StackGresCluster.class) + "/status").withVerbs("update").build()).build();
}
Also used : StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) RoleBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBuilder) PolicyRuleBuilder(io.fabric8.kubernetes.api.model.rbac.PolicyRuleBuilder)

Example 9 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps 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;
}
Also used : PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) StackGresDbOpsVacuum(io.stackgres.common.crd.sgdbops.StackGresDbOpsVacuum) Singleton(javax.inject.Singleton) Seq(org.jooq.lambda.Seq) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) Inject(javax.inject.Inject) StackGresDbOpsVacuumConfig(io.stackgres.common.crd.sgdbops.StackGresDbOpsVacuumConfig) StackGresDbOpsContext(io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext) ImmutableList(com.google.common.collect.ImmutableList) DbOpsEnvironmentVariables(io.stackgres.operator.cluster.factory.DbOpsEnvironmentVariables) ClusterStatefulSetPath(io.stackgres.common.ClusterStatefulSetPath) LabelFactoryForDbOps(io.stackgres.common.LabelFactoryForDbOps) PatroniSecret(io.stackgres.operator.conciliation.factory.cluster.patroni.PatroniSecret) ResourceFactory(io.stackgres.operator.conciliation.factory.ResourceFactory) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) PatroniServices(io.stackgres.operator.conciliation.factory.cluster.patroni.PatroniServices) CdiUtil(io.stackgres.common.CdiUtil) List(java.util.List) Optional(java.util.Optional) StackGresDbOpsVacuum(io.stackgres.common.crd.sgdbops.StackGresDbOpsVacuum) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

Example 10 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class PgbenchBenchmark method getRunEnvVars.

@Override
protected List<EnvVar> getRunEnvVars(StackGresDbOpsContext context) {
    StackGresDbOps dbOps = context.getSource();
    StackGresDbOpsBenchmark benchmark = dbOps.getSpec().getBenchmark();
    StackGresDbOpsPgbench pgbench = benchmark.getPgbench();
    final String primaryServiceDns = PatroniServices.readWriteName(context);
    final String serviceDns;
    if (benchmark.isConnectionTypePrimaryService()) {
        serviceDns = primaryServiceDns;
    } else {
        serviceDns = PatroniServices.readOnlyName(context);
    }
    final String scale = Quantity.getAmountInBytes(Quantity.parse(pgbench.getDatabaseSize())).divide(Quantity.getAmountInBytes(Quantity.parse("16Mi"))).toPlainString();
    final String duration = String.valueOf(Duration.parse(pgbench.getDuration()).getSeconds());
    return ImmutableList.of(new EnvVarBuilder().withName("PGHOST").withValue(serviceDns).build(), new EnvVarBuilder().withName("PRIMARY_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(), new EnvVarBuilder().withName("SCALE").withValue(scale).build(), new EnvVarBuilder().withName("DURATION").withValue(duration).build(), new EnvVarBuilder().withName("PROTOCOL").withValue(Optional.of(pgbench).map(StackGresDbOpsPgbench::getUsePreparedStatements).map(usePreparedStatements -> usePreparedStatements ? "prepared" : "simple").orElse("simple")).build(), new EnvVarBuilder().withName("READ_WRITE").withValue(Optional.of(benchmark).map(StackGresDbOpsBenchmark::isConnectionTypePrimaryService).map(String::valueOf).orElse("true")).build(), new EnvVarBuilder().withName("CLIENTS").withValue(Optional.of(pgbench).map(StackGresDbOpsPgbench::getConcurrentClients).map(String::valueOf).orElse("1")).build(), new EnvVarBuilder().withName("JOBS").withValue(Optional.of(pgbench).map(StackGresDbOpsPgbench::getThreads).map(String::valueOf).orElse("1")).build());
}
Also used : StackGresDbOpsPgbench(io.stackgres.common.crd.sgdbops.StackGresDbOpsPgbench) StackGresDbOpsBenchmark(io.stackgres.common.crd.sgdbops.StackGresDbOpsBenchmark) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

Aggregations

StackGresDbOps (io.stackgres.common.crd.sgdbops.StackGresDbOps)23 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)8 List (java.util.List)8 Inject (javax.inject.Inject)8 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)7 Optional (java.util.Optional)6 ImmutableList (com.google.common.collect.ImmutableList)5 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)5 LabelFactoryForDbOps (io.stackgres.common.LabelFactoryForDbOps)5 Collectors (java.util.stream.Collectors)5 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)4 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)4 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)4 PodSecurityContext (io.fabric8.kubernetes.api.model.PodSecurityContext)4 JobBuilder (io.fabric8.kubernetes.api.model.batch.v1.JobBuilder)4 ClusterStatefulSetPath (io.stackgres.common.ClusterStatefulSetPath)4 DbOpsEnvironmentVariables (io.stackgres.operator.cluster.factory.DbOpsEnvironmentVariables)4 StackGresDbOpsContext (io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext)4