This is an automated email from the ASF dual-hosted git repository. marat pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-karavan.git
The following commit(s) were added to refs/heads/main by this push: new f4b6c7a2 First prototype for #804 f4b6c7a2 is described below commit f4b6c7a206afb5a062f5c8b1e112b68290c3a61a Author: Marat Gubaidullin <marat.gubaidul...@gmail.com> AuthorDate: Wed Jun 21 09:53:30 2023 -0400 First prototype for #804 --- .../camel/karavan/service/KubernetesService.java | 12 ++- .../org/apache/camel/karavan/cli/CommandUtils.java | 9 +- .../org/apache/camel/karavan/cli/KaravanCli.java | 6 +- .../apache/camel/karavan/cli/KaravanConfig.java | 12 ++- .../karavan/cli/resources/KaravanConfigMap.java | 67 +++++++++++++ .../apache/camel/karavan/cli/resources/Nexus.java | 111 +++++++++++++++++++++ karavan-cli/src/main/resources/settings.xml | 10 ++ 7 files changed, 220 insertions(+), 7 deletions(-) diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/KubernetesService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/KubernetesService.java index 6d0042c3..50c0fb3d 100644 --- a/karavan-app/src/main/java/org/apache/camel/karavan/service/KubernetesService.java +++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/KubernetesService.java @@ -391,7 +391,7 @@ public class KubernetesService implements HealthCheck{ ProjectFile properties = infinispanService.getProjectFile(project.getProjectId(), APPLICATION_PROPERTIES_FILENAME); Map<String,String> containerResources = ServiceUtil .getRunnerContainerResourcesMap(properties, isOpenshift(), project.getRuntime().equals("quarkus")); - Pod pod = getPod(project.getProjectId(), runnerName, containerResources); + Pod pod = getRunnerPod(project.getProjectId(), runnerName, containerResources); Pod result = kubernetesClient().resource(pod).createOrReplace(); LOGGER.info("Created pod " + result.getMetadata().getName()); } @@ -421,7 +421,7 @@ public class KubernetesService implements HealthCheck{ .build(); } - private Pod getPod(String projectId, String name, Map<String,String> containerResources) { + private Pod getRunnerPod(String projectId, String name, Map<String,String> containerResources) { Map<String,String> labels = new HashMap<>(); labels.putAll(getRuntimeLabels()); labels.putAll(getKaravanRunnerLabels(name)); @@ -448,7 +448,9 @@ public class KubernetesService implements HealthCheck{ .withResources(resources) .withImagePullPolicy("Always") .withVolumeMounts( - new VolumeMountBuilder().withName(name).withMountPath("/karavan/.jbang/cache").build()) + new VolumeMountBuilder().withName("maven-settings") + .withMountPath("/karavan/maven-settings.xml") + .withSubPath("maven-settings").build()) .build(); PodSpec spec = new PodSpecBuilder() @@ -456,7 +458,9 @@ public class KubernetesService implements HealthCheck{ .withContainers(container) .withRestartPolicy("Never") .withVolumes( - new VolumeBuilder().withName(name).withNewPersistentVolumeClaim(name, false).build()) + new VolumeBuilder().withName("maven-settings") + .withConfigMap(new ConfigMapVolumeSourceBuilder() + .withName("maven-settings").build()).build()) .build(); return new PodBuilder() diff --git a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/CommandUtils.java b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/CommandUtils.java index c465f48b..53b10eef 100644 --- a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/CommandUtils.java +++ b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/CommandUtils.java @@ -87,7 +87,14 @@ public class CommandUtils { log("Karavan secrets found"); } - // Create service accounts + // Create Nexus Proxy + if (config.isNexusProxy()) { + createOrReplace(Nexus.getDeployment(config), client); + createOrReplace(Nexus.getService(config), client); + } + // Create ConfigMap + createOrReplace(KaravanConfigMap.getConfigMap(config), client); + // Create Service Accounts createOrReplace(KaravanServiceAccount.getServiceAccount(config), client); createOrReplace(KaravanServiceAccount.getServiceAccountPipeline(config), client); // Create Roles and role bindings diff --git a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanCli.java b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanCli.java index 38c3aa8f..a852c8e9 100644 --- a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanCli.java +++ b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanCli.java @@ -65,6 +65,9 @@ public class KaravanCli implements Callable<Integer> { @CommandLine.Option(names = {"--image-registry-password"}, description = "Image registry password") private String imageRegistryPassword; + @CommandLine.Option(names = {"--nexus-proxy"}, description = "Deploy nexus proxy") + private boolean nexusProxy; + @CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display help") private boolean helpRequested; @@ -94,7 +97,8 @@ public class KaravanCli implements Callable<Integer> { imageRegistry, imageGroup, imageRegistryUsername, - imageRegistryPassword + imageRegistryPassword, + nexusProxy ); if (yaml) { Files.writeString(Path.of(file), ResourceUtils.generateResources(config)); diff --git a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanConfig.java b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanConfig.java index 320e856f..476acf1e 100644 --- a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanConfig.java +++ b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/KaravanConfig.java @@ -46,13 +46,14 @@ public class KaravanConfig { private String imageGroup; private String imageRegistryUsername; private String imageRegistryPassword; + private boolean nexusProxy; public KaravanConfig(String version, String namespace, String environment, String runtimes, String auth, int nodePort, int instances, String baseImage, String baseBuilderImage, boolean isOpenShift, Map<String, String> labels, String masterPassword, String oidcSecret, String oidcServerUrl, String oidcFrontendUrl, String gitRepository, String gitUsername, String gitPassword, String gitBranch, String gitPullInterval, String imageRegistry, String imageGroup, - String imageRegistryUsername, String imageRegistryPassword) { + String imageRegistryUsername, String imageRegistryPassword, boolean nexusProxy) { this.version = version; this.namespace = namespace; this.environment = environment; @@ -77,6 +78,7 @@ public class KaravanConfig { this.imageGroup = imageGroup; this.imageRegistryUsername = imageRegistryUsername; this.imageRegistryPassword = imageRegistryPassword; + this.nexusProxy = nexusProxy; } public boolean gitConfigured() { @@ -291,4 +293,12 @@ public class KaravanConfig { public void setImageGroup(String imageGroup) { this.imageGroup = imageGroup; } + + public boolean isNexusProxy() { + return nexusProxy; + } + + public void setNexusProxy(boolean nexusProxy) { + this.nexusProxy = nexusProxy; + } } diff --git a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/resources/KaravanConfigMap.java b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/resources/KaravanConfigMap.java new file mode 100644 index 00000000..83e8aab6 --- /dev/null +++ b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/resources/KaravanConfigMap.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.karavan.cli.resources; + +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; +import org.apache.camel.karavan.cli.Constants; +import org.apache.camel.karavan.cli.KaravanConfig; +import org.apache.camel.karavan.cli.ResourceUtils; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Map; +import java.util.stream.Collectors; + +public class KaravanConfigMap { + + private static final String MAVEN_URL = "<url>https://repo.maven.apache.org/maven2/</url>"; + + public static ConfigMap getConfigMap(KaravanConfig config) { + + String xml = getXml(config); + + return new ConfigMapBuilder() + .withNewMetadata() + .withName(Constants.NAME) + .withNamespace(config.getNamespace()) + .withLabels(ResourceUtils.getLabels(Constants.NAME, config.getVersion(), Map.of())) + .endMetadata() + .withData(Map.of("maven-settings", xml)) + .build(); + } + + private static String getXml(KaravanConfig config) { + try { + InputStream inputStream = KaravanConfigMap.class.getResourceAsStream("/settings.xml"); + return new BufferedReader(new InputStreamReader(inputStream)) + .lines() + .map(s -> { + if (config.isNexusProxy() && s.contains("<url>http://nexus.karavan/</url>")) { + String newMavenUrl = "<url>http://nexus." + config.getNamespace() + "/</url>"; + return s.replace(MAVEN_URL, newMavenUrl); + } else { + return s; + } + }) + .collect(Collectors.joining(System.getProperty("line.separator"))); + } catch (Exception e) { + return null; + } + } +} diff --git a/karavan-cli/src/main/java/org/apache/camel/karavan/cli/resources/Nexus.java b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/resources/Nexus.java new file mode 100644 index 00000000..cb4aaecb --- /dev/null +++ b/karavan-cli/src/main/java/org/apache/camel/karavan/cli/resources/Nexus.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.karavan.cli.resources; + +import io.fabric8.kubernetes.api.model.*; +import io.fabric8.kubernetes.api.model.apps.Deployment; +import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; +import org.apache.camel.karavan.cli.Constants; +import org.apache.camel.karavan.cli.KaravanConfig; + +import java.util.Map; + +public class Nexus { + + public static final String NEXUS_NAME = "nexus"; + public static final String NEXUS_IMAGE = "sonatype/nexus3"; + public static final String NEXUS_DATA = "nexus-data"; + public static final int NEXUS_PORT = 8081; + + public static Service getService(KaravanConfig config) { + + ServicePortBuilder portBuilder = new ServicePortBuilder() + .withPort(80) + .withProtocol("TCP") + .withTargetPort(new IntOrString(NEXUS_PORT)); + + return new ServiceBuilder() + .withNewMetadata() + .withName(NEXUS_NAME) + .withNamespace(config.getNamespace()) + .endMetadata() + .withNewSpec() + .withSelector(Map.of("app", NEXUS_NAME)) + .withPorts(portBuilder.build()) + .endSpec() + .build(); + } + + public static Deployment getDeployment (KaravanConfig config) { + return new DeploymentBuilder() + .withNewMetadata() + .withName(NEXUS_NAME) + .withNamespace(config.getNamespace()) + .endMetadata() + + .withNewSpec() + .withNewSelector() + .addToMatchLabels(Map.of("app", NEXUS_NAME)) + .endSelector() + + .withNewTemplate() + .withNewMetadata() + .addToLabels(Map.of("app", NEXUS_NAME)) + .endMetadata() + + .withNewSpec() + .addNewContainer() + .withName(NEXUS_NAME) + .withImage(NEXUS_IMAGE) + .withImagePullPolicy("Always") + .addNewPort() + .withContainerPort(NEXUS_PORT) + .withName("8081-tcp") + .endPort() + .withVolumeMounts( + new VolumeMountBuilder().withName(NEXUS_DATA).withMountPath("/" + NEXUS_DATA).build() + ) + .withLivenessProbe( + new ProbeBuilder() + .withHttpGet(new HTTPGetActionBuilder() + .withPath("/service/rest/v1/status") + .withPort(new IntOrString(NEXUS_PORT)) + .build()) + .withInitialDelaySeconds(90) + .withPeriodSeconds(3) + .build()) + .withReadinessProbe( + new ProbeBuilder() + .withHttpGet(new HTTPGetActionBuilder() + .withPath("/service/rest/v1/status") + .withPort(new IntOrString(NEXUS_PORT)) + .build()) + .withInitialDelaySeconds(90) + .withPeriodSeconds(3) + .build()) + .endContainer() + .withServiceAccount(Constants.NAME) + .withVolumes( + new VolumeBuilder().withName(NEXUS_DATA).withEmptyDir(new EmptyDirVolumeSource()).build() + ) + .endSpec() + .endTemplate() + .endSpec() + .build(); + } + +} diff --git a/karavan-cli/src/main/resources/settings.xml b/karavan-cli/src/main/resources/settings.xml new file mode 100644 index 00000000..9acf76d3 --- /dev/null +++ b/karavan-cli/src/main/resources/settings.xml @@ -0,0 +1,10 @@ +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> + <mirrors> + <mirror> + <id>karavan-maven-repository-manager</id> + <name>Maven Repository Manager</name> + <url>https://repo.maven.apache.org/maven2/</url> + <mirrorOf>*</mirrorOf> + </mirror> + </mirrors> +</settings> \ No newline at end of file