blob: bf2bc121a7008d48721d83ac51d6d9f3591ca11d [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{
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
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() {
59 generate_config_map "$1 - $1"
60}
61
62install_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
69automatic_configuration() {
70 install_metallb
71 generate_config_from_single_addr $(find_nodes_with_external_addrs)
72}
73
74manual_configuration() {
75 install_metallb
76 generate_config_map $@
77}
78
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020079if [ $# -eq 1 ] && [ "$1" = "-h" ]; then
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020080 usage
Guillaume Lambert7abd8c32021-04-26 21:48:38 +020081if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
82 usage
83elif [ $# -eq 0 ]; then
Lucjan Bryndzaddf2e3a2020-03-31 10:28:03 +020084 automatic_configuration
85else
86 manual_configuration $@
87fi