Guillaume Lambert | 85b1492 | 2021-03-12 13:53:18 +0100 | [diff] [blame] | 1 | #!/bin/sh -e |
| 2 | |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 3 | # |
| 4 | # Copyright 2020 Samsung Electronics Co., Ltd. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
Guillaume Lambert | 85b1492 | 2021-03-12 13:53:18 +0100 | [diff] [blame] | 19 | usage() |
| 20 | { |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 21 | cat << ==usage |
| 22 | $0 Automatic configuration using external addresess from nodes |
| 23 | $0 --help This message |
| 24 | $0 -h This message |
| 25 | $0 [cluster_ip1] ... [cluster_ipn] Cluster address or ip ranges |
| 26 | ==usage |
| 27 | } |
| 28 | |
| 29 | |
| 30 | find_nodes_with_external_addrs() |
| 31 | { |
guillaume.lambert | f657a81 | 2021-12-07 20:21:17 +0100 | [diff] [blame] | 32 | local WORKER_NODES |
| 33 | WORKER_NODES=$(kubectl get no -l node-role.kubernetes.io/worker=true -o jsonpath='{.items..metadata.name}') |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 34 | for worker in $WORKER_NODES; do |
guillaume.lambert | f657a81 | 2021-12-07 20:21:17 +0100 | [diff] [blame] | 35 | local external_ip |
| 36 | external_ip=$(kubectl get no $worker -o jsonpath='{.metadata.annotations.rke\.cattle\.io/external-ip }') |
| 37 | local internal_ip |
| 38 | internal_ip=$(kubectl get no $worker -o jsonpath='{.metadata.annotations.rke\.cattle\.io/internal-ip }') |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 39 | if [ $internal_ip != $external_ip ]; then |
| 40 | echo $external_ip |
| 41 | fi |
| 42 | done |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | generate_config_map() |
| 46 | { |
| 47 | cat <<CNFEOF | kubectl apply -f - |
| 48 | apiVersion: v1 |
| 49 | kind: ConfigMap |
| 50 | metadata: |
| 51 | namespace: metallb-system |
| 52 | name: config |
| 53 | data: |
| 54 | config: | |
| 55 | address-pools: |
| 56 | - name: default |
| 57 | protocol: layer2 |
| 58 | addresses: |
| 59 | $(for value in "$@"; do echo -e " - $value"; done) |
| 60 | CNFEOF |
| 61 | } |
| 62 | |
| 63 | generate_config_from_single_addr() { |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 64 | generate_config_map "$1 - $1" |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | install_metallb() { |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 68 | kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/namespace.yaml |
| 69 | kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml |
| 70 | # Only when install |
| 71 | kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)" |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | automatic_configuration() { |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 75 | install_metallb |
| 76 | generate_config_from_single_addr $(find_nodes_with_external_addrs) |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | manual_configuration() { |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 80 | install_metallb |
| 81 | generate_config_map $@ |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Guillaume Lambert | 7abd8c3 | 2021-04-26 21:48:38 +0200 | [diff] [blame] | 84 | if [ $# -eq 1 ] && [ "$1" = "-h" ]; then |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 85 | usage |
Guillaume Lambert | 7abd8c3 | 2021-04-26 21:48:38 +0200 | [diff] [blame] | 86 | if [ $# -eq 1 ] && [ "$1" = "--help" ]; then |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 87 | usage |
Guillaume Lambert | 7abd8c3 | 2021-04-26 21:48:38 +0200 | [diff] [blame] | 88 | elif [ $# -eq 0 ]; then |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 89 | automatic_configuration |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 90 | else |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 91 | manual_configuration $@ |
Lucjan Bryndza | ddf2e3a | 2020-03-31 10:28:03 +0200 | [diff] [blame] | 92 | fi |