Skip to content

Commit

Permalink
Fix k8s object names
Browse files Browse the repository at this point in the history
Generate a valid RFC 1035 label name from an MSP ID, peer ID, and
chaincode ID.

Closes #16

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Jan 27, 2023
1 parent 73afa85 commit 8cb9295
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/run/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ 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_ID in kubernetes pod chaincode/cc-mspid-core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789chai`))
).Should(gbytes.Say(`run \[\d+\]: Running chaincode ID CHAINCODE_ID in kubernetes pod chaincode/cc-m4eml38l6lv2ost7v1i3q6698a5p4gu1l3lnflb4boi7jnle6lt0`))

pipe := script.Exec(
"kubectl wait --for=condition=ready pod --timeout=120s --namespace=chaincode -l fabric-builder-k8s-peerid=core-peer-id-abcdefghijklmnopqrstuvwxyz-0123456789",
Expand Down
29 changes: 13 additions & 16 deletions internal/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ package util

import (
"context"
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"fmt"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -37,8 +38,6 @@ const (
TLSClientRootCertFile string = "/etc/hyperledger/fabric/peer.crt"
)

var mangledRegExp = regexp.MustCompile("[^a-zA-Z0-9-_.]")

func waitForPod(
ctx context.Context,
timeout time.Duration,
Expand Down Expand Up @@ -303,7 +302,7 @@ func getChaincodePodObject(
Name: "certs",
VolumeSource: apiv1.VolumeSource{
Secret: &apiv1.SecretVolumeSource{
SecretName: getSecretName(
SecretName: GetValidName(
chaincodeData.MspID,
peerID,
chaincodeData.ChaincodeID,
Expand All @@ -320,7 +319,7 @@ func getChaincodeSecretApplyConfiguration(
namespace, peerID string,
chaincodeData *ChaincodeJSON,
) *applycorev1.SecretApplyConfiguration {
name := getSecretName(chaincodeData.MspID, peerID, chaincodeData.ChaincodeID)
name := GetValidName(chaincodeData.MspID, peerID, chaincodeData.ChaincodeID)

annotations := map[string]string{
"fabric-builder-k8s-ccid": chaincodeData.ChaincodeID,
Expand Down Expand Up @@ -351,10 +350,6 @@ func getChaincodeSecretApplyConfiguration(
WithType(apiv1.SecretTypeOpaque)
}

func getPodName(mspID, peerID, chaincodeID string) string {
return mangleName("cc-" + mspID + "-" + peerID + chaincodeID)
}

func ApplyChaincodeSecrets(
ctx context.Context,
logger *log.CmdLogger,
Expand Down Expand Up @@ -443,7 +438,7 @@ func CreateChaincodePod(
chaincodeData *ChaincodeJSON,
imageData *ImageJSON,
) (*apiv1.Pod, error) {
podName := getPodName(chaincodeData.MspID, peerID, chaincodeData.ChaincodeID)
podName := GetValidName(chaincodeData.MspID, peerID, chaincodeData.ChaincodeID)
podDefinition := getChaincodePodObject(
imageData,
namespace,
Expand Down Expand Up @@ -492,11 +487,13 @@ func CreateChaincodePod(
return pod, nil
}

func getSecretName(mspID, peerID, chaincodeID string) string {
return mangleName("cc-" + mspID + "-" + peerID + chaincodeID)
}
// GetValidName returns a valid RFC 1035 label name.
func GetValidName(mspID, peerID, chaincodeID string) string {
qualifiedChaincodeID := mspID + ":" + peerID + ":" + chaincodeID
h := sha256.New()
h.Write([]byte(qualifiedChaincodeID))
sum := h.Sum(nil)
encodedChaincodeID := base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(sum)

func mangleName(name string) string {
// TODO need sensible unique naming scheme for deployments and secrets!
return strings.ToLower(mangledRegExp.ReplaceAllString(name, "-")[:63])
return "cc-" + strings.ToLower(encodedChaincodeID)
}
42 changes: 42 additions & 0 deletions internal/util/k8s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package util_test

import (
"github.com/hyperledger-labs/fabric-builder-k8s/internal/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("K8s", func() {
Describe("GetValidName", func() {
It("should return a string with a maximum of 63 characters", func() {
name := util.GetValidName("CongaCongaCongaCongaCongaCongaCongaCongaCongaCongaCongaCongaOrgMsp", "CongaCongaCongaCongaCongaCongaCongaCongaCongaCongaCongaCongaOrgPeer0", "fabfabfabfabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
Expect(len(name)).To(BeNumerically("<=", 63))
})

It("should return a string which starts with an alphabetic character", func() {
name := util.GetValidName("GreenCongaOrg", "GreenCongaOrgPeer0", "fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
Expect(name).To(MatchRegexp("^[a-z]"))
})

It("should return a string which ends with an alphanumeric character", func() {
name := util.GetValidName("BlueCongaOrg", "BlueCongaOrgPeer0", "fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
Expect(name).To(MatchRegexp("[a-z0-9]$"))
})

It("should return a string which only contains lowercase alphanumeric characters or '-'", func() {
name := util.GetValidName("BlueCongaOrg", "BlueCongaOrgPeer0", "fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
Expect(name).To(MatchRegexp("^(?:[a-z0-9]|-)+$"))
})

It("should return different names for different input", func() {
name1 := util.GetValidName("GreenCongaOrg", "GreenCongaOrgPeer0", "fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
name2 := util.GetValidName("BlueCongaOrg", "BlueCongaOrgPeer0", "fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
Expect(name1).NotTo(Equal(name2))
})

It("should return deterministic names", func() {
name := util.GetValidName("CongaOrg", "CongaOrgPeer0", "fabcar:cffa266294278404e5071cb91150d550dc0bf855149908a170b1169d6160004b")
Expect(name).To(Equal("cc-ocqvh9ir0mi0ef6urh12f3l0dar6csdmtjfhgbfvdp2d22u109r0"))
})
})
})
13 changes: 13 additions & 0 deletions internal/util/util_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestUtil(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Util Suite")
}

0 comments on commit 8cb9295

Please sign in to comment.