blob: c62e2a51bd12b93fd1c0cd3fd2ec4a0ea60a2f19 [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.lambert7bf306e2021-09-08 12:03:22 +020032 local WORKER_NODES=$(kubectl get no -l node-role.kubernetes.io/worker=true -o jsonpath='{.items..metadata.name}')
33 for worker in $WORKER_NODES; do
34 local external_ip=$(kubectl get no $worker -o jsonpath='{.metadata.annotations.rke\.cattle\.io/external-ip }')
35 local internal_ip=$(kubectl get no $worker -o jsonpath='{.metadata.annotations.rke\.cattle\.io/internal-ip }')
36 if [ $internal_ip != $external_ip ]; then
37 echo $external_ip
38 fi
39 done
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020040}
41
42generate_config_map()
43{
44cat <<CNFEOF | kubectl apply -f -
45apiVersion: v1
46kind: ConfigMap
47metadata:
48 namespace: metallb-system
49 name: config
50data:
51 config: |
52 address-pools:
53 - name: default
54 protocol: layer2
55 addresses:
56$(for value in "$@"; do echo -e " - $value"; done)
57CNFEOF
58}
59
60generate_config_from_single_addr() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020061 generate_config_map "$1 - $1"
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020062}
63
64install_metallb() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020065 kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/namespace.yaml
66 kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml
67 # Only when install
68 kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020069}
70
71automatic_configuration() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020072 install_metallb
73 generate_config_from_single_addr $(find_nodes_with_external_addrs)
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020074}
75
76manual_configuration() {
guillaume.lambert7bf306e2021-09-08 12:03:22 +020077 install_metallb
78 generate_config_map $@
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020079}
80
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020081if [ $# -eq 1 ] && [ "$1" = "-h" ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020082 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020083if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020084 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020085elif [ $# -eq 0 ]; then
guillaume.lambert7bf306e2021-09-08 12:03:22 +020086 automatic_configuration
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020087else
guillaume.lambert7bf306e2021-09-08 12:03:22 +020088 manual_configuration $@
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020089fi