blob: 495d540905fdf80d8eff6ef459121b6a37468110 [file] [log] [blame]
Guillaume Lambert85b14922021-03-12 13:53:18 +01001#!/bin/sh -e
2
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +02003#
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 Lambert85b14922021-03-12 13:53:18 +010019usage()
20{
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020021cat << ==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
30find_nodes_with_external_addrs()
31{
guillaume.lambertf657a812021-12-07 20:21:17 +010032 local WORKER_NODES
33 WORKER_NODES=$(kubectl get no -l node-role.kubernetes.io/worker=true -o jsonpath='{.items..metadata.name}')
guillaume.lambert7bf306e2021-09-08 12:03:22 +020034 for worker in $WORKER_NODES; do
guillaume.lambertf657a812021-12-07 20:21:17 +010035 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.lambert7bf306e2021-09-08 12:03:22 +020039 if [ $internal_ip != $external_ip ]; then
40 echo $external_ip
41 fi
42 done
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020043}
44
45generate_config_map()
46{
47cat <<CNFEOF | kubectl apply -f -
48apiVersion: v1
49kind: ConfigMap
50metadata:
51 namespace: metallb-system
52 name: config
53data:
54 config: |
55 address-pools:
56 - name: default
57 protocol: layer2
58 addresses:
59$(for value in "$@"; do echo -e " - $value"; done)
60CNFEOF
61}
62
63generate_config_from_single_addr() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020064 generate_config_map "$1 - $1"
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020065}
66
67install_metallb() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020068 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 Bryndzaddf2e3a2020-03-31 10:28:03 +020072}
73
74automatic_configuration() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020075 install_metallb
76 generate_config_from_single_addr $(find_nodes_with_external_addrs)
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020077}
78
79manual_configuration() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020080 install_metallb
81 generate_config_map $@
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020082}
83
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020084if [ $# -eq 1 ] && [ "$1" = "-h" ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020085 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020086if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020087 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020088elif [ $# -eq 0 ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020089 automatic_configuration
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020090else
guillaume.lambert7bf306e2021-09-08 12:03:22 +020091 manual_configuration $@
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020092fi