[COMMON] Enforce checkbashisms tox profile
[oom.git] / kubernetes / contrib / metallb-loadbalancer-inst / install-metallb-on-cluster.sh
1 #!/bin/sh -e
2
3 #
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
19 usage()
20 {
21 cat << ==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
30 find_nodes_with_external_addrs()
31 {
32     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
40 }
41
42 generate_config_map()
43 {
44 cat <<CNFEOF | kubectl apply -f -
45 apiVersion: v1
46 kind: ConfigMap
47 metadata:
48   namespace: metallb-system
49   name: config
50 data:
51   config: |
52     address-pools:
53     - name: default
54       protocol: layer2
55       addresses:
56 $(for value in "$@"; do echo -e "      - $value"; done)
57 CNFEOF
58 }
59
60 generate_config_from_single_addr() {
61     generate_config_map "$1 - $1"
62 }
63
64 install_metallb() {
65     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)"
69 }
70
71 automatic_configuration() {
72     install_metallb
73     generate_config_from_single_addr $(find_nodes_with_external_addrs)
74 }
75
76 manual_configuration() {
77     install_metallb
78     generate_config_map $@
79 }
80
81 if [ $# -eq 1 ] && [ "$1" = "-h" ]; then
82     usage
83 if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
84     usage
85 elif [ $# -eq 0 ]; then
86     automatic_configuration
87 else
88     manual_configuration $@
89 fi