Skip to content

Commit

Permalink
[WIP] Add minikube to devcontainer
Browse files Browse the repository at this point in the history
Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Apr 26, 2024
1 parent 60f50b5 commit 4ffe2ba
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
18 changes: 17 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@
"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": "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"

"remoteEnv": {
"FABRIC_K8S_BUILDER_DEBUG": "true"
}
}
23 changes: 15 additions & 8 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 @@ -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
69 changes: 69 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,75 @@ 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 4ffe2ba

Please sign in to comment.