From 190f1480d25a240968197cce1b118ae430d318e7 Mon Sep 17 00:00:00 2001 From: transcaffeine Date: Sun, 13 Oct 2024 17:01:04 +0200 Subject: [PATCH] feat(cadvisor): add state=absent support to role, move to fully qualified module names, cleanup --- README.md | 3 ++ roles/cadvisor/defaults/main.yml | 52 -------------------- roles/cadvisor/defaults/main/container.yml | 56 ++++++++++++++++++++++ roles/cadvisor/defaults/main/main.yml | 18 +++++++ roles/cadvisor/meta/main.yml | 11 +++++ roles/cadvisor/tasks/deploy-docker.yml | 25 ++++++++++ roles/cadvisor/tasks/main.yml | 38 ++++++--------- roles/cadvisor/vars/main.yml | 6 +++ 8 files changed, 134 insertions(+), 75 deletions(-) delete mode 100644 roles/cadvisor/defaults/main.yml create mode 100644 roles/cadvisor/defaults/main/container.yml create mode 100644 roles/cadvisor/defaults/main/main.yml create mode 100644 roles/cadvisor/meta/main.yml create mode 100644 roles/cadvisor/tasks/deploy-docker.yml create mode 100644 roles/cadvisor/vars/main.yml diff --git a/README.md b/README.md index 7cf1566..d29c0bf 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ metrics or alerting. alertmanager for receiving alerts from prometheus and routing them to the correct configured receivers. +- [`cadvisor`](roles/cadvisor/README.md): Run and configure cAdvisor, googles' + container performance and resource usage collection and aggregation daemon. + - [`grafana`](roles/grafana/README.md): a popular visualization and dashboard creation tool able to use various datasources. diff --git a/roles/cadvisor/defaults/main.yml b/roles/cadvisor/defaults/main.yml deleted file mode 100644 index 7cbf09a..0000000 --- a/roles/cadvisor/defaults/main.yml +++ /dev/null @@ -1,52 +0,0 @@ ---- -cadvisor_version: 0.49.1 - -cadvisor_container_name: cadvisor -cadvisor_container_image_name: gcr.io/cadvisor/cadvisor -cadvisor_container_image_tag: ~ -cadvisor_container_image_ref: >- - {{ cadvisor_container_image_name }}:{{ cadvisor_container_image_tag | default('v' + cadvisor_version, True) }} -cadvisor_container_volumes: >- - {{ cadvisor_container_base_volumes + cadvisor_container_extra_volumes | default([], True) }} -cadvisor_container_extra_volumes: ~ -cadvisor_container_env: ~ -cadvisor_container_labels: "{{ cadvisor_container_base_labels | combine(cadvisor_container_extra_labels) }}" -cadvisor_container_extra_labels: {} -cadvisor_container_ports: ~ -cadvisor_container_networks: ~ -cadvisor_container_etc_hosts: ~ -cadvisor_container_devices: [ "/dev/kmsg:/dev/kmsg:rwm" ] -cadvisor_container_privileged: yes -cadvisor_container_pid_mode: "host" -cadvisor_container_userns_mode: "host" -cadvisor_container_capabilities: ~ -cadvisor_container_restart_policy: unless-stopped -cadvisor_container_command: >-2 - {{ ["--docker_only=false"] - + (["--disable_metrics=" + cadvisor_disabled_metrics | join( ',' )] - if cadvisor_disabled_metrics | default(false, True) else []) - + (["--enable_metrics=" + cadvisor_force_enable_metrics | join( ',' )] - if cadvisor_force_enable_metrics | default(false, True) else []) - }} -cadvisor_container_base_labels: - version: "{{ cadvisor_version }}" -cadvisor_container_base_volumes: - - "/:/rootfs:ro" - - "/var/run:/var/run:ro" - - "/sys:/sys:ro" - - "/var/lib/docker/:/var/lib/docker:ro" - - "/dev/disk/:/dev/disk:ro" - -cadvisor_disabled_metrics: - - advtcp - - cpu_topology - - cpuset - - hugetlb - - memory_numa - - process - - referenced_memory - - resctrl - - sched - - tcp - - udp -cadvisor_force_enable_metrics: [] diff --git a/roles/cadvisor/defaults/main/container.yml b/roles/cadvisor/defaults/main/container.yml new file mode 100644 index 0000000..1151972 --- /dev/null +++ b/roles/cadvisor/defaults/main/container.yml @@ -0,0 +1,56 @@ +--- +cadvisor_container_image_registry: gcr.io +cadvisor_container_image_namespace: cadvisor +cadvisor_container_image_name: cadvisor +cadvisor_container_image: >-2 + {{ + [ + cadvisor_container_image_registry, + cadvisor_container_image_namespace, + cadvisor_container_image_name, + ] | flatten | join('/') + }} +cadvisor_container_image_tag: ~ +cadvisor_container_image_ref: >-2 + {{ cadvisor_container_image }}:{{ cadvisor_container_image_tag | default('v' + cadvisor_version, true) }} +cadvisor_container_image_source: pull +cadvisor_container_image_force_source: >-2 + {{ cadvisor_container_image_tag | default(false, true) | bool }} + +cadvisor_container_state: >-2 + {{ (cadvisor_state == 'present') | ternary('started', 'absent') }} + +cadvisor_container_name: cadvisor +cadvisor_container_volumes: >-2 + {{ cadvisor_container_base_volumes + cadvisor_container_extra_volumes | default([], true) }} +cadvisor_container_extra_volumes: ~ +cadvisor_container_env: ~ +cadvisor_container_labels: >-2 + {{ cadvisor_container_base_labels | combine(cadvisor_container_extra_labels) }} +cadvisor_container_extra_labels: {} +cadvisor_container_ports: ~ +cadvisor_container_networks: ~ +cadvisor_container_etc_hosts: ~ +cadvisor_container_devices: + - "/dev/kmsg:/dev/kmsg:rwm" +cadvisor_container_privileged: true +cadvisor_container_pid_mode: "host" +cadvisor_container_userns_mode: "host" +cadvisor_container_capabilities: ~ +cadvisor_container_restart_policy: "unless-stopped" +cadvisor_container_command: >-2 + {{ ["--docker_only=false"] + + (["--disable_metrics=" + cadvisor_disabled_metrics | join( ',' )] + if cadvisor_disabled_metrics | default(false, true) else []) + + (["--enable_metrics=" + cadvisor_force_enable_metrics | join( ',' )] + if cadvisor_force_enable_metrics | default(false, true) else []) + }} +cadvisor_container_base_labels: + version: "{{ cadvisor_version }}" +cadvisor_container_base_volumes: + - "/:/rootfs:ro" + - "/var/run:/var/run:ro" + - "/sys:/sys:ro" + - "/var/lib/docker/:/var/lib/docker:ro" + - "/dev/disk/:/dev/disk:ro" + diff --git a/roles/cadvisor/defaults/main/main.yml b/roles/cadvisor/defaults/main/main.yml new file mode 100644 index 0000000..6a87fd0 --- /dev/null +++ b/roles/cadvisor/defaults/main/main.yml @@ -0,0 +1,18 @@ +--- +cadvisor_version: 0.49.1 +cadvisor_state: present +cadvisor_deployment_method: docker + +cadvisor_disabled_metrics: + - advtcp + - cpu_topology + - cpuset + - hugetlb + - memory_numa + - process + - referenced_memory + - resctrl + - sched + - tcp + - udp +cadvisor_force_enable_metrics: [] diff --git a/roles/cadvisor/meta/main.yml b/roles/cadvisor/meta/main.yml new file mode 100644 index 0000000..3aebb3c --- /dev/null +++ b/roles/cadvisor/meta/main.yml @@ -0,0 +1,11 @@ +--- +allow_duplicates: true +dependencies: [] +galaxy_info: + role_name: cadvisor + description: Deploy cadvisor (Container Advisor), a container performance and resource usage aggregation daemon + galaxy_tags: + - cadvisor + - observability + - container + - docker diff --git a/roles/cadvisor/tasks/deploy-docker.yml b/roles/cadvisor/tasks/deploy-docker.yml new file mode 100644 index 0000000..920638a --- /dev/null +++ b/roles/cadvisor/tasks/deploy-docker.yml @@ -0,0 +1,25 @@ +--- +- name: Ensure cadvisor container image '{{ cadvisor_container_image_ref }}' is {{ cadvisor_state }} + community.docker.docker_image: + name: "{{ cadvisor_container_image_ref }}" + state: "{{ cadvisor_state }}" + source: "{{ cadvisor_container_image_source }}" + force_source: "{{ cadvisor_container_image_force_source }}" + +- name: Ensure cadvisor container '{{ cadvisor_container_name }}' is {{ cadvisor_container_state }} + community.docker.docker_container: + name: "{{ cadvisor_container_name }}" + image: "{{ cadvisor_container_image_ref }}" + env: "{{ cadvisor_container_env | default(omit, true) }}" + ports: "{{ cadvisor_container_ports | default(omit, true) }}" + labels: "{{ cadvisor_container_labels }}" + devices: "{{ cadvisor_container_devices }}" + volumes: "{{ cadvisor_container_volumes }}" + networks: "{{ cadvisor_container_networks | default(omit, true) }}" + etc_hosts: "{{ cadvisor_container_etc_hosts | default(omit, true) }}" + privileged: "{{ cadvisor_container_privileged }}" + command: "{{ cadvisor_container_command }}" + pid_mode: "{{ cadvisor_container_pid_mode | default(omit, true) }}" + userns_mode: "{{ cadvisor_container_userns_mode | default(omit, true) }}" + restart_policy: "{{ cadvisor_container_restart_policy }}" + state: "{{ cadvisor_container_state }}" diff --git a/roles/cadvisor/tasks/main.yml b/roles/cadvisor/tasks/main.yml index 2233c0d..5420864 100644 --- a/roles/cadvisor/tasks/main.yml +++ b/roles/cadvisor/tasks/main.yml @@ -1,26 +1,18 @@ --- +- name: Ensure state is valid + ansible.builtin.fail: + msg: >-2 + Unknown state '{{ cadvisor_state }}'! Supported + states are: {{ cadvisor_states | join(', ') }}. + when: cadvisor_state not in cadvisor_states -- name: Ensure container image is present - docker_image: - name: "{{ cadvisor_container_image_ref }}" - state: present - source: pull - force_source: "{{ cadvisor_container_image_tag|default(False, True) | bool }}" +- name: Ensure deployment method is valid + ansible.builtin.fail: + msg: >-2 + Unknown deployment method '{{ cadvisor_deployment_method }}'! Supported + deployment methods are: {{ cadvisor_deployment_methods | join(', ') }}. + when: cadvisor_deployment_method not in cadvisor_deployment_methods -- name: Ensure cadvisor container is running - docker_container: - name: "{{ cadvisor_container_name }}" - image: "{{ cadvisor_container_image_ref }}" - env: "{{ cadvisor_container_env | default(omit, True) }}" - ports: "{{ cadvisor_container_ports | default(omit, True) }}" - labels: "{{ cadvisor_container_labels }}" - devices: "{{ cadvisor_container_devices }}" - volumes: "{{ cadvisor_container_volumes }}" - networks: "{{ cadvisor_container_networks | default(omit, True) }}" - etc_hosts: "{{ cadvisor_container_etc_hosts | default(omit, True) }}" - privileged: "{{ cadvisor_container_privileged }}" - command: "{{ cadvisor_container_command }}" - pid_mode: "{{ cadvisor_container_pid_mode | default(omit, True) }}" - userns_mode: "{{ cadvisor_container_userns_mode | default(omit, True) }}" - restart_policy: "{{ cadvisor_container_restart_policy }}" - state: started +- name: Deploy using {{ cadvisor_deployment_method }} + ansible.builtin.include_tasks: + file: "deploy-{{ cadvisor_deployment_method }}.yml" diff --git a/roles/cadvisor/vars/main.yml b/roles/cadvisor/vars/main.yml new file mode 100644 index 0000000..7bb259b --- /dev/null +++ b/roles/cadvisor/vars/main.yml @@ -0,0 +1,6 @@ +--- +cadvisor_states: + - present + - absent +cadvisor_deployment_methods: + - docker