From a5ecaafe50c0226415b8e31613a4e170fe5242b8 Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Fri, 19 May 2023 11:37:15 +0700 Subject: [PATCH] refactor(metallb)!: use CRD instead of ConfigMap Deprecated https://metallb.universe.tf/configuration/migration_to_crds --- metal/cluster.yml | 7 --- metal/roles/metallb_config/defaults/main.yml | 9 ---- metal/roles/metallb_config/tasks/main.yml | 10 ---- .../templates/configmap.yaml.j2 | 12 ----- scripts/configure | 46 ++++++++++++------- .../templates/ipaddresspool.yaml | 8 ++++ .../templates/l2advertisement.yaml | 8 ++++ 7 files changed, 46 insertions(+), 54 deletions(-) delete mode 100644 metal/roles/metallb_config/defaults/main.yml delete mode 100644 metal/roles/metallb_config/tasks/main.yml delete mode 100644 metal/roles/metallb_config/templates/configmap.yaml.j2 create mode 100644 system/metallb-system/templates/ipaddresspool.yaml create mode 100644 system/metallb-system/templates/l2advertisement.yaml diff --git a/metal/cluster.yml b/metal/cluster.yml index ef7e3f6b..304d54ca 100644 --- a/metal/cluster.yml +++ b/metal/cluster.yml @@ -3,10 +3,3 @@ roles: - k3s - automatic_upgrade - -- name: Create some basic config - hosts: localhost - environment: - KUBECONFIG: ./kubeconfig.yaml - roles: - - metallb_config diff --git a/metal/roles/metallb_config/defaults/main.yml b/metal/roles/metallb_config/defaults/main.yml deleted file mode 100644 index 6f111cc8..00000000 --- a/metal/roles/metallb_config/defaults/main.yml +++ /dev/null @@ -1,9 +0,0 @@ -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.utils.ipaddr('net') - | ansible.utils.ipsubnet(metallb_subnet_cidr_prefix, metallb_subnet_index) - }} diff --git a/metal/roles/metallb_config/tasks/main.yml b/metal/roles/metallb_config/tasks/main.yml deleted file mode 100644 index 8b3f3727..00000000 --- a/metal/roles/metallb_config/tasks/main.yml +++ /dev/null @@ -1,10 +0,0 @@ -- 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 }}" diff --git a/metal/roles/metallb_config/templates/configmap.yaml.j2 b/metal/roles/metallb_config/templates/configmap.yaml.j2 deleted file mode 100644 index 7aa186c5..00000000 --- a/metal/roles/metallb_config/templates/configmap.yaml.j2 +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: metallb-config - namespace: {{ metallb_namespace }} -data: - config: | - address-pools: - - name: default - protocol: layer2 - addresses: - - {{ metallb_addresses }} diff --git a/scripts/configure b/scripts/configure index d7f6afad..cadf807a 100755 --- a/scripts/configure +++ b/scripts/configure @@ -13,10 +13,13 @@ import sys from rich.prompt import Confirm, Prompt -default_seed_repo = "https://github.com/khuedoan/homelab" -default_domain = "khuedoan.com" -default_timezone = "Asia/Ho_Chi_Minh" -default_terraform_workspace = "khuedoan" +upstream_config = { + "seed_repo": "https://github.com/khuedoan/homelab", + "domain": "khuedoan.com", + "timezone": "Asia/Ho_Chi_Minh", + "terraform_workspace": "khuedoan", + "loadbalancer_ip_range": "192.168.1.224/27", +} def check_python_version(required_version: str) -> None: @@ -43,12 +46,13 @@ def main() -> None: ) editor = Prompt.ask("Select text editor", default='nvim') - domain = Prompt.ask("Enter your domain", default=default_domain) - seed_repo = Prompt.ask("Enter seed repo", default=default_seed_repo) - timezone = Prompt.ask("Enter time zone", default=default_timezone) + domain = Prompt.ask("Enter your domain", default=upstream_config['domain']) + seed_repo = Prompt.ask("Enter seed repo", default=upstream_config['seed_repo']) + timezone = Prompt.ask("Enter time zone", default=upstream_config['timezone']) + loadbalancer_ip_range = Prompt.ask("Enter IP range for load balancer", default=upstream_config['loadbalancer_ip_range']) find_and_replace( - pattern=default_domain, + pattern=upstream_config['domain'], replacement=domain, paths=[ ".ci", @@ -61,7 +65,7 @@ def main() -> None: ) find_and_replace( - pattern=default_seed_repo, + pattern=upstream_config['seed_repo'], replacement=seed_repo, paths=[ "bootstrap", @@ -70,7 +74,7 @@ def main() -> None: ) find_and_replace( - pattern=default_timezone, + pattern=upstream_config['timezone'], replacement=timezone, paths=[ "apps", @@ -79,21 +83,31 @@ def main() -> None: ] ) + find_and_replace( + pattern=upstream_config['loadbalancer_ip_range'], + replacement=loadbalancer_ip_range, + paths=[ + "system/metallb-system/templates/ipaddresspool.yaml", + ] + ) + + if Confirm.ask("Update server list?", default=True): + subprocess.run( + [editor, 'metal/inventories/prod.yml'] + ) + + if Confirm.ask("Do you want to use managed services?"): - terraform_workspace = Prompt.ask("Enter Terraform Workspace", default=default_terraform_workspace) + terraform_workspace = Prompt.ask("Enter Terraform Workspace", default=upstream_config['terraform_workspace']) find_and_replace( - pattern=default_terraform_workspace, + pattern=upstream_config['terraform_workspace'], replacement=terraform_workspace, paths=[ "external/versions.tf" ] ) - subprocess.run( - [editor, 'metal/inventories/prod.yml'] - ) - if __name__ == '__main__': main() diff --git a/system/metallb-system/templates/ipaddresspool.yaml b/system/metallb-system/templates/ipaddresspool.yaml new file mode 100644 index 00000000..9fdf5953 --- /dev/null +++ b/system/metallb-system/templates/ipaddresspool.yaml @@ -0,0 +1,8 @@ +apiVersion: metallb.io/v1beta1 +kind: IPAddressPool +metadata: + name: default + namespace: {{ .Release.Namespace }} +spec: + addresses: + - 192.168.1.224/27 diff --git a/system/metallb-system/templates/l2advertisement.yaml b/system/metallb-system/templates/l2advertisement.yaml new file mode 100644 index 00000000..3964056a --- /dev/null +++ b/system/metallb-system/templates/l2advertisement.yaml @@ -0,0 +1,8 @@ +apiVersion: metallb.io/v1beta1 +kind: L2Advertisement +metadata: + name: default + namespace: {{ .Release.Namespace }} +spec: + ipAddressPools: + - default