Search in sources :

Example 1 with Decorator

use of io.fabric8.crd.generator.decorator.Decorator in project kubernetes-client by fabric8io.

the class Resources method applyConstraints.

public List<Decorator> applyConstraints(Set<Decorator> decorators) {
    List<Decorator> result = new ArrayList<>();
    Decorator[] array = decorators.toArray(new Decorator[decorators.size()]);
    // But hey, let's have an upper limit of 5 just to prevent infinite loops
    for (int i = 0; i < 10 && bubbleSort(array); i++) {
        System.out.println("Sorting again:" + (i + 1));
    }
    for (Decorator d : array) {
        result.add(d);
    }
    return result;
}
Also used : Decorator(io.fabric8.crd.generator.decorator.Decorator) ArrayList(java.util.ArrayList)

Example 2 with Decorator

use of io.fabric8.crd.generator.decorator.Decorator in project quarkus by quarkusio.

the class ContainerAdapter method adapt.

public static Container adapt(io.dekorate.kubernetes.config.Container container) {
    String name = container.getName();
    if (Strings.isNullOrEmpty(name)) {
        name = Images.getName(container.getImage());
    }
    ContainerBuilder builder = new ContainerBuilder().withName(name).withImage(container.getImage()).withWorkingDir(container.getWorkingDir()).withCommand(container.getCommand()).withArgs(container.getArguments());
    for (Env env : container.getEnvVars()) {
        builder.accept(new AddEnvVarDecorator(ANY, name, env));
    }
    for (Port port : container.getPorts()) {
        // this was changed to use our patched port decorator
        builder.accept(new AddPortDecorator(ANY, name, port));
    }
    for (Mount mount : container.getMounts()) {
        builder.accept(new AddMountDecorator(ANY, name, mount));
    }
    builder.accept(new ApplyImagePullPolicyDecorator(name, container.getImagePullPolicy()));
    builder.accept(new AddLivenessProbeDecorator(name, container.getLivenessProbe()));
    builder.accept(new AddReadinessProbeDecorator(name, container.getReadinessProbe()));
    return builder.build();
}
Also used : ApplyImagePullPolicyDecorator(io.dekorate.kubernetes.decorator.ApplyImagePullPolicyDecorator) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) AddReadinessProbeDecorator(io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator) AddLivenessProbeDecorator(io.dekorate.kubernetes.decorator.AddLivenessProbeDecorator) Port(io.dekorate.kubernetes.config.Port) Mount(io.dekorate.kubernetes.config.Mount) AddEnvVarDecorator(io.dekorate.kubernetes.decorator.AddEnvVarDecorator) Env(io.dekorate.kubernetes.config.Env) AddPortDecorator(io.dekorate.kubernetes.decorator.AddPortDecorator) AddMountDecorator(io.dekorate.kubernetes.decorator.AddMountDecorator)

Example 3 with Decorator

use of io.fabric8.crd.generator.decorator.Decorator in project quarkus by quarkusio.

the class S2iUtils method waitForImageStreamTags.

/**
 * Wait for the references ImageStreamTags to become available.
 *
 * @param client The openshift client used to check the status of the ImageStream
 * @param items A list of items, possibly referencing image stream tags.
 * @param amount The max amount of time to wait.
 * @param timeUnit The time unit of the time to wait.
 * @return True if the items became available false otherwise.
 */
public static boolean waitForImageStreamTags(OpenShiftClient client, Collection<HasMetadata> items, long amount, TimeUnit timeUnit) {
    if (items == null || items.isEmpty()) {
        return true;
    }
    final List<String> tags = new ArrayList<>();
    new KubernetesListBuilder().withItems(new ArrayList<>(items)).accept(new Decorator<SourceBuildStrategyFluent>() {

        @Override
        public void visit(SourceBuildStrategyFluent strategy) {
            ObjectReference from = strategy.buildFrom();
            if (from.getKind().equals("ImageStreamTag")) {
                tags.add(from.getName());
            }
        }
    }).build();
    boolean tagsMissing = true;
    long started = System.currentTimeMillis();
    long elapsed = 0;
    while (tagsMissing && elapsed < timeUnit.toMillis(amount) && !Thread.interrupted()) {
        tagsMissing = false;
        for (String tag : tags) {
            ImageStreamTag t = client.imageStreamTags().withName(tag).get();
            if (t == null) {
                tagsMissing = true;
            }
        }
        if (tagsMissing) {
            try {
                Thread.sleep(1000);
                elapsed = System.currentTimeMillis() - started;
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
    return !tagsMissing;
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Decorator(io.dekorate.kubernetes.decorator.Decorator) ObjectReference(io.fabric8.kubernetes.api.model.ObjectReference) ArrayList(java.util.ArrayList) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) SourceBuildStrategyFluent(io.fabric8.openshift.api.model.SourceBuildStrategyFluent)

Example 4 with Decorator

use of io.fabric8.crd.generator.decorator.Decorator in project stackgres by ongres.

the class ClusterRequiredResourcesGenerator method getRequiredResources.

@Override
public List<HasMetadata> getRequiredResources(StackGresCluster config) {
    final ObjectMeta metadata = config.getMetadata();
    final String clusterName = metadata.getName();
    final String clusterNamespace = metadata.getNamespace();
    final StackGresClusterSpec spec = config.getSpec();
    final StackGresClusterConfiguration clusterConfiguration = spec.getConfiguration();
    final StackGresPostgresConfig clusterPgConfig = postgresConfigFinder.findByNameAndNamespace(clusterConfiguration.getPostgresConfig(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent SGPostgresConfig postgresconf"));
    final StackGresProfile clusterProfile = profileFinder.findByNameAndNamespace(spec.getResourceProfile(), clusterNamespace).orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have a non existent " + StackGresProfile.KIND + " " + spec.getResourceProfile()));
    final Optional<StackGresBackupConfig> backupConfig = Optional.ofNullable(clusterConfiguration.getBackupConfig()).flatMap(backupConfigName -> backupConfigFinder.findByNameAndNamespace(backupConfigName, clusterNamespace));
    final Optional<StackGresPoolingConfig> clusterPooling = Optional.ofNullable(clusterConfiguration.getConnectionPoolingConfig()).flatMap(poolingConfigName -> poolingConfigFinder.findByNameAndNamespace(poolingConfigName, clusterNamespace));
    Optional<StackGresClusterRestore> restoreConfig = Optional.ofNullable(config.getSpec().getInitData()).map(StackGresClusterInitData::getRestore);
    final Optional<StackGresBackup> restoreBackup;
    if (restoreConfig.isEmpty()) {
        restoreBackup = Optional.empty();
    } else {
        restoreBackup = restoreConfig.map(restore -> {
            final List<StackGresBackup> backups = backupScanner.getResources();
            return backups.stream().filter(backup -> backup.getMetadata().getUid().equals(restore.getFromBackup().getUid())).peek(backup -> {
                Preconditions.checkNotNull(backup.getStatus(), "Backup is " + BackupPhase.RUNNING.label());
                Preconditions.checkNotNull(backup.getStatus().getProcess(), "Backup is " + BackupPhase.RUNNING.label());
                Preconditions.checkArgument(backup.getStatus().getProcess().getStatus().equals(BackupPhase.COMPLETED.label()), "Backup is " + backup.getStatus().getProcess().getStatus());
            }).findFirst().orElseThrow(() -> new IllegalArgumentException("SGCluster " + clusterNamespace + "/" + clusterName + " have an invalid restore backup Uid"));
        });
    }
    StackGresClusterContext context = ImmutableStackGresClusterContext.builder().source(config).postgresConfig(clusterPgConfig).stackGresProfile(clusterProfile).backupConfig(backupConfig).poolingConfig(clusterPooling).restoreBackup(restoreBackup).prometheus(getPrometheus(config)).internalScripts(List.of(getPostgresExporterInitScript())).databaseCredentials(secretFinder.findByNameAndNamespace(clusterName, clusterNamespace)).build();
    final List<ResourceGenerator<StackGresClusterContext>> resourceGenerators = generators.getResourceGenerators(context);
    final List<HasMetadata> resources = resourceGenerators.stream().flatMap(generator -> generator.generateResource(context)).collect(Collectors.toUnmodifiableList());
    List<Decorator<StackGresClusterContext>> decorators = decoratorDiscoverer.discoverDecorator(context);
    decorators.forEach(decorator -> decorator.decorate(context, resources));
    return resources;
}
Also used : StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) Inject(javax.inject.Inject) OperatorPropertyContext(io.stackgres.operator.configuration.OperatorPropertyContext) ResourceGenerationDiscoverer(io.stackgres.operator.conciliation.ResourceGenerationDiscoverer) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) Map(java.util.Map) Prometheus(io.stackgres.operator.common.Prometheus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) CustomResourceScanner(io.stackgres.common.resource.CustomResourceScanner) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) Unchecked(org.jooq.lambda.Unchecked) Logger(org.slf4j.Logger) Resources(com.google.common.io.Resources) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PrometheusConfigSpec(io.stackgres.operator.customresource.prometheus.PrometheusConfigSpec) StandardCharsets(java.nio.charset.StandardCharsets) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) BackupPhase(io.stackgres.common.crd.sgbackup.BackupPhase) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) List(java.util.List) StackGresClusterScriptEntry(io.stackgres.common.crd.sgcluster.StackGresClusterScriptEntry) PrometheusInstallation(io.stackgres.operator.customresource.prometheus.PrometheusInstallation) DecoratorDiscoverer(io.stackgres.operator.conciliation.factory.DecoratorDiscoverer) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) Optional(java.util.Optional) Secret(io.fabric8.kubernetes.api.model.Secret) ResourceFinder(io.stackgres.common.resource.ResourceFinder) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Preconditions(com.google.common.base.Preconditions) OperatorProperty(io.stackgres.common.OperatorProperty) PrometheusConfig(io.stackgres.operator.customresource.prometheus.PrometheusConfig) StackGresBackupConfig(io.stackgres.common.crd.sgbackupconfig.StackGresBackupConfig) StackGresBackup(io.stackgres.common.crd.sgbackup.StackGresBackup) StackGresPoolingConfig(io.stackgres.common.crd.sgpooling.StackGresPoolingConfig) StackGresClusterInitData(io.stackgres.common.crd.sgcluster.StackGresClusterInitData) List(java.util.List) StackGresClusterConfiguration(io.stackgres.common.crd.sgcluster.StackGresClusterConfiguration) RequiredResourceGenerator(io.stackgres.operator.conciliation.RequiredResourceGenerator) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StackGresClusterSpec(io.stackgres.common.crd.sgcluster.StackGresClusterSpec) StackGresClusterRestore(io.stackgres.common.crd.sgcluster.StackGresClusterRestore) Decorator(io.stackgres.operator.conciliation.factory.Decorator) StackGresProfile(io.stackgres.common.crd.sgprofile.StackGresProfile) StackGresPostgresConfig(io.stackgres.common.crd.sgpgconfig.StackGresPostgresConfig)

Example 5 with Decorator

use of io.fabric8.crd.generator.decorator.Decorator 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)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)10 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 ApplicationScoped (javax.enterprise.context.ApplicationScoped)6 Inject (javax.inject.Inject)6 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)4 Map (java.util.Map)4 Decorator (io.dekorate.kubernetes.decorator.Decorator)3 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)3 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)3 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 KubernetesClientFactory (io.stackgres.common.KubernetesClientFactory)3 StackGresBackup (io.stackgres.common.crd.sgbackup.StackGresBackup)3 CustomResourceFinder (io.stackgres.common.resource.CustomResourceFinder)3 DeployedResourceDecorator (io.stackgres.operator.conciliation.DeployedResourceDecorator)3 DeployedResourcesScanner (io.stackgres.operator.conciliation.DeployedResourcesScanner)3 ReconciliationOperations (io.stackgres.operator.conciliation.ReconciliationOperations)3