From cd2847c1b970e50eaf00dfe16fc8cf12eab339b9 Mon Sep 17 00:00:00 2001 From: ant31 <2t.antoine@gmail.com> Date: Mon, 2 May 2016 17:07:03 +0200 Subject: [PATCH 1/4] Add kpm role --- library/kpm.py | 104 +++++++++++++++++++++++ roles/kubernetes-apps/defaults/main.yaml | 4 + roles/kubernetes-apps/tasks/main.yaml | 9 ++ 3 files changed, 117 insertions(+) create mode 100644 library/kpm.py create mode 100644 roles/kubernetes-apps/defaults/main.yaml create mode 100644 roles/kubernetes-apps/tasks/main.yaml diff --git a/library/kpm.py b/library/kpm.py new file mode 100644 index 000000000..69e42d612 --- /dev/null +++ b/library/kpm.py @@ -0,0 +1,104 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import kpm.deploy +from ansible.module_utils.basic import * + +DOCUMENTATION = """ +--- +module: kpm +short_description: Application deployment on kubernetes with kpm registry +description: + - Create, remove, and update resources within a Kubernetes Cluster +version_added: "2.0" +options: + name: + required: true + default: null + description: + - The name of the kpm package + namespace: + required: false + default: 'default' + description: + - The namespace to deploy package. It will be created if doesn't exist + force: + required: false + default: false + description: + - A flag to indicate to force delete, replace. + registry: + required: false + default: 'https://api.kpm.sh' + description: + - The registry url to fetch packages + version: + required: false + default: 'None' + description: + - The package version + state: + required: false + choices: ['present', 'absent'] + default: present + description: + - present handles checking existence or creating resources, + absent handles deleting resource(s). +requirements: + - kubectl + - kpm +author: "Antoine Legrand (ant31_2t@msn.com)" +""" + +EXAMPLES = """ +- name: check presence or install ghost + kpm: name=ghost/ghost state=present + +- name: check absence or remove rocketchat + kpm: name=ant31/rocketchat state=absent +""" + +RETURN = """ +""" + + +def check_changed(result, state='present'): + no_change = ["ok", 'protected', 'absent'] + for r in result: + if r['status'] not in no_change: + return True + return False + + +def main(): + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + version = dict(default=None, required=False), + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + force = dict(required=False, default=False, type='bool'), + registry = dict(required=False, default="https://api.kpm.sh"), + namespace=dict(default='default', required=False))) + + params = {"version": module.params.get("version"), + "namespace": module.params.get('namespace'), + "endpoint": module.params.get('registry'), + "dry": module.check_mode, + "proxy": None, + "fmt": "json"} + state = module.params.get("state") + try: + if state == 'present': + r = kpm.deploy.deploy(module.params.get('name'), **params) + elif state == 'absent': + r = kpm.deploy.delete(module.params.get('name'), **params) + except Exception as e: + module.fail_json(msg=e.message) + res = {} + res['kpm'] = r + res['changed'] = check_changed(r, state) + module.exit_json(**res) + +if __name__ == '__main__': + main() diff --git a/roles/kubernetes-apps/defaults/main.yaml b/roles/kubernetes-apps/defaults/main.yaml new file mode 100644 index 000000000..d95001316 --- /dev/null +++ b/roles/kubernetes-apps/defaults/main.yaml @@ -0,0 +1,4 @@ +--- +kpm_registry: "https://api.kpm.sh" +kpm_namespace: "default" +kpm_packages: [] \ No newline at end of file diff --git a/roles/kubernetes-apps/tasks/main.yaml b/roles/kubernetes-apps/tasks/main.yaml new file mode 100644 index 000000000..36b010bf9 --- /dev/null +++ b/roles/kubernetes-apps/tasks/main.yaml @@ -0,0 +1,9 @@ +- name: manage kubernetes applications + kpm: + namespace: "{{item.namespace | default(kpm_namespace | default('default'))}}" + registry: "{{item.registry | default(kpm_registry | default('https://api.kpm.sh'))}}" + state: "{{item.state | default(omit)}}" + version: "{{item.version | default(omit)}}" + name: "{{item.name}}" + with_items: "{{kpm_packages}}" + register: kpmresults From c43f9bc705170a1a0893418e40571d525ea15b40 Mon Sep 17 00:00:00 2001 From: ant31 <2t.antoine@gmail.com> Date: Wed, 4 May 2016 17:46:29 +0200 Subject: [PATCH 2/4] Add variables to kpm module --- library/kpm.py | 7 +++++++ roles/kubernetes-apps/tasks/main.yaml | 1 + 2 files changed, 8 insertions(+) diff --git a/library/kpm.py b/library/kpm.py index 69e42d612..aa44d4cc8 100644 --- a/library/kpm.py +++ b/library/kpm.py @@ -37,6 +37,11 @@ options: default: 'None' description: - The package version + variables: + required: false + default: 'None' + description: + - Set package variables state: required: false choices: ['present', 'absent'] @@ -78,11 +83,13 @@ def main(): state = dict(default='present', choices=['present', 'absent']), name = dict(required=True), force = dict(required=False, default=False, type='bool'), + variables = dict(required=False, default=None, type='dict'), registry = dict(required=False, default="https://api.kpm.sh"), namespace=dict(default='default', required=False))) params = {"version": module.params.get("version"), "namespace": module.params.get('namespace'), + "variables": module.params.get('variables'), "endpoint": module.params.get('registry'), "dry": module.check_mode, "proxy": None, diff --git a/roles/kubernetes-apps/tasks/main.yaml b/roles/kubernetes-apps/tasks/main.yaml index 36b010bf9..c74761dfc 100644 --- a/roles/kubernetes-apps/tasks/main.yaml +++ b/roles/kubernetes-apps/tasks/main.yaml @@ -4,6 +4,7 @@ registry: "{{item.registry | default(kpm_registry | default('https://api.kpm.sh'))}}" state: "{{item.state | default(omit)}}" version: "{{item.version | default(omit)}}" + variables: "{{item.variables | default(omit)}}" name: "{{item.name}}" with_items: "{{kpm_packages}}" register: kpmresults From eb6025a18475d2d543f601dab0daf0b353023d70 Mon Sep 17 00:00:00 2001 From: ant31 <2t.antoine@gmail.com> Date: Wed, 4 May 2016 18:00:46 +0200 Subject: [PATCH 3/4] Add kubedns as default package to install --- cluster.yml | 4 ++++ inventory/group_vars/all.yml | 8 ++++++++ roles/kubernetes-apps/tasks/main.yaml | 3 +++ 3 files changed, 15 insertions(+) diff --git a/cluster.yml b/cluster.yml index 4ccef1963..0c75b21b9 100644 --- a/cluster.yml +++ b/cluster.yml @@ -14,3 +14,7 @@ - hosts: k8s-cluster roles: - { role: dnsmasq, tags: dnsmasq } + +- hosts: kube-master[0] + roles: + - {role: kubernetes-apps, tags: apps} diff --git a/inventory/group_vars/all.yml b/inventory/group_vars/all.yml index b73fb66b2..e82c83147 100644 --- a/inventory/group_vars/all.yml +++ b/inventory/group_vars/all.yml @@ -134,3 +134,11 @@ dns_server: "{{ kube_service_addresses|ipaddr('net')|ipaddr(2)|ipaddr('address') ## An obvious use case is allowing insecure-registry access ## to self hosted registries like so: docker_options: "--insecure-registry={{ kube_service_addresses }}" + +# default packages to install within the cluster +kpm_packages: + - name: kube-system/kubedns + namespace: kube-system + variables: + cluster_ip: "{{skydns_server}}" +# - name: kube-system/grafana diff --git a/roles/kubernetes-apps/tasks/main.yaml b/roles/kubernetes-apps/tasks/main.yaml index c74761dfc..315c880ef 100644 --- a/roles/kubernetes-apps/tasks/main.yaml +++ b/roles/kubernetes-apps/tasks/main.yaml @@ -1,3 +1,6 @@ +- name: install kpm + pip: name=kpm state=latest + - name: manage kubernetes applications kpm: namespace: "{{item.namespace | default(kpm_namespace | default('default'))}}" From 922c6897d134bb3478607c15f374100347c34355 Mon Sep 17 00:00:00 2001 From: Smana Date: Sun, 12 Jun 2016 18:19:07 +0200 Subject: [PATCH 4/4] Install python-pip on first master --- roles/kubernetes-apps/tasks/main.yaml | 14 +++++++++++++- roles/kubernetes/preinstall/defaults/main.yml | 1 - roles/kubernetes/preinstall/tasks/main.yml | 4 ---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/roles/kubernetes-apps/tasks/main.yaml b/roles/kubernetes-apps/tasks/main.yaml index 315c880ef..c30a73d24 100644 --- a/roles/kubernetes-apps/tasks/main.yaml +++ b/roles/kubernetes-apps/tasks/main.yaml @@ -1,5 +1,15 @@ +- name: Install pip + action: + module: "{{ ansible_pkg_mgr }}" + name: "python-pip" + state: latest + when: ansible_os_family != "CoreOS" and kpm_packages | length > 0 + - name: install kpm - pip: name=kpm state=latest + pip: + name: "kpm" + state: "latest" + when: kpm_packages | length > 0 - name: manage kubernetes applications kpm: @@ -11,3 +21,5 @@ name: "{{item.name}}" with_items: "{{kpm_packages}}" register: kpmresults + environment: + PATH: "{{ ansible_env.PATH }}:{{ bin_dir }}" diff --git a/roles/kubernetes/preinstall/defaults/main.yml b/roles/kubernetes/preinstall/defaults/main.yml index 376663519..49f9b7c0e 100644 --- a/roles/kubernetes/preinstall/defaults/main.yml +++ b/roles/kubernetes/preinstall/defaults/main.yml @@ -2,7 +2,6 @@ run_gitinfos: false common_required_pkgs: - - python-pip - python-httplib2 - openssl - curl diff --git a/roles/kubernetes/preinstall/tasks/main.yml b/roles/kubernetes/preinstall/tasks/main.yml index 08998e695..95cd134ce 100644 --- a/roles/kubernetes/preinstall/tasks/main.yml +++ b/roles/kubernetes/preinstall/tasks/main.yml @@ -106,8 +106,4 @@ mode: 0640 when: cloud_provider is defined and cloud_provider == "openstack" -- name: Install kpm - pip: - name: "kpm" - - include: etchosts.yml