Skip to content

Commit

Permalink
Add minikube to devcontainer
Browse files Browse the repository at this point in the history
Simplify local development with instructions to use the Fabric nano
test network with minikube

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Apr 29, 2024
1 parent 60f50b5 commit 22cd0d3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
22 changes: 21 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,38 @@
"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"
}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "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"
}
}
25 changes: 16 additions & 9 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ..
Expand All @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
.idea/

.fabric

*.log
70 changes: 70 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,76 @@ 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.
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"]}'
```

## Code of Conduct Guidelines <a name="conduct"></a>

See our [Code of Conduct Guidelines](./CODE_OF_CONDUCT.md).
Expand Down
2 changes: 1 addition & 1 deletion docs/KUBERNETES_CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22cd0d3

Please sign in to comment.