Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Kubernetes - pullPolicy can be defined through the Cartridge definition
Browse files Browse the repository at this point in the history
  • Loading branch information
chamilad committed Jan 28, 2016
1 parent a3e5deb commit 25083bf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ public class KubernetesIaas extends Iaas {
public static final String POD_ID_PREFIX = "pod";
public static final String SERVICE_NAME_PREFIX = "service";
public static final String IMAGE_PULL_SECRETS = "IMAGE_PULL_SECRETS";
public static final String IMAGE_PULL_POLICY = "IMAGE_PULL_POLICY";

private PartitionValidator partitionValidator;
private List<NameValuePair> payload;
private Long podActivationTimeout;
private List<String> imagePullSecrets;

public KubernetesIaas(IaasProvider iaasProvider) {
super(iaasProvider);
partitionValidator = new KubernetesPartitionValidator();
payload = new ArrayList<>();
imagePullSecrets = new ArrayList<>();

podActivationTimeout = Long.getLong("stratos.pod.activation.timeout");
if (podActivationTimeout == null) {
Expand Down Expand Up @@ -360,6 +359,9 @@ private void createPod(ClusterContext clusterContext, MemberContext memberContex
KubernetesApiClient kubernetesApi, KubernetesClusterContext kubernetesClusterContext)
throws KubernetesClientException, RegistryException {

List<String> imagePullSecrets = new ArrayList<>();
String imagePullPolicy = null;

String applicationId = memberContext.getApplicationId();
String cartridgeType = memberContext.getCartridgeType();
String clusterId = memberContext.getClusterId();
Expand Down Expand Up @@ -404,6 +406,11 @@ private void createPod(ClusterContext clusterContext, MemberContext memberContex
imagePullSecrets.add(imagePullSecretsProperty.getValue());
}

Property imagePullPolicyProperty = cartridge.getProperties().getProperty(IMAGE_PULL_POLICY);
if (imagePullPolicyProperty != null){
imagePullPolicy = imagePullPolicyProperty.getValue();
}

IaasProvider iaasProvider = CloudControllerContext.getInstance()
.getIaasProviderOfPartition(cartridge.getType(), partition.getId());
if (iaasProvider == null) {
Expand Down Expand Up @@ -455,7 +462,7 @@ private void createPod(ClusterContext clusterContext, MemberContext memberContex
podAnnotations.put(CloudControllerConstants.MEMBER_ID_LABEL, memberContext.getMemberId());

kubernetesApi.createPod(podId, podName, podLabels, podAnnotations, dockerImage, cpu, memory, ports,
environmentVariables, imagePullSecrets);
environmentVariables, imagePullSecrets, imagePullPolicy);

log.info(String.format("Pod started successfully: [application] %s [cartridge] %s [member] %s "
+ "[pod] %s [pod-label] %s [cpu] %s [memory] %s", memberContext.getApplicationId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public KubernetesApiClient(String endpointUrl) {
* @param ports Ports exposed by the pod
* @param environmentVariables Environment variables to be passed to the pod
* @param imagePullSecrets Image Pull Secret to be passed to the pod
* @param imagePullPolicy Image Pull policy to be passed to the pod
* @throws KubernetesClientException
*/
@Override
public void createPod(String podId, String podName, Map<String, String> podLabels, Map<String, String> annotations,
String dockerImage, String cpu, String memory, List<ContainerPort> ports,
List<EnvVar> environmentVariables, List<String> imagePullSecrets)
List<EnvVar> environmentVariables, List<String> imagePullSecrets, String imagePullPolicy)
throws KubernetesClientException {

try {
Expand Down Expand Up @@ -111,7 +112,21 @@ public void createPod(String podId, String podName, Map<String, String> podLabel
containerTemplate.setResources(resources);

containerTemplate.setPorts(ports);
containerTemplate.setImagePullPolicy(KubernetesConstants.POLICY_PULL_IF_NOT_PRESENT);

if (imagePullPolicy == null) {
// default pull policy
imagePullPolicy = KubernetesConstants.POLICY_PULL_IF_NOT_PRESENT;
} else if (
!imagePullPolicy.equals(KubernetesConstants.POLICY_PULL_ALWAYS) &&
!imagePullPolicy.equals(KubernetesConstants.POLICY_PULL_NEVER) &&
!imagePullPolicy.equals(KubernetesConstants.POLICY_PULL_IF_NOT_PRESENT)) {

// pull policy validation failed
throw new KubernetesClientException("Invalid Image Pull Policy defined : " + imagePullPolicy);
}

containerTemplate.setImagePullPolicy(imagePullPolicy);

if (environmentVariables != null) {
containerTemplate.setEnv(environmentVariables);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class KubernetesConstants {

public static final String POD_STATUS_RUNNING = "Running";
public static final String POLICY_PULL_IF_NOT_PRESENT = "IfNotPresent";
public static final String POLICY_PULL_ALWAYS = "Always";
public static final String POLICY_PULL_NEVER = "Never";
public static final String SESSION_AFFINITY_CLIENT_IP = "ClientIP";
public static final String KIND_POD = "Pod";
public static final String KIND_SERVICE = "Service";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ public interface KubernetesAPIClientInterface {
* @param ports ports to be opened
* @param environmentVariables environment variables
* @param imagePullSecrets Image Pull Secret to be passed to the pod
* @param imagePullPolicy Image Pull policy to be passed to the pod
* @throws KubernetesClientException
*/
public void createPod(String podId, String podName, Map<String, String> podLabels, Map<String, String> annotations,
String dockerImage, String cpu, String memory, List<ContainerPort> ports,
List<EnvVar> environmentVariables, List<String> imagePullSecrets)
List<EnvVar> environmentVariables, List<String> imagePullSecrets, String imagePullPolicy)
throws KubernetesClientException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ public void tearDown() {
}

protected void createPod(String podId, String podName, Map<String, String> labelMap, Map<String, String>
annotations, String containerPortName, String cpu, String memory, List<String> imagePullSecrets)
annotations, String containerPortName, String cpu, String memory, List<String> imagePullSecrets,
String imagePullPolicy)
throws KubernetesClientException {

log.info("Creating pod: [pod] " + podId);
List<ContainerPort> ports = createPorts(containerPortName);
client.createPod(podId, podName, annotations, labelMap, dockerImage, cpu, memory, ports, null, imagePullSecrets);
client.createPod(podId, podName, annotations, labelMap, dockerImage, cpu, memory, ports, null, imagePullSecrets, imagePullPolicy);
podIdList.add(podId);

sleep(2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public void testPodCreation() throws Exception {
podLabels1.put("applicationId", "my-application-1");
Map<String, String> podAnnocations1 = new HashMap<>();
podAnnocations1.put("test", "test");
createPod("stratos-test-pod-1", "stratos-test-pod", podLabels1, podAnnocations1, "http-1", "1", "512Mi", null);
createPod("stratos-test-pod-1", "stratos-test-pod", podLabels1, podAnnocations1, "http-1", "1", "512Mi", null, null);

Map<String, String> podLabels2 = new HashMap<>();
podLabels2.put("applicationId", "my-application-2");
Map<String, String> podAnnocations2 = new HashMap<>();
podAnnocations2.put("test", "test");
createPod("stratos-test-pod-2", "stratos-test-pod", podLabels2, podAnnocations2, "http-1", "2", "4Gi", null);
createPod("stratos-test-pod-2", "stratos-test-pod", podLabels2, podAnnocations2, "http-1", "2", "4Gi", null, null);

deletePod("stratos-test-pod-1");
deletePod("stratos-test-pod-2");
Expand Down Expand Up @@ -95,13 +95,13 @@ public void testServiceCreation() throws Exception {
podLabels3.put("applicationId", "my-application-3");
Map<String, String> podAnnocations3 = new HashMap<>();
podAnnocations3.put("test", "test");
createPod("stratos-test-pod-3", serviceName, podLabels3, podAnnocations3, containerPortName, "1", "512", null);
createPod("stratos-test-pod-3", serviceName, podLabels3, podAnnocations3, containerPortName, "1", "512", null, null);

Map<String, String> podLabels4 = new HashMap<>();
podLabels4.put("applicationId", "my-application-4");
Map<String, String> podAnnocations4 = new HashMap<>();
podAnnocations4.put("test", "test");
createPod("stratos-test-pod-4", serviceName, podLabels4, podAnnocations4, containerPortName, "2", "512", null);
createPod("stratos-test-pod-4", serviceName, podLabels4, podAnnocations4, containerPortName, "2", "512", null, null);

if (testServiceSocket) {
// test service accessibility
Expand Down
4 changes: 4 additions & 0 deletions samples/cartridges/kubernetes/php-secret.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
{
"name": "IMAGE_PULL_SECRETS",
"value": "privateDockerSecret"
},
{
"name": "IMAGE_PULL_POLICY",
"value": "Always"
}
]
}

0 comments on commit 25083bf

Please sign in to comment.