use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8 by fabric8io.
the class YamlSerialiseTest method testSerialiseYaml.
@Test
public void testSerialiseYaml() throws Exception {
Deployment deployment = new DeploymentBuilder().withNewMetadata().withName("foo").endMetadata().withNewSpec().withReplicas(1).withNewTemplate().withNewSpec().addNewContainer().withImage("cheese").endContainer().endSpec().endTemplate().endSpec().build();
File outFile = new File(new File(basedir), "target/test-data/deployment.yml");
outFile.getParentFile().mkdirs();
KubernetesHelper.saveYamlNotEmpty(deployment, outFile);
String yaml = IOHelpers.readFully(outFile);
System.out.println("YAML: " + yaml);
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project kubernetes by ballerinax.
the class DeploymentHandler method generate.
/**
* Generate kubernetes deployment definition from annotation.
*
* @param deploymentModel @{@link DeploymentModel} definition
* @throws KubernetesPluginException If an error occurs while generating artifact.
*/
private void generate(DeploymentModel deploymentModel) throws KubernetesPluginException {
List<ContainerPort> containerPorts = null;
if (deploymentModel.getPorts() != null) {
containerPorts = populatePorts(deploymentModel.getPorts());
}
Container container = generateContainer(deploymentModel, containerPorts);
Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(deploymentModel.getName()).withLabels(deploymentModel.getLabels()).withAnnotations(deploymentModel.getAnnotations()).withNamespace(dataHolder.getNamespace()).endMetadata().withNewSpec().withNewSelector().withMatchLabels(deploymentModel.getLabels()).endSelector().withStrategy(deploymentModel.getStrategy()).withReplicas(deploymentModel.getReplicas()).withNewTemplate().withNewMetadata().addToLabels(deploymentModel.getLabels()).addToAnnotations(deploymentModel.getPodAnnotations()).endMetadata().withNewSpec().withServiceAccountName(deploymentModel.getServiceAccountName()).withContainers(container).withImagePullSecrets(getImagePullSecrets(deploymentModel)).withInitContainers(generateInitContainer(deploymentModel)).withVolumes(populateVolume(deploymentModel)).withTolerations(populatePodTolerations(deploymentModel.getPodTolerations())).withNodeSelector(deploymentModel.getNodeSelector()).endSpec().endTemplate().endSpec().build();
try {
String deploymentContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(deployment);
KubernetesUtils.writeToFile(deploymentContent, DEPLOYMENT_FILE_POSTFIX + YAML);
} catch (IOException e) {
String errorMessage = "error while generating yaml file for deployment: " + deploymentModel.getName();
throw new KubernetesPluginException(errorMessage, e);
}
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.
the class MergeResourceTest method testMergeDeploymentMetadataAndEnvVars.
@Test
public void testMergeDeploymentMetadataAndEnvVars() throws Exception {
Deployment resource = new DeploymentBuilder().withNewMetadata().withName("cheese").addToAnnotations("overwriteKey", "originalValue").addToAnnotations("unchangedKey", "shouldNotChange").addToAnnotations("unchangedBlankKey", "").addToAnnotations("deletedKey", "shouldBeDeleted").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").addToEnv(new EnvVarBuilder().withName("ENV_UPDATED").withValue("OLD_VALUE").build()).addToEnv(new EnvVarBuilder().withName("ENV_UNMODIFIED").withValue("SHOULD_NOT_CHANGE").build()).addToEnv(new EnvVarBuilder().withName("ENV_DELETED").withValue("DELETE_ME").build()).endContainer().endSpec().endTemplate().endSpec().build();
Deployment override = new DeploymentBuilder().withNewMetadata().withName("cheese").addToAnnotations("overwriteKey", "newValue").addToAnnotations("deletedKey", "").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().addToEnv(new EnvVarBuilder().withName("ENV_UPDATED").withValue("NEW_VALUE").build()).addToEnv(new EnvVarBuilder().withName("ENV_DELETED").withValue("").build()).addToEnv(new EnvVarBuilder().withName("ENV_ADDED").withValue("ADDED_VALUE").build()).endContainer().endSpec().endTemplate().endSpec().build();
HasMetadata answer = KubernetesResourceUtil.mergeResources(resource, override, log, false);
assertThat(answer).describedAs("mergeResult").isInstanceOf(Deployment.class);
Deployment result = (Deployment) answer;
log.info("Override metadata on Deployment generated: " + ResourceUtil.toYaml(answer));
Map<String, String> annotations = answer.getMetadata().getAnnotations();
assertDataModified(annotations, "Deployment.metadata.annotations");
assertDataNotModified(resource.getMetadata().getAnnotations(), "Original Deployment.metadata.annotations");
assertEnvModified(result.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv(), "Deployment.spec.template.spec.containers[0].env");
assertEnvNotModified(resource.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv(), "Original Deployment.spec.template.spec.containers[0].env");
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.
the class MergeResourceTest method testMergeDeploymentMetadataWithNoSpec.
@Test
public void testMergeDeploymentMetadataWithNoSpec() throws Exception {
Deployment resource = new DeploymentBuilder().withNewMetadata().withName("cheese").addToAnnotations("overwriteKey", "originalValue").addToAnnotations("unchangedKey", "shouldNotChange").addToAnnotations("unchangedBlankKey", "").addToAnnotations("deletedKey", "shouldBeDeleted").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").endContainer().endSpec().endTemplate().endSpec().build();
Deployment override = new DeploymentBuilder().withNewMetadata().withName("cheese").addToAnnotations("overwriteKey", "newValue").addToAnnotations("deletedKey", "").endMetadata().build();
HasMetadata answer = KubernetesResourceUtil.mergeResources(resource, override, log, false);
assertNotNull(answer);
log.info("Override metadata on Deployment with no spec generated: " + ResourceUtil.toYaml(answer));
Map<String, String> annotations = answer.getMetadata().getAnnotations();
assertDataModified(annotations, "Deployment.metadata.annotations");
assertDataNotModified(resource.getMetadata().getAnnotations(), "Original Deployment.metadata.annotations");
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project fabric8-maven-plugin by fabric8io.
the class ServiceAccountEnricherTest method testServiceAccountCreationFromFragment.
@Test
public void testServiceAccountCreationFromFragment() {
final KubernetesListBuilder builder = new KubernetesListBuilder().withItems(new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").endContainer().withServiceAccount("ribbon").endSpec().endTemplate().endSpec().build());
enrichAndAssert(builder);
}
Aggregations