Ensure block images created by the operator's provisioner do not exceed 100 chars
This commit is contained in:
1 parent
e3bdfb6c6a
commit
be2e27db11
2 files changed
+43
-1
No files matched your search
@@ -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.<imageName>",
|
||||
// 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)
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
Reference in new issue
Block a user