Skip to content

Commit

Permalink
[WIP] Improve kubernetes object names
Browse files Browse the repository at this point in the history
Resolves #105

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Apr 22, 2024
1 parent fdc5486 commit 0b2171c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
4 changes: 4 additions & 0 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ func main() {
kubeServiceAccount := util.GetOptionalEnv(util.ChaincodeServiceAccountVariable, "default")
logger.Debugf("%s=%s", util.ChaincodeServiceAccountVariable, kubeServiceAccount)

kubeNamePrefix := util.GetOptionalEnv(util.ObjectNamePrefixVariable, "cc")
logger.Debugf("%s=%s", util.ObjectNamePrefixVariable, kubeNamePrefix)

run := &builder.Run{
BuildOutputDirectory: buildOutputDirectory,
RunMetadataDirectory: runMetadataDirectory,
PeerID: peerID,
KubeconfigPath: kubeconfigPath,
KubeNamespace: kubeNamespace,
KubeServiceAccount: kubeServiceAccount,
KubeNamePrefix: kubeNamePrefix,
}

if err := run.Run(ctx); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions internal/builder/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Run struct {
KubeconfigPath string
KubeNamespace string
KubeServiceAccount string
KubeNamePrefix string
}

func (r *Run) Run(ctx context.Context) error {
Expand All @@ -33,6 +34,10 @@ func (r *Run) Run(ctx context.Context) error {
return err
}

kubeObjectName := util.GetValidRfc1035LabelName(r.KubeNamePrefix, chaincodeData.MspID, r.PeerID, chaincodeData.ChaincodeID)

// TODO create a common set of labels here and pass them in where required?

clientset, err := util.GetKubeClientset(logger, r.KubeconfigPath)
if err != nil {
return fmt.Errorf(
Expand All @@ -48,6 +53,7 @@ func (r *Run) Run(ctx context.Context) error {
ctx,
logger,
secretsClient,
kubeObjectName,
r.KubeNamespace,
r.PeerID,
chaincodeData,
Expand All @@ -66,6 +72,7 @@ func (r *Run) Run(ctx context.Context) error {
ctx,
logger,
podsClient,
kubeObjectName,
r.KubeNamespace,
r.KubeServiceAccount,
r.PeerID,
Expand Down
1 change: 1 addition & 0 deletions internal/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
const (
builderVariablePrefix = "FABRIC_K8S_BUILDER_"
ChaincodeNamespaceVariable = builderVariablePrefix + "NAMESPACE"
ObjectNamePrefixVariable = builderVariablePrefix + "OBJECT_NAME_PREFIX"
ChaincodeServiceAccountVariable = builderVariablePrefix + "SERVICE_ACCOUNT"
DebugVariable = builderVariablePrefix + "DEBUG"
KubeconfigPathVariable = "KUBECONFIG_PATH"
Expand Down
9 changes: 9 additions & 0 deletions internal/util/fabric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

package util

// GetChaincodeLabel returns the label portion of a chaincode ID.
func GetChaincodeLabel(chaincode_id string) string {

Check warning on line 6 in internal/util/fabric.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; func parameter chaincode_id should be chaincodeID (revive)
// TODO split the label out of the chaincode ID
return ""
}
37 changes: 17 additions & 20 deletions internal/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func getChaincodePodObject(
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: namespace,
// TODO: pass in labels?
Labels: map[string]string{
"app.kubernetes.io/name": "fabric",
"app.kubernetes.io/component": "chaincode",
Expand Down Expand Up @@ -302,11 +303,7 @@ func getChaincodePodObject(
Name: "certs",
VolumeSource: apiv1.VolumeSource{
Secret: &apiv1.SecretVolumeSource{
SecretName: GetValidName(
chaincodeData.MspID,
peerID,
chaincodeData.ChaincodeID,
),
SecretName: podName,
},
},
},
Expand All @@ -316,11 +313,9 @@ func getChaincodePodObject(
}

func getChaincodeSecretApplyConfiguration(
namespace, peerID string,
secretName, namespace, peerID string,
chaincodeData *ChaincodeJSON,
) *applycorev1.SecretApplyConfiguration {
name := GetValidName(chaincodeData.MspID, peerID, chaincodeData.ChaincodeID)

annotations := map[string]string{
"fabric-builder-k8s-ccid": chaincodeData.ChaincodeID,
}
Expand All @@ -333,6 +328,7 @@ func getChaincodeSecretApplyConfiguration(
"client.key": base64.StdEncoding.EncodeToString([]byte(chaincodeData.ClientKey)),
}

// TODO: pass in labels?
labels := map[string]string{
"app.kubernetes.io/name": "fabric",
"app.kubernetes.io/component": "chaincode",
Expand All @@ -343,7 +339,7 @@ func getChaincodeSecretApplyConfiguration(
}

return applycorev1.
Secret(name, namespace).
Secret(secretName, namespace).
WithAnnotations(annotations).
WithLabels(labels).
WithStringData(data).
Expand All @@ -354,10 +350,10 @@ func ApplyChaincodeSecrets(
ctx context.Context,
logger *log.CmdLogger,
secretsClient v1.SecretInterface,
namespace, peerID string,
secretName, namespace, peerID string,
chaincodeData *ChaincodeJSON,
) error {
secret := getChaincodeSecretApplyConfiguration(namespace, peerID, chaincodeData)
secret := getChaincodeSecretApplyConfiguration(secretName, namespace, peerID, chaincodeData)

result, err := secretsClient.Apply(
ctx,
Expand Down Expand Up @@ -434,26 +430,25 @@ func CreateChaincodePod(
ctx context.Context,
logger *log.CmdLogger,
podsClient v1.PodInterface,
namespace, serviceAccount, peerID string,
objectName, namespace, serviceAccount, peerID string,
chaincodeData *ChaincodeJSON,
imageData *ImageJSON,
) (*apiv1.Pod, error) {
podName := GetValidName(chaincodeData.MspID, peerID, chaincodeData.ChaincodeID)
podDefinition := getChaincodePodObject(
imageData,
namespace,
serviceAccount,
podName,
objectName,
peerID,
chaincodeData,
)

err := deleteChaincodePod(ctx, logger, podsClient, podName, namespace, chaincodeData)
err := deleteChaincodePod(ctx, logger, podsClient, objectName, namespace, chaincodeData)
if err != nil {
return nil, fmt.Errorf(
"unable to delete existing chaincode pod %s/%s for chaincode ID %s: %w",
namespace,
podName,
objectName,
chaincodeData.ChaincodeID,
err,
)
Expand All @@ -463,15 +458,15 @@ func CreateChaincodePod(
"Creating chaincode pod for chaincode ID %s: %s/%s",
chaincodeData.ChaincodeID,
namespace,
podName,
objectName,
)

pod, err := podsClient.Create(ctx, podDefinition, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf(
"unable to create chaincode pod %s/%s for chaincode ID %s: %w",
namespace,
podName,
objectName,
chaincodeData.ChaincodeID,
err,
)
Expand All @@ -487,8 +482,10 @@ func CreateChaincodePod(
return pod, nil
}

// GetValidName returns a valid RFC 1035 label name.
func GetValidName(mspID, peerID, chaincodeID string) string {
// GetValidRfc1035LabelName returns a valid RFC 1035 label name.
func GetValidRfc1035LabelName(prefix, mspID, peerID, chaincodeID string) string {

Check warning on line 486 in internal/util/k8s.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'prefix' seems to be unused, consider removing or renaming it as _ (revive)
// TODO update name to be...
// <prefix>-<truncated_chaincode_label>-<chaincode_run_hash>
qualifiedChaincodeID := mspID + ":" + peerID + ":" + chaincodeID
h := sha256.New()
h.Write([]byte(qualifiedChaincodeID))
Expand Down

0 comments on commit 0b2171c

Please sign in to comment.