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();
}
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);
}
}
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));
}
}
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();
}
}
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("=========================================================================");
}
Aggregations