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

Upgrade kubernetes-client to 4.9.0 #16345

Merged
merged 13 commits into from
Mar 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static java.lang.String.format;
import static org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger.DEPLOYMENT_NAME_LABEL;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabel;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.setSelector;

import io.fabric8.kubernetes.api.model.ConfigMap;
Expand Down Expand Up @@ -177,12 +178,8 @@ private void mergePods(
// multiple pods/deployments are merged to one deployment
// to avoid issues because of overriding labels
// provision const label and selector to match all services to merged Deployment
deployment
.getSpec()
.getTemplate()
.getMetadata()
.getLabels()
.put(DEPLOYMENT_NAME_LABEL, deploymentName);
putLabel(
deployment.getSpec().getTemplate().getMetadata(), DEPLOYMENT_NAME_LABEL, deploymentName);
services.values().forEach(s -> setSelector(s, DEPLOYMENT_NAME_LABEL, deploymentName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package org.eclipse.che.workspace.infrastructure.kubernetes.environment;

import static java.lang.String.format;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putAnnotations;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabels;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.LocalObjectReference;
Expand Down Expand Up @@ -72,8 +74,9 @@ public Deployment merge(List<PodData> podsData) throws ValidationException {
for (PodData podData : podsData) {
// if there are entries with such keys then values will be overridden
ObjectMeta podMeta = podData.getMetadata();
basePodMeta.getLabels().putAll(podMeta.getLabels());
basePodMeta.getAnnotations().putAll(podMeta.getAnnotations());
putLabels(basePodMeta, podMeta.getLabels());
putAnnotations(basePodMeta, podMeta.getAnnotations());

basePodMeta.getAdditionalProperties().putAll(podMeta.getAdditionalProperties());

for (Container container : podData.getSpec().getContainers()) {
Expand Down Expand Up @@ -144,7 +147,7 @@ public Deployment merge(List<PodData> podsData) throws ValidationException {

Map<String, String> matchLabels = new HashMap<>();
matchLabels.put(DEPLOYMENT_NAME_LABEL, baseDeployment.getMetadata().getName());
basePodMeta.getLabels().putAll(matchLabels);
putLabels(basePodMeta, matchLabels);
baseDeployment.getSpec().getSelector().setMatchLabels(matchLabels);

return baseDeployment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ public static void putLabel(HasMetadata target, String key, String value) {
putLabel(target.getMetadata(), key, value);
}

/** Adds labels to target Kubernetes object. */
public static void putLabels(ObjectMeta metadata, Map<String, String> labels) {
if (labels == null || labels.isEmpty()) {
return;
}

Map<String, String> metaLabels = metadata.getLabels();
if (metaLabels == null) {
metadata.setLabels(new HashMap<>(labels));
} else {
metaLabels.putAll(labels);
}
}

/** Adds label to target Kubernetes object. */
public static void putLabel(ObjectMeta metadata, String key, String value) {
Map<String, String> labels = metadata.getLabels();
Expand Down Expand Up @@ -101,6 +115,20 @@ public static void putAnnotation(ObjectMeta metadata, String key, String value)
annotations.put(key, value);
}

/** Adds annotations to target ObjectMeta object. */
public static void putAnnotations(ObjectMeta metadata, Map<String, String> annotations) {
if (annotations == null || annotations.isEmpty()) {
return;
}

Map<String, String> metaAnnotations = metadata.getAnnotations();
if (metaAnnotations == null) {
metadata.setAnnotations(new HashMap<>(annotations));
} else {
metaAnnotations.putAll(annotations);
}
}

/** Adds selector into target Kubernetes service. */
public static void putSelector(Service target, String key, String value) {
ServiceSpec spec = target.getSpec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

import static com.google.common.base.Strings.isNullOrEmpty;

import io.fabric8.kubernetes.api.model.rbac.KubernetesPolicyRuleBuilder;
import io.fabric8.kubernetes.api.model.rbac.KubernetesRole;
import io.fabric8.kubernetes.api.model.rbac.KubernetesRoleBinding;
import io.fabric8.kubernetes.api.model.rbac.KubernetesRoleBindingBuilder;
import io.fabric8.kubernetes.api.model.rbac.KubernetesRoleBuilder;
import io.fabric8.kubernetes.api.model.rbac.KubernetesSubjectBuilder;
import io.fabric8.kubernetes.api.model.rbac.PolicyRuleBuilder;
skabashnyuk marked this conversation as resolved.
Show resolved Hide resolved
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder;
import io.fabric8.kubernetes.api.model.rbac.RoleBuilder;
import io.fabric8.kubernetes.api.model.rbac.SubjectBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientFactory;
Expand Down Expand Up @@ -76,35 +76,27 @@ void prepare() throws InfrastructureException {
}

String execRoleName = "exec";
if (k8sClient.rbac().kubernetesRoles().inNamespace(namespace).withName(execRoleName).get()
if (k8sClient.rbac().clusterRoles().inNamespace(namespace).withName(execRoleName).get()
== null) {
createExecRole(k8sClient, execRoleName);
}

String viewRoleName = "workspace-view";
if (k8sClient.rbac().kubernetesRoles().inNamespace(namespace).withName(viewRoleName).get()
if (k8sClient.rbac().clusterRoles().inNamespace(namespace).withName(viewRoleName).get()
== null) {
createViewRole(k8sClient, viewRoleName);
}

k8sClient
.rbac()
.kubernetesRoleBindings()
.inNamespace(namespace)
.createOrReplace(createExecRoleBinding());
k8sClient
.rbac()
.kubernetesRoleBindings()
.inNamespace(namespace)
.createOrReplace(createViewRoleBinding());
k8sClient.rbac().roleBindings().inNamespace(namespace).createOrReplace(createExecRoleBinding());
k8sClient.rbac().roleBindings().inNamespace(namespace).createOrReplace(createViewRoleBinding());

// If the user specified an additional cluster role for the workspace,
// create a role binding for it too
if (!isNullOrEmpty(this.clusterRoleName)) {
if (k8sClient.rbac().kubernetesClusterRoles().withName(this.clusterRoleName).get() != null) {
if (k8sClient.rbac().clusterRoles().withName(this.clusterRoleName).get() != null) {
k8sClient
.rbac()
.kubernetesRoleBindings()
.roleBindings()
.inNamespace(namespace)
.createOrReplace(createCustomRoleBinding(this.clusterRoleName));
} else {
Expand All @@ -128,39 +120,39 @@ private void createWorkspaceServiceAccount(KubernetesClient k8sClient) {
}

private void createExecRole(KubernetesClient k8sClient, String name) {
KubernetesRole execRole =
new KubernetesRoleBuilder()
Role execRole =
new RoleBuilder()
.withNewMetadata()
.withName(name)
.endMetadata()
.withRules(
new KubernetesPolicyRuleBuilder()
new PolicyRuleBuilder()
.withResources("pods/exec")
.withApiGroups("")
.withVerbs("create")
.build())
.build();
k8sClient.rbac().kubernetesRoles().inNamespace(namespace).create(execRole);
k8sClient.rbac().roles().inNamespace(namespace).create(execRole);
}

private void createViewRole(KubernetesClient k8sClient, String name) {
KubernetesRole viewRole =
new KubernetesRoleBuilder()
Role viewRole =
new RoleBuilder()
.withNewMetadata()
.withName(name)
.endMetadata()
.withRules(
new KubernetesPolicyRuleBuilder()
new PolicyRuleBuilder()
.withResources("pods", "services")
.withApiGroups("")
.withVerbs("list")
.build())
.build();
k8sClient.rbac().kubernetesRoles().inNamespace(namespace).create(viewRole);
k8sClient.rbac().roles().inNamespace(namespace).create(viewRole);
}

private KubernetesRoleBinding createViewRoleBinding() {
return new KubernetesRoleBindingBuilder()
private RoleBinding createViewRoleBinding() {
return new RoleBindingBuilder()
.withNewMetadata()
.withName(serviceAccountName + "-view")
.withNamespace(namespace)
Expand All @@ -170,16 +162,16 @@ private KubernetesRoleBinding createViewRoleBinding() {
.withName("workspace-view")
.endRoleRef()
.withSubjects(
new KubernetesSubjectBuilder()
new SubjectBuilder()
.withKind("ServiceAccount")
.withName(serviceAccountName)
.withNamespace(namespace)
.build())
.build();
}

private KubernetesRoleBinding createExecRoleBinding() {
return new KubernetesRoleBindingBuilder()
private RoleBinding createExecRoleBinding() {
return new RoleBindingBuilder()
.withNewMetadata()
.withName(serviceAccountName + "-exec")
.withNamespace(namespace)
Expand All @@ -189,16 +181,16 @@ private KubernetesRoleBinding createExecRoleBinding() {
.withName("exec")
.endRoleRef()
.withSubjects(
new KubernetesSubjectBuilder()
new SubjectBuilder()
.withKind("ServiceAccount")
.withName(serviceAccountName)
.withNamespace(namespace)
.build())
.build();
}

private KubernetesRoleBinding createCustomRoleBinding(String clusterRoleName) {
return new KubernetesRoleBindingBuilder()
private RoleBinding createCustomRoleBinding(String clusterRoleName) {
return new RoleBindingBuilder()
.withNewMetadata()
.withName(serviceAccountName + "-custom")
.withNamespace(namespace)
Expand All @@ -208,7 +200,7 @@ private KubernetesRoleBinding createCustomRoleBinding(String clusterRoleName) {
.withName(clusterRoleName)
.endRoleRef()
.withSubjects(
new KubernetesSubjectBuilder()
new SubjectBuilder()
.withKind("ServiceAccount")
.withName(serviceAccountName)
.withNamespace(namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_WORKSPACE_ID_LABEL;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabel;

import com.google.common.collect.ImmutableMap;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim;
Expand Down Expand Up @@ -88,7 +89,7 @@ protected PersistentVolumeClaim createCommonPVC(String workspaceId) {

PersistentVolumeClaim perWorkspacePVC =
newPVC(pvcName, pvcAccessMode, pvcQuantity, pvcStorageClassName);
perWorkspacePVC.getMetadata().getLabels().put(CHE_WORKSPACE_ID_LABEL, workspaceId);
putLabel(perWorkspacePVC.getMetadata(), CHE_WORKSPACE_ID_LABEL, workspaceId);
return perWorkspacePVC;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ private Pod createJwtProxyPod() {
null,
"che-jwtproxy-config-volume",
false,
null,
null))
.withArgs("-config", JWT_PROXY_CONFIG_FOLDER + "/" + JWT_PROXY_CONFIG_FILE)
.addNewEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static long getRamLimit(Container container) {
&& resources.getLimits() != null
&& (quantity = resources.getLimits().get("memory")) != null
&& quantity.getAmount() != null) {
return KubernetesSize.toBytes(quantity.getAmount());
return Quantity.getAmountInBytes(quantity).longValue();
}
return 0;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public static long getRamRequest(Container container) {
&& resources.getRequests() != null
&& (quantity = resources.getRequests().get("memory")) != null
&& quantity.getAmount() != null) {
return KubernetesSize.toBytes(quantity.getAmount());
return Quantity.getAmountInBytes(quantity).longValue();
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Quantity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -117,7 +118,9 @@ private void normalizeMemory(Container container, InternalMachineConfig machineC
.getAttributes()
.put(
MEMORY_LIMIT_ATTRIBUTE,
Long.toString(KubernetesSize.toBytes(overriddenSidecarMemLimit)));
Long.toString(
Quantity.getAmountInBytes(Quantity.parse(overriddenSidecarMemLimit))
.longValue()));
}

long ramRequest = Containers.getRamRequest(container);
Expand All @@ -130,7 +133,9 @@ private void normalizeMemory(Container container, InternalMachineConfig machineC
.getAttributes()
.put(
MEMORY_REQUEST_ATTRIBUTE,
Long.toString(KubernetesSize.toBytes(overriddenSidecarMemRequest)));
Long.toString(
Quantity.getAmountInBytes(Quantity.parse(overriddenSidecarMemRequest))
.longValue()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ private Container newContainer(
.withImagePullPolicy(brokerPullPolicy)
.withEnv(envVars);
if (brokerVolumeName != null) {
cb.withVolumeMounts(new VolumeMount(CONF_FOLDER + "/", null, brokerVolumeName, true, null));
cb.withVolumeMounts(
new VolumeMount(CONF_FOLDER + "/", null, brokerVolumeName, true, null, null));
cb.addToArgs("-metas", CONF_FOLDER + "/" + CONFIG_FILE);
}
Container container = cb.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public void shouldProvisionContainerWithMemoryLimitSpecified() throws Exception
assertEquals(podTemplate.getSpec().getContainers().size(), 1);
Container container = podTemplate.getSpec().getContainers().get(0);
Quantity memoryLimit = container.getResources().getLimits().get("memory");
assertEquals(memoryLimit.getAmount(), "1G");
assertEquals(memoryLimit.getAmount(), "1");
assertEquals(memoryLimit.getFormat(), "G");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
import io.fabric8.kubernetes.api.model.apps.DoneableDeployment;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
Expand All @@ -57,7 +59,7 @@
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.ScalableResource;
import io.fabric8.kubernetes.client.dsl.RollableScalableResource;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -93,9 +95,24 @@ public class KubernetesDeploymentsTest {

// Deployments Mocks
@Mock private AppsAPIGroupDSL apps;
@Mock private MixedOperation deploymentsMixedOperation;
@Mock private NonNamespaceOperation deploymentsNamespaceOperation;
@Mock private ScalableResource deploymentResource;

@Mock
private MixedOperation<
Deployment,
DeploymentList,
DoneableDeployment,
RollableScalableResource<Deployment, DoneableDeployment>>
deploymentsMixedOperation;

@Mock
private NonNamespaceOperation<
Deployment,
DeploymentList,
DoneableDeployment,
RollableScalableResource<Deployment, DoneableDeployment>>
deploymentsNamespaceOperation;

@Mock private RollableScalableResource<Deployment, DoneableDeployment> deploymentResource;
@Mock private Deployment deployment;
@Mock private ObjectMeta deploymentMetadata;
@Mock private DeploymentSpec deploymentSpec;
Expand Down
Loading