Skip to content

Commit

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

Signed-off-by: James Taylor <jamest@uk.ibm.com>
  • Loading branch information
jt-nti committed May 10, 2024
1 parent 3c6f7a0 commit 2fd1b46
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 302 deletions.
16 changes: 11 additions & 5 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.

```
```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
6 changes: 3 additions & 3 deletions cmd/run/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ 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:CHAINCODE_HASH in kubernetes pod chaincode/hlfcc-chaincodelabel-f15ukm9v906aq`))
).Should(gbytes.Say(`run \[\d+\]: Running chaincode ID CHAINCODE_LABEL:CHAINCODE_HASH in kubernetes pod chaincode/hlfcc-chaincodelabel-piihcaj6ryttc-[a-z0-9]{5}`))

pipe := script.Exec(
"kubectl wait --for=condition=ready pod --timeout=120s --namespace=chaincode -l fabric-builder-k8s-peerid=core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789",
"kubectl wait --for=condition=ready pod --timeout=120s --namespace=chaincode -l fabric-builder-k8s-cchash=N6MMJOZJIFDXCMJO3XI2QE7O6WB56IJBYI24I6LXSLYUDJDNJNCQ",
)
_, err = pipe.Stdout()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -109,7 +109,7 @@ var _ = Describe("Main", func() {
"pod",
"--namespace=chaincode",
"-l",
"fabric-builder-k8s-peerid=core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789",
"fabric-builder-k8s-cchash=N6MMJOZJIFDXCMJO3XI2QE7O6WB56IJBYI24I6LXSLYUDJDNJNCQ",
}
descCommand := exec.Command("kubectl", descArgs...)
descSession, err := gexec.Start(descCommand, GinkgoWriter, GinkgoWriter)
Expand Down
2 changes: 1 addition & 1 deletion cmd/run/testdata/validchaincode/chaincode.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"chaincode_id": "CHAINCODE_LABEL:CHAINCODE_HASH",
"chaincode_id": "CHAINCODE_LABEL:6f98c4bb29414771312eddd1a813eef583df2121c235c4797792f141a46d4b45",
"peer_address": "PEER_ADDRESS",
"client_cert": "CLIENT_CERT",
"client_key": "CLIENT_KEY",
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 2fd1b46

Please sign in to comment.