diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e53db26..9188899 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -10,6 +10,15 @@ "ghcr.io/devcontainers/features/python:1": {}, "ghcr.io/devcontainers-contrib/features/mkdocs:2": { "plugins": "mkdocs-material mike" + }, + "ghcr.io/devcontainers/features/docker-in-docker:2": { + "enableNonRootDocker": "true", + "moby": "true" + }, + "ghcr.io/devcontainers/features/kubectl-helm-minikube:1": { + "version": "latest", + "helm": "latest", + "minikube": "latest" } }, @@ -17,11 +26,22 @@ // "forwardPorts": [], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": ".devcontainer/postCreateCommand.sh" + "postCreateCommand": ".devcontainer/postCreateCommand.sh", + + // Use 'postStartCommand' to run commands after the container is created like starting minikube. + "postStartCommand": { + "minikube": "nohup bash -c 'minikube start &' > minikube.log 2>&1" + }, // Configure tool-specific properties. // "customizations": {}, // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root" + + "containerEnv": { + "FABRIC_K8S_BUILDER_DEBUG": "true", + "CORE_PEER_CHAINCODEADDRESS_HOST_OVERRIDE": "host.minikube.internal", + "CORE_PEER_CHAINCODELISTENADDRESS_HOST_OVERRIDE": "0.0.0.0" + } } diff --git a/.devcontainer/postCreateCommand.sh b/.devcontainer/postCreateCommand.sh index e20d813..cbe14c3 100755 --- a/.devcontainer/postCreateCommand.sh +++ b/.devcontainer/postCreateCommand.sh @@ -4,14 +4,21 @@ # set -eu -export GOOS=$(go env GOOS) -export GOARCH=$(go env GOARCH) +mkdir -p "${HOME}"/.local/bin + +export GOENV_OS=$(go env GOOS) +export GOENV_ARCH=$(go env GOARCH) +export UNAME_KERNAL=$(uname -s) + +# +# Install k9s +# +curl -sSL https://github.com/derailed/k9s/releases/download/v0.32.4/k9s_${UNAME_KERNAL}_${GOENV_ARCH}.tar.gz | tar -zxf - -C "${HOME}/.local/bin/" k9s && chmod +x "${HOME}/.local/bin/k9s" # # Install yq # -mkdir -p "${HOME}"/.local/bin -curl -sSLo "${HOME}"/.local/bin/yq https://github.com/mikefarah/yq/releases/download/v4.43.1/yq_${GOOS}_${GOARCH} && chmod +x "${HOME}"/.local/bin/yq +curl -sSLo "${HOME}/.local/bin/yq" https://github.com/mikefarah/yq/releases/download/v4.43.1/yq_${GOENV_OS}_${GOENV_ARCH} && chmod +x "${HOME}/.local/bin/yq" # # Install fabric binaries and the nano test network @@ -23,7 +30,7 @@ cd .fabric curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh ./install-fabric.sh binary -export FABRIC_SAMPLES_COMMIT=20009ecd029fa887a8a98122fa5df0ec5181cdb1 +export FABRIC_SAMPLES_COMMIT=0db64487e5e89a81d68e6871af3f0907c67e7d75 curl -sSL "https://github.com/hyperledger/fabric-samples/archive/${FABRIC_SAMPLES_COMMIT}.tar.gz" | tar -xzf - --strip-components=1 fabric-samples-${FABRIC_SAMPLES_COMMIT}/test-network-nano-bash cd .. @@ -32,9 +39,9 @@ cd .. # Add k8s builder config to fabric core.yaml # # To install the k8s builder use the following command: -# GOBIN="${PWD}"/.fabric/builders/k8s_builder go install ./cmd/... +# GOBIN="${PWD}"/.fabric/builders/k8s_builder/bin go install ./cmd/... # -export FABRIC_K8S_BUILDER_PATH="${PWD}"/.fabric/builders/k8s_builder -mkdir -p "${FABRIC_K8S_BUILDER_PATH}" +export FABRIC_K8S_BUILDER_PATH="${PWD}/.fabric/builders/k8s_builder" +mkdir -p "${FABRIC_K8S_BUILDER_PATH}/bin" -yq -i '.chaincode.externalBuilders += { "name": "k8s_builder", "path": "${FABRIC_K8S_BUILDER_PATH}" | envsubst(ne), "propagateEnvironment": [ "CORE_PEER_ID", "KUBECONFIG_PATH" ] }' .fabric/config/core.yaml +yq -i '.chaincode.externalBuilders += { "name": "k8s_builder", "path": "${FABRIC_K8S_BUILDER_PATH}" | envsubst(ne), "propagateEnvironment": [ "CORE_PEER_ID", "KUBECONFIG_PATH", "FABRIC_K8S_BUILDER_DEBUG" ] }' .fabric/config/core.yaml diff --git a/.gitignore b/.gitignore index 54e1952..eaf115d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ .idea/ .fabric + +*.log diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5e77f44..91ea521 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,6 +34,78 @@ We use [pull requests](http://help.github.com/pull-requests/) to deliver changes Please to try to be consistent with the rest of the code and conform to linting rules where they are provided. +## Development environment + +There is a [Visual Studio Code Dev Container](https://code.visualstudio.com/docs/devcontainers/containers) which should help develop and test the k8s builder in a consistent development environment. +It includes a preconfigured nano Fabric test network and minikube which can be used to run end to end tests. + +Build your latest k8s builder changes. + +``` +GOBIN="${PWD}"/.fabric/builders/k8s_builder/bin go install ./cmd/... +``` + +[Configure kubernetes](./docs/KUBERNETES_CONFIG.md) and export the kubeconfig path. + +``` +export KUBECONFIG_PATH="${HOME}/.kube/config" +``` + +Start the Fabric test network in the `.fabric/test-network-nano-bash` directory. + +``` +./network.sh start +``` + +In a new shell in the `.fabric/test-network-nano-bash` directory. + +```shell +curl -fsSL \ + https://github.com/hyperledger-labs/fabric-builder-k8s/releases/download/v0.7.2/go-contract-v0.7.2.tgz \ + -o go-contract-v0.7.2.tgz +``` + +Set up the environment for running peer commands and check everything is working. + +``` +. ./peer1admin.sh +peer channel list +``` + +Deploy the chaincode package as usual, starting by installing the k8s chaincode package. + +```shell +peer lifecycle chaincode install go-contract-v0.7.2.tgz +``` + +Export a `PACKAGE_ID` environment variable for use in the following commands. + +```shell +export PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid go-contract-v0.7.2.tgz) && echo $PACKAGE_ID +``` + +Note: the `PACKAGE_ID` must match the chaincode code package identifier shown by the `peer lifecycle chaincode install` command. + +Approve the chaincode. + +```shell +peer lifecycle chaincode approveformyorg -o 127.0.0.1:6050 --channelID mychannel --name sample-contract --version 1 --package-id $PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt +``` + +Commit the chaincode. + +```shell +peer lifecycle chaincode commit -o 127.0.0.1:6050 --channelID mychannel --name sample-contract --version 1 --sequence 1 --tls --cafile "${PWD}"/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt +``` + +Query the chaincode metadata! + +```shell +peer chaincode query -C mychannel -n sample-contract -c '{"Args":["org.hyperledger.fabric:GetMetadata"]}' +``` + +Debug chaincode!! + ## Code of Conduct Guidelines See our [Code of Conduct Guidelines](./CODE_OF_CONDUCT.md). diff --git a/docs/KUBERNETES_CONFIG.md b/docs/KUBERNETES_CONFIG.md index f4451fd..745b6dc 100644 --- a/docs/KUBERNETES_CONFIG.md +++ b/docs/KUBERNETES_CONFIG.md @@ -63,7 +63,7 @@ EOF 3. Check the permissions, e.g. ```shell -kubectl auth can-i patch secrets --namespace test-network --as system:serviceaccount:default:default +kubectl auth can-i patch secrets --namespace default --as system:serviceaccount:default:default ``` ## Chaincode requirements