diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..fb66949 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,26 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/python +{ + "name": "Fabric k8s builder", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye", + + // Features to add to the dev container. More info: https://containers.dev/features. + "features": { + "ghcr.io/devcontainers-contrib/features/mkdocs:2": { + "plugins": "mkdocs-material mike" + } + } + + // 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": "pip3 install --user -r requirements.txt", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f33a02c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for more information: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates +# https://containers.dev/guide/dependabot + +version: 2 +updates: + - package-ecosystem: "devcontainers" + directory: "/" + schedule: + interval: weekly diff --git a/README.md b/README.md index 50e8709..9513923 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,6 @@ Kubernetes [external chaincode builder](https://hyperledger-fabric.readthedocs.io/en/latest/cc_launcher.html) for Hyperledger Fabric. -With the k8s builder, the Fabric administrator is responsible for [preparing a chaincode image](#chaincode-docker-image), publishing to a container registry, and [preparing a chaincode package](#chaincode-package) with coordinates of the contract's immutable image digest. -When Fabric detects the installation of a `type=k8s` contract, the builder assumes full ownership of the lifecycle of pods, containers, and network linkages necessary to communicate securely with the peer. - - Advantages: 🚀 Chaincode runs _immediately_ on channel commit. @@ -19,160 +15,3 @@ Advantages: 🛡️ Pre-published chaincode images remove any and all dependencies on a root-level _docker daemon_. 🕵️ Pre-published chaincode images provide traceability and change management features (e.g. Git commit hash as image tag) - -The aim is for the builder to work as closely as possible with the existing [Fabric chaincode lifecycle](https://hyperledger-fabric.readthedocs.io/en/latest/chaincode_lifecycle.html), making sensible compromises for deploying chaincode on Kubernetes within those constraints. -(The assumption being that there are more people with Kubernetes skills than are familiar with the inner workings of Fabric!) - -For example: - -- The contents of the chaincode package must uniquely identify the chaincode functions executed on the ledger. - - In the case of the k8s builder the chaincode source code is not actually inside the package. In order not to break the Fabric chaincode lifecycle, the chaincode image must be specified using an immutable `@digest`, not a `:label` which can be altered post commit. - - See [Pull an image by digest (immutable identifier)](https://docs.docker.com/engine/reference/commandline/pull/#pull-an-image-by-digest-immutable-identifier) for more details. - - -- The Fabric peer manages the chaincode process, not Kubernetes. - - Running the chaincode in server mode, i.e. allowing the peer to initiate the gRPC connection, would make it possible to leave Kubernetes to manage the chaincode process by creating a chaincode deployment. - - Unfortunately due to limitations in Fabric's builder and launcher implementation, that is not possible and the peer expects to control the chaincode process. - - -**Status:** the k8s builder is [close to a version 1 release](https://github.com/hyperledger-labs/fabric-builder-k8s/milestone/1) and has been tested in a number of Kubernetes environments, deployment platforms, and provides semantic-revision aware [release tags](https://github.com/hyperledger-labs/fabric-builder-k8s/tags) for the external builder binaries. -The current status should be considered as STABLE and any bugs or enhancements delivered as GitHub Issues in conjunction with community PRs. - -## Usage - -The k8s builder can be run in cluster using the `KUBERNETES_SERVICE_HOST` and `KUBERNETES_SERVICE_PORT` environment variables, or it can connect using a `KUBECONFIG_PATH` environment variable. - -The following optional environment variables can be used to configure the k8s builder: - -- `FABRIC_K8S_BUILDER_DEBUG` whether to enable additional logging -- `FABRIC_K8S_BUILDER_NAMESPACE` specifies the namespace to deploy chaincode to -- `FABRIC_K8S_BUILDER_SERVICE_ACCOUNT` specifies the service account for the chaincode pod - -A `CORE_PEER_ID` environment variable is also currently required. - -External builders are configured in the `core.yaml` file, for example: - -``` - externalBuilders: - - name: k8s_builder - path: /opt/hyperledger/k8s_builder - propagateEnvironment: - - CORE_PEER_ID - - FABRIC_K8S_BUILDER_DEBUG - - FABRIC_K8S_BUILDER_NAMESPACE - - FABRIC_K8S_BUILDER_SERVICE_ACCOUNT - - KUBERNETES_SERVICE_HOST - - KUBERNETES_SERVICE_PORT -``` - -See [External Builders and Launchers](https://hyperledger-fabric.readthedocs.io/en/latest/cc_launcher.html) for details of Hyperledger Fabric builders. - -As well as configuring Fabric to use the k8s builder, you will need to [configure Kubernetes](docs/KUBERNETES_CONFIG.md) to allow the builder to start chaincode pods successfully. - -There are addition docs with more detailed usage instructions for specific Fabric network deployments: - -- [Kubernetes Test Network](docs/TEST_NETWORK_K8S.md) -- [Nano Test Network](docs/TEST_NETWORK_NANO.md) -- [Fabric Operator](docs/FABRIC_OPERATOR.md) -- [HLF Operator](docs/HLF_OPERATOR.md) - -## Chaincode Docker image - -Unlike the traditional chaincode language support for Go, Java, and Node.js, the k8s builder *does not* build a chaincode Docker image using Docker-in-Docker. -Instead, a chaincode Docker image must be built and published before it can be used with the k8s builder. - -The chaincode will have access to the following environment variables: - -- CORE_CHAINCODE_ID_NAME -- CORE_PEER_ADDRESS -- CORE_PEER_TLS_ENABLED -- CORE_PEER_TLS_ROOTCERT_FILE -- CORE_TLS_CLIENT_KEY_PATH -- CORE_TLS_CLIENT_CERT_PATH -- CORE_TLS_CLIENT_KEY_FILE -- CORE_TLS_CLIENT_CERT_FILE -- CORE_PEER_LOCALMSPID - -See the [sample contracts for Go, Java, and Node.js](samples/README.md) for basic docker images which will work with the k8s builder. - -## Chaincode package - -The k8s chaincode package file, which is installed by the `peer lifecycle chaincode install` command, must contain the Docker image name and digest of the chaincode being deployed. - -[Fabric chaincode packages](https://hyperledger-fabric.readthedocs.io/en/latest/cc_launcher.html#chaincode-packages) are `.tgz` files which contain two files: - -- metadata.json - the chaincode label and type -- code.tar.gz - source artifacts for the chaincode - -To create a k8s chaincode package file, start by creating an `image.json` file. -For example, - -```shell -cat << IMAGEJSON-EOF > image.json -{ - "name": "ghcr.io/hyperledger-labs/go-contract", - "digest": "sha256:802c336235cc1e7347e2da36c73fa2e4b6437cfc6f52872674d1e23f23bba63b" -} -IMAGEJSON-EOF -``` - -Note: the k8s chaincode package file uses digests because these are immutable, unlike tags. -The docker inspect command can be used to find the digest if required. - -``` -docker pull ghcr.io/hyperledger-labs/go-contract:v0.7.2 -docker inspect --format='{{index .RepoDigests 0}}' ghcr.io/hyperledger-labs/go-contract:v0.7.2 | cut -d'@' -f2 -``` - -Create a `code.tar.gz` archive containing the `image.json` file. - -```shell -tar -czf code.tar.gz image.json -``` - -Create a `metadata.json` file for the chaincode package. -For example, - -```shell -cat << METADATAJSON-EOF > metadata.json -{ - "type": "k8s", - "label": "go-contract" -} -METADATAJSON-EOF -``` - -Create the final chaincode package archive. - -```shell -tar -czf go-contract.tgz metadata.json code.tar.gz -``` - -Ideally the chaincode package should be created in the same CI/CD pipeline which builds the docker image. -There is an example [package-k8s-chaincode-action](https://github.com/hyperledgendary/package-k8s-chaincode-action) GitHub Action which can create the required k8s chaincode package. - -The GitHub Action repository includes a basic shell script which can also be used for automating the process above outside GitHub workflows. -For example, to create a basic k8s chaincode package using the `pkgk8scc.sh` helper script. - -```shell -curl -fsSL https://raw.githubusercontent.com/hyperledgendary/package-k8s-chaincode-action/main/pkgk8scc.sh -o pkgk8scc.sh && chmod u+x pkgk8scc.sh -./pkgk8scc.sh -l go-contract -n ghcr.io/hyperledger-labs/go-contract -d sha256:802c336235cc1e7347e2da36c73fa2e4b6437cfc6f52872674d1e23f23bba63b -``` - -## Chaincode deploy - -Deploy the chaincode package as usual, starting by installing the k8s chaincode package. - -```shell -peer lifecycle chaincode install go-contract.tgz -``` - -You can also use the `peer` command to get the chaincode package ID. - -```shell -export PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid go-contract.tgz) && echo $PACKAGE_ID -``` diff --git a/docs/assets/project-icon.png b/docs/assets/project-icon.png new file mode 100644 index 0000000..7266b04 Binary files /dev/null and b/docs/assets/project-icon.png differ diff --git a/docs/assets/project-logo.png b/docs/assets/project-logo.png new file mode 100644 index 0000000..3075f9a Binary files /dev/null and b/docs/assets/project-logo.png differ diff --git a/docs/concepts/chaincode-builder.md b/docs/concepts/chaincode-builder.md new file mode 100644 index 0000000..c466f30 --- /dev/null +++ b/docs/concepts/chaincode-builder.md @@ -0,0 +1,30 @@ +## Usage + +The k8s builder can be run in cluster using the `KUBERNETES_SERVICE_HOST` and `KUBERNETES_SERVICE_PORT` environment variables, or it can connect using a `KUBECONFIG_PATH` environment variable. + +The following optional environment variables can be used to configure the k8s builder: + +- `FABRIC_K8S_BUILDER_DEBUG` whether to enable additional logging +- `FABRIC_K8S_BUILDER_NAMESPACE` specifies the namespace to deploy chaincode to +- `FABRIC_K8S_BUILDER_SERVICE_ACCOUNT` specifies the service account for the chaincode pod + +A `CORE_PEER_ID` environment variable is also currently required. + +External builders are configured in the `core.yaml` file, for example: + +``` + externalBuilders: + - name: k8s_builder + path: /opt/hyperledger/k8s_builder + propagateEnvironment: + - CORE_PEER_ID + - FABRIC_K8S_BUILDER_DEBUG + - FABRIC_K8S_BUILDER_NAMESPACE + - FABRIC_K8S_BUILDER_SERVICE_ACCOUNT + - KUBERNETES_SERVICE_HOST + - KUBERNETES_SERVICE_PORT +``` + +See [External Builders and Launchers](https://hyperledger-fabric.readthedocs.io/en/latest/cc_launcher.html) for details of Hyperledger Fabric builders. + +As well as configuring Fabric to use the k8s builder, you will need to [configure Kubernetes](docs/KUBERNETES_CONFIG.md) to allow the builder to start chaincode pods successfully. \ No newline at end of file diff --git a/docs/concepts/chaincode-image.md b/docs/concepts/chaincode-image.md new file mode 100644 index 0000000..b235059 --- /dev/null +++ b/docs/concepts/chaincode-image.md @@ -0,0 +1,18 @@ +## Chaincode Docker image + +Unlike the traditional chaincode language support for Go, Java, and Node.js, the k8s builder *does not* build a chaincode Docker image using Docker-in-Docker. +Instead, a chaincode Docker image must be built and published before it can be used with the k8s builder. + +The chaincode will have access to the following environment variables: + +- CORE_CHAINCODE_ID_NAME +- CORE_PEER_ADDRESS +- CORE_PEER_TLS_ENABLED +- CORE_PEER_TLS_ROOTCERT_FILE +- CORE_TLS_CLIENT_KEY_PATH +- CORE_TLS_CLIENT_CERT_PATH +- CORE_TLS_CLIENT_KEY_FILE +- CORE_TLS_CLIENT_CERT_FILE +- CORE_PEER_LOCALMSPID + +See the [sample contracts for Go, Java, and Node.js](samples/README.md) for basic docker images which will work with the k8s builder. diff --git a/docs/concepts/chaincode-package.md b/docs/concepts/chaincode-package.md new file mode 100644 index 0000000..451cf0d --- /dev/null +++ b/docs/concepts/chaincode-package.md @@ -0,0 +1,63 @@ +## Chaincode package + +The k8s chaincode package file, which is installed by the `peer lifecycle chaincode install` command, must contain the Docker image name and digest of the chaincode being deployed. + +[Fabric chaincode packages](https://hyperledger-fabric.readthedocs.io/en/latest/cc_launcher.html#chaincode-packages) are `.tgz` files which contain two files: + +- metadata.json - the chaincode label and type +- code.tar.gz - source artifacts for the chaincode + +To create a k8s chaincode package file, start by creating an `image.json` file. +For example, + +```shell +cat << IMAGEJSON-EOF > image.json +{ + "name": "ghcr.io/hyperledger-labs/go-contract", + "digest": "sha256:802c336235cc1e7347e2da36c73fa2e4b6437cfc6f52872674d1e23f23bba63b" +} +IMAGEJSON-EOF +``` + +Note: the k8s chaincode package file uses digests because these are immutable, unlike tags. +The docker inspect command can be used to find the digest if required. + +``` +docker pull ghcr.io/hyperledger-labs/go-contract:v0.7.2 +docker inspect --format='{{index .RepoDigests 0}}' ghcr.io/hyperledger-labs/go-contract:v0.7.2 | cut -d'@' -f2 +``` + +Create a `code.tar.gz` archive containing the `image.json` file. + +```shell +tar -czf code.tar.gz image.json +``` + +Create a `metadata.json` file for the chaincode package. +For example, + +```shell +cat << METADATAJSON-EOF > metadata.json +{ + "type": "k8s", + "label": "go-contract" +} +METADATAJSON-EOF +``` + +Create the final chaincode package archive. + +```shell +tar -czf go-contract.tgz metadata.json code.tar.gz +``` + +Ideally the chaincode package should be created in the same CI/CD pipeline which builds the docker image. +There is an example [package-k8s-chaincode-action](https://github.com/hyperledgendary/package-k8s-chaincode-action) GitHub Action which can create the required k8s chaincode package. + +The GitHub Action repository includes a basic shell script which can also be used for automating the process above outside GitHub workflows. +For example, to create a basic k8s chaincode package using the `pkgk8scc.sh` helper script. + +```shell +curl -fsSL https://raw.githubusercontent.com/hyperledgendary/package-k8s-chaincode-action/main/pkgk8scc.sh -o pkgk8scc.sh && chmod u+x pkgk8scc.sh +./pkgk8scc.sh -l go-contract -n ghcr.io/hyperledger-labs/go-contract -d sha256:802c336235cc1e7347e2da36c73fa2e4b6437cfc6f52872674d1e23f23bba63b +``` diff --git a/docs/faqs.md b/docs/faqs.md new file mode 100644 index 0000000..e04ec41 --- /dev/null +++ b/docs/faqs.md @@ -0,0 +1,11 @@ +# Frequently Asked Questions + +## Chaincode + +### Are private chaincode images supported? + +Yes. [Link to details tbc] + +### Will chaincode work in multi-architecture Fabric networks? + +Yes. [Link to details tbc] diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..2775935 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,69 @@ +# Introduction + +Kubernetes [external chaincode builder](https://hyperledger-fabric.readthedocs.io/en/latest/cc_launcher.html) for Hyperledger Fabric. + +With the k8s builder, the Fabric administrator is responsible for [preparing a chaincode image](#chaincode-docker-image), publishing to a container registry, and [preparing a chaincode package](#chaincode-package) with coordinates of the contract's immutable image digest. +When Fabric detects the installation of a `type=k8s` contract, the builder assumes full ownership of the lifecycle of pods, containers, and network linkages necessary to communicate securely with the peer. + + +Advantages: + +🚀 Chaincode runs _immediately_ on channel commit. + +✨ Avoids the complexity and administrative burdens associated with Chaincode-as-a-Service. + +🔥 Pre-published chaincode images avoid code-compilation errors at deployment time. + +🏗️ Pre-published chaincode images encourage modern, industry accepted CI/CD best practices. + +🛡️ Pre-published chaincode images remove any and all dependencies on a root-level _docker daemon_. + +🕵️ Pre-published chaincode images provide traceability and change management features (e.g. Git commit hash as image tag) + +The aim is for the builder to work as closely as possible with the existing [Fabric chaincode lifecycle](https://hyperledger-fabric.readthedocs.io/en/latest/chaincode_lifecycle.html), making sensible compromises for deploying chaincode on Kubernetes within those constraints. +(The assumption being that there are more people with Kubernetes skills than are familiar with the inner workings of Fabric!) + +For example: + +- The contents of the chaincode package must uniquely identify the chaincode functions executed on the ledger. + + In the case of the k8s builder the chaincode source code is not actually inside the package. In order not to break the Fabric chaincode lifecycle, the chaincode image must be specified using an immutable `@digest`, not a `:label` which can be altered post commit. + + See [Pull an image by digest (immutable identifier)](https://docs.docker.com/engine/reference/commandline/pull/#pull-an-image-by-digest-immutable-identifier) for more details. + + +- The Fabric peer manages the chaincode process, not Kubernetes. + + Running the chaincode in server mode, i.e. allowing the peer to initiate the gRPC connection, would make it possible to leave Kubernetes to manage the chaincode process by creating a chaincode deployment. + + Unfortunately due to limitations in Fabric's builder and launcher implementation, that is not possible and the peer expects to control the chaincode process. + + +**Status:** the k8s builder is [close to a version 1 release](https://github.com/hyperledger-labs/fabric-builder-k8s/milestone/1) and has been tested in a number of Kubernetes environments, deployment platforms, and provides semantic-revision aware [release tags](https://github.com/hyperledger-labs/fabric-builder-k8s/tags) for the external builder binaries. +The current status should be considered as STABLE and any bugs or enhancements delivered as GitHub Issues in conjunction with community PRs. + + + +There are addition docs with more detailed usage instructions for specific Fabric network deployments: + +- [Kubernetes Test Network](tutorials/test-network-k8s.md) +- [Nano Test Network](tutorials/test-network-nano.md) +- [Fabric Operator](tutorials/fabric-operator.md) +- [HLF Operator](tutorials/hlf-operator.md) + + + + +## Chaincode deploy + +Deploy the chaincode package as usual, starting by installing the k8s chaincode package. + +```shell +peer lifecycle chaincode install go-contract.tgz +``` + +You can also use the `peer` command to get the chaincode package ID. + +```shell +export PACKAGE_ID=$(peer lifecycle chaincode calculatepackageid go-contract.tgz) && echo $PACKAGE_ID +``` diff --git a/docs/FABRIC_OPERATOR.md b/docs/tutorials/fabric-operator.md similarity index 100% rename from docs/FABRIC_OPERATOR.md rename to docs/tutorials/fabric-operator.md diff --git a/docs/HLF_OPERATOR.md b/docs/tutorials/hlf-operator.md similarity index 100% rename from docs/HLF_OPERATOR.md rename to docs/tutorials/hlf-operator.md diff --git a/docs/TEST_NETWORK_K8S.md b/docs/tutorials/test-network-k8s.md similarity index 100% rename from docs/TEST_NETWORK_K8S.md rename to docs/tutorials/test-network-k8s.md diff --git a/docs/TEST_NETWORK_NANO.md b/docs/tutorials/test-network-nano.md similarity index 100% rename from docs/TEST_NETWORK_NANO.md rename to docs/tutorials/test-network-nano.md diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..236db25 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,90 @@ +site_name: Hyperledger Fabric Chaincode Builder for Kubernetes +repo_name: hyperledger-labs/fabric-builder-k8s +repo_url: https://github.com/hyperledger-labs/fabric-builder-k8s +theme: + name: material + logo: assets/project-logo.png + favicon: assets/project-icon.png + icon: + repo: fontawesome/brands/github + palette: + # Palette toggle for light mode + - media: "(prefers-color-scheme: light)" + scheme: default + toggle: + icon: material/brightness-7 + name: Switch to dark mode + # Palette toggle for dark mode + - media: "(prefers-color-scheme: dark)" + scheme: slate + toggle: + icon: material/brightness-4 + name: Switch to light mode + features: + - content.code.copy + - navigation.expand + - navigation.footer + - navigation.instant + - navigation.tabs + - navigation.tabs.sticky + - navigation.top + - navigation.tracking + - toc.follow + - toc.integrate +markdown_extensions: + - abbr + - admonition + - attr_list + - def_list + - footnotes + - md_in_html + - toc: + permalink: true + toc_depth: 3 + - pymdownx.arithmatex: + generic: true + - pymdownx.betterem: + smart_enable: all + - pymdownx.caret + - pymdownx.details + - pymdownx.emoji: + emoji_generator: !!python/name:materialx.emoji.to_svg + emoji_index: !!python/name:materialx.emoji.twemoji + - pymdownx.highlight: + anchor_linenums: true + - pymdownx.inlinehilite + - pymdownx.keys + - pymdownx.magiclink: + repo_url_shorthand: true + user: squidfunk + repo: mkdocs-material + - pymdownx.mark + - pymdownx.smartsymbols + - pymdownx.superfences: + custom_fences: + - name: mermaid + class: mermaid + format: !!python/name:pymdownx.superfences.fence_code_format + - pymdownx.tabbed: + alternate_style: true + - pymdownx.tasklist: + custom_checkbox: true + - pymdownx.tilde +plugins: + - search + - mike +extra: + version: + provider: mike +nav: + - Introduction: index.md + - Concepts: + - Chaincode builder: concepts/chaincode-builder.md + - Chaincode image: concepts/chaincode-image.md + - Chaincode package: concepts/chaincode-package.md + - Tutorials: + - Kubernetes Test Network: tutorials/test-network-k8s.md + - Nano Test Network: tutorials/test-network-nano.md + - Fabric Operator: tutorials/fabric-operator.md + - HLF Operator: tutorials/hlf-operator.md + - FAQs: faqs.md diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..04ebb9b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +mkdocs-material +mike