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 Nov 16, 2020
1 parent 5c745b9 commit 8dd2f64
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 312 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 @@ -64,6 +65,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 @@ -85,6 +87,7 @@
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;
Expand Down Expand Up @@ -114,9 +117,8 @@ private KubernetesHelper() {}
* @param currentValue current value
* @param description description
* @return valid value according to kubernetes ID parsing rules
* @throws IllegalArgumentException exception if arguement is invalid
*/
public static String validateKubernetesId(String currentValue, String description) throws IllegalArgumentException {
public static String validateKubernetesId(String currentValue, String description) {
if (StringUtils.isBlank(currentValue)) {
throw new IllegalArgumentException("No " + description + " is specified!");
}
Expand All @@ -136,10 +138,9 @@ public static String validateKubernetesId(String currentValue, String descriptio
*
* @param entity Kubernetes generic resource object
* @return list of objects of type HasMetadata
* @throws IOException IOException if anything wrong happens
*/
@SuppressWarnings("unchecked")
public static List<HasMetadata> toItemList(Object entity) throws IOException {
public static List<HasMetadata> toItemList(Object entity) {
if (entity instanceof List) {
return (List<HasMetadata>) entity;
} else if (entity instanceof HasMetadata[]) {
Expand Down Expand Up @@ -903,5 +904,51 @@ 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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
*/
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.Quantity;
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 +35,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 +186,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 +259,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
Loading

0 comments on commit 8dd2f64

Please sign in to comment.