Skip to content

Commit

Permalink
Restart any existing chaincode pod
Browse files Browse the repository at this point in the history
If the chaincode pod already exists when the peer calls run, delete and recreate the pod so that the chaincode calls the peer again

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed May 27, 2022
1 parent d388ded commit 88074c7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
38 changes: 36 additions & 2 deletions docs/TEST_NETWORK_K8S.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,44 @@ export TEST_NETWORK_CHAINCODE_BUILDER="k8s"

network kind
network cluster init
network up
```

Note: the `fabric-builder-role` needs updating when specifying a `TEST_NETWORK_K8S_CHAINCODE_BUILDER_VERSION` later than `v0.4.0`.
Use `kubectl` to apply and check the required changes.

```
cat <<EOF | kubectl apply -f -
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: fabric-builder-role
namespace: test-network
rules:
- apiGroups:
- ""
- apps
resources:
- pods
- deployments
- configmaps
- secrets
verbs:
- get
- list
- watch
- create
- delete
- patch
EOF
kubectl auth can-i list pods --namespace test-network --as system:serviceaccount:test-network:default
kubectl auth can-i delete pods --namespace test-network --as system:serviceaccount:test-network:default
kubectl auth can-i patch secrets --namespace test-network --as system:serviceaccount:test-network:default
```

```shell
network up
network channel create
```

Expand Down Expand Up @@ -122,4 +156,4 @@ Reset the stage with:

```shell
network down && network up && network channel create
```
```
41 changes: 35 additions & 6 deletions internal/builder/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/hyperledgendary/fabric-builder-k8s/internal/util"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -80,17 +81,45 @@ func (r *Run) Run() error {

podsClient := clientset.CoreV1().Pods(r.KubeNamespace)

// TODO check if pod exists already, and delete/restart if it does

podName := util.GetPodName(chaincodeData.MspID, r.PeerID, chaincodeData.ChaincodeID)

pod := util.GetChaincodePodObject(imageData, r.KubeNamespace, podName, r.PeerID, chaincodeData)

p, err := podsClient.Create(context.TODO(), pod, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create chaincode pod: %w", err)
createAttempts := 0
for {
createAttempts += 1
p, err := podsClient.Create(context.TODO(), pod, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
if createAttempts > 3 {
// give up
return fmt.Errorf("unable to create chaincode pod %s/%s on final attempt: %w", r.KubeNamespace, podName, err)
}

err = podsClient.Delete(context.TODO(), podName, metav1.DeleteOptions{})
if err != nil {
if !errors.IsNotFound(err) {
fmt.Fprintf(os.Stderr, "Error deleting existing chaincode pod: %v", err)
}
}

_, err := util.WaitForPodTermination(context.TODO(), time.Minute, podsClient, podName, r.KubeNamespace)
if err != nil {
if !errors.IsNotFound(err) {
fmt.Fprintf(os.Stderr, "Error waiting for existing chaincode pod to terminate: %v", err)
}
}

// try again
continue
}

return fmt.Errorf("unable to create chaincode pod %s/%s: %w", r.KubeNamespace, podName, err)
}

fmt.Printf("Created chaincode pod: %s/%s\n", p.Namespace, p.Name)
break
}
fmt.Printf("Created pod %s\n", p.Name)

_, err = util.WaitForPodRunning(context.TODO(), time.Minute, podsClient, podName, r.KubeNamespace)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func GetChaincodePodObject(imageData ImageJson, namespace, podName, peerID strin
},
},
},
RestartPolicy: apiv1.RestartPolicyNever,
Volumes: []apiv1.Volume{
{
Name: "certs",
Expand Down

0 comments on commit 88074c7

Please sign in to comment.