Search in sources :

Example 11 with StackGresDbOps

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

the class ClusterEventResourceTest method setup.

@BeforeAll
public static void setup() {
    StackGresDbOps dbOps = new StackGresDbOps();
    dbOps.setMetadata(new ObjectMeta());
    dbOps.getMetadata().setNamespace("test-namespace");
    dbOps.getMetadata().setName("test-operation");
    dbOps.getMetadata().setUid("1");
    dbOps.setSpec(new StackGresDbOpsSpec());
    dbOps.getSpec().setSgCluster("test");
    DbOpsScanner dbOpsScanner = Mockito.mock(DbOpsScanner.class);
    Mockito.when(dbOpsScanner.getResources(ArgumentMatchers.any())).thenReturn(ImmutableList.of(dbOps));
    QuarkusMock.installMockForType(dbOpsScanner, DbOpsScanner.class);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) DbOpsScanner(io.stackgres.common.resource.DbOpsScanner) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 12 with StackGresDbOps

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

the class DbOpsTransformer method toCustomResource.

@Override
public StackGresDbOps toCustomResource(DbOpsDto source, StackGresDbOps original) {
    StackGresDbOps transformation = Optional.ofNullable(original).orElseGet(StackGresDbOps::new);
    transformation.setMetadata(getCustomResourceMetadata(source, original));
    transformation.setSpec(getCustomResourceSpec(source.getSpec()));
    return transformation;
}
Also used : StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps)

Example 13 with StackGresDbOps

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

the class DbOpsDeployedResourceScanner method getDeployedResources.

@Override
public List<HasMetadata> getDeployedResources(StackGresDbOps config) {
    try (KubernetesClient client = clientFactory.create()) {
        final Map<String, String> genericLabels = labelFactory.genericLabels(config);
        Stream<HasMetadata> inNamespace = IN_NAMESPACE_RESOURCE_OPERATIONS.values().stream().flatMap(resourceOperationGetter -> resourceOperationGetter.apply(client).inNamespace(config.getMetadata().getNamespace()).withLabels(genericLabels).list().getItems().stream());
        List<HasMetadata> deployedResources = inNamespace.filter(resource -> resource.getMetadata().getOwnerReferences().stream().anyMatch(ownerReference -> ownerReference.getKind().equals(StackGresDbOps.KIND) && ownerReference.getName().equals(config.getMetadata().getName()) && ownerReference.getUid().equals(config.getMetadata().getUid()))).collect(Collectors.toUnmodifiableList());
        deployedResources.forEach(resource -> {
            Instance<DeployedResourceDecorator> decorator = decorators.select(new ReconciliationScopeLiteral(StackGresDbOps.class, resource.getKind()));
            if (decorator.isResolvable()) {
                decorator.get().decorate(resource);
            }
        });
        return deployedResources;
    }
}
Also used : KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ReconciliationOperations(io.stackgres.operator.conciliation.ReconciliationOperations) Role(io.fabric8.kubernetes.api.model.rbac.Role) Function(java.util.function.Function) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) Inject(javax.inject.Inject) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) Resource(io.fabric8.kubernetes.client.dsl.Resource) DeployedResourceDecorator(io.stackgres.operator.conciliation.DeployedResourceDecorator) Map(java.util.Map) LabelFactoryForDbOps(io.stackgres.common.LabelFactoryForDbOps) KubernetesClientFactory(io.stackgres.common.KubernetesClientFactory) Any(javax.enterprise.inject.Any) Job(io.fabric8.kubernetes.api.model.batch.v1.Job) Instance(javax.enterprise.inject.Instance) ReconciliationScopeLiteral(io.stackgres.operator.conciliation.ReconciliationScopeLiteral) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DeployedResourcesScanner(io.stackgres.operator.conciliation.DeployedResourcesScanner) List(java.util.List) Stream(java.util.stream.Stream) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReconciliationScopeLiteral(io.stackgres.operator.conciliation.ReconciliationScopeLiteral) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) DeployedResourceDecorator(io.stackgres.operator.conciliation.DeployedResourceDecorator)

Example 14 with StackGresDbOps

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

the class DbOpsRequiredResourcesGenerator method getRequiredResources.

@Override
public List<HasMetadata> getRequiredResources(StackGresDbOps config) {
    final ObjectMeta metadata = config.getMetadata();
    final String dbOpsName = metadata.getName();
    final String dbOpsNamespace = metadata.getNamespace();
    final StackGresDbOpsSpec spec = config.getSpec();
    final StackGresCluster cluster = clusterFinder.findByNameAndNamespace(spec.getSgCluster(), dbOpsNamespace).orElseThrow(() -> new IllegalArgumentException("SGDbOps " + dbOpsNamespace + "/" + dbOpsName + " have a non existent SGCluster " + spec.getSgCluster()));
    StackGresDbOpsContext context = ImmutableStackGresDbOpsContext.builder().source(config).cluster(cluster).build();
    final List<ResourceGenerator<StackGresDbOpsContext>> resourceGenerators = generators.getResourceGenerators(context);
    final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
    List<Decorator<StackGresDbOpsContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
    decorators.forEach(decorator -> decorator.decorate(context, resources));
    return resources;
}
Also used : RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) Logger(org.slf4j.Logger) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) Inject(javax.inject.Inject) List(java.util.List) ResourceGenerationDiscoverer(io.stackgres.operator.conciliation.ResourceGenerationDiscoverer) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) DecoratorDiscoverer(io.stackgres.operator.conciliation.factory.DecoratorDiscoverer) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) Decorator(io.stackgres.operator.conciliation.factory.Decorator) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster)

Example 15 with StackGresDbOps

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

the class DbOpsSecurityUpgradeJob 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, "security-upgrade")).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("security-upgrade").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();
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder) 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