blob: ce5a19ba253bbb6b0a1d195691395d00124124f5 [file] [log] [blame]
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +02001#!/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
18usage() {
19cat << ==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
28find_nodes_with_external_addrs()
29{
guillaume.lambert7bf306e2021-09-08 12:03:22 +020030 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
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020038}
39
40generate_config_map()
41{
42cat <<CNFEOF | kubectl apply -f -
43apiVersion: v1
44kind: ConfigMap
45metadata:
46 namespace: metallb-system
47 name: config
48data:
49 config: |
50 address-pools:
51 - name: default
52 protocol: layer2
53 addresses:
54$(for value in "$@"; do echo -e " - $value"; done)
55CNFEOF
56}
57
58generate_config_from_single_addr() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020059 generate_config_map "$1 - $1"
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020060}
61
62install_metallb() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020063 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)"
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020067}
68
69automatic_configuration() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020070 install_metallb
71 generate_config_from_single_addr $(find_nodes_with_external_addrs)
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020072}
73
74manual_configuration() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020075 install_metallb
76 generate_config_map $@
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020077}
78
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020079if [ $# -eq 1 ] && [ "$1" = "-h" ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020080 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020081if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020082 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020083elif [ $# -eq 0 ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020084 automatic_configuration
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020085else
guillaume.lambert7bf306e2021-09-08 12:03:22 +020086 manual_configuration $@
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020087fi