add kubeconfig generator

Change-Id: I321f7eb185b2107670bd8c1b99b59880bcac1948
diff --git a/README.rst b/README.rst
index 7e236e6..c162851 100644
--- a/README.rst
+++ b/README.rst
@@ -5,8 +5,20 @@
 
 Kubernetes is an open-source system for automating deployment, scaling, and
 management of containerized applications. This formula deploys production
-ready Kubernetes and generate Kubernetes manifests as well. 
+ready Kubernetes and generate Kubernetes manifests as well.
 
+You can download `kubectl` configuration and connect to your cluster. However,
+keep in mind `kubernetes_control_address` needs to be accessible from your computer:
+
+.. code-block:: yaml
+
+  mkdir -p ~/.kube
+  [ -f ~/.kube/config ] && cp -v ~/.kube/config ~/.kube/config-backup
+  ssh cfg01 "sudo ssh ctl01 /etc/kubenetes/kubeconfig.sh" > ~/.kube/config
+  kubectl get no
+
+
+`cfg01` is Salt master node and `ctl01` is one of Kubernetes masters
 
 Sample Pillars
 ==============
diff --git a/kubernetes/files/kubeconfig.sh b/kubernetes/files/kubeconfig.sh
new file mode 100644
index 0000000..ff1eda1
--- /dev/null
+++ b/kubernetes/files/kubeconfig.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# server url
+server="$(cat /etc/kubernetes/kubelet.kubeconfig  | grep server | awk '{ print $2 }')"
+
+# certificates
+cert="$(cat /etc/kubernetes/ssl/kubelet-client.crt | base64 | sed 's/^/      /g')"
+key="$(cat /etc/kubernetes/ssl/kubelet-client.key | base64 | sed 's/^/      /g')"
+ca="$(cat /etc/kubernetes/ssl/ca-kubernetes.crt | base64 | sed 's/^/      /g')"
+
+echo "apiVersion: v1
+clusters:
+- cluster:
+    certificate-authority-data: |
+${ca}
+    server: ${server}
+  name: mycluster
+- cluster:
+    server: http://localhost:8080
+  name: local
+contexts:
+- context:
+    cluster: mycluster
+    user: "cluster-admin"
+  name: mycluster
+- context:
+    cluster: local
+    namespace: default
+    user: ""
+  name: local
+current-context: mycluster
+users:
+- name: cluster-admin
+  user:
+    client-certificate-data: |
+${cert}
+    client-key-data: |
+${key}
+kind: Config
+preferences: {}"
diff --git a/kubernetes/master/setup.sls b/kubernetes/master/setup.sls
index 60e9f73..5d22277 100644
--- a/kubernetes/master/setup.sls
+++ b/kubernetes/master/setup.sls
@@ -1,6 +1,21 @@
 {%- from "kubernetes/map.jinja" import master with context %}
 {%- if master.enabled %}
 
+/etc/kubernetes/kubeconfig.sh:
+  file.managed:
+    - source: salt://kubernetes/files/kubeconfig.sh
+    - template: jinja
+    - user: root
+    - group: root
+    - mode: 755
+    - makedirs: True
+
+generate_admin_kube_config:
+  cmd.run:
+    - name: /etc/kubernetes/kubeconfig.sh > /etc/kubernetes/admin-kube-config
+    - watch:
+      - file: /etc/kubernetes/kubeconfig.sh
+
 {%- for addon_name, addon in master.addons.iteritems() %}
 {%- if addon.enabled %}