Skip to content

Commit

Permalink
Fix eclipse-jkube#146: ApplyMojo should use ApplyService in JKubeServ…
Browse files Browse the repository at this point in the history
…iceHub
  • Loading branch information
rohanKanojia committed Dec 3, 2020
1 parent 7ee0a71 commit 9a2c48a
Show file tree
Hide file tree
Showing 8 changed files with 583 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,20 @@ public static void callMavenPluginWithGoal(
mojoExecutionService.callPluginGoal(mavenPluginGoal);
}
}

/**
* Returns the root project folder
*/
public static File getRootProjectFolder(MavenProject project) {
File answer = null;
while (project != null) {
File basedir = project.getBasedir();
if (basedir != null) {
answer = basedir;
}
project = project.getParent();
}
return answer;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class MavenUtilTest {
@Rule
Expand Down Expand Up @@ -157,6 +158,8 @@ public void testLoadedPomFromFile() throws Exception {

private MavenProject getMavenProject() {
MavenProject mavenProject = new MavenProject();
File baseDir = new File("test-project-base-dir");
mavenProject.setFile(baseDir);
mavenProject.setName("testProject");
mavenProject.setGroupId("org.eclipse.jkube");
mavenProject.setArtifactId("test-project");
Expand Down Expand Up @@ -222,4 +225,24 @@ public void testCallMavenPluginWithGoal(@Mocked BuildPluginManager pluginManager
times = 1;
}};
}

@Test
public void testgetRootProjectFolder(@Mocked MavenProject project) {
// Given
File projectBaseDir = new File("projectBaseDir");
new Expectations() {{
project.getBasedir();
result = projectBaseDir;

project.getParent();
result = null;
}};

// When
File rootFolder = MavenUtil.getRootProjectFolder(project);

// Then
assertNotNull(rootFolder);
assertEquals("projectBaseDir", rootFolder.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.Context;
import io.fabric8.kubernetes.api.model.DoneablePod;
import io.fabric8.kubernetes.api.model.DoneableService;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
Expand All @@ -55,6 +56,7 @@
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder;
import io.fabric8.kubernetes.api.model.LabelSelectorRequirement;
import io.fabric8.kubernetes.api.model.NamedContext;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodCondition;
Expand All @@ -64,6 +66,7 @@
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.ReplicationControllerSpec;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.DaemonSet;
import io.fabric8.kubernetes.api.model.apps.DaemonSetSpec;
import io.fabric8.kubernetes.api.model.apps.Deployment;
Expand All @@ -84,10 +87,12 @@
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.api.model.HasMetadataComparator;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.Build;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigSpec;
import io.fabric8.openshift.api.model.Project;
import io.fabric8.openshift.api.model.Template;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -900,5 +905,60 @@ public static String getNewestApplicationPodName(KubernetesClient client, String
}
return null;
}

public static boolean isExposeService(Service service) {
String expose = KubernetesHelper.getLabels(service).get("expose");
return expose != null && expose.equalsIgnoreCase("true");
}

public static String getServiceExposeUrl(KubernetesClient kubernetes, Collection<HasMetadata> resources, long serviceUrlWaitTimeSeconds, String exposeServiceAnnotationKey) throws InterruptedException {
for (HasMetadata entity : resources) {
if (entity instanceof Service) {
Service service = (Service) entity;
String name = KubernetesHelper.getName(service);
String namespace = kubernetes.getNamespace();
Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(namespace).withName(name);
String url = pollServiceForExposeUrl(serviceUrlWaitTimeSeconds, service, serviceResource, exposeServiceAnnotationKey);

// lets not wait for other services
serviceUrlWaitTimeSeconds = 1;
if (StringUtils.isNotBlank(url) && url.startsWith("http")) {
return url;
}
}
}
return null;
}

private static String pollServiceForExposeUrl(long serviceUrlWaitTimeSeconds, Service service, Resource<Service, DoneableService> serviceResource, String exposeSvcAnnotationKey) throws InterruptedException {
String url = null;
// lets wait a little while until there is a service URL in case the exposecontroller is running slow
for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
if (i > 0) {
Thread.sleep(1000);
}
url = KubernetesHelper.getAnnotationValue(serviceResource.get(), exposeSvcAnnotationKey);
if (StringUtils.isNotBlank(url) || !KubernetesHelper.isExposeService(service)) {
break;
}
}
return url;
}

public static String getAnnotationValue(HasMetadata item, String annotationKey) {
if (item != null) {
return getOrCreateAnnotations(item).get(annotationKey);
}
return null;
}

public static String getNamespaceFromKubernetesList(Collection<HasMetadata> entities) {
for (HasMetadata h : entities) {
if (h instanceof Namespace || h instanceof Project) {
return h.getMetadata().getName();
}
}
return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
*/
package org.eclipse.jkube.kit.common.util;

import io.fabric8.kubernetes.api.model.DoneableService;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.Resource;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Verifications;
import org.eclipse.jkube.kit.common.KitLogger;
import org.junit.Test;

Expand All @@ -27,10 +37,13 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -175,6 +188,59 @@ public void testRemoveEnvVar() {
assertEquals("defaultValue", KubernetesHelper.getEnvVar(envVarList, "FOO", "defaultValue"));
}

@Test
public void testIsExposedServiceReturnsTrue() {
// Given
Service service = new ServiceBuilder()
.withNewMetadata()
.addToLabels("expose", "true")
.withName("svc1")
.endMetadata()
.build();

// When
boolean result = KubernetesHelper.isExposeService(service);

// Then
assertTrue(result);
}

@Test
public void testIsExposedServiceReturnsFalse() {
// Given
Service service = new ServiceBuilder().withNewMetadata().withName("svc1").endMetadata().build();

// When
boolean result = KubernetesHelper.isExposeService(service);

// Then
assertFalse(result);
}

@Test
public void testGetAnnotationValue() {
// Given
Service svc = new ServiceBuilder()
.withNewMetadata()
.withName("svc1")
.addToAnnotations("expose", "true")
.addToAnnotations("exposeUrl", "http://12.4.1.4:8223/test")
.endMetadata().build();
Service svc2 = null;

// When
String result1 = KubernetesHelper.getAnnotationValue(svc, "expose");
String result2 = KubernetesHelper.getAnnotationValue(svc, "exposeUrl");
String result3 = KubernetesHelper.getAnnotationValue(svc, "iDontExist");
String result4 = KubernetesHelper.getAnnotationValue(svc2, "expose");

// Then
assertEquals("true", result1);
assertEquals("http://12.4.1.4:8223/test", result2);
assertNull(result3);
assertNull(result4);
}

@Test
public void testConvertToEnvVarList() {
// Given
Expand All @@ -195,6 +261,67 @@ public void testConvertToEnvVarList() {

}

@Test
public void testGetServiceExposeUrlReturnsUrlFromAnnotation(@Mocked KubernetesClient kubernetesClient, @Mocked Resource<Service, DoneableService> svcResource) throws InterruptedException {
// Given
Service svc = new ServiceBuilder().withNewMetadata().withName("svc1").endMetadata().build();
Set<HasMetadata> entities = new HashSet<>();
entities.add(svc);
new Expectations() {{
kubernetesClient.services().inNamespace(anyString).withName("svc1");
result = svcResource;
svcResource.get();
result = new ServiceBuilder()
.withNewMetadata()
.withName("svc1")
.addToAnnotations("exposeUrl", "http://example.com")
.endMetadata()
.build();
}};

// When
String result = KubernetesHelper.getServiceExposeUrl(kubernetesClient, entities, 3, "exposeUrl");

// Then
assertEquals("http://example.com", result);
new Verifications() {{
kubernetesClient.services().inNamespace(anyString).withName("svc1");
times = 1;
svcResource.get();
times = 1;
}};
}

@Test
public void testGetServiceExposeUrlReturnsNull(@Mocked KubernetesClient kubernetesClient, @Mocked Resource<Service, DoneableService> svcResource) throws InterruptedException {
// Given
Service svc = new ServiceBuilder().withNewMetadata().withName("svc1").endMetadata().build();
Set<HasMetadata> entities = new HashSet<>();
entities.add(svc);
new Expectations() {{
kubernetesClient.services().inNamespace(anyString).withName("svc1");
result = svcResource;
svcResource.get();
result = new ServiceBuilder()
.withNewMetadata()
.withName("svc1")
.endMetadata()
.build();
}};

// When
String result = KubernetesHelper.getServiceExposeUrl(kubernetesClient, entities, 1, "exposeUrl");

// Then
assertNull(result);
new Verifications() {{
kubernetesClient.services().inNamespace(anyString).withName("svc1");
times = 1;
svcResource.get();
times = 1;
}};
}

@Test
public void getCustomResourcesFileToNameMapWithNoFragmentsShouldReturnEmptyMap() throws Exception {
// When
Expand Down Expand Up @@ -242,6 +369,21 @@ public void testGetFullyQualifiedApiGroupWithKind() {
assertEquals("networking.istio.io/v1alpha3#Gateway", result2);
}

@Test
public void testGetNamespaceFromKubernetesList() {
// Given
List<HasMetadata> entities = new ArrayList<>();
entities.add(new NamespaceBuilder().withNewMetadata().withName("ns1").endMetadata().build());
entities.add(new DeploymentBuilder().withNewMetadata().withName("d1").endMetadata().build());

// When
String namespace = KubernetesHelper.getNamespaceFromKubernetesList(entities);

// Then
assertNotNull(namespace);
assertEquals("ns1", namespace);
}

private void assertLocalFragments(File[] fragments, int expectedSize) {
assertEquals(expectedSize, fragments.length);
assertTrue(Arrays.stream(fragments).anyMatch( f -> f.getName().equals("deployment.yml")));
Expand Down
Loading

0 comments on commit 9a2c48a

Please sign in to comment.