Search in sources :

Example 61 with Resource

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

the class AbstractAssemblyOperator method reconcileAssembly.

/**
 * Reconcile assembly resources in the given namespace having the given {@code assemblyName}.
 * Reconciliation works by getting the assembly ConfigMap in the given namespace with the given assemblyName and
 * comparing with the corresponding {@linkplain #getResources(String) resource}.
 * <ul>
 * <li>An assembly will be {@linkplain #createOrUpdate(Reconciliation, ConfigMap, Handler) created or updated} if ConfigMap is without same-named resources</li>
 * <li>An assembly will be {@linkplain #delete(Reconciliation, Handler) deleted} if resources without same-named ConfigMap</li>
 * </ul>
 */
public final void reconcileAssembly(Reconciliation reconciliation, Handler<AsyncResult<Void>> handler) {
    String namespace = reconciliation.namespace();
    String assemblyName = reconciliation.assemblyName();
    final String lockName = getLockName(assemblyType, namespace, assemblyName);
    vertx.sharedData().getLockWithTimeout(lockName, LOCK_TIMEOUT, res -> {
        if (res.succeeded()) {
            log.debug("{}: Lock {} acquired", reconciliation, lockName);
            Lock lock = res.result();
            try {
                // get ConfigMap and related resources for the specific cluster
                ConfigMap cm = configMapOperations.get(namespace, assemblyName);
                if (cm != null) {
                    log.info("{}: assembly {} should be created or updated", reconciliation, assemblyName);
                    createOrUpdate(reconciliation, cm, createResult -> {
                        lock.release();
                        log.debug("{}: Lock {} released", reconciliation, lockName);
                        handler.handle(createResult);
                    });
                } else {
                    log.info("{}: assembly {} should be deleted", reconciliation, assemblyName);
                    delete(reconciliation, deleteResult -> {
                        lock.release();
                        log.debug("{}: Lock {} released", reconciliation, lockName);
                        handler.handle(deleteResult);
                    });
                }
            } catch (Throwable ex) {
                lock.release();
                log.debug("{}: Lock {} released", reconciliation, lockName);
                handler.handle(Future.failedFuture(ex));
            }
        } else {
            log.warn("{}: Failed to acquire lock {}.", reconciliation, lockName);
        }
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Lock(io.vertx.core.shareddata.Lock)

Example 62 with Resource

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

the class AbstractResourceOperatorTest method createWhenExistsIsAPatch.

public void createWhenExistsIsAPatch(TestContext context, boolean cascade) {
    T resource = resource();
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(resource);
    when(mockResource.cascading(cascade)).thenReturn(mockResource);
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(resource.getMetadata().getNamespace()))).thenReturn(mockNameable);
    C mockClient = mock(clientType());
    mocker(mockClient, mockCms);
    AbstractResourceOperator<C, T, L, D, R, P> op = createResourceOperations(vertx, mockClient);
    Async async = context.async();
    Future<ReconcileResult<P>> fut = op.createOrUpdate(resource);
    fut.setHandler(ar -> {
        assertTrue(ar.succeeded());
        verify(mockResource).get();
        verify(mockResource).patch(any());
        verify(mockResource, never()).create(any());
        verify(mockResource, never()).createNew();
        verify(mockResource, never()).createOrReplace(any());
        verify(mockCms, never()).createOrReplace(any());
        async.complete();
    });
}
Also used : Resource(io.fabric8.kubernetes.client.dsl.Resource) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Async(io.vertx.ext.unit.Async) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation)

Example 63 with Resource

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

the class KubernetesAssert method deployments.

/**
 * Finds all the resources that create pod selections (Deployment, DeploymentConfig, ReplicaSet, ReplicationController)
 * and create a {@link HasPodSelectionAssert} to make assertions on their pods that they startup etc.
 *
 * @return the assertion object for the deployment
 */
public HasPodSelectionAssert deployments() {
    List<HasPodSelectionAssert> asserters = new ArrayList<>();
    List<HasMetadata> resources = new ArrayList<>();
    try {
        resources = KubernetesHelper.findKubernetesResourcesOnClasspath(new Controller(client));
    } catch (IOException e) {
        fail("Failed to load kubernetes resources on the classpath: " + e, e);
    }
    for (HasMetadata resource : resources) {
        HasPodSelectionAssert asserter = createPodSelectionAssert(resource);
        if (asserter != null) {
            asserters.add(asserter);
        }
    }
    String message = "No pod selection kinds found on the classpath such as Deployment, DeploymentConfig, ReplicaSet, ReplicationController";
    // TODO we don't yet support size > 1
    assertThat(asserters).describedAs(message).isNotEmpty();
    if (asserters.size() == 1) {
        return asserters.get(0);
    }
    return new MultiHasPodSelectionAssert(asserters);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 64 with Resource

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

the class SpacesTest method testLoadSpaces.

@Test
public void testLoadSpaces() {
    String namespace = "myproject";
    String resourceName = "fabric8-spaces.yml";
    KubernetesClient client = getKubernetesClient();
    URL resource = getClass().getClassLoader().getResource(resourceName);
    assertNotNull("Failed to load resource from classpath: " + resourceName, resourceName);
    InputStream inputStream = null;
    try {
        inputStream = resource.openStream();
    } catch (IOException e) {
        fail("Failed to open " + resourceName + ". " + e);
    }
    assertNotNull("Failed to open resource from classpath: " + resourceName, resourceName);
    ConfigMap configMap = null;
    try {
        configMap = KubernetesHelper.loadYaml(inputStream, ConfigMap.class);
    } catch (IOException e) {
        fail("Failed to parse YAML: " + resourceName + ". " + e);
    }
    server.expect().withPath("/api/v1/namespaces/" + namespace + "/configmaps/" + FABRIC8_SPACES).andReturn(200, configMap).once();
    Spaces spaces = Spaces.load(kubernetesClient, namespace);
    List<Space> spaceList = new ArrayList<>(spaces.getSpaceSet());
    assertEquals("Size of spaceList: " + spaceList, 3, spaceList.size());
    Space space0 = spaceList.get(0);
    assertEquals("space0.name", "Foo", space0.getName());
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 65 with Resource

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

the class J4pClientProvider method lookup.

@Override
public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
    KubernetesClient client = this.clientInstance.get();
    Session session = this.sessionInstance.get();
    JolokiaClients jolokiaClients = new JolokiaClients(client);
    String serviceName = getServiceName(qualifiers);
    String podName = getPodName(qualifiers);
    String replicationControllerName = getReplicationControllerName(qualifiers);
    if (Strings.isNotBlank(serviceName)) {
        Service service = client.services().inNamespace(session.getNamespace()).withName(serviceName).get();
        if (service != null) {
            return jolokiaClients.clientForService(service);
        }
    }
    if (Strings.isNotBlank(podName)) {
        Pod pod = client.pods().inNamespace(session.getNamespace()).withName(serviceName).get();
        if (pod != null) {
            return jolokiaClients.clientForPod(pod);
        }
    }
    if (Strings.isNotBlank(replicationControllerName)) {
        ReplicationController replicationController = client.replicationControllers().inNamespace(session.getNamespace()).withName(replicationControllerName).get();
        if (replicationController != null) {
            return jolokiaClients.clientForReplicationController(replicationController);
        }
    }
    return null;
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Service(io.fabric8.kubernetes.api.model.Service) JolokiaClients(io.fabric8.kubernetes.jolokia.JolokiaClients) Session(io.fabric8.arquillian.kubernetes.Session)

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