Search in sources :

Example 31 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project iobserve-analysis by research-iobserve.

the class DeploymentExecutor method execute.

@Override
public void execute(final DeployComponentAction action) {
    // Can't be loaded earlier because it references the other models received via TCP
    final CorrespondenceModel correspondenceModel = new FileModelHandler<CorrespondenceModel>(this.resourceSet, CorrespondencePackage.eINSTANCE).load(URI.createFileURI(this.correspondenceModelFile.getAbsolutePath()));
    final KubernetesClient client = new DefaultKubernetesClient();
    final String rcName = this.normalizeComponentName(action.getTargetAllocationContext().getResourceContainer_AllocationContext().getEntityName());
    final Deployment deployment = client.extensions().deployments().inNamespace(this.namespace).withName(rcName).get();
    final AssemblyContext targetAssemblyContext = action.getTargetAllocationContext().getAssemblyContext_AllocationContext();
    final String imageName = this.getImageName(correspondenceModel, targetAssemblyContext);
    if (deployment != null) {
        // Increase number of replicas if pod is already deployed...
        final int replicas = deployment.getSpec().getReplicas();
        deployment.getSpec().setReplicas(replicas + 1);
        client.extensions().deployments().inNamespace(this.namespace).withName(rcName).replace(deployment);
        if (DeploymentExecutor.LOGGER.isDebugEnabled()) {
            DeploymentExecutor.LOGGER.debug("Scaled pod deployment of " + deployment.getMetadata().getName() + " to " + (replicas + 1));
        }
    } else {
        // ... deploy new pod if this is not the case
        final Deployment newDeployment = this.podsToDeploy.get(rcName);
        final String imageLocator = newDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage();
        newDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(imageLocator + "/" + imageName);
        newDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).setName(this.normalizeComponentName(targetAssemblyContext.getEntityName()));
        client.extensions().deployments().inNamespace(this.namespace).create(newDeployment);
        if (DeploymentExecutor.LOGGER.isDebugEnabled()) {
            DeploymentExecutor.LOGGER.debug("Image set to " + imageLocator + "/" + imageName);
            DeploymentExecutor.LOGGER.debug("Created new pod deployment " + newDeployment.getMetadata().getName());
        }
    }
    // Wait until deployed instance is ready
    while (!client.extensions().deployments().inNamespace(this.namespace).withName(rcName).isReady()) {
        if (DeploymentExecutor.LOGGER.isDebugEnabled()) {
            DeploymentExecutor.LOGGER.debug(rcName + " is not ready yet.");
        }
    }
    client.close();
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) CorrespondenceModel(org.iobserve.model.correspondence.CorrespondenceModel) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) AssemblyContext(org.palladiosimulator.pcm.core.composition.AssemblyContext) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 32 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project iobserve-analysis by research-iobserve.

the class DeallocationExecutor method execute.

@Override
public void execute(final DeallocateNodeAction action) {
    final KubernetesClient client = new DefaultKubernetesClient();
    final String rcName = this.normalizeComponentName(action.getTargetResourceContainer().getEntityName());
    client.extensions().deployments().inNamespace(this.namespace).withName(rcName).delete();
    client.close();
    if (DeallocationExecutor.LOGGER.isDebugEnabled()) {
        DeallocationExecutor.LOGGER.debug("Successfully deleted pod deployment with name " + rcName);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 33 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project iobserve-analysis by research-iobserve.

the class UndeploymentExecutor method execute.

@Override
public void execute(final UndeployComponentAction action) {
    final KubernetesClient client = new DefaultKubernetesClient();
    final String rcName = this.normalizeComponentName(action.getTargetAllocationContext().getResourceContainer_AllocationContext().getEntityName());
    final Deployment deployment = client.extensions().deployments().inNamespace(this.namespace).withName(rcName).get();
    final int replicas = deployment.getSpec().getReplicas();
    // deployment is deleted in the DeallocationExecutor.
    if (replicas > 0) {
        deployment.getSpec().setReplicas(replicas - 1);
        client.extensions().deployments().inNamespace(this.namespace).withName(rcName).replace(deployment);
    }
    client.close();
    if (UndeploymentExecutor.LOGGER.isDebugEnabled()) {
        UndeploymentExecutor.LOGGER.debug("Scaled pod deployment of " + deployment.getMetadata().getName() + " to " + (replicas - 1));
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 34 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project fabric8 by fabric8io.

the class Example method main.

public static void main(String[] args) {
    try {
        final KubernetesClient client = new DefaultKubernetesClient();
        assertThat(client).pods().runningStatus().hasSize(6);
        assertThat(client).pods().runningStatus().filterLabel("provider", "fabric8").assertSize().isGreaterThan(0);
        assertThat(client.services().inNamespace("default").withName("fabric8").get().getMetadata()).name().isEqualTo("fabric8");
        Map<String, String> consoleLabels = new HashMap<>();
        consoleLabels.put("component", "console");
        consoleLabels.put("provider", "fabric8");
        assertThat(client).podsForService("fabric8").runningStatus().extracting("metadata").extracting("labels").contains(consoleLabels);
        assertThat(client).podsForService("fabric8").runningStatus().hasSize(1).extracting("metadata").extracting("labels").contains(consoleLabels);
        assertThat(client).podsForService("fabric8").logs().doesNotContainText("Exception", "Error");
        assertThat(client).pods().logs().doesNotContainText("Exception", "Error");
        assertAssertionError(new Block() {

            @Override
            public void invoke() throws Exception {
                try {
                    assertThat(client.services().inNamespace("default").withName("doesNotExist").get().getMetadata()).name().isEqualTo("fabric8-console-controller");
                } catch (KubernetesClientException e) {
                    if (e.getCode() != 404) {
                        throw e;
                    } else {
                        throw new AssertionError(e);
                    }
                }
            }
        });
        assertAssertionError(new Block() {

            @Override
            public void invoke() throws Exception {
                try {
                    assertThat(client).pods().runningStatus().filterLabel("component", "doesNotExist").hasSize(1);
                } catch (KubernetesClientException e) {
                    if (e.getCode() != 404) {
                        throw e;
                    } else {
                        throw new AssertionError(e);
                    }
                }
            }
        });
        System.out.println("Done!");
    } catch (Throwable e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HashMap(java.util.HashMap) Asserts.assertAssertionError(io.fabric8.utils.Asserts.assertAssertionError) Block(io.fabric8.utils.Block) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 35 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project fabric8 by fabric8io.

the class Example method main.

public static void main(String... args) {
    System.out.println("\n\nfabric8 Kubernetes-api example");
    KubernetesClient kube = new DefaultKubernetesClient();
    System.out.println("=========================================================================");
    try {
        listPods(kube);
        listReplicationControllers(kube);
        listServices(kube);
        listServiceAccounts(kube);
        listEndpoints(kube);
    } catch (Exception e) {
        System.out.println("FAILED: " + e);
        e.printStackTrace();
    } finally {
        kube.close();
    }
    System.out.println("=========================================================================");
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Aggregations

DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)101 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)77 File (java.io.File)27 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)22 FileInputStream (java.io.FileInputStream)22 HashMap (java.util.HashMap)13 Test (org.testng.annotations.Test)12 Config (io.fabric8.kubernetes.client.Config)11 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)11 BeforeClass (org.testng.annotations.BeforeClass)10 Service (io.fabric8.kubernetes.api.model.Service)8 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)6 TaskAction (org.gradle.api.tasks.TaskAction)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 Map (java.util.Map)5 Test (org.junit.Test)5 KubernetesModelGenerator (com.github.isdream.chameleon.container.kubernetes.KubernetesModelGenerator)4 ImmutableMap (com.google.common.collect.ImmutableMap)4