feat(metallb): generate address pool automatically

This commit is contained in:
Khue Doan 2021-12-27 12:24:23 +07:00
parent 9e59f1ad81
commit 8ab9ac0817
5 changed files with 39 additions and 7 deletions

View File

@ -2,3 +2,10 @@
hosts: metal
roles:
- k3s
- name: Create some basic config
hosts: localhost
environment:
KUBECONFIG: ./kubeconfig.yaml
roles:
- metallb_config

View File

@ -0,0 +1,9 @@
metallb_namespace: metallb-system
metallb_subnet_cidr_prefix: 27 # /27
metallb_subnet_index: -1 # last subnet
metallb_addresses: | # Use the last subnet with the specified size from the network
{{
(ansible_default_ipv4.network + '/' + ansible_default_ipv4.netmask)
| ansible.netcommon.ipaddr('net')
| ansible.netcommon.ipsubnet(metallb_subnet_cidr_prefix, metallb_subnet_index)
}}

View File

@ -0,0 +1,10 @@
- name: Ensure MetalLB namespace exist
kubernetes.core.k8s:
api_version: v1
kind: Namespace
name: "{{ metallb_namespace }}"
state: present
- name: Create MetalLB config base on network info
kubernetes.core.k8s:
definition: "{{ lookup('template', 'configmap.yaml.j2') | from_yaml }}"

View File

@ -0,0 +1,12 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: metallb-config
namespace: {{ metallb_namespace }}
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- {{ metallb_addresses }}

View File

@ -1,8 +1,2 @@
metallb:
configInline:
address-pools:
- name: default
protocol: layer2
addresses:
# TODO (optimize) Use metal values for MetalLB values
- 192.168.1.150-192.168.1.180
existingConfigMap: metallb-config