Run a helm subchart based on some environment property

6/14/2021

I am new to using kubernetes and helm, though I have developed a few helms already. In one of the requirement, I am creating a helm of helms. Basically, the installable helm has some dependent helms. And, to install this, I have the install script something of the below form:

${HELM_BIN} install final_helm helms/final-helm \
    --set old-service.fullnameOverride=old-service \
    --set old-service.image.registry=${DOCKER_REGISTRY} \
    --set old-service.ingress.enabled=true \
    --set old-service.ingress.hosts[0].host=${LOAD_BALANCER} \
    --set old-service.ingress.hosts[0].paths[0]="/old-service-api-${NAMESPACE}(/|$)(.*)" \
    --set old-service.ingress.tls[0].hosts[0]=${LOAD_BALANCER} \
    --set old-service.ingress.tls[0].secretName=ssl-certificate \
    --set old-service.jvm.options="-Xmx${OLD_HEAPSIZEMB}M -Xms${OLD_HEAPSIZEMB}M" \
    --set old-service.metrics.serviceMonitor.enabled=true \
    --set old-service.metrics.serviceMonitor.namespace=${NAMESPACE} \
    --set old-service.replicaCount=${OLD_REPLICAS} \
    --set old-service.resources.requests.cpu=100m \
    --set old-service.resources.requests.memory=${OLD_HEAPSIZEMB}Mi \
	--set new-service.fullnameOverride=new-service \
    --set new-service.image.registry=${DOCKER_REGISTRY} \
    --set new-service.ingress.enabled=true \
    --set new-service.ingress.hosts[0].host=${LOAD_BALANCER} \
    --set new-service.ingress.hosts[0].paths[0]="/new-service-api-${NAMESPACE}(/|$)(.*)" \
    --set new-service.ingress.tls[0].hosts[0]=${LOAD_BALANCER} \
    --set new-service.ingress.tls[0].secretName=ssl-certificate \
    --set new-service.jvm.options="-Xmx${NEW_HEAPSIZEMB}M -Xms${NEW_HEAPSIZEMB}M" \
    --set new-service.metrics.serviceMonitor.enabled=true \
    --set new-service.metrics.serviceMonitor.namespace=${NAMESPACE} \
    --set new-service.replicaCount=${OLD_REPLICAS} \
    --set new-service.resources.requests.cpu=100m \
    --set new-service.resources.requests.memory=${NEW_HEAPSIZEMB}Mi \
    --namespace ${NAMESPACE} \
    --version ${FINAL_HELM_VERSION}

I had a version of a Spring Boot service(old-service) which has been optimized by removing some adapter design pattern to new-service. However, considering the stage the project is in, integration tests could not be performed with the new-service supposed to replace the old-service.

So, I need to spin up one of the service only out of new-service and old-service based on some environment property set via env.sh script in which I have all the environment variables present.

env.sh

export USE_NEW_SERVICE=true

Chart.yaml for helms/final-helm

dependencies:
- name: old-service
  repository: "file://../old-service"
- name: new-service
  repository: "file://../new-service"

I do not know how I can use the environment property USE_NEW_SERVICE so that if it is true, I spin up new-service leaving behind old-service and if it is false, I spin up old-service leaving behind new-service. Simply, using if else in install script will not help as well as final-helm is dependent on new-service and old-service both and they will come up with default values specified if not from values obtained from install script.

Any help is appreciated. Thanks.

-- KnockingHeads
bash
cloud
java
kubernetes
kubernetes-helm

1 Answer

6/16/2021

There is another link at SOF here:

https://stackoverflow.com/questions/54032974/helm-conditionally-install-subchart

However, it uses condition with a subchart. The value of condition for any subchart can be set by any shell script to be true or false. Then, the subchart wont be included as a subchart of the main chart.

But, my requirement was about spinning up the new_service or old_service based on some condition, but to have it as a subchart of the main chart.

So, I simply set the new_service.replicaCount or old_service.replicaCount to 1 based on which condition is true. This way, both my charts are included as subcharts but only one would spin up based on the condition I set in my shell script.

-- KnockingHeads
Source: StackOverflow