Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed NPE during provisioning of selector for Deployments #13041

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.POD_STATUS_PHASE_RUNNING;
import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.POD_STATUS_PHASE_SUCCEEDED;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabel;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.setSelector;

import com.google.common.base.Strings;
import io.fabric8.kubernetes.api.model.DoneablePod;
Expand Down Expand Up @@ -159,7 +160,7 @@ public Pod deploy(Deployment deployment) throws InfrastructureException {
putLabel(podMeta, CHE_DEPLOYMENT_NAME_LABEL, deployment.getMetadata().getName());
putLabel(deployment.getMetadata(), CHE_WORKSPACE_ID_LABEL, workspaceId);
// Match condition for a deployment is an AND of all labels
deployment.getSpec().getSelector().setMatchLabels(podMeta.getLabels());
setSelector(deployment, podMeta.getLabels());
// Avoid accidental setting of multiple replicas
deployment.getSpec().setReplicas(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.collect.ImmutableMap;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder;
Expand All @@ -28,6 +29,8 @@
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -107,6 +110,24 @@ public static void setSelector(Service target, String key, String value) {
spec.setSelector(selector);
}

/** Sets the specified match labels for a selector into target Kubernetes Deployment. */
public static void setSelector(Deployment target, Map<String, String> matchLabels) {
DeploymentSpec spec = target.getSpec();

if (spec == null) {
spec = new DeploymentSpec();
target.setSpec(spec);
}

LabelSelector selector = spec.getSelector();
if (selector == null) {
selector = new LabelSelector();
spec.setSelector(selector);
}

selector.setMatchLabels(new HashMap<>(matchLabels));
}

/**
* Returns new instance of {@link PersistentVolumeClaim} with specified name, accessMode and
* quantity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ private Deployment buildDeployment(
.withName(name)
.endMetadata()
.withNewSpec()
.withNewSelector()
.addToMatchLabels(CHE_COMPONENT_NAME_LABEL, name)
.endSelector()
.withNewTemplate()
.withNewMetadata()
.withName(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.PodTemplateSpec;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.Service;
Expand Down Expand Up @@ -110,8 +111,16 @@ public void setUp() throws Exception {
assertTrue(objects.get(0) instanceof Deployment);
Deployment deployment = (Deployment) objects.get(0);
PodTemplateSpec podTemplate = deployment.getSpec().getTemplate();
Map<String, String> annotations = podTemplate.getMetadata().getAnnotations();
ObjectMeta podMeta = podTemplate.getMetadata();
assertEquals(podMeta.getName(), "jdk");

Map<String, String> deploymentSelector = deployment.getSpec().getSelector().getMatchLabels();
assertFalse(deploymentSelector.isEmpty());
assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet()));

Map<String, String> annotations = podMeta.getAnnotations();
assertEquals(annotations.get(String.format(MACHINE_NAME_ANNOTATION_FMT, "jdk")), "jdk");

Container container = podTemplate.getSpec().getContainers().get(0);
assertEquals(container.getName(), "jdk");
assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest");
Expand Down