{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "How to create a ConfigMap and use its data in Pods\n",
    "===========================\n",
    "\n",
    "[ConfigMaps](https://kubernetes.io/docs/tasks/configure-pod-container/configmap/) allow you to decouple configuration artifacts from image content to keep containerized applications portable. In this notebook we would learn how to create a ConfigMap and also how to use its data in Pods as seen in https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from kubernetes import client, config\n",
    "from kubernetes.client.rest import ApiException"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load config from default location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "config.load_kube_config()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create API endpoint instance and API resource instances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "api_instance = client.CoreV1Api()\n",
    "cmap = client.V1ConfigMap()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create key value pair data for the ConfigMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "cmap.metadata = client.V1ObjectMeta(name=\"special-config\")\n",
    "cmap.data = {}\n",
    "cmap.data[\"special.how\"] = \"very\"\n",
    "cmap.data[\"special.type\"] = \"charm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create ConfigMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "api_instance.create_namespaced_config_map(namespace=\"default\", body=cmap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create API endpoint instance and API resource instances for test Pod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "pod = client.V1Pod()\n",
    "spec = client.V1PodSpec()\n",
    "pod.metadata = client.V1ObjectMeta(name=\"dapi-test-pod\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialize test Pod container"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "container = client.V1Container()\n",
    "container.name = \"test-container\"\n",
    "container.image = \"gcr.io/google_containers/busybox\"\n",
    "container.command = [\"/bin/sh\", \"-c\", \"env\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define Pod environment variables with data from ConfigMaps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "container.env = [client.V1EnvVar(name=\"SPECIAL_LEVEL_KEY\"), client.V1EnvVar(name=\"SPECIAL_TYPE_KEY\")]\n",
    "container.env[0].value_from = client.V1EnvVarSource()\n",
    "container.env[0].value_from.config_map_key_ref = client.V1ConfigMapKeySelector(name=\"special-config\", key=\"special.how\")\n",
    "\n",
    "container.env[1].value_from = client.V1EnvVarSource()\n",
    "container.env[1].value_from.config_map_key_ref = client.V1ConfigMapKeySelector(name=\"special-config\", key=\"special.type\")\n",
    "\n",
    "spec.restart_policy = \"Never\"\n",
    "spec.containers = [container]\n",
    "pod.spec = spec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create Pod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "api_instance.create_namespaced_pod(namespace=\"default\",body=pod)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### View ConfigMap data from Pod log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "log = \"\"\n",
    "try: \n",
    "    log = api_instance.read_namespaced_pod_log(name=\"dapi-test-pod\", namespace=\"default\")\n",
    "except ApiException as e:\n",
    "    if str(e).find(\"ContainerCreating\") != -1:\n",
    "        print(\"Creating Pod container.\\nRe-run current cell.\")\n",
    "    else:\n",
    "        print(\"Exception when calling CoreV1Api->read_namespaced_pod_log: %s\\n\" % e)\n",
    "\n",
    "for line in log.split(\"\\n\"):\n",
    "    if line.startswith(\"SPECIAL\"):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete ConfigMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "api_instance.delete_namespaced_config_map(name=\"special-config\", namespace=\"default\", body=cmap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete Pod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "api_instance.delete_namespaced_pod(name=\"dapi-test-pod\", namespace=\"default\", body=client.V1DeleteOptions())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
