Java Code Examples for org.springframework.cloud.deployer.spi.core.AppDeploymentRequest#getDeploymentProperties()

The following examples show how to use org.springframework.cloud.deployer.spi.core.AppDeploymentRequest#getDeploymentProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaCommandBuilder.java    From spring-cloud-deployer-local with Apache License 2.0 5 votes vote down vote up
@Override
public String[] buildExecutionCommand(AppDeploymentRequest request, Map<String, String> appInstanceEnv,
		Optional<Integer> appInstanceNumber) {
	ArrayList<String> commands = new ArrayList<>();
	Map<String, String> deploymentProperties = request.getDeploymentProperties();
	commands.add(bindDeploymentProperties(deploymentProperties).getJavaCmd());
	// Add Java System Properties (ie -Dmy.prop=val) before main class or -jar
	addJavaOptions(commands, deploymentProperties, properties);
	addJavaExecutionOptions(commands, request);
	commands.addAll(request.getCommandlineArguments());
	logger.debug("Java Command = " + commands);
	return commands.toArray(new String[0]);
}
 
Example 2
Source File: TaskSanitizer.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
public TaskManifest sanitizeTaskManifest(TaskManifest taskManifest) {
	if (taskManifest == null) {
		return null;
	}
	TaskManifest sanitizedTaskManifest = new TaskManifest();
	sanitizedTaskManifest.setPlatformName(taskManifest.getPlatformName());
	AppDeploymentRequest existingAppDeploymentRequest = taskManifest.getTaskDeploymentRequest();
	// Sanitize App Properties
	Map<String, String> existingAppProperties = existingAppDeploymentRequest.getDefinition().getProperties();
	Map<String, String> sanitizedAppProperties = this.argumentSanitizer.sanitizeProperties(existingAppProperties);

	// Sanitize Deployment Properties
	Map<String, String> existingDeploymentProperties = existingAppDeploymentRequest.getDeploymentProperties();
	Map<String, String> sanitizedDeploymentProperties = this.argumentSanitizer.sanitizeProperties(existingDeploymentProperties);

	AppDefinition sanitizedAppDefinition = new AppDefinition(existingAppDeploymentRequest.getDefinition().getName(),
			sanitizedAppProperties);
	List<String> sanitizedCommandLineArgs = existingAppDeploymentRequest.getCommandlineArguments().stream()
			.map(argument -> (this.argumentSanitizer.sanitize(argument))).collect(Collectors.toList());
	AppDeploymentRequest sanitizedAppDeploymentRequest = new AppDeploymentRequest(
			sanitizedAppDefinition,
			existingAppDeploymentRequest.getResource(),
			sanitizedDeploymentProperties,
			sanitizedCommandLineArgs);
	sanitizedTaskManifest.setTaskDeploymentRequest(sanitizedAppDeploymentRequest);
	return sanitizedTaskManifest;
}
 
Example 3
Source File: ProbeCreator.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getDeploymentProperties() {
	AppDeploymentRequest appDeploymentRequest = this.containerConfiguration.getAppDeploymentRequest();
	return (appDeploymentRequest instanceof ScheduleRequest) ?
			((ScheduleRequest) appDeploymentRequest).getSchedulerProperties() : appDeploymentRequest.getDeploymentProperties();
}
 
Example 4
Source File: KubernetesAppDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
private Deployment createDeployment(AppDeploymentRequest request) {

		String appId = createDeploymentId(request);

		logger.debug(String.format("Creating Deployment: %s", appId));

		int replicas = getCountFromRequest(request);

		Map<String, String> idMap = createIdMap(appId, request);

		Map<String, String> kubernetesDeployerProperties = request.getDeploymentProperties();

		Map<String, String> annotations = this.deploymentPropertiesResolver.getPodAnnotations(kubernetesDeployerProperties);
		Map<String, String> deploymentLabels = this.deploymentPropertiesResolver.getDeploymentLabels(kubernetesDeployerProperties);

		PodSpec podSpec = createPodSpec(request);

		Deployment d = new DeploymentBuilder().withNewMetadata().withName(appId).withLabels(idMap)
				.addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).addToLabels(deploymentLabels).endMetadata()
				.withNewSpec().withNewSelector().addToMatchLabels(idMap).endSelector().withReplicas(replicas)
				.withNewTemplate().withNewMetadata().withLabels(idMap).addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE)
				.addToLabels(deploymentLabels).withAnnotations(annotations).endMetadata().withSpec(podSpec).endTemplate()
				.endSpec().build();

		return client.apps().deployments().create(d);
	}
 
Example 5
Source File: KubernetesTaskLauncher.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
private void launch(String appId, AppDeploymentRequest request) {
	Map<String, String> idMap = createIdMap(appId, request);
	Map<String, String> podLabelMap = new HashMap<>();
	podLabelMap.put("task-name", request.getDefinition().getName());
	podLabelMap.put(SPRING_MARKER_KEY, SPRING_MARKER_VALUE);

	Map<String, String> deploymentProperties = request.getDeploymentProperties();
	Map<String, String> deploymentLabels = this.deploymentPropertiesResolver.getDeploymentLabels(deploymentProperties);
	if (!CollectionUtils.isEmpty(deploymentLabels)) {
		logger.debug(String.format("Adding deploymentLabels: %s", deploymentLabels));
	}
	PodSpec podSpec = createPodSpec(request);

	podSpec.setRestartPolicy(getRestartPolicy(request).name());
	if (this.properties.isCreateJob()) {
		logger.debug(String.format("Launching Job for task: %s", appId));
		ObjectMeta objectMeta = new ObjectMetaBuilder()
				.withLabels(podLabelMap)
				.addToLabels(idMap)
				.addToLabels(deploymentLabels)
				.withAnnotations(this.deploymentPropertiesResolver.getJobAnnotations(deploymentProperties))
				.build();
		PodTemplateSpec podTemplateSpec = new PodTemplateSpec(objectMeta, podSpec);

		JobSpec jobSpec = new JobSpecBuilder()
				.withTemplate(podTemplateSpec)
				.withBackoffLimit(getBackoffLimit(request))
				.build();

		this.client.batch().jobs()
				.createNew()
				.withNewMetadata()
				.withName(appId)
				.withLabels(Collections.singletonMap("task-name", podLabelMap.get("task-name")))
				.addToLabels(idMap)
				.withAnnotations(this.deploymentPropertiesResolver.getJobAnnotations(deploymentProperties))
				.endMetadata()
				.withSpec(jobSpec)
				.done();
	}
	else {
		logger.debug(String.format("Launching Pod for task: %s", appId));
		this.client.pods()
				.createNew()
				.withNewMetadata()
				.withName(appId)
				.withLabels(podLabelMap)
				.addToLabels(deploymentLabels)
				.withAnnotations(this.deploymentPropertiesResolver.getJobAnnotations(deploymentProperties))
				.addToLabels(idMap)
				.endMetadata()
				.withSpec(podSpec)
				.done();
	}
}
 
Example 6
Source File: AbstractKubernetesDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
/**
 * Create PodSpec for the given {@link AppDeploymentRequest}

 * @param appDeploymentRequest the app deployment request to use to create the PodSpec
 * @return the PodSpec
 */
PodSpec createPodSpec(AppDeploymentRequest appDeploymentRequest) {

	String appId = createDeploymentId(appDeploymentRequest);

	Map<String, String>  deploymentProperties = (appDeploymentRequest instanceof ScheduleRequest) ?
			((ScheduleRequest) appDeploymentRequest).getSchedulerProperties() : appDeploymentRequest.getDeploymentProperties();

	PodSpecBuilder podSpec = new PodSpecBuilder();

	String imagePullSecret = this.deploymentPropertiesResolver.getImagePullSecret(deploymentProperties);

	if (imagePullSecret != null) {
		podSpec.addNewImagePullSecret(imagePullSecret);
	}

	boolean hostNetwork = this.deploymentPropertiesResolver.getHostNetwork(deploymentProperties);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration(appId, appDeploymentRequest)
			.withProbeCredentialsSecret(getProbeCredentialsSecret(deploymentProperties))
			.withHostNetwork(hostNetwork);

	if (KubernetesAppDeployer.class.isAssignableFrom(this.getClass())) {
		containerConfiguration.withExternalPort(getExternalPort(appDeploymentRequest));
	}

	Container container = containerFactory.create(containerConfiguration);

	// add memory and cpu resource limits
	ResourceRequirements req = new ResourceRequirements();
	req.setLimits(this.deploymentPropertiesResolver.deduceResourceLimits(deploymentProperties));
	req.setRequests(this.deploymentPropertiesResolver.deduceResourceRequests(deploymentProperties));
	container.setResources(req);
	ImagePullPolicy pullPolicy = this.deploymentPropertiesResolver.deduceImagePullPolicy(deploymentProperties);
	container.setImagePullPolicy(pullPolicy.name());

	Map<String, String> nodeSelectors = this.deploymentPropertiesResolver.getNodeSelectors(deploymentProperties);
	if (nodeSelectors.size() > 0) {
		podSpec.withNodeSelector(nodeSelectors);
	}

	podSpec.withTolerations(this.deploymentPropertiesResolver.getTolerations(deploymentProperties));

	// only add volumes with corresponding volume mounts
	podSpec.withVolumes(this.deploymentPropertiesResolver.getVolumes(deploymentProperties).stream()
			.filter(volume -> container.getVolumeMounts().stream()
					.anyMatch(volumeMount -> volumeMount.getName().equals(volume.getName())))
			.collect(Collectors.toList()));

	if (hostNetwork) {
		podSpec.withHostNetwork(true);
	}
	podSpec.addToContainers(container);

	podSpec.withRestartPolicy(this.deploymentPropertiesResolver.getRestartPolicy(deploymentProperties).name());

	String deploymentServiceAcccountName = this.deploymentPropertiesResolver.getDeploymentServiceAccountName(deploymentProperties);

	if (deploymentServiceAcccountName != null) {
		podSpec.withServiceAccountName(deploymentServiceAcccountName);
	}

	PodSecurityContext podSecurityContext = this.deploymentPropertiesResolver.getPodSecurityContext(deploymentProperties);
	if (podSecurityContext != null) {
		podSpec.withSecurityContext(podSecurityContext);
	}

	Affinity affinity = this.deploymentPropertiesResolver.getAffinityRules(deploymentProperties);
	// Make sure there is at least some rule.
	if (affinity.getNodeAffinity() != null
			|| affinity.getPodAffinity() != null
			|| affinity.getPodAntiAffinity() != null) {
		podSpec.withAffinity(affinity);
	}

	Container initContainer = this.deploymentPropertiesResolver.getInitContainer(deploymentProperties);
	if (initContainer != null) {
		podSpec.addToInitContainers(initContainer);
	}

	return podSpec.build();
}
 
Example 7
Source File: DefaultContainerFactory.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
private Map<String, String> getDeploymentProperties(AppDeploymentRequest request) {
	return (request instanceof ScheduleRequest) ? ((ScheduleRequest)request).getSchedulerProperties() :
			request.getDeploymentProperties();
}
 
Example 8
Source File: CfEnvAwareAppDeploymentRequest.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 4 votes vote down vote up
private CfEnvAwareAppDeploymentRequest(AppDeploymentRequest appDeploymentRequest) {
	super(appDeploymentRequest.getDefinition(),
			CfEnvAwareResource.of(appDeploymentRequest.getResource()),
			appDeploymentRequest.getDeploymentProperties(),
			appDeploymentRequest.getCommandlineArguments());
}
 
Example 9
Source File: KubernetesAppDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 2 votes vote down vote up
/**
 * Create a StatefulSet
 *
 * @param request the {@link AppDeploymentRequest}
 */
protected void createStatefulSet(AppDeploymentRequest request) {

	String appId = createDeploymentId(request);

	int externalPort = getExternalPort(request);

	Map<String, String> idMap = createIdMap(appId, request);

	int replicas = getCountFromRequest(request);

	Map<String, String> kubernetesDeployerProperties = request.getDeploymentProperties();

	logger.debug(String.format("Creating StatefulSet: %s on %d with %d replicas", appId, externalPort, replicas));

	Map<String, Quantity> storageResource = Collections.singletonMap("storage",
			new Quantity(this.deploymentPropertiesResolver.getStatefulSetStorage(kubernetesDeployerProperties)));

	String storageClassName = this.deploymentPropertiesResolver.getStatefulSetStorageClassName(kubernetesDeployerProperties);

	PersistentVolumeClaimBuilder persistentVolumeClaimBuilder = new PersistentVolumeClaimBuilder().withNewSpec().
			withStorageClassName(storageClassName).withAccessModes(Collections.singletonList("ReadWriteOnce"))
			.withNewResources().addToLimits(storageResource).addToRequests(storageResource).endResources()
			.endSpec().withNewMetadata().withName(appId).withLabels(idMap)
			.addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).endMetadata();

	PodSpec podSpec = createPodSpec(request);

	podSpec.getVolumes().add(new VolumeBuilder().withName("config").withNewEmptyDir().endEmptyDir().build());

	podSpec.getContainers().get(0).getVolumeMounts()
		.add(new VolumeMountBuilder().withName("config").withMountPath("/config").build());

	String statefulSetInitContainerImageName = this.deploymentPropertiesResolver.getStatefulSetInitContainerImageName(kubernetesDeployerProperties);

	podSpec.getInitContainers().add(createStatefulSetInitContainer(statefulSetInitContainerImageName));
	
	Map<String, String> deploymentLabels=  this.deploymentPropertiesResolver.getDeploymentLabels(request.getDeploymentProperties());
	
	StatefulSetSpec spec = new StatefulSetSpecBuilder().withNewSelector().addToMatchLabels(idMap)
			.addToMatchLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).endSelector()
			.withVolumeClaimTemplates(persistentVolumeClaimBuilder.build()).withServiceName(appId)
			.withPodManagementPolicy("Parallel").withReplicas(replicas).withNewTemplate().withNewMetadata()
			.withLabels(idMap).addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).addToLabels(deploymentLabels)
			.endMetadata().withSpec(podSpec).endTemplate().build();

	StatefulSet statefulSet = new StatefulSetBuilder().withNewMetadata().withName(appId).withLabels(idMap)
			.addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).addToLabels(deploymentLabels).endMetadata().withSpec(spec).build();

	client.apps().statefulSets().create(statefulSet);
}