Skip to content

Commit

Permalink
feat: experiment with metallb
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Torres <[email protected]>
  • Loading branch information
samuel-form3 committed Dec 19, 2024
1 parent eaaedc7 commit a8810fc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
38 changes: 11 additions & 27 deletions hack/common.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
#!/bin/bash

function docker_bridge_cidr() {
docker network inspect -f json kind | jq -r '.[0].IPAM.Config[] | select(.Subnet | contains(":") | not) | .Subnet'
function docker_bridge_base_cidr() {
local DOCKER_CIDR=$(docker network inspect -f json kind | jq -r '.[0].IPAM.Config[] | select(.Subnet | contains(":") | not) | .Subnet')
echo $DOCKER_CIDR | cut -d'.' -f1-2
}

function metallb_cidr() {
CLUSTER_ID="${1}"
NET_NUM=$((1024 - $CLUSTER_ID))
BRIDGE_CIDR=$(docker_bridge_cidr "${CLUSTER_ID}")
echo "cidrsubnet(\"$BRIDGE_CIDR\", 10, $NET_NUM)" | terraform console | sed 's/"//g'
function metallb_address_cidr() {
local CLUSTER_ID=$1
local DOCKER_BASE_CIDR=$(docker_bridge_base_cidr)
echo "$DOCKER_BASE_CIDR.$((250+$CLUSTER_ID)).250/28"
}

function host_min() {
CIDR="${1}"
echo "cidrhost(\"$CIDR\", 1)" | terraform console | sed 's/"//g'
}

function host_max() {
CIDR="${1}"
echo "cidrhost(\"$CIDR\", -2)" | terraform console | sed 's/"//g'
}

function metallb_host_min() {
CLUSTER_ID="${1}"
METALLB_CIDR=$(metallb_cidr "${CLUSTER_ID}")
host_min "$METALLB_CIDR"
}

function metallb_host_max() {
CLUSTER_ID="${1}"
METALLB_CIDR=$(metallb_cidr "${CLUSTER_ID}")
host_max "$METALLB_CIDR"
function x_pdb_address() {
local CLUSTER_ID=$1
local DOCKER_BASE_CIDR=$(docker_bridge_base_cidr)
echo "$DOCKER_BASE_CIDR.$((250+$CLUSTER_ID)).251"
}
4 changes: 2 additions & 2 deletions hack/env/metallb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ metadata:
namespace: metallb-system
spec:
addresses:
- ${METALLB_HOST_MIN}-${METALLB_HOST_MAX}
- ${METALLB_ADDRESS_CIDR}
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: empty
namespace: metallb-system
namespace: metallb-system
16 changes: 4 additions & 12 deletions hack/install-metallb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
set -e
set -o pipefail

CONTEXT="${1}"
CLUSTER="${2}"
CONTEXT=$1
CLUSTER=$2

source ./hack/common.sh

# We expect a x.x.x.x/16 on the docker network bridge,
# and we need to have a /26 of that and assign it to metallb.
# Because we need non-overlapping ip ranges on metallb we use the cluster number (1, 2, 3)
# to calculate the last /26 subnets of that /16 range.
# cluster=1 gets the last /26
# cluster=2 gets the second to last /26
# etc. etc.
export METALLB_HOST_MIN=$(metallb_host_min "$CLUSTER")
export METALLB_HOST_MAX=$(metallb_host_max "$CLUSTER")
echo "using metallb range $METALLB_HOST_MIN-$METALLB_HOST_MAX"
export METALLB_ADDRESS_CIDR=$(metallb_address_cidr $CLUSTER)
echo "Using MetalLB Address ${METALLB_ADDRESS_CIDR}"

kubectl apply --wait=true -f ./hack/env/metallb-native.yaml --context "${CONTEXT}"
kubectl wait deployment -n metallb-system controller --for condition=Available=True --timeout=90s --context "${CONTEXT}"
Expand Down
2 changes: 1 addition & 1 deletion hack/install-xpdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source ./hack/common.sh
this_address=""
remote_endpoints=""
for i in 1 2 3; do
METALLB_HOST_MIN=$(metallb_host_min $i)
METALLB_HOST_MIN=$(x_pdb_address $i)
if [[ "$i" -eq "$CLUSTER" ]]; then
this_address="$METALLB_HOST_MIN"
continue
Expand Down

0 comments on commit a8810fc

Please sign in to comment.