manusa / helm-java

Helm client for Java
https://www.marcnuri.com
Apache License 2.0
20 stars 7 forks source link

Unable to set values Object types with UpgradeCommand #136

Closed vsingh58 closed 3 months ago

vsingh58 commented 4 months ago

I am using Helm UpdateCommand to install open search helm chart but would like to override few values in values.yaml file.

e.g. I want to send list of roles, e.g master, ingest and data. https://github.com/opensearch-project/helm-charts/blob/main/charts/opensearch/values.yaml#L16

   // Test code
   Map<String, Object> values = new HashMap<>();   
   values.put("clusterName", "my-opensearch-cluster");
    List<String> roles = new ArrayList<>();
    roles.add("master"); roles.add("ingest"); roles.add("data"); 
    values.put("roles", roles);
    values.put("replicas", 2);
    boolean result = helmInstaller.installDataStoreHelmCharts("opensearch", "default", values);

    public boolean installDataStoreHelmCharts(String chartName, String namespace, Map<String, Object> values) {
    try {
      LOGGER.info("Installing " + chartName);   
      UpgradeCommand upgradeCommand = new Helm(Paths.get(basePath, chartName)).upgrade();
      upgradeCommand = upgradeCommand.withName(chartName).install().withNamespace(namespace).createNamespace();

      // For each value in the optional values map, call the set method on the upgradeCommand object to override the
      // value in the chart's `values.yaml` file.
      if (values != null) values.forEach(upgradeCommand::set);

      // This call will actually start the installation of the chart onto the cluster.
      Release result = upgradeCommand.debug().call();

      LOGGER.info("Released   - " + chartName +" : " + "revision :" + result.getRevision());
      return true;
    } catch (Exception e) {
      LOGGER.error(e.getMessage());
      LOGGER.error("Error occurred upgrading {}: ", chartName, e);
      return false;
    }
  }

getting following error,

2024-06-24 14:35:07,461 INFO  [com.esr.rea.con.ccm.ada.out.hel.HelmInstaller] (main) Installing opensearch
2024-06-24 14:35:31,824 ERROR [com.esr.rea.con.ccm.ada.out.hel.HelmInstaller] (main) template: opensearch/templates/statefulset.yaml:402:21: executing "opensearch/templates/statefulset.yaml" at <has "master" .Values.roles>: error calling has: Cannot find has on type string
2024-06-24 14:35:31,825 ERROR [com.esr.rea.con.ccm.ada.out.hel.HelmInstaller] (main) opensearch: java.lang.IllegalStateException: template: opensearch/templates/statefulset.yaml:402:21: executing "opensearch/templates/statefulset.yaml" at <has "master" .Values.roles>: error calling has: Cannot find has on type string
    at com.marcnuri.helm.HelmCommand.run(HelmCommand.java:33)
    at com.marcnuri.helm.UpgradeCommand.call(UpgradeCommand.java:56)
    at com.esri.realtime.controlplane.ccm.adapter.out.helm.HelmInstaller.installDataStoreHelmCharts(HelmInstaller.java:41)
    at com.esri.realtime.controlplane.ccm.adapter.in.HelmInstallerTest.testHelmInstall(HelmInstallerTest.java:33)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)

Is this possible using helm-java? Appreciate any help.

manusa commented 4 months ago

The problem is the way the value gets converted to a string. The mechanism in the UpgradeCommand is quite simple: https://github.com/manusa/helm-java/blob/cb98ddf91e0cbf32150e1f06c4aed7fcf6956330/helm-java/src/main/java/com/marcnuri/helm/UpgradeCommand.java#L308-L311

What you need to do is pre-convert the value to a valid String yourself.

How would you do this if you were passing the value through the command line using a --set flag? Whatever you'd do to do that using the CLI client is what you need to do here.

vsingh58 commented 3 months ago

Closing this issue. Thanks