Skip to content

Commit c66a300

Browse files
authored
Add environment variable support for external chaincode commands (#259)
- Introduced an `Env` field to the `createExternalChaincodeCmd` and `updateExternalChaincodeCmd` structs to allow users to specify environment variables for the chaincode. - Implemented `handleEnv` method to parse and set environment variables from the provided input. - Updated command flags to include `--env` option for both create and update commands. These changes enhance the flexibility of external chaincode configuration by allowing custom environment variables to be passed during creation and updates. Signed-off-by: dviejokfs <[email protected]>
1 parent 8799a58 commit c66a300

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

kubectl-hlf/cmd/externalchaincode/create.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
78
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
89
"github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
910
"github.com/spf13/cobra"
@@ -24,6 +25,7 @@ type createExternalChaincodeCmd struct {
2425
replicas int
2526
tlsRequired bool
2627
ImagePullSecrets []string
28+
Env []string
2729
}
2830

2931
func (c *createExternalChaincodeCmd) validate() error {
@@ -58,6 +60,7 @@ func (c *createExternalChaincodeCmd) validate() error {
5860
}
5961
return nil
6062
}
63+
6164
func (c *createExternalChaincodeCmd) run() error {
6265
oclient, err := helpers.GetKubeOperatorClient()
6366
if err != nil {
@@ -72,6 +75,7 @@ func (c *createExternalChaincodeCmd) run() error {
7275
ImagePullSecrets: []corev1.LocalObjectReference{},
7376
Credentials: nil,
7477
Replicas: c.replicas,
78+
Env: []corev1.EnvVar{},
7579
}
7680
if len(c.ImagePullSecrets) > 0 {
7781
imagePullSecrets := []corev1.LocalObjectReference{}
@@ -105,6 +109,13 @@ func (c *createExternalChaincodeCmd) run() error {
105109
Enrollsecret: c.enrollSecret,
106110
}
107111
}
112+
if len(c.Env) > 0 {
113+
env, err := c.handleEnv()
114+
if err != nil {
115+
return err
116+
}
117+
fabricChaincodeSpec.Env = env
118+
}
108119
fabricChaincode := &v1alpha1.FabricChaincode{
109120
ObjectMeta: v1.ObjectMeta{
110121
Name: c.name,
@@ -123,6 +134,22 @@ func (c *createExternalChaincodeCmd) run() error {
123134
fmt.Printf("Created external chaincode %s\n", fabricChaincode.Name)
124135
return nil
125136
}
137+
138+
func (c *createExternalChaincodeCmd) handleEnv() ([]corev1.EnvVar, error) {
139+
var env []corev1.EnvVar
140+
for _, literalSource := range c.Env {
141+
keyName, value, err := ParseEnv(literalSource)
142+
if err != nil {
143+
return nil, err
144+
}
145+
env = append(env, corev1.EnvVar{
146+
Name: keyName,
147+
Value: value,
148+
})
149+
}
150+
return env, nil
151+
}
152+
126153
func newExternalChaincodeCreateCmd() *cobra.Command {
127154
c := &createExternalChaincodeCmd{}
128155
cmd := &cobra.Command{
@@ -147,5 +174,6 @@ func newExternalChaincodeCreateCmd() *cobra.Command {
147174
f.IntVar(&c.replicas, "replicas", 1, "Replicas of the external chaincode")
148175
f.BoolVar(&c.tlsRequired, "tls-required", false, "Whether the chaincode requires TLS or not")
149176
f.StringArrayVarP(&c.ImagePullSecrets, "image-pull-secrets", "", []string{}, "Image Pull Secrets for the Chaincode Image")
177+
f.StringArrayVarP(&c.Env, "env", "", []string{}, "Environment variable for the Chaincode (key=value)")
150178
return cmd
151179
}

kubectl-hlf/cmd/externalchaincode/sync.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
"strings"
8+
79
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
810
"github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
911
"github.com/spf13/cobra"
1012
corev1 "k8s.io/api/core/v1"
1113
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"strings"
1314
)
1415

1516
type syncExternalChaincodeCmd struct {

kubectl-hlf/cmd/externalchaincode/update.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
"time"
8+
79
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
810
"github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
911
"github.com/spf13/cobra"
1012
corev1 "k8s.io/api/core/v1"
1113
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"time"
1314
)
1415

1516
type updateExternalChaincodeCmd struct {
@@ -27,6 +28,7 @@ type updateExternalChaincodeCmd struct {
2728
replicas int
2829

2930
tlsRequired bool
31+
Env []string
3032
}
3133

3234
func (c *updateExternalChaincodeCmd) validate() error {
@@ -82,6 +84,13 @@ func (c *updateExternalChaincodeCmd) run() error {
8284
fabricChaincode.Spec.PackageID = c.packageID
8385
fabricChaincode.Spec.ImagePullSecrets = []corev1.LocalObjectReference{}
8486
fabricChaincode.Spec.Replicas = c.replicas
87+
if len(c.Env) > 0 {
88+
env, err := c.handleEnv()
89+
if err != nil {
90+
return err
91+
}
92+
fabricChaincode.Spec.Env = env
93+
}
8594
if c.tlsRequired {
8695
fabricCA, err := oclient.HlfV1alpha1().FabricCAs(c.caNamespace).Get(ctx, c.caName, v1.GetOptions{})
8796
if err != nil {
@@ -118,6 +127,22 @@ func (c *updateExternalChaincodeCmd) run() error {
118127
fmt.Printf("Updated external chaincode %s\n", fabricChaincode.Name)
119128
return nil
120129
}
130+
131+
func (c *updateExternalChaincodeCmd) handleEnv() ([]corev1.EnvVar, error) {
132+
var env []corev1.EnvVar
133+
for _, literalSource := range c.Env {
134+
keyName, value, err := ParseEnv(literalSource)
135+
if err != nil {
136+
return nil, err
137+
}
138+
env = append(env, corev1.EnvVar{
139+
Name: keyName,
140+
Value: value,
141+
})
142+
}
143+
return env, nil
144+
}
145+
121146
func newExternalChaincodeUpdateCmd() *cobra.Command {
122147
c := &updateExternalChaincodeCmd{}
123148
cmd := &cobra.Command{
@@ -141,5 +166,6 @@ func newExternalChaincodeUpdateCmd() *cobra.Command {
141166
f.BoolVarP(&c.force, "force", "", false, "Force restart of chaincode")
142167
f.IntVar(&c.replicas, "replicas", 1, "Replicas of the chaincode")
143168
f.BoolVar(&c.tlsRequired, "tls-required", false, "Require TLS for chaincode")
169+
f.StringArrayVarP(&c.Env, "env", "", []string{}, "Environment variable for the Chaincode (key=value)")
144170
return cmd
145171
}

0 commit comments

Comments
 (0)