From be2e27db11d2394111f4a573c01d3475fb8a665c Mon Sep 17 00:00:00 2001 From: Jared Watts Date: Thu, 20 Apr 2017 16:28:29 -0700 Subject: [PATCH] Ensure block images created by the operator's provisioner do not exceed 100 chars --- pkg/operator/provisioner.go | 24 +++++++++++++++++++++++- pkg/operator/provisioner_test.go | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/operator/provisioner.go b/pkg/operator/provisioner.go index 1c235bb58..5324c3418 100644 --- a/pkg/operator/provisioner.go +++ b/pkg/operator/provisioner.go @@ -29,6 +29,12 @@ import ( "k8s.io/client-go/pkg/api/v1" ) +const ( + imageNameMaxLen = 100 // image name should be under 100 chars to support kernels older than 4.7 + imageNamePrefix = "k8s-dynamic" + rbdIDPrefix = "rbd_id." +) + type rookVolumeProvisioner struct { clusterManager *clusterManager @@ -74,7 +80,7 @@ func (p *rookVolumeProvisioner) Provision(options controller.VolumeOptions) (*v1 capacity := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)] requestBytes := capacity.Value() - imageName := fmt.Sprintf("kubernetes-dynamic-%s-%s", options.PVName, uuid.NewUUID()) + imageName := createImageName(options.PVName) rookClient, err := p.clusterManager.getRookClient(p.provConfig.clusterNamespace) if err != nil { @@ -201,3 +207,19 @@ func processMonAddresses(monAddresses []string) []string { } return monAddrs } + +func createImageName(pvName string) string { + // generate a UUID for our image name + u := string(uuid.NewUUID()) + + // image name should be under 100 chars to support kernels older than 4.7 + // when the RBD kernel module converts the image name to an OID, it will use "rbd_id.", + // so we have to leave room for that "rbd_id." prefix too. + pvNameMaxLen := imageNameMaxLen - len(rbdIDPrefix) - len(imageNamePrefix) - len(u) - 2 // 2 hyphens + if len(pvName) > pvNameMaxLen { + // the PV name is too long, truncate it before including it in the final image name + pvName = pvName[:pvNameMaxLen] + } + + return fmt.Sprintf("%s-%s-%s", imageNamePrefix, pvName, u) +} diff --git a/pkg/operator/provisioner_test.go b/pkg/operator/provisioner_test.go index 21517acc0..982c59425 100644 --- a/pkg/operator/provisioner_test.go +++ b/pkg/operator/provisioner_test.go @@ -17,8 +17,11 @@ limitations under the License. package operator import ( + "fmt" "testing" + "strings" + "github.com/stretchr/testify/assert" ) @@ -75,3 +78,20 @@ func TestParseClassParametersInvalidOption(t *testing.T) { _, err := parseClassParameters(cfg) assert.EqualError(t, err, "invalid option \"foo\" for volume plugin rookVolumeProvisioner") } + +func TestCreateImageName(t *testing.T) { + // use a PV name that is typical, it should not be truncated because the resultant image name is not over max length + pvName := "pvc-023d0ff3-261d-11e7-aa63-001c42669caf" + imageName := createImageName(pvName) + assert.True(t, strings.HasPrefix(imageName, "k8s-dynamic-pvc-023d0ff3-261d-11e7-aa63-001c42669caf-")) + assert.Equal(t, 89, len(imageName)) + + // now try a PV name that is too long, it should be properly truncated + pvName = "pvc-0fd5988f-2516-11e7-aa55-02420ac00002-2d8-this-is-where-it-gets-too-long" + imageName = createImageName(pvName) + assert.True(t, strings.HasPrefix(imageName, "k8s-dynamic-pvc-0fd5988f-2516-11e7-aa55-02420ac00002-2d8-")) + + // length of our image name plus the rbd ID prefix that the RBD kernel module will add should be equal to the max allowed + assert.Equal(t, imageNameMaxLen, len(imageName)+len(rbdIDPrefix), + fmt.Sprintf("unexpected image name length: %d, image name: '%s'", len(imageName), imageName)) +}