Add tiller namespace/host specification

Change-Id: I5f996566ea081bb1e7e0560be6dfafbeef38830b
diff --git a/_modules/helm.py b/_modules/helm.py
index 76f2bb1..80632d1 100644
--- a/_modules/helm.py
+++ b/_modules/helm.py
@@ -15,53 +15,71 @@
     return msg
 
 
-def _helm_cmd(*args):
+def _helm_cmd(*args, **tiller_kwargs):
+    if tiller_kwargs['tiller_host']:
+        tiller_args = ('--host', tiller_kwargs['tiller_host'])
+    else:
+        tiller_args = ('--tiller-namespace', tiller_kwargs['tiller_namespace'])
     return {
-        'cmd': ('helm',) + args,
+        'cmd': ('helm',) + tiller_args + args,
         'env': {'HELM_HOME': HELM_HOME},
     }
 
 
-def release_exists(name, namespace='default'):
-    cmd = _helm_cmd('list', '--short', '--all', '--namespace', namespace, name)
+def release_exists(name, namespace='default',
+                   tiller_namespace='kube-system', tiller_host=None):
+    cmd = _helm_cmd('list', '--short', '--all', '--namespace', namespace, name,
+                    tiller_namespace=tiller_namespace, tiller_host=tiller_host)
     return __salt__['cmd.run_stdout'](**cmd) == name
 
 
 def release_create(name, chart_name, namespace='default',
-                   version=None, values=None):
+                   version=None, values=None,
+                   tiller_namespace='kube-system', tiller_host=None):
+    tiller_args = {
+        'tiller_namespace': tiller_namespace,
+        'tiller_host': tiller_host,
+    }
     args = []
     if version is not None:
         args += ['--version', version]
     if values is not None:
         args += ['--values', '/dev/stdin']
     cmd = _helm_cmd('install', '--namespace', namespace,
-                    '--name', name, chart_name, *args)
+                    '--name', name, chart_name, *args, **tiller_args)
     if values is not None:
         cmd['stdin'] = yaml.serialize(values, default_flow_style=False)
     LOG.debug('Creating release with args: %s', cmd)
     return ok_or_output(cmd, 'Failed to create release "{}"'.format(name))
 
 
-def release_delete(name):
-    cmd = _helm_cmd('delete', '--purge', name)
+def release_delete(name, tiller_namespace='kube-system', tiller_host=None):
+    cmd = _helm_cmd('delete', '--purge', name,
+                    tiller_namespace=tiller_namespace, tiller_host=tiller_host)
     return ok_or_output(cmd, 'Failed to delete release "{}"'.format(name))
 
 
 def release_upgrade(name, chart_name, namespace='default',
-                    version=None, values=None):
+                    version=None, values=None,
+                    tiller_namespace='kube-system', tiller_host=None):
+    tiller_args = {
+        'tiller_namespace': tiller_namespace,
+        'tiller_host': tiller_host,
+    }
     args = []
     if version is not None:
         args += ['--version', version]
     if values is not None:
         args += ['--values', '/dev/stdin']
     cmd = _helm_cmd('upgrade', '--namespace', namespace,
-                    name, chart_name, *args)
+                    name, chart_name, *args, **tiller_args)
     if values is not None:
         cmd['stdin'] = yaml.serialize(values, default_flow_style=False)
     LOG.debug('Upgrading release with args: %s', cmd)
     return ok_or_output(cmd, 'Failed to upgrade release "{}"'.format(name))
 
 
-def get_values(name):
-    cmd = _helm_cmd('get', 'values', '--all', name)
+def get_values(name, tiller_namespace='kube-system', tiller_host=None):
+    cmd = _helm_cmd('get', 'values', '--all', name,
+                    tiller_namespace=tiller_namespace, tiller_host=tiller_host)
     return yaml.deserialize(__salt__['cmd.run_stdout'](**cmd))
diff --git a/_states/helm_release.py b/_states/helm_release.py
index f20c301..e60c6dc 100644
--- a/_states/helm_release.py
+++ b/_states/helm_release.py
@@ -1,5 +1,4 @@
 import difflib
-import logging
 
 from salt.serializers import yaml
 
@@ -13,11 +12,16 @@
     }
 
 
-def present(name, chart_name, namespace, version=None, values=None):
-    exists =  __salt__['helm.release_exists'](name, namespace)
+def present(name, chart_name, namespace, version=None, values=None,
+            tiller_namespace='kube-system', tiller_host=None):
+    tiller_args = {
+        'tiller_namespace': tiller_namespace,
+        'tiller_host': tiller_host,
+    }
+    exists = __salt__['helm.release_exists'](name, namespace, **tiller_args)
     if not exists:
         err = __salt__['helm.release_create'](
-            name, chart_name, namespace, version, values)
+            name, chart_name, namespace, version, values, **tiller_args)
         if err:
             return failure(name, err)
         return {
@@ -27,13 +31,13 @@
             'comment': 'Release "{}" was created'.format(name),
         }
 
-    old_values = __salt__['helm.get_values'](name)
+    old_values = __salt__['helm.get_values'](name, **tiller_args)
     err = __salt__['helm.release_upgrade'](
-        name, chart_name, namespace, version, values)
+        name, chart_name, namespace, version, values, **tiller_args)
     if err:
         return failure(name, err)
 
-    new_values = __salt__['helm.get_values'](name)
+    new_values = __salt__['helm.get_values'](name, **tiller_args)
     if new_values == old_values:
         return {
             'name': name,
@@ -54,8 +58,12 @@
     }
 
 
-def absent(name, namespace):
-    exists =  __salt__['helm.release_exists'](name, namespace)
+def absent(name, namespace, tiller_namespace='kube-system', tiller_host=None):
+    tiller_args = {
+        'tiller_namespace': tiller_namespace,
+        'tiller_host': tiller_host,
+    }
+    exists = __salt__['helm.release_exists'](name, namespace, **tiller_args)
     if not exists:
         return {
             'name': name,
@@ -63,7 +71,7 @@
             'result': True,
             'comment': 'Release "{}" doesn\'t exist'.format(name),
         }
-    err = __salt__['helm.release_delete'](name)
+    err = __salt__['helm.release_delete'](name, **tiller_args)
     if err:
         return failure(name, err)
     return {
diff --git a/helm/client.sls b/helm/client.sls
index f997020..6fc7ba7 100644
--- a/helm/client.sls
+++ b/helm/client.sls
@@ -4,6 +4,13 @@
 {%- set helm_tmp = "/tmp/helm-" + client.version %}
 {%- set helm_bin = "/usr/bin/helm-" + client.version %}
 {%- set helm_home = "/srv/helm/home" %}
+{%- if client.tiller.host %}
+{%- set helm_run = "helm --host '{}'".format(client.tiller.host) %}
+{%- set tiller_arg = "- tiller_host: \"{}\"".format(client.tiller.host) %}
+{%- else %}
+{%- set helm_run = "helm --tiller-namespace '{}'".format(client.tiller.namespace) %}
+{%- set tiller_arg = "- tiller_namespace: \"{}\"".format(client.tiller.namespace) %}
+{%- endif %}
 
 {{ helm_tmp }}:
   file.directory:
@@ -39,7 +46,7 @@
 
 prepare_client:
   cmd.run:
-    - name: helm init --client-only
+    - name: {{ helm_run }} init --client-only
     - env:
       - HELM_HOME: {{ helm_home }}
     - unless: test -d {{ helm_home }}
@@ -49,16 +56,16 @@
 {%- if client.tiller.install %}
 install_tiller:
   cmd.run:
-    - name: helm init --upgrade
+    - name: {{ helm_run }} init --upgrade
     - env:
       - HELM_HOME: {{ helm_home }}
-    - unless: "helm version --server --short | grep -E 'Server: v{{ client.version }}(\\+|$)'"
+    - unless: "{{ helm_run }} version --server --short | grep -E 'Server: v{{ client.version }}(\\+|$)'"
     - require:
       - cmd: prepare_client
 
 wait_for_tiller:
   cmd.run:
-    - name: while ! helm list; do sleep 3; done
+    - name: while ! {{ helm_run }} list; do sleep 3; done
     - env:
       - HELM_HOME: {{ helm_home }}
     - onchanges:
@@ -68,10 +75,10 @@
 {%- for repo_name, repo_url in client.repos.items() %}
 ensure_{{ repo_name }}_repo:
   cmd.run:
-    - name: helm repo add {{ repo_name }} {{ repo_url }}
+    - name: {{ helm_run }} repo add {{ repo_name }} {{ repo_url }}
     - env:
       - HELM_HOME: {{ helm_home }}
-    - unless: helm repo list | grep '^{{ repo_name }}[[:space:]]{{ repo_url|replace(".", "\.") }}'
+    - unless: {{ helm_run }} repo list | grep '^{{ repo_name }}[[:space:]]{{ repo_url|replace(".", "\.") }}'
     - require:
       - cmd: prepare_client
 {%- endfor %}
@@ -86,6 +93,7 @@
     - name: {{ release_name }}
     - chart_name: {{ release['chart'] }}
     - namespace: {{ namespace }}
+    {{ tiller_arg }}
     {%- if release.get('version') %}
     - version: {{ release['version'] }}
     {%- endif %}
@@ -104,6 +112,7 @@
   helm_release.absent:
     - name: {{ release_name }}
     - namespace: {{ namespace }}
+    {{ tiller_arg }}
     - require:
 {%- if client.tiller.install %}
       - cmd: wait_for_tiller
diff --git a/metadata/service/client.yml b/metadata/service/client.yml
index 6782c36..6349fc0 100644
--- a/metadata/service/client.yml
+++ b/metadata/service/client.yml
@@ -11,5 +11,7 @@
       download_hash: sha256=96f74ff04ec7eb38e5f53aba73132bfe4d6b81168f20574dad25a9bcaceec81b
       tiller:
         install: true
+        namespace: kube-system
+        host:
       repos: {}
       releases: {}