Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kubeblocks changes to fix issue #71 #72

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions kubeblocks.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#!/usr/bin/env ansible-playbook
---
# ~/.kube/config should be valid
- name: Manage KubeBlocks
- name: Manage kbcli
hosts: controller
become: true
tasks:
- name: Configure KubeBlocks
ansible.builtin.include_role:
name: clusterlust.kbcli

- name: Manage KubeBlocks
hosts: localhost
become: false
connection: local
vars:
# Semaphore does not pass $HOME so K8S_AUTH_KUBECONFIG needs to be set.
k8s_auth_kubeconfig: /home/semaphore/.kube/config
# To use KubeBlocks addons set this Boolean
#use_kb_addons: true
# To manage KubeBlocks addons set this Boolean
# use_kb_addons: true
tasks:
- name: Install KubeBlocks
ansible.builtin.include_role:
name: bbaassssiiee.kubeblocks

- name: Configure KubeBlocks
ansible.builtin.include_role:
name: clusterlust.kubeblocks
3 changes: 1 addition & 2 deletions prepare.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash
ansible-galaxy install -r roles/requirements.yml
ansible-galaxy collection install -r collections/requirements.yml
ansible-galaxy collection download -r collections/requirements.yml
5 changes: 5 additions & 0 deletions roles/clusterlust.kbcli/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# kbcli
kbcli_releases_url: 'https://api.github.com/repos/apecloud/kbcli/releases/latest'
kbcli_repo_url: 'https://github.com/apecloud/kbcli/releases/download'
use_kbcli: true
desired_state: present
7 changes: 7 additions & 0 deletions roles/clusterlust.kbcli/tasks/absent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

- name: Remove kbcli
when: use_kbcli | bool
ansible.builtin.package:
name: kbcli
state: absent
65 changes: 65 additions & 0 deletions roles/clusterlust.kbcli/tasks/kbcli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
- name: Find kbcli release
when: kbcli_download_url is not defined
become: false
block:

- name: Get kbcli Release info from Github
ansible.builtin.uri:
url: "{{ kbcli_releases_url }}"
return_content: true
body_format: json
register: _sem_rel
check_mode: false

- name: Set kbcli requirements
ansible.builtin.set_fact:
kbcli_version: "{{ _sem_rel['json']['tag_name'][1:] }}"
kbcli_pkg_ext: "{{ (ansible_facts['os_family'] == 'RedHat') | ternary('rpm', 'deb') }}"
kbcli_pkg_arch: "{{ (ansible_facts['architecture'] == 'x86_64') | ternary('amd64', 'arm64') }}"

- name: Set kbcli_download_url
ansible.builtin.set_fact:
kbcli_download_url: "{{ kbcli_repo_url }}/{{ _sem_rel.json.name }}/kbcli-linux-{{ kbcli_pkg_arch }}-v{{ kbcli_version }}.{{ kbcli_pkg_ext }}"

- name: Install kbcli package
when: kbcli_download_package | bool
block:

- name: Download kbcli
become: false
ansible.builtin.get_url:
url: "{{ kbcli_download_url }}"
dest: "/tmp/kbcli.{{ kbcli_pkg_ext }}"
mode: '0644'
register: downloaded_kbcli

# This task will be skipped in check mode
- name: Ensure kbcli installed
become: true
ansible.builtin.dnf:
name: "file:///tmp/kbcli.{{ kbcli_pkg_ext }}"
disable_gpg_check: true
state: present
when:
- not ansible_check_mode
- downloaded_kbcli is success
- ansible_facts['os_family'] == 'RedHat'

# This task will be skipped in check mode
- name: Ensure kbcli installed
become: true
ansible.builtin.apt:
deb: "file:///tmp/kbcli.{{ kbcli_pkg_ext }}"
allow_unauthenticated: true
state: present
when:
- not ansible_check_mode
- downloaded_kbcli is success
- ansible_facts['os_family'] == 'Debian'

always:
- name: Remove package from /tmp
ansible.builtin.file:
path: "/tmp/kbcli.{{ kbcli_pkg_ext }}"
state: absent
4 changes: 4 additions & 0 deletions roles/clusterlust.kbcli/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
# desired_state in ['absent', 'present']
- name: "Converge state - {{ desired_state }}"
ansible.builtin.include_tasks: "{{ desired_state }}.yml"
5 changes: 5 additions & 0 deletions roles/clusterlust.kbcli/tasks/present.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

- name: Install kbcli
when: use_kbcli | bool
ansible.builtin.include_tasks: "kbcli.yml"
39 changes: 39 additions & 0 deletions roles/clusterlust.kubeblocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
kubeblocks
==========

A role to install kbcli to work with KubeBlocks

https://kubeblocks.io

Run Databases on Kubernetes? Run with KubeBlocks.

KubeBlocks is crafted for managing databases on Kubernetes, designed by domain experts with decades of experience.
It supports a wide range of stateful workloads, including relational databases, NoSQL, message queues.
By streamlining operations, enhancing flexibility, and offering extensions, KubeBlocks makes database management easier in cloud-native environments.

Requirements
------------

EL or Ubuntu Linux.

Role Variables
--------------

```yml
# defaults
desired_state: present # or absent
```

Dependencies
------------
None.

Example Playbook
----------------


```yaml
- hosts: controller
roles:
- role: clusterlust.kubeblocks
```
69 changes: 61 additions & 8 deletions roles/clusterlust.kubeblocks/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
---
# no-op
kubeblocks_clusters: []
# kubeblocks_clusters:
# - kb_cluster_name: database
# kb_cluster_type: postgresql
# kb_cluster_version: postgresql-16.4.0
# options: '--cpu=0.5 --storage=10 --mode=replication'
# kb_cluster_ns: postgresql
# kbcli
kbcli_releases_url: 'https://api.github.com/repos/apecloud/kbcli/releases/latest'
kbcli_repo_url: 'https://github.com/apecloud/kbcli/releases/download'
use_kbcli: true
# set true when k8s is up
use_kubeblocks: false
desired_state: present
verify_state: true
kbcli_download_package: true
kbcli_path: /usr/local/bin
# Semaphore does not pass $HOME so path might need to be more explicit.
k8s_auth_kubeconfig: "{{ lookup('env', 'HOME') }}/.kube/config"
# KubeBlocks
crd_url: 'https://github.com/apecloud/kubeblocks/releases/download/v0.9.2/kubeblocks_crds.yaml'
helm_repo_url: 'https://apecloud.github.io/helm-charts'
helm_binary: /usr/local/bin/helm
# KubeBlocks configuration depends on this Boolean:
use_kb_addons: false

# state: absent removes addons, TODO disabling/enabling
kubeblocks_add_ons:
- addon_name: apecloud-mysql
state: absent
enabled: false
version: 0.9.0
- addon_name: aws-load-balancer-controller
state: absent
enabled: false
version: 1.4.8
- addon_name: kafka
state: present
enabled: true
version: 0.9.0
- addon_name: llm
state: present
enabled: false
version: 0.9.0
- addon_name: mongodb
state: absent
enabled: false
version: 0.9.1
- addon_name: mysql
state: absent
enabled: false
version: 0.9.3
- addon_name: postgresql
state: present
enabled: true
version: 0.9.0
- addon_name: pulsar
state: present
enabled: false
version: 0.9.1
- addon_name: qdrant
state: present
enabled: false
version: 0.9.1
- addon_name: redis
state: present
enabled: true
version: 0.9.0
1 change: 1 addition & 0 deletions roles/clusterlust.kubeblocks/files/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated files are kept here.
5 changes: 5 additions & 0 deletions roles/clusterlust.kubeblocks/tasks/absent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
# when: desired_state == 'absent'
- name: Remove KubeBlocks
when: use_kubeblocks | bool
ansible.builtin.include_tasks: "kubeblocks.yml"
38 changes: 38 additions & 0 deletions roles/clusterlust.kubeblocks/tasks/addons.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
- name: "Ensure add-on state present: {{ item.addon_name }}"
when: item.state == 'present'
ansible.builtin.command: "kbcli --kubeconfig={{ k8s_auth_kubeconfig }} addon install {{ item.addon_name }} --version {{ item.version }}"
register: addon_state
changed_when: "'AlreadyExists' not in addon_state.stderr"
failed_when:
- addon_state.rc != '0'
- "'AlreadyExists' not in addon_state.stderr"
- "'installed successfully' not in addon_state.stdout"
ignore_errors: true

- name: "Ensure add-on enablement: {{ item.addon_name }}."
when:
- item.state == 'present'
- not addon_state.changed | bool
ansible.builtin.command: "kbcli --kubeconfig={{ k8s_auth_kubeconfig }} addon {{ 'enable' if item.enabled else 'disable' }} {{ item.addon_name }}"
register: addon_enablement
failed_when:
- addon_enablement.rc != '0'
- "'enabled' not in addon_enablement.stdout"
- "'disabled' not in addon_enablement.stdout"
- "'already disabled' not in addon_enablement.stdout"
- "'no change' not in addon_enablement.stdout"
- "'not being updated' not in addon_enablement.stdout"
changed_when:
- "'enabled' in addon_enablement.stdout"
- "'disabled' in addon_enablement.stdout"
- "'already disabled' not in addon_enablement.stdout"
- "'no change' not in addon_enablement.stdout"
- "'not being updated' not in addon_enablement.stdout"

- name: "Ensure add-on state absent: {{ item.addon_name }}"
when: item.state == 'absent'
ansible.builtin.command: "kbcli --kubeconfig={{ k8s_auth_kubeconfig }} addon uninstall {{ item.addon_name }}"
register: item_state
failed_when: false
ignore_errors: true
changed_when: false
12 changes: 0 additions & 12 deletions roles/clusterlust.kubeblocks/tasks/clusters.yml

This file was deleted.

70 changes: 70 additions & 0 deletions roles/clusterlust.kubeblocks/tasks/kubeblocks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# ~/.kube/config should be valid

- name: Configure KubeBlocks
when: desired_state == 'present'
block:
- name: Create temp dir
ansible.builtin.tempfile:
state: directory
suffix: temp
register: tempdir

- name: Download the manifest
ansible.builtin.get_url:
url: "{{ crd_url }}"
dest: "{{ tempdir.path }}/{{ crd_manifest }}"
mode: '0664'

- name: Create dependent CRDs
kubernetes.core.k8s:
state: present
src: "{{ tempdir.path }}/{{ crd_manifest }}"
namespace: kube-system

- name: Remove the temp dir
when: tempdir.path is defined
ansible.builtin.file:
path: "{{ tempdir.path }}"
state: absent

- name: Add the KubeBlocks Helm repository
kubernetes.core.helm_repository:
binary_path: "{{ helm_binary }}"
name: "{{ helm_repo_name }}"
url: "{{ helm_repo_url }}"
state: present

- name: Remove KubeBlocks
when: desired_state == 'absent'
ansible.builtin.command: >
kbcli --kubeconfig={{ k8s_auth_kubeconfig }} kubeblocks uninstall -n kb-system
--auto-approve=true
changed_when: true

- name: Manage the Helm chart
kubernetes.core.helm:
binary_path: "{{ helm_binary }}"
name: "{{ helm_chart_name }}"
chart_ref: "{{ helm_repo_name }}/{{ helm_chart_name }}"
release_namespace: "{{ namespace }}"
create_namespace: true
state: "{{ desired_state }}"
wait: true

- name: Remove KubeBlocks Helm repository
when: desired_state == 'absent'
kubernetes.core.helm_repository:
binary_path: "{{ helm_binary }}"
name: "{{ helm_repo_name }}"
state: absent

- name: Remove KubeBlocks crds
when: desired_state == 'absent'
ansible.builtin.shell: |
set -o pipefail;
kubectl --kubeconfig={{ k8s_auth_kubeconfig }} get crd -o name \
| grep kubeblocks.io \
| xargs kubectl --kubeconfig={{ k8s_auth_kubeconfig }} delete
changed_when: true
failed_when: false # for example rc 123 is empty list as inputs
12 changes: 7 additions & 5 deletions roles/clusterlust.kubeblocks/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
- name: Install KubeBlock clusters
when:
- kubeblocks_clusters|length >= 1
ansible.builtin.include_tasks: clusters.yml
loop: "{{ kubeblocks_clusters }}"
# desired_state in ['absent', 'present']
- name: "Converge state - {{ desired_state }}"
ansible.builtin.include_tasks: "{{ desired_state }}.yml"

- name: "Verify state - {{ desired_state }}"
ansible.builtin.include_tasks: verify.yml
when: verify_state | bool
10 changes: 10 additions & 0 deletions roles/clusterlust.kubeblocks/tasks/present.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# when: desired_state == 'present'
- name: Install KubeBlocks
when: use_kubeblocks | bool
ansible.builtin.include_tasks: "kubeblocks.yml"

- name: Manage KubeBlocks Add-ons
when: use_kb_addons | bool
ansible.builtin.include_tasks: "addons.yml"
loop: "{{ kubeblocks_add_ons }}"
Loading