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