Code sample to deploy a Helm chart via API?

3/16/2018

I am looking for any code example that deploys Helm chart without via CLI call. The reason behind this is:

  1. My company got couple existing pipelines written with AWS CodePipeline / CodeBuild / CodeDeploy. They don't like to investigate more time on re-writing all pipelines.
  2. My company does not have any plan to maintain extra instance(s) just for deployment.
  3. AWS CodePipeline could trigger Lambda, and theoretically I could write some Python code to do the job if Helm provides Python client.

Currently I steal Lambda function from this:

https://github.com/aws-samples/aws-kube-codesuite

Whereas this does not provide same level features as Helm does. We have to provide our release-name system, template system, etc. In other words, it functions poorly if I have big change on the manifest, and does not handle the first time deployment (means deploying manifest to an empty K8S cluster) Also we use Github, although not really relevant.

For python client of Helm chart, the best I could find is pyhelm listed on pip. But it does not have sample code for calling deployment, and from some user group / forum feedback the installation process is painful. Somebody also points to azure/draft and another repo but I have no clue on how to come out a solid example that only use Python to deploy Helm chart.

Please let me know where I miss. Thanks.

-- Ming Hsieh
amazon-web-services
kubernetes
kubernetes-helm
python

2 Answers

3/31/2018

You can find my fork of pyhelm with examples and Python3 support.

git clone git@github.com:andriisoldatenko/pyhelm.git
cd pyhelm && python setup.py install

How to use Pyhelm

First you need repo_url and chart name to download chart

from pyhelm.repo import from_repo

chart_path = chart_versions = from_repo('https://kubernetes-charts.storage.googleapis.com/', 'mariadb')

print(chart_path)
"/tmp/pyhelm-kibwtj8d/mongodb"

Now you can see that chart folder of mongodb::

In [3]: ls -la /tmp/pyhelm-kibwtj8d/mongodb
total 40
drwxr-xr-x  7 andrii  wheel   224 Mar 21 17:26 ./
drwx------  3 andrii  wheel    96 Mar 21 17:26 ../
-rwxr-xr-x  1 andrii  wheel     5 Jan  1  1970 .helmignore*
-rwxr-xr-x  1 andrii  wheel   261 Jan  1  1970 Chart.yaml*
-rwxr-xr-x  1 andrii  wheel  4394 Jan  1  1970 README.md*
drwxr-xr-x  8 andrii  wheel   256 Mar 21 17:26 templates/

Next step to build ChartBuilder instance to manipulate with Tiller::

from pyhelm.chartbuilder import ChartBuilder

chart = ChartBuilder({'name': 'mongodb', 'source': {'type': 'directory', 'location': '/tmp/pyhelm-kibwtj8d/mongodb'}})

# than we can get chart meta data etc
In [9]: chart.get_metadata()
Out[9]:
name: "mongodb"
version: "0.4.0"
description: "Chart for MongoDB"

Install chart::

from pyhelm.chartbuilder import ChartBuilder
from pyhelm.tiller import Tiller

chart = ChartBuilder({'name': 'mongodb', 'source': {'type': 'directory', 'location': '/tmp/pyhelm-kibwtj8d/mongodb'}})
chart.install_release(chart.get_helm_chart(), dry_run=False, namespace='default')

Out[9]:
release {
  name: "fallacious-bronco"
  info {
    status {
      code: 6
    }
    first_deployed {
      seconds: 1521647335
      nanos: 746785000
    }
    last_deployed {
      seconds: 1521647335
      nanos: 746785000
    }
    Description: "Dry run complete"
  }
  chart {....
}
-- Andrii Soldatenko
Source: StackOverflow

3/18/2018

I suggest using the official python client for Kubernetes rather than Helm. It requires that you write deployments, services, persistent volumes, etc yourself, but it's going to be quicker than any other approach. Keep in mind that you'll have to work out how to do cluster authentication to make changes through the client but there are several examples in the repo. I don't know enough about AWS Lambda to offer anything on how/if it would work.

Helm is a wonderful product but oriented towards the command line rather than use of its API which requires GRPC. Of course, it's possible to create a Python library for Tiller (Helm's API server) using the Helm proto file and GRPC client for Python, but no one seems to built one that has found traction in the community.

-- citizenrich
Source: StackOverflow