Skip to content

Commit

Permalink
Merge pull request quarkusio#10558 from iocanel/knative-cluster-local
Browse files Browse the repository at this point in the history
feat(quarkusio#10557): Add knative cluster-local support
  • Loading branch information
iocanel committed Jul 8, 2020
2 parents 29a7fcb + 0e9271f commit 87b6727
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<aws-alexa-sdk.version>2.30.0</aws-alexa-sdk.version>
<azure-functions-java-library.version>1.3.0</azure-functions-java-library.version>
<kotlin.version>1.3.72</kotlin.version>
<dekorate.version>0.12.3</dekorate.version>
<dekorate.version>0.12.4</dekorate.version>
<maven-artifact-transfer.version>0.10.0</maven-artifact-transfer.version>
<jline.version>2.14.6</jline.version>
<maven-invoker.version>3.0.1</maven-invoker.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,12 @@ public Map<String, EnvConfig> getEnvVars() {
public EnvVarsConfig getEnv() {
return env;
}

/**
* Whether or not this service is cluster-local.
* Cluster local services are not exposed to the outside world.
*/
@ConfigItem
public boolean clusterLocal;

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import io.dekorate.kubernetes.config.Annotation;
import io.dekorate.kubernetes.config.EnvBuilder;
import io.dekorate.kubernetes.config.Label;
import io.dekorate.kubernetes.config.LabelBuilder;
import io.dekorate.kubernetes.config.PortBuilder;
import io.dekorate.kubernetes.configurator.AddPort;
import io.dekorate.kubernetes.decorator.AddAnnotationDecorator;
Expand Down Expand Up @@ -306,6 +307,8 @@ public void build(ApplicationInfoBuildItem applicationInfo,
applyConfig(session, project, KNATIVE, getResourceName(knativeConfig, applicationInfo), knativeConfig, now,
determineImagePullPolicy(knativeConfig, needToForceUpdateImagePullPolicy));

applyKnativeConfig(session, project, getResourceName(knativeConfig, applicationInfo), knativeConfig);

//apply build item configurations to the dekorate session.
applyBuildItems(session,
applicationInfo,
Expand Down Expand Up @@ -489,6 +492,16 @@ private void applyConfig(Session session, Project project, String target, String
session.resources().decorate(target, new RemoveOptionalFromConfigMapKeySelectorDecorator());
}

private void applyKnativeConfig(Session session, Project project, String name, KnativeConfig config) {
if (config.clusterLocal) {
session.resources().decorate(KNATIVE, new AddLabelDecorator(name,
new LabelBuilder()
.withKey("serving.knative.dev/visibility")
.withValue("cluster-local")
.build()));
}
}

/**
* When there is no registry defined and s2i isn't being used, the only ImagePullPolicy that can work is 'IfNotPresent'.
* This case comes up when users want to deploy their application to a cluster like Minikube where no registry is used
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

package io.quarkus.it.kubernetes;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.fail;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.knative.serving.v1.Service;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class KnativeClusterLocalTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(GreetingResource.class))
.setApplicationName("knative-cluster-local").setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource("knative-cluster-local.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void assertGeneratedResources() throws IOException {
Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("knative.json"))
.isDirectoryContaining(p -> p.getFileName().endsWith("knative.yml"));

assertThat(getKNativeService(kubernetesDir)).satisfies(service -> {
assertThat(service.getMetadata()).satisfies(m -> {
assertThat(m.getLabels()).contains(entry("serving.knative.dev/visibility", "cluster-local"));
});
});
}

private Service getKNativeService(Path kubernetesDir) throws IOException {
String[] yamlFiles = DeserializationUtil.splitDocument(
Files.readAllLines(kubernetesDir.resolve("knative.yml"), StandardCharsets.UTF_8).toArray(new String[0]));

for (String yamlFile : yamlFiles) {
if (yamlFile.contains("Service") && !yamlFile.contains("ServiceAccount")) {
return DeserializationUtil.MAPPER.readValue(yamlFile, Service.class);
}
}

fail("No KNative Service was generated");
return null; // keep the compiler happy
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.kubernetes.deployment-target=knative
quarkus.knative.cluster-local=true

0 comments on commit 87b6727

Please sign in to comment.