Skip to content

Commit

Permalink
Refactor network creation for kubernetes
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dzikowski <[email protected]>
  • Loading branch information
dzikowski committed Dec 1, 2023
1 parent 4a7d17c commit 41e9d7d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 59 deletions.
8 changes: 3 additions & 5 deletions src/setup-k8s/templates/fabric-k8s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ BLUE="$(printf '\033[34m')"
networkUp() {
printHeadline "Checking dependencies..." "U1F984"
verifyKubernetesConnectivity
printHeadline "Starting Network..." "U1F984"
deployPeer
deployOrderer
installChannels
installChaincodes
startNetwork
# installChannels
# installChaincodes
printHeadline "Done! Enjoy your fresh network" "U1F984"
}

Expand Down
5 changes: 2 additions & 3 deletions src/setup-k8s/templates/fabric-k8s/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ FABLO_BUILD=<%= fabloBuild %>
FABLO_CONFIG=<%= paths.fabloConfig %>
ORDERER_IMAGE=hyperledger/fabric-orderer
ORDERER_VERSION=<%= global.fabricVersion %>
PEER_IMAGE=quay.io/kfsoftware/fabric-peer
PEER_VERSION=2.4.1-v0.0.3
# PEER_VERSION=<%= global.fabricVersion %>
PEER_IMAGE=hyperledger/fabric-peer
PEER_VERSION=<%= global.fabricVersion %>
CA_IMAGE=hyperledger/fabric-ca
CA_VERSION=<%= global.fabricCaVersion %>
LOGGING_LEVEL=<%= global.monitoring.loglevel %>
Expand Down
162 changes: 111 additions & 51 deletions src/setup-k8s/templates/fabric-k8s/scripts/base-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,131 @@

source "$FABLO_NETWORK_ROOT/fabric-k8s/scripts/util.sh"

deployCA() {
local CA_HOST="$1"
local MSPID="$2"
local CA_NAME="$(echo "$CA_HOST" | sed 's/\./-/g')"

inputLog "Deploying $CA_ID ($CA_IMAGE:$CA_VERSION)"
kubectl hlf ca create \
--image="$CA_IMAGE" \
--version="$CA_VERSION" \
--storage-class="$STORAGE_CLASS" \
--capacity=2Gi \
--name="$CA_NAME" \
--hosts="$CA_HOST" \
--enroll-id=enroll \
--enroll-pw=enrollpw

kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces
sleep 10
kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces

kubectl wait --timeout=60s --for=condition=Running fabriccas.hlf.kungfusoftware.es --all
}

deployPeer() {

<% orgs.forEach((org) => { -%>
<% if(org.peers.length > 0 ) { -%>
printItalics "Deploying <%= org.name %> CA" "U1F984"
kubectl hlf ca create --image="$CA_IMAGE" --version="$CA_VERSION" --storage-class="$STORAGE_CLASS" --capacity=2Gi --name=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> --enroll-id=<%= org.name.toLowerCase() %> --enroll-pw="$<%= org.ca.caAdminPassVar %>"
sleep 3
while [[ $(kubectl get pods -l release=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
sleep 5
inputLog "waiting for CA"
done
kubectl hlf ca register --name=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> --user=peer --secret="$<%= org.ca.caAdminPassVar %>" --type=peer --enroll-id <%= org.name.toLowerCase() %> --enroll-secret="$<%= org.ca.caAdminPassVar %>" --mspid <%= org.mspName %>
inputLog "registered <%= org.name %> -ca"
printItalics "Deploying Peers" "U1F984"
sleep 10
<% orgs.forEach((org) => { org.peers.forEach((peer) => { %>
kubectl hlf peer create --statedb=<%= peer.db.type.toLowerCase() %> --version="$PEER_VERSION" --storage-class="$STORAGE_CLASS" --enroll-id=peer --mspid=<%= org.mspName %> \
--enroll-pw="$<%= org.ca.caAdminPassVar %>" --capacity=5Gi --name=<%= peer.name %> --ca-name="<%= org.name.toLowerCase() %>-<%= org.ca.prefix %>.$NAMESPACE" --k8s-builder=true --external-service-builder=false
<% })}) %>
registerPeerUser() {
local CA_HOST="$1"
local MSPID="$2"

inputLog "Registering peer user $PEER_USER on $CA_HOST"
kubectl hlf ca register \
--name="$CA_HOST" \
--user=peer \
--secret=peerpw \
--type=peer \
--enroll-id=enroll \
--enroll-secret=enrollpw \
--mspid="$MSPID"
}

<% } -%>
<% }) %>
deployPeer() {
local PEER_HOST="$1"
local CA_HOST="$2"
local MSPID="$3"

inputLog "Deploying $PEER_HOST ($PEER_IMAGE:$PEER_VERSION)"
kubectl hlf peer create \
--statedb=leveldb \ # TODO: make this configurable
--image="$PEER_IMAGE" \
--version="$PEER_VERSION" \
--storage-class="$STORAGE_CLASS" \
--capacity=5Gi \
--enroll-id=peer \
--enroll-pw=peerpw \
--name="$PEER_HOST" \
--hosts="$PEER_HOST" \
--ca-name="$CA_HOST" \
--mspid="$MSPID"

# todo move all waits after all peers are created
kubectl wait --timeout=180s --for=condition=Running fabricpeers.hlf.kungfusoftware.es --all
}

while [[ $(kubectl get pods -l app=hlf-peer --output=jsonpath='{.items[*].status.containerStatuses[0].ready}') != "true true" ]]; do
sleep 5
inputLog "waiting for peer nodes to be ready"
done
registerOrdererUser() {
local CA_HOST="$1"
local MSPID="$2"

inputLog "Registering orderer user $ORDERER_USER on $CA_HOST"
kubectl hlf ca register \
--name="$CA_HOST" \
--user=orderer \
--secret=ordererpw \
--type=orderer \
--enroll-id=enroll \
--enroll-secret=enrollpw \
--mspid="$MSPID"
}

deployOrderer() {
local ORDERER_HOST="$1"
local CA_HOST="$2"
local MSPID="$3"

inputLog "Deploying $ORDERER_HOST ($ORDERER_IMAGE:$ORDERER_VERSION)"
kubectl hlf ordnode create \
--image="$ORDERER_IMAGE" \
--version="$ORDERER_VERSION" \
--storage-class="$STORAGE_CLASS" \
--capacity=2Gi \
--enroll-id=orderer \
--enroll-pw=ordererpw \
--name="$ORDERER_HOST" \
--hosts="$ORDERER_HOST" \
--ca-name="$CA_HOST" \
--mspid="$MSPID"

kubectl wait --timeout=180s --for=condition=Running fabricorderernodes.hlf.kungfusoftware.es --all
}

printItalics "Deploying Orderers" "U1F984"
startNetwork() {
printHeadline "Starting network" "U1F680"

<% orgs.forEach((org) => { -%>
<% if(org.name == "Orderer") { -%>
kubectl hlf ca create --storage-class="$STORAGE_CLASS" --capacity=2Gi --name=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> --enroll-id=<%= org.name.toLowerCase() %> --enroll-pw="$<%= org.ca.caAdminPassVar %>"
while [[ $(kubectl get pods -l release=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
sleep 5
inputLog "waiting for <%= org.name.toLowerCase() %>-<%= org.ca.prefix %> CA to be ready" "$RESETBG"
done
kubectl hlf ca register --name=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> --user="$<%= org.ca.caAdminNameVar %>" --secret="$<%= org.ca.caAdminPassVar %>" --type=orderer --enroll-id=<%= org.name.toLowerCase() %> --enroll-secret="$<%= org.ca.caAdminPassVar %>" --mspid <%= org.mspName %> &&
inputLog "registered <%= org.name.toLowerCase() %>-<%= org.ca.prefix %>"
deployCA "<%= org.ca.address %>" "<%= org.mspName %>"
<% }) -%>
kubectl hlf ordnode create --version="$ORDERER_VERSION" \
--storage-class="$STORAGE_CLASS" --enroll-id="$<%= org.ca.caAdminNameVar %>" --mspid=<%= org.mspName %> \
--enroll-pw="$<%= org.ca.caAdminPassVar %>" --capacity=2Gi --name=<%= org.name.toLowerCase() %>-node --ca-name="<%= org.name.toLowerCase() %>-<%= org.ca.prefix %>.$NAMESPACE"
while [[ $(kubectl get pods -l app=hlf-ordnode -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
sleep 5
inputLog "waiting for <%= org.name.toLowerCase() %> Node to be ready"
done
<% orgs.forEach((org) => { -%>
<% if(org.ordererGroups.length > 0 ) { -%>
registerOrdererUser "<%= org.ca.address %>" "<%= org.mspName %>"
<% org.ordererGroups.forEach((group) => { -%>
<% group.orderers.forEach((orderer) => { -%>
deployOrderer "<%= orderer.address %>" "<%= org.ca.address %>" "<%= org.mspName %>"
<% }) -%>
<% }) -%>
<% } -%>
<% }) -%>
kubectl hlf inspect --output "$CONFIG_DIR/ordservice.yaml" -o <%= org.mspName %>
kubectl hlf ca enroll --name=<%= org.name.toLowerCase() %>-<%= org.ca.prefix %> --user="$<%= org.ca.caAdminNameVar %>" --secret="$<%= org.ca.caAdminPassVar %>" --mspid <%= org.mspName %> --ca-name ca --output "$CONFIG_DIR/admin-ordservice.yaml" &&
kubectl hlf utils adduser --userPath="$CONFIG_DIR/admin-ordservice.yaml" --config="$CONFIG_DIR/ordservice.yaml" --username="$<%= org.ca.caAdminNameVar %>" --mspid=<%= org.mspName %>
<% orgs.forEach((org) => { -%>
<% if(org.peers.length > 0 ) { -%>
registerPeerUser "<%= org.ca.address %>" "<%= org.mspName %>"
<% org.peers.forEach((peer) => { -%>
deployPeer "<%= peer.address %>" "<%= org.ca.address %>" "<%= org.mspName %>"
<% }) -%>
<% } -%>
<% }) %>
<% }) -%>
}
installChannels() {
<% channels.forEach((channel) => { -%>
Expand Down

0 comments on commit 41e9d7d

Please sign in to comment.