Search in sources :

Example 56 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class DeploymentConfigOpenShiftConverter method convert.

@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
    if (item instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) item;
        if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
            DeploymentConfigBuilder builder = new DeploymentConfigBuilder(resource);
            DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder;
            if (resource.getSpec() != null) {
                specBuilder = builder.editSpec();
            } else {
                specBuilder = builder.withNewSpec();
            }
            specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
            specBuilder.endSpec();
            return builder.build();
        }
    }
    return item;
}
Also used : DeploymentConfigFluent(io.fabric8.openshift.api.model.DeploymentConfigFluent) DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 57 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class DockerImageWatcher method updateImageName.

private void updateImageName(KubernetesClient kubernetes, String namespace, HasMetadata entity, String imagePrefix, String imageName) {
    String name = KubernetesHelper.getName(entity);
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                kubernetes.extensions().deployments().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                kubernetes.replicationControllers().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
                if (openshiftClient == null) {
                    log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
                }
                openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    }
}
Also used : DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 58 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project strimzi by strimzi.

the class AbstractResourceOperatorTest method deleteWhenResourceExistsStillDeletes.

@Test
public void deleteWhenResourceExistsStillDeletes(TestContext context) {
    T resource = resource();
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(resource);
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    C mockClient = mock(clientType());
    mocker(mockClient, mockCms);
    AbstractResourceOperator<C, T, L, D, R, P> op = createResourceOperations(vertx, mockClient);
    Async async = context.async();
    op.reconcile(resource.getMetadata().getNamespace(), resource.getMetadata().getName(), null).setHandler(ar -> {
        assertTrue(ar.succeeded());
        verify(mockResource).delete();
        async.complete();
    });
}
Also used : Async(io.vertx.ext.unit.Async) Resource(io.fabric8.kubernetes.client.dsl.Resource) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 59 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project strimzi by strimzi.

the class KafkaCluster method fromAssembly.

/**
 * Create a Kafka cluster from the deployed StatefulSet resource
 *
 * @param ss The StatefulSet from which the cluster state should be recovered.
 * @param namespace Kubernetes/OpenShift namespace where cluster resources belong to
 * @param cluster   overall cluster name
 * @return  Kafka cluster instance
 */
public static KafkaCluster fromAssembly(StatefulSet ss, String namespace, String cluster) {
    KafkaCluster kafka = new KafkaCluster(namespace, cluster, Labels.fromResource(ss));
    kafka.setReplicas(ss.getSpec().getReplicas());
    Container container = ss.getSpec().getTemplate().getSpec().getContainers().get(0);
    kafka.setImage(container.getImage());
    kafka.setHealthCheckInitialDelay(container.getReadinessProbe().getInitialDelaySeconds());
    kafka.setHealthCheckTimeout(container.getReadinessProbe().getTimeoutSeconds());
    Map<String, String> vars = containerEnvVars(container);
    kafka.setZookeeperConnect(vars.getOrDefault(KEY_KAFKA_ZOOKEEPER_CONNECT, ss.getMetadata().getName() + "-zookeeper:2181"));
    kafka.setDefaultReplicationFactor(Integer.parseInt(vars.getOrDefault(KEY_KAFKA_DEFAULT_REPLICATION_FACTOR, String.valueOf(DEFAULT_KAFKA_DEFAULT_REPLICATION_FACTOR))));
    kafka.setOffsetsTopicReplicationFactor(Integer.parseInt(vars.getOrDefault(KEY_KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR, String.valueOf(DEFAULT_KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR))));
    kafka.setTransactionStateLogReplicationFactor(Integer.parseInt(vars.getOrDefault(KEY_KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR, String.valueOf(DEFAULT_KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR))));
    kafka.setMetricsEnabled(Boolean.parseBoolean(vars.getOrDefault(KEY_KAFKA_METRICS_ENABLED, String.valueOf(DEFAULT_KAFKA_METRICS_ENABLED))));
    if (kafka.isMetricsEnabled()) {
        kafka.setMetricsConfigName(metricConfigsName(cluster));
    }
    if (!ss.getSpec().getVolumeClaimTemplates().isEmpty()) {
        Storage storage = Storage.fromPersistentVolumeClaim(ss.getSpec().getVolumeClaimTemplates().get(0));
        if (ss.getMetadata().getAnnotations() != null) {
            String deleteClaimAnnotation = String.format("%s/%s", ClusterController.STRIMZI_CLUSTER_CONTROLLER_DOMAIN, Storage.DELETE_CLAIM_FIELD);
            storage.withDeleteClaim(Boolean.valueOf(ss.getMetadata().getAnnotations().computeIfAbsent(deleteClaimAnnotation, s -> "false")));
        }
        kafka.setStorage(storage);
    } else {
        Storage storage = new Storage(Storage.StorageType.EPHEMERAL);
        kafka.setStorage(storage);
    }
    return kafka;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container)

Example 60 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project strimzi by strimzi.

the class KafkaConnectS2ICluster method generateBuildConfig.

/**
 * Generate new BuildConfig
 *
 * @return      BuildConfig resource definition
 */
public BuildConfig generateBuildConfig() {
    BuildTriggerPolicy triggerConfigChange = new BuildTriggerPolicy();
    triggerConfigChange.setType("ConfigChange");
    BuildTriggerPolicy triggerImageChange = new BuildTriggerPolicy();
    triggerImageChange.setType("ImageChange");
    triggerImageChange.setImageChange(new ImageChangeTrigger());
    BuildConfig build = new BuildConfigBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).endMetadata().withNewSpec().withFailedBuildsHistoryLimit(5).withNewOutput().withNewTo().withKind("ImageStreamTag").withName(image).endTo().endOutput().withRunPolicy("Serial").withNewSource().withType("Binary").withBinary(new BinaryBuildSource()).endSource().withNewStrategy().withType("Source").withNewSourceStrategy().withNewFrom().withKind("ImageStreamTag").withName(getSourceImageStreamName() + ":" + sourceImageTag).endFrom().endSourceStrategy().endStrategy().withTriggers(triggerConfigChange, triggerImageChange).endSpec().build();
    return build;
}
Also used : BinaryBuildSource(io.fabric8.openshift.api.model.BinaryBuildSource) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) ImageChangeTrigger(io.fabric8.openshift.api.model.ImageChangeTrigger) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) BuildTriggerPolicy(io.fabric8.openshift.api.model.BuildTriggerPolicy)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)30 IOException (java.io.IOException)30 Test (org.junit.Test)29 Resource (io.fabric8.kubernetes.client.dsl.Resource)25 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)23 Map (java.util.Map)23 HashMap (java.util.HashMap)19 ArrayList (java.util.ArrayList)18 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)17 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)15 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)15 Session (io.fabric8.arquillian.kubernetes.Session)14 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)14 File (java.io.File)14 Resource (org.osgi.resource.Resource)14 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)12 Service (io.fabric8.kubernetes.api.model.Service)12 Async (io.vertx.ext.unit.Async)12 List (java.util.List)11 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)10