Skip to content

Commit

Permalink
Merge pull request #77 from kabir/kabir-changes
Browse files Browse the repository at this point in the history
The ability to use Helm {{--set}} overrides
  • Loading branch information
fabiobrz authored Aug 23, 2023
2 parents d0137d6 + 234419f commit 844e15d
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed 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.jboss.intersmash.testsuite.provision.openshift;

import java.net.URL;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.intersmash.deployments.IntersmashDelpoyableWildflyApplication;
import org.jboss.intersmash.model.helm.charts.values.wildfly.HelmWildflyRelease;
import org.jboss.intersmash.tools.IntersmashConfig;
import org.jboss.intersmash.tools.application.openshift.helm.HelmChartRelease;
import org.jboss.intersmash.tools.application.openshift.helm.WildflyHelmChartOpenShiftApplication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;

import io.fabric8.kubernetes.api.model.Secret;

public class WildflyHelmChartExistingValuesOpenShiftExampleApplicaton
implements WildflyHelmChartOpenShiftApplication, IntersmashDelpoyableWildflyApplication {
private static final String APP_NAME = "wildfly-helm-helloworld-qs";

private final HelmChartRelease release;
private final Map<String, String> setOverrides = new HashMap<>();

public WildflyHelmChartExistingValuesOpenShiftExampleApplicaton() {
this.release = new HelmChartRelease(loadRelease());
}

WildflyHelmChartExistingValuesOpenShiftExampleApplicaton addSetOverride(String name, String value) {
setOverrides.put(name, value);
return this;
}

@Override
public Map<String, String> getSetOverrides() {
return setOverrides;
}

private HelmWildflyRelease loadRelease() {
URL url = this.getClass().getResource("wildfly-helm-values.yaml");
if (url == null) {
throw new IllegalStateException("No wildfly-helm-values.yaml found");
}
try {
Path valuePath = Path.of(url.toURI());
ObjectMapper mapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
return mapper.readValue(valuePath.toFile(), HelmWildflyRelease.class);
} catch (Error | RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public HelmChartRelease getRelease() {
return release;
}

@Override
public List<Secret> getSecrets() {
return Collections.emptyList();
}

@Override
public String getName() {
return APP_NAME;
}

@Override
public String getHelmChartsRepositoryUrl() {
return IntersmashConfig.getWildflyHelmChartsRepo();
}

@Override
public String getHelmChartsRepositoryRef() {
return IntersmashConfig.getWildflyHelmChartsBranch();
}

@Override
public String getHelmChartsRepositoryName() {
return IntersmashConfig.getWildflyHelmChartsName();
}

@Override
public String getBuilderImage() {
return IntersmashConfig.wildflyImageURL();
}

@Override
public String getRuntimeImage() {
return IntersmashConfig.wildflyRuntimeImageURL();
}

@Override
public String bomServerVersionPropertyValue() {
return IntersmashConfig.getWildflyBomsEeServerVersion();
}

@Override
public String eeFeaturePackLocation() {
// this value is supposed to be overridden by the CI Jenkins job by passing e.g.
// "mvn ... -Dwildfly.ee-feature-pack.location="
return IntersmashConfig.getWildflyEeFeaturePackLocation();
}

@Override
public String cloudFeaturePackLocation() {
// this value is supposed to be overridden by the CI Jenkins job by passing e.g.
// "mvn ... -Dwildfly.cloud-feature-pack.location="
return IntersmashConfig.getWildflyCloudFeaturePackLocation();
}

@Override
public String eeChannelLocation() {
// this value is supposed to be overridden by the CI Jenkins job by passing e.g.
// "mvn ... -Dwildfly.ee-channel.location="
return IntersmashConfig.getWildflyEeChannelLocation();
}

@Override
public String wildflyMavenPluginGroupId() {
return IntersmashConfig.getWildflyMavenPluginGroupId();
}

@Override
public String wildflyMavenPluginArtifactId() {
return IntersmashConfig.getWildflyMavenPluginArtifactId();
}

@Override
public String wildflyMavenPluginVersion() {
return IntersmashConfig.getWildflyMavenPluginVersion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed 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.jboss.intersmash.testsuite.provision.openshift;

import org.jboss.intersmash.tools.IntersmashConfig;
import org.jboss.intersmash.tools.provision.helm.HelmChartOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.helm.WildflyHelmChartOpenShiftProvisioner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import cz.xtf.junit5.annotations.CleanBeforeAll;

/**
* Test case to verify the basic {@link HelmChartOpenShiftProvisioner}
* life cycle management operations on a WildFly application which provides
* a Helm values file AND {@code --set} overrides
*/
@CleanBeforeAll
public class WildflyHelmChartExistingValuesProvisionerTest {

@Test
public void basicProvisioningTest() {
// initialize the application service descriptor
final WildflyHelmChartExistingValuesOpenShiftExampleApplicaton application = new WildflyHelmChartExistingValuesOpenShiftExampleApplicaton();
application
.addSetOverride("build.uri", IntersmashConfig.deploymentsRepositoryUrl())
.addSetOverride("build.ref", IntersmashConfig.deploymentsRepositoryRef())
.addSetOverride("deploy.builderImage", application.getBuilderImage())
.addSetOverride("deployRuntimeImage", application.getRuntimeImage());

// and now get an EAP 8/WildFly provisioner for that application
final WildflyHelmChartOpenShiftProvisioner provisioner = new WildflyHelmChartOpenShiftProvisioner(application);
// deploy
provisioner.preDeploy();
try {
provisioner.deploy();
try {
Assertions.assertEquals(1, provisioner.getPods().size(),
"Unexpected number of cluster operator pods for '"
+ provisioner.getApplication().getName() + "' after deploy");

// scale
int scaledNum = provisioner.getApplication().getRelease().getReplicas() + 1;
provisioner.scale(scaledNum, true);
Assertions.assertEquals(scaledNum, provisioner.getPods().size(),
"Unexpected number of cluster operator pods for '"
+ provisioner.getApplication().getName()
+ "' after scaling");
} finally {
// undeploy
provisioner.undeploy();
Assertions.assertEquals(0, provisioner.getPods().size(),
"Unexpected number of pods after undeploy");
}
} finally {
provisioner.postUndeploy();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
build:
enabled: true
mode: "s2i"
# uri and ref will be specified via Helm --set arguments
contextDir: "deployments/openshift-jakarta-sample-standalone"
env:
- name: "MAVEN_ARGS_APPEND"
value: "-Denforcer.skip=true"
s2i:
kind: "DockerImage"
buildApplicationImage: true
# builderImage and runtimeImage will be specified via Helm --set arguments
deploy:
enabled: true
replicas: 1
env: []
envFrom: []
volumeMounts: []
volumes: []
initContainers: []
extraContainers: []
'imagePullSecrets:': []
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.jboss.intersmash.tools.application.openshift.helm;

import java.util.Collections;
import java.util.Map;

import org.jboss.intersmash.tools.application.openshift.HasSecrets;
import org.jboss.intersmash.tools.application.openshift.OpenShiftApplication;
import org.jboss.intersmash.tools.provision.helm.HelmChartOpenShiftProvisioner;
Expand Down Expand Up @@ -48,4 +51,12 @@ public interface HelmChartOpenShiftApplication extends OpenShiftApplication, Has
* @return A string that identifies the used Helm Charts repository name
*/
String getHelmChartsRepositoryName();

/**
* Get values that should be overridden via {@code --set} parameters when running {@code helm install}
* @return A map of name/value pairs to override. May not be {@code null}
*/
default Map<String, String> getSetOverrides() {
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -129,6 +131,7 @@ private static String[] getHelmChartUpgradeArguments(
List<String> arguments = Stream.of("upgrade", application.getName(), helmChartPath.toAbsolutePath().toString())
.collect(Collectors.toList());
arguments.addAll(Arrays.asList(getHelmChartValuesFilesArguments(application)));
arguments.addAll(getSetOverrideArguments(application));
arguments.addAll(Arrays.asList(
"--kubeconfig", OpenShifts.adminBinary().getOcConfigPath(),
// since we deploy from cloned charts repository, we need to set the "--dependency-update"
Expand All @@ -143,6 +146,7 @@ private static String[] getHelmChartInstallArguments(
List<String> arguments = Stream.of("install", application.getName(), helmChartPath.toAbsolutePath().toString(),
"--replace").collect(Collectors.toList());
arguments.addAll(Arrays.asList(getHelmChartValuesFilesArguments(application)));
arguments.addAll(getSetOverrideArguments(application));
arguments.addAll(Arrays.asList(
"--kubeconfig", OpenShifts.adminBinary().getOcConfigPath(),
// since we deploy from cloned charts repository, we need to set the "--dependency-update"
Expand All @@ -169,6 +173,19 @@ private static String[] getHelmChartValuesFilesArguments(HelmChartOpenShiftAppli
return arguments.stream().toArray(String[]::new);
}

private static List<String> getSetOverrideArguments(HelmChartOpenShiftApplication application) {
Map<String, String> setOverrides = application.getSetOverrides();
if (setOverrides.isEmpty()) {
return Collections.emptyList();
}
List<String> arguments = new ArrayList<>();
for (Map.Entry<String, String> entry : setOverrides.entrySet()) {
arguments.add("--set");
arguments.add(entry.getKey() + "=" + entry.getValue());
}
return arguments;
}

protected Map<String, Path> getHelmCharts() {
final String cachedChartsKey = forgeHelmChartsKey();
if (!HELM_CHARTS.containsKey(cachedChartsKey)) {
Expand Down

0 comments on commit 844e15d

Please sign in to comment.