Skip to content

Commit

Permalink
Run chaincode using jobs
Browse files Browse the repository at this point in the history
See #119

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed May 24, 2024
1 parent 897246c commit a4e50ad
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 288 deletions.
18 changes: 12 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ We use [pull requests](http://help.github.com/pull-requests/) to deliver changes
1. [Fork the repository](https://guides.github.com/activities/forking/#fork) and create a new branch from `main`.
2. If you've added code that should be tested, add tests!
3. If you've added any new features or made breaking changes, update the documentation.
4. Ensure all the tests pass.
4. Ensure all the tests pass, using `go test -v ./...`.
5. Include a descriptive message, and the [Developer Certificate of Origin (DCO) sign-off](https://github.com/probot/dco#how-it-works) on all commit messages.
6. [Issue a pull request](https://guides.github.com/activities/forking/#making-a-pull-request)!
7. [GitHub Actions](https://github.com/hyperledger-labs/fabric-builder-k8s/actions) builds must succeed before the pull request can be reviewed and merged.
Expand All @@ -34,26 +34,32 @@ 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.

To run the linter, use the following command.

```shell
golangci-lint run ./...
```

## 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.

```
```shell
GOBIN="${PWD}"/.fabric/builders/k8s_builder/bin go install ./cmd/...
```

[Configure kubernetes](./docs/KUBERNETES_CONFIG.md) and export the kubeconfig path.
[Configure kubernetes permissions](https://labs.hyperledger.org/fabric-builder-k8s/configuring/kubernetes-permissions/) and export the kubeconfig path.

```
```shell
export KUBECONFIG_PATH="${HOME}/.kube/config"
```

Start the Fabric test network in the `.fabric/test-network-nano-bash` directory.

```
```shell
./network.sh start
```

Expand All @@ -67,7 +73,7 @@ curl -fsSL \

Set up the environment for running peer commands and check everything is working.

```
```shell
. ./peer1admin.sh
peer channel list
```
Expand Down
29 changes: 17 additions & 12 deletions cmd/run/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"
"os/exec"
"time"

"github.com/bitfield/script"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
Expand Down Expand Up @@ -68,7 +68,7 @@ var _ = Describe("Main", func() {
)

It(
"should start a chaincode pod using the supplied configuration environment variables",
"should start a chaincode job using the supplied configuration environment variables",
Label("kind"),
func() {
homedir, err := os.UserHomeDir()
Expand All @@ -95,18 +95,25 @@ var _ = Describe("Main", func() {
).Should(gbytes.Say(`run \[\d+\] DEBUG: FABRIC_K8S_BUILDER_SERVICE_ACCOUNT=chaincode`))
Eventually(
session.Err,
).Should(gbytes.Say(`run \[\d+\]: Running chaincode ID CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45 in kubernetes pod chaincode/hlfcc-chaincodelabel-f887209uhojj2`))
).Should(gbytes.Say(`run \[\d+\]: Running chaincode ID CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45 with kubernetes job chaincode/hlfcc-chaincodelabel-piihcaj6ryttc`))

pipe := script.Exec(
"kubectl wait --for=condition=ready pod --timeout=120s --namespace=chaincode -l fabric-builder-k8s-cclabel=CHAINCODE_LABEL",
)
_, err = pipe.Stdout()
waitArgs := []string{
"wait",
"--for=jsonpath=.status.ready=1",
"job",
"--timeout=120s",
"--namespace=chaincode",
"-l",
"fabric-builder-k8s-cclabel=CHAINCODE_LABEL",
}
waitCommand := exec.Command("kubectl", waitArgs...)
waitSession, err := gexec.Start(waitCommand, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Expect(pipe.ExitStatus()).To(Equal(0))
Eventually(waitSession).WithTimeout(240 * time.Second).Should(gexec.Exit(0))

descArgs := []string{
"describe",
"pod",
"job",
"--namespace=chaincode",
"-l",
"fabric-builder-k8s-cclabel=CHAINCODE_LABEL",
Expand All @@ -123,9 +130,7 @@ var _ = Describe("Main", func() {
Eventually(descSession.Out).Should(gbytes.Say(`fabric-builder-k8s-mspid:\s+MSPID`))
Eventually(descSession.Out).Should(gbytes.Say(`fabric-builder-k8s-peeraddress:\s+PEER_ADDRESS`))
Eventually(descSession.Out).Should(gbytes.Say(`fabric-builder-k8s-peerid:\s+core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789`))
Eventually(
descSession.Out,
).Should(gbytes.Say(`CORE_CHAINCODE_ID_NAME:\s+CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45`))
Eventually(descSession.Out).Should(gbytes.Say(`CORE_CHAINCODE_ID_NAME:\s+CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45`))
Eventually(descSession.Out).Should(gbytes.Say(`CORE_PEER_ADDRESS:\s+PEER_ADDRESS`))
Eventually(descSession.Out).Should(gbytes.Say(`CORE_PEER_LOCALMSPID:\s+MSPID`))
},
Expand Down
18 changes: 10 additions & 8 deletions internal/builder/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *Run) Run(ctx context.Context) error {
return err
}

kubeObjectName := util.GetValidRfc1035LabelName(r.KubeNamePrefix, r.PeerID, chaincodeData)
kubeObjectName := util.GetValidRfc1035LabelName(r.KubeNamePrefix, r.PeerID, chaincodeData, util.ObjectNameSuffixLength+1)

clientset, err := util.GetKubeClientset(logger, r.KubeconfigPath)
if err != nil {
Expand Down Expand Up @@ -64,12 +64,12 @@ func (r *Run) Run(ctx context.Context) error {
)
}

podsClient := clientset.CoreV1().Pods(r.KubeNamespace)
jobsClient := clientset.BatchV1().Jobs(r.KubeNamespace)

pod, err := util.CreateChaincodePod(
job, err := util.CreateChaincodeJob(
ctx,
logger,
podsClient,
jobsClient,
kubeObjectName,
r.KubeNamespace,
r.KubeServiceAccount,
Expand All @@ -82,11 +82,13 @@ func (r *Run) Run(ctx context.Context) error {
}

logger.Printf(
"Running chaincode ID %s in kubernetes pod %s/%s",
"Running chaincode ID %s with kubernetes job %s/%s",
chaincodeData.ChaincodeID,
pod.Namespace,
pod.Name,
job.Namespace,
job.Name,
)

return util.WaitForChaincodePod(ctx, logger, podsClient, pod, chaincodeData.ChaincodeID)
batchClient := clientset.BatchV1().RESTClient()

return util.WaitForChaincodeJob(ctx, logger, batchClient, job, chaincodeData.ChaincodeID)
}
Loading

0 comments on commit a4e50ad

Please sign in to comment.