diff --git a/k8s/sentry-node/Dockerfile-test.tpl b/k8s/sentry-node/Dockerfile-test.tpl new file mode 100644 index 0000000..1d91b58 --- /dev/null +++ b/k8s/sentry-node/Dockerfile-test.tpl @@ -0,0 +1,7 @@ +FROM bluele/hypermint: + +ENV WORKDIR=/go/src/github.com/bluele/hypermint +WORKDIR ${WORKDIR} + +RUN apk add bash +RUN /hmd testnet -v= -n= --address= -o=/mytestnet diff --git a/k8s/sentry-node/Makefile b/k8s/sentry-node/Makefile new file mode 100644 index 0000000..12355e6 --- /dev/null +++ b/k8s/sentry-node/Makefile @@ -0,0 +1,31 @@ +BUILD_DIR?=../../build +CONFIG_DIR?=./config +VERSION?=$(shell cat ../../version) +RESOURCES?=$(wildcard $(CONFIG_DIR)/*.yaml) +RESOURCES_OPT?=$(addprefix -f,$(RESOURCES)) +VALS_NUM?=4 +# mnemonic "token dash time stand brisk fatal health honey frozen brown flight kitchen" +# m/44'/60'/0'/0 +GENESIS_ADDR?=0x1221a0726d56aEdeA9dBe2522DdAE3Dd8ED0f36c + +create: + kubectl apply $(RESOURCES_OPT) + +destory: + kubectl delete $(RESOURCES_OPT) + +gen-config: + @rm -f ./config/*.yaml + @bash ./gen_testnet.sh $(VALS_NUM) + +destory-pvc: + @for n in $$(kubectl get pvc -o go-template --template '{{range .items}}{{.metadata.labels.app}}{{"\n"}}{{end}}' | grep 'hm-validator'); do \ + kubectl delete pvc -l app=$$n; \ + done + +build-image: + sed -e "s//${VERSION}/g" -e "s//$(VALS_NUM)/g" -e "s//$(GENESIS_ADDR)/g" Dockerfile-test.tpl | docker build --no-cache=true -t bluele/hypermint-testnet-sentry:${VERSION} - + +balance0: + $(eval port := $(shell kubectl get services/hm-validator-0 -o go-template='{{- range .spec.ports }}{{- if (eq "rpc" .name) }}{{.nodePort}}{{- end}}{{- end}}')) + @$(BUILD_DIR)/hmcli balance --address=$(GENESIS_ADDR) --node=tcp://localhost:$(port) diff --git a/k8s/sentry-node/README.md b/k8s/sentry-node/README.md new file mode 100644 index 0000000..24276e1 --- /dev/null +++ b/k8s/sentry-node/README.md @@ -0,0 +1,14 @@ +# Testnet + +## Getting started + +``` +# Generate k8s yaml files, and build docker image for testnet. +$ make VALS_NUM=4 gen-config + +# Create a testnet with generated config. +$ make create + +# Destory testnet +$ make destroy +``` diff --git a/k8s/sentry-node/config.yaml.tpl b/k8s/sentry-node/config.yaml.tpl new file mode 100644 index 0000000..567d255 --- /dev/null +++ b/k8s/sentry-node/config.yaml.tpl @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: hm-config +data: + validators: "" diff --git a/k8s/sentry-node/config/.gitignore b/k8s/sentry-node/config/.gitignore new file mode 100644 index 0000000..1e82fc7 --- /dev/null +++ b/k8s/sentry-node/config/.gitignore @@ -0,0 +1 @@ +*.yaml diff --git a/k8s/sentry-node/deployment.yaml.tpl b/k8s/sentry-node/deployment.yaml.tpl new file mode 100644 index 0000000..45cb9c6 --- /dev/null +++ b/k8s/sentry-node/deployment.yaml.tpl @@ -0,0 +1,128 @@ +apiVersion: v1 +kind: Service +metadata: + name: hm-validator- +spec: + type: NodePort + selector: + app: hm-validator- + ports: + - port: 26656 + name: p2p + - port: 26657 + name: rpc + +--- + +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hm-validator- +spec: + replicas: 1 + selector: + matchLabels: + app: hm-validator- + serviceName: hm-validator- + template: + metadata: + labels: + app: hm-validator- + spec: + containers: + - name: hm-validator + image: bluele/hypermint-testnet-sentry: + command: + - bash + - "-c" + - | + set -ex + sentryid=$(/hmd tendermint show-node-id --home /mytestnet/node/hmd) + peer="${sentryid}@127.0.0.1:26656" + state_file=/mytestnet/node/hmd/data/priv_validator_state.json + if [ ! -e $state_file ]; then + cat << EOF > $state_file + { + "height": "0", + "round": "0", + "step": 0 + } + EOF + fi + TM_PARAMS="p2p.allow_duplicate_ip=true,p2p.addr_book_strict=false" /hmd start --home=/mytestnet/node/hmd \ + --log_level="*:info" \ + --p2p.persistent_peers=$peer \ + --p2p.laddr="tcp://0.0.0.0:36656" \ + --rpc.laddr="tcp://0.0.0.0:36657" \ + --address="tcp://0.0.0.0:36658" + volumeMounts: + - mountPath: /mytestnet/node/hmd/data + name: hmdir-validator- + + - name: hm-sentry + image: bluele/hypermint-testnet-sentry: + env: + - name: VALIDATORS + valueFrom: + configMapKeyRef: + name: hm-config + key: validators + command: + - bash + - "-c" + - | + set -ex + IFS=',' read -ra VALIDATORS_NUM <<< "$VALIDATORS" + peers=() + + for n in "${VALIDATORS_NUM[@]}"; do + nodeid=$(/hmd tendermint show-node-id --home /mytestnet/node$(expr ${n} + 4)/hmd) + peers+=("${nodeid}@hm-validator-${n}:26656") + done + + validatorid=$(/hmd tendermint show-node-id --home /mytestnet/node/hmd) + peers+=("${validatorid}@127.0.0.1:36656") + peers=$(IFS=','; echo "${peers[*]}") + + state_file=/mytestnet/node/hmd/data/priv_validator_state.json + if [ ! -e $state_file ]; then + cat << EOF > $state_file + { + "height": "0", + "round": "0", + "step": 0 + } + EOF + fi + TM_PARAMS="p2p.allow_duplicate_ip=true,p2p.addr_book_strict=false,p2p.private_peer_ids=${validatorid}" /hmd start --home=/mytestnet/node/hmd \ + --log_level="*:info" \ + --p2p.persistent_peers="$peers" \ + --p2p.laddr="tcp://0.0.0.0:26656" \ + --rpc.laddr="tcp://0.0.0.0:26657" \ + --address="tcp://0.0.0.0:26658" + ports: + - containerPort: 26656 + name: p2p + - containerPort: 26657 + name: rpc + volumeMounts: + - mountPath: /mytestnet/node/hmd/data + name: hmdir-sentry- + + volumeClaimTemplates: + - metadata: + name: hmdir-validator- + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + - metadata: + name: hmdir-sentry- + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi diff --git a/k8s/sentry-node/gen_testnet.sh b/k8s/sentry-node/gen_testnet.sh new file mode 100644 index 0000000..d3438f6 --- /dev/null +++ b/k8s/sentry-node/gen_testnet.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -eu +VERSION=$(cat ../../version) +VALNUM=$1 + +vals=() +nvals=() +for i in $(seq 0 $(expr $VALNUM - 1)); do + SENTRYNO=$(expr $i + $VALNUM) + sed -e "s//${VERSION}/g" -e "s//$i/g" -e "s//${SENTRYNO}/g" deployment.yaml.tpl > ./config/deployment${i}.yaml + echo "generate deployment$i.yaml" + vals+=(${i}) + nvals+=($SENTRYNO) +done +vals=$(IFS=','; echo "${vals[*]}") +nvals=$(IFS=','; echo "${nvals[*]}") +sed -e "s//${vals}/g" -e "s//${nvals}/g" config.yaml.tpl > ./config/config.yaml +echo "generate config.yaml"