Skip to content

Commit

Permalink
Make setting deployment selector NPE safe
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <[email protected]>
  • Loading branch information
sleshchenko committed Apr 2, 2019
1 parent f46838c commit ae132cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit ae132cd

Please sign in to comment.