Skip to content

Commit

Permalink
feat (jkube-kit/helm) : HelmService exposes helm install functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jul 12, 2024
1 parent 2b5d271 commit e9887d1
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.util;

import io.fabric8.kubernetes.api.model.APIGroupBuilder;
import io.fabric8.kubernetes.api.model.APIGroupList;
import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
import io.fabric8.kubernetes.api.model.APIResourceBuilder;
import io.fabric8.kubernetes.api.model.APIResourceList;
import io.fabric8.kubernetes.api.model.APIResourceListBuilder;
import io.fabric8.kubernetes.client.VersionInfo;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;

public class KubernetesMockServerUtil {

private KubernetesMockServerUtil() { }

// TODO: Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed
public static void prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(KubernetesMockServer server) {
server.expect().get()
.withPath("/version?timeout=32s")
.andReturn(200, new VersionInfo.Builder()
.withMajor("1")
.withMinor("30")
.build())
.always();
server.expect().get().withPath("/api?timeout=32s")
.andReturn(200, String.format("{\"kind\":\"APIVersions\",\"versions\":[\"v1\"],\"serverAddressByClientCIDRs\":[{\"clientCIDR\":\"0.0.0.0/0\",\"serverAddress\":\"%s:%d\"}]}", server.getHostName(), server.getPort()))
.always();
server.expect().get().withPath("/apis?timeout=32s")
.andReturn(200, createNewAPIGroupList())
.always();
server.expect().get().withPath("/api/v1?timeout=32s")
.andReturn(200, createNewAPIResourceList())
.always();
server.expect().get().withPath("/apis/apps/v1?timeout=32s")
.andReturn(200, createNewAPIResourceList())
.always();
}

private static APIResourceList createNewAPIResourceList() {
APIResourceListBuilder apiResourceListBuilder = new APIResourceListBuilder();
apiResourceListBuilder.addToResources(new APIResourceBuilder()
.withNamespaced()
.withKind("Service")
.withName("services")
.withSingularName("service")
.build());
apiResourceListBuilder.addToResources(new APIResourceBuilder()
.withName("deployments")
.withKind("Deployment")
.withSingularName("deployment")
.withNamespaced()
.build());
apiResourceListBuilder.addToResources(new APIResourceBuilder()
.withName("serviceaccounts")
.withKind("ServiceAccount")
.withSingularName("serviceaccount")
.withNamespaced()
.build());
apiResourceListBuilder.addToResources(new APIResourceBuilder()
.withName("secrets")
.withKind("Secret")
.withSingularName("secret")
.withNamespaced()
.build());
return apiResourceListBuilder.build();
}

private static APIGroupList createNewAPIGroupList() {
return new APIGroupListBuilder()
.addToGroups(new APIGroupBuilder()
.withName("apps")
.addNewVersion()
.withGroupVersion("apps/v1")
.withVersion("v1")
.endVersion()
.withNewPreferredVersion()
.withGroupVersion("apps/v1")
.withVersion("v1")
.endPreferredVersion()
.build())
.build();
}
}
4 changes: 4 additions & 0 deletions jkube-kit/helm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@
<groupId>io.fabric8</groupId>
<artifactId>mockwebserver</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>openshift-server-mock</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class HelmConfig {
private boolean debug;
private boolean dependencyVerify;
private boolean dependencySkipRefresh;
private String releaseName;
private boolean installDependencyUpdate;
private boolean installWaitReady;
private boolean disableOpenAPIValidation;


@JsonProperty("dependencies")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -35,8 +36,12 @@

import com.marcnuri.helm.DependencyCommand;
import com.marcnuri.helm.Helm;
import com.marcnuri.helm.InstallCommand;
import com.marcnuri.helm.LintCommand;
import com.marcnuri.helm.LintResult;
import com.marcnuri.helm.Release;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.common.JKubeException;
import org.eclipse.jkube.kit.common.KitLogger;
Expand All @@ -63,6 +68,7 @@
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.eclipse.jkube.kit.common.JKubeFileInterpolator.DEFAULT_FILTER;
import static org.eclipse.jkube.kit.common.JKubeFileInterpolator.interpolate;
import static org.eclipse.jkube.kit.common.util.KubernetesHelper.exportKubernetesClientConfigToFile;
import static org.eclipse.jkube.kit.common.util.MapUtil.getNestedMap;
import static org.eclipse.jkube.kit.common.util.TemplateUtil.escapeYamlTemplate;
import static org.eclipse.jkube.kit.common.util.YamlUtil.listYamls;
Expand Down Expand Up @@ -189,6 +195,52 @@ public void dependencyUpdate(HelmConfig helmConfig) {
}
}

public void install(HelmConfig helmConfig) {
for (HelmConfig.HelmType helmType : helmConfig.getTypes()) {
logger.info("Installing Helm Chart %s %s", helmConfig.getChart(), helmConfig.getVersion());
InstallCommand installCommand = new Helm(Paths.get(helmConfig.getOutputDir(), helmType.getOutputDir()))
.install();
if (helmConfig.isInstallDependencyUpdate()) {
installCommand.dependencyUpdate();
}
if (StringUtils.isNotBlank(helmConfig.getReleaseName())) {
installCommand.withName(helmConfig.getReleaseName());
}
if (helmConfig.isInstallWaitReady()) {
installCommand.waitReady();
}
if (helmConfig.isDisableOpenAPIValidation()) {
installCommand.disableOpenApiValidation();
}
installCommand.withKubeConfig(createTemporaryKubeConfigForInstall());
Release release = installCommand.call();
logger.info("[[W]]NAME : %s", release.getName());
logger.info("[[W]]NAMESPACE : %s", release.getNamespace());
logger.info("[[W]]STATUS : %s", release.getStatus());
logger.info("[[W]]REVISION : %s", release.getRevision());
logger.info("[[W]]LAST DEPLOYED : %s", release.getLastDeployed().format(DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss yyyy")));
Arrays.stream(release.getOutput().split("---"))
.filter(o -> o.contains("Deleting outdated charts"))
.findFirst()
.ifPresent(s -> Arrays.stream(s.split("\r?\n"))
.filter(StringUtils::isNotBlank)
.forEach(l -> logger.info("[[W]]%s", l)));
}
}

private Path createTemporaryKubeConfigForInstall() {
try {
File kubeConfigParentDir = new File(jKubeConfiguration.getProject().getBuildDirectory(), "jkube-temp");
FileUtil.createDirectory(kubeConfigParentDir);
File helmInstallKubeConfig = new File(kubeConfigParentDir, "config");
helmInstallKubeConfig.deleteOnExit();
exportKubernetesClientConfigToFile(jKubeConfiguration.getClusterConfiguration().getConfig(), helmInstallKubeConfig.toPath());
return helmInstallKubeConfig.toPath();
} catch (IOException ioException) {
throw new JKubeException("Failure in creating temporary kubeconfig file", ioException);
}
}

public void lint(HelmConfig helmConfig) {
for (HelmConfig.HelmType helmType : helmConfig.getTypes()) {
final Path helmPackage = resolveTarballFile(helmConfig, helmType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.Maintainer;
import org.eclipse.jkube.kit.common.RegistryServerConfiguration;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.Serialization;
import org.eclipse.jkube.kit.config.resource.GroupArtifactVersion;
import org.eclipse.jkube.kit.config.resource.JKubeAnnotations;

import java.io.File;
Expand Down Expand Up @@ -80,6 +82,10 @@ public class HelmServiceUtil {
protected static final String PROPERTY_HELM_DEBUG = "jkube.helm.debug";
protected static final String PROPERTY_HELM_DEPENDENCY_VERIFY = "jkube.helm.dependencyVerify";
protected static final String PROPERTY_HELM_DEPENDENCY_SKIP_REFRESH = "jkube.helm.dependencySkipRefresh";
protected static final String PROPERTY_HELM_RELEASE_NAME = "jkube.helm.release.name";
protected static final String PROPERTY_HELM_INSTALL_DEPENDENCY_UPDATE = "jkube.helm.install.dependencyUpdate";
protected static final String PROPERTY_HELM_INSTALL_WAIT_READY = "jkube.helm.install.waitReady";
protected static final String PROPERTY_HELM_DISABLE_OPENAPI_VALIDATION = "jkube.helm.disableOpenAPIValidation";

private HelmServiceUtil() { }

Expand Down Expand Up @@ -123,6 +129,11 @@ public static HelmConfig.HelmConfigBuilder initHelmConfig(
helmConfig.setDebug(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DEBUG, project, helmConfig::isDebug));
helmConfig.setDependencyVerify(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DEPENDENCY_VERIFY, project, helmConfig::isDependencyVerify));
helmConfig.setDependencySkipRefresh(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DEPENDENCY_SKIP_REFRESH, project, helmConfig::isDependencySkipRefresh));
helmConfig.setReleaseName(resolveFromPropertyOrDefault(PROPERTY_HELM_RELEASE_NAME, project, helmConfig::getReleaseName, () -> JKubeProjectUtil.createDefaultResourceName(new GroupArtifactVersion(project.getGroupId(), project.getArtifactId(), project.getVersion()).getSanitizedArtifactId())));
helmConfig.setInstallDependencyUpdate(original == null || original.isInstallDependencyUpdate());
helmConfig.setInstallDependencyUpdate(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_INSTALL_DEPENDENCY_UPDATE, project, helmConfig::isInstallDependencyUpdate));
helmConfig.setInstallWaitReady(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_INSTALL_WAIT_READY, project, helmConfig::isInstallWaitReady));
helmConfig.setDisableOpenAPIValidation(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DISABLE_OPENAPI_VALIDATION, project, helmConfig::isDisableOpenAPIValidation));
return helmConfig.toBuilder();
}

Expand Down
Loading

0 comments on commit e9887d1

Please sign in to comment.