Skip to content

Commit

Permalink
Use different network for each cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
mclarkson committed Oct 19, 2024
1 parent 6b6a468 commit 187d8f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/containerutils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,16 @@ CU_get_cluster_size() {
# Args: arg1 - docker container id or container name to query.
CU_get_container_ip() {

local info

[[ -z ${1} ]] && {
printf 'INTERNAL ERROR: Container ID (arg1) cannot be empty.\n' >"${STDERR}"
err || return
}

docker inspect \
--format='{{.NetworkSettings.Networks.mok_network.IPAddress}}' \
"$1" || {
printf 'ERROR: %s inspect failed\n' "${_CU[containerrt]}" >"${STDERR}"
info=$(CU_get_container_info "$1") || return
JSONPath '.[0].NetworkSettings.Networks.*.IPAddress' -b <<<"${info}" ||
err || return
}
}

# CU_get_container_info uses 'docker/podman inspect $id' to output
Expand All @@ -125,7 +124,7 @@ CU_get_container_info() {
# arg3 - The k8s base image version to use.
CU_create_container() {

local imagename img allimgs systemd_always
local imagename img allimgs systemd_always clustername

[[ -z $1 || -z $2 || -z $3 ]] && {
printf 'INTERNAL ERROR: Neither arg1, arg2 nor arg3 can be empty.\n' \
Expand Down Expand Up @@ -161,15 +160,19 @@ EnD
return "${ERROR}"
fi

clustername=$(CC_clustername) || err || return

if [[ ${_CU[containerrt]} == "podman" ]];
then
create_network=$(docker network exists mok_network)
docker network exists "${clustername}_network" >/dev/null 2>&1
create_network=$?
else
create_network=$(docker network inspect mok_network >/dev/null 2>&1)
docker network inspect "${clustername}_network" >/dev/null 2>&1
create_network=$?
fi

[[ ${create_network} -eq 1 ]] && {
docker network create mok_network || {
[[ ${create_network} -ne 0 ]] && {
docker network create "${clustername}_network" || {
printf 'ERROR: docker network create failed\n' >"${STDERR}"
err || return
}
Expand All @@ -178,7 +181,7 @@ EnD
[[ ${_CU[containerrt]} == "podman" ]] && systemd_always="--systemd=always"

docker run --privileged ${systemd_always} \
--network mok_network \
--network "${clustername}_network" \
-v /lib/modules:/lib/modules:ro \
--detach \
--name "$1" \
Expand Down
5 changes: 5 additions & 0 deletions src/createcluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ declare OK ERROR STDERR STOP TRUE FALSE K8SVERSION

# Getters/Setters -------------------------------------------------------------

# CC_set_clustername setter sets the clustername array item.
CC_clustername() {
printf '%s' "${_CC[clustername]}"
}

# CC_set_clustername setter sets the clustername array item.
CC_set_clustername() {
_CC[clustername]="$1"
Expand Down
2 changes: 1 addition & 1 deletion src/getcluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ GC_run() {
containerhostname=$(JSONPath '.[0].Config.Hostname' -b <<<"${info}") ||
err || return

containerip=$(JSONPath '.[0].NetworkSettings.Networks.mok_network.IPAddress' -b <<<"${info}") ||
containerip=$(JSONPath '.[0].NetworkSettings.Networks.*.IPAddress' -b <<<"${info}") ||
err || return

printf '%s %s %s %s\n' "${clustname}" "${id}" "${containerhostname}" "${containerip}"
Expand Down

0 comments on commit 187d8f9

Please sign in to comment.