From 3f42db5ce09ac6610a92132df9a6b33c8706976b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Wed, 19 Aug 2020 16:51:27 +0200 Subject: [PATCH 1/4] ceph: support osd deployment update on encrypted pvc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the deployment spec was updated or restarted, the "encryption-open" init container would fail to open the device since it was already opened on the node. Because they are no reliable post stop hook on Kubernetes, the trick is to use a small bash init container. Signed-off-by: Sébastien Han --- pkg/operator/ceph/cluster/osd/spec.go | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/pkg/operator/ceph/cluster/osd/spec.go b/pkg/operator/ceph/cluster/osd/spec.go index 647820ced..e65a6c615 100644 --- a/pkg/operator/ceph/cluster/osd/spec.go +++ b/pkg/operator/ceph/cluster/osd/spec.go @@ -110,6 +110,22 @@ else ceph-volume "$CV_MODE" activate "${ARGS[@]}" fi +` + + openEncryptedBlock = ` +set -xe + +KEY_FILE_PATH=%s +BLOCK_PATH=%s +DM_NAME=%s +DM_PATH=%s + +if [ -b "$DM_PATH" ]; then + echo "Encrypted device "$BLOCK_PATH" already opened at "$DM_PATH"" +else + echo "Opening encrypted device "$BLOCK_PATH" at "$DM_PATH"" + cryptsetup luksOpen --verbose --allow-discards --key-file "$KEY_FILE_PATH" "$BLOCK_PATH" "$DM_NAME" +fi ` ) @@ -576,18 +592,12 @@ func (c *Cluster) generateEncryptionOpenBlockContainer(resources v1.ResourceRequ return v1.Container{ Name: containerName, Image: c.spec.CephVersion.Image, + // Running via bash allows us to check whether the device is already opened or not + // If we don't the cryptsetup command will fail saying the device is already opened Command: []string{ - "cryptsetup", - }, - // Runs "cryptsetup --verbose --key-file /etc/ceph/luks_key --allow-discards luksOpen /set1-data-0-7dwll set1-data-0-7dwll-block-dmcrypt" - Args: []string{ - "--verbose", - "--key-file", - encryptionKeyPath(), - "--allow-discards", - "luksOpen", - fmt.Sprintf("/%s", pvcName), - encryptionDMName(pvcName, blockType), + "/bin/bash", + "-c", + fmt.Sprintf(openEncryptedBlock, encryptionKeyPath(), fmt.Sprintf("/%s", pvcName), encryptionDMName(pvcName, blockType), encryptionDMPath(pvcName, blockType)), }, VolumeDevices: []v1.VolumeDevice{ { From cb81ee7d1dc9d4dbb8a9fe05eb48156faab7493c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Wed, 19 Aug 2020 17:00:56 +0200 Subject: [PATCH 2/4] ceph: add pvc expansion support for encrypted osds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now support the expansion of encrypted osds. We added a new init container to run "cryptsetup resize". Signed-off-by: Sébastien Han --- pkg/operator/ceph/cluster/osd/spec.go | 41 +++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/operator/ceph/cluster/osd/spec.go b/pkg/operator/ceph/cluster/osd/spec.go index e65a6c615..22fea93cc 100644 --- a/pkg/operator/ceph/cluster/osd/spec.go +++ b/pkg/operator/ceph/cluster/osd/spec.go @@ -50,6 +50,7 @@ const ( blockPVCWalMapperInitContainer = "blkdevmapper-wal" activatePVCOSDInitContainer = "activate" expandPVCOSDInitContainer = "expand-bluefs" + expandEncryptedPVCOSDInitContainer = "expand-encrypted-bluefs" encryptionKeyFileName = "luks_key" // DmcryptBlockType is a portion of the device mapper name for the encrypted OSD on PVC block.db (rocksdb db) DmcryptBlockType = "block-dmcrypt" @@ -351,6 +352,8 @@ func (c *Cluster) makeDeployment(osdProps osdProperties, osd OSDInfo, provisionC // Add a new init container to open the encrypted disk! initContainers = append(initContainers, c.getPVCEncryptionOpenInitContainerActivate(osdProps)...) initContainers = append(initContainers, c.getPVCEncryptionInitContainerActivate(osdDataDirPath, osdProps)...) + // Resize the encrypted device if necessary, this must be done after the encrypted block is opened + initContainers = append(initContainers, c.getExpandEncryptedPVCInitContainer(osdDataDirPath, osdProps)) } else { initContainers = append(initContainers, c.getPVCInitContainerActivate(osdDataDirPath, osdProps)) if osdProps.onPVCWithMetadata() { @@ -361,10 +364,8 @@ func (c *Cluster) makeDeployment(osdProps osdProperties, osd OSDInfo, provisionC } } initContainers = append(initContainers, c.getActivatePVCInitContainer(osdProps, osdID)) - // Expansion is not supported on encrypted device - if !osdProps.encrypted { - initContainers = append(initContainers, c.getExpandPVCInitContainer(osdProps, osdID)) - } + initContainers = append(initContainers, c.getExpandPVCInitContainer(osdProps, osdID)) + } if doActivateOSDInit { initContainers = append(initContainers, *activateOSDContainer) @@ -798,9 +799,19 @@ func (c *Cluster) getActivatePVCInitContainer(osdProps osdProperties, osdID stri } func (c *Cluster) getExpandPVCInitContainer(osdProps osdProperties, osdID string) v1.Container { + /* Output example from 10GiB to 20GiB: + + inferring bluefs devices from bluestore path + 1 : device size 0x4ffe00000 : own 0x[11ff00000~40000000] = 0x40000000 : using 0x470000(4.4 MiB) : bluestore has 0x23fdd0000(9.0 GiB) available + Expanding DB/WAL... + Expanding Main... + 1 : expanding from 0x27fe00000 to 0x4ffe00000 + 1 : size label updated to 21472739328 + + */ osdDataPath := activateOSDMountPath + osdID - container := v1.Container{ + return v1.Container{ Name: expandPVCOSDInitContainer, Image: c.spec.CephVersion.Image, Command: []string{ @@ -811,6 +822,26 @@ func (c *Cluster) getExpandPVCInitContainer(osdProps osdProperties, osdID string SecurityContext: PrivilegedContext(), Resources: osdProps.resources, } +} + +func (c *Cluster) getExpandEncryptedPVCInitContainer(mountPath string, osdProps osdProperties) v1.Container { + /* Command example + [root@rook-ceph-osd-0-59b9947547-w8mdq /]# cryptsetup resize set1-data-2-8n462-block-dmcrypt + Command successful. + */ + + return v1.Container{ + Name: expandEncryptedPVCOSDInitContainer, + Image: c.spec.CephVersion.Image, + Command: []string{ + "cryptsetup", + }, + Args: []string{"--verbose", "resize", encryptionDMName(osdProps.pvc.ClaimName, DmcryptBlockType)}, + VolumeMounts: []v1.VolumeMount{getPvcOSDBridgeMountActivate(mountPath, osdProps.pvc.ClaimName)}, + SecurityContext: PrivilegedContext(), + Resources: osdProps.resources, + } +} return container } From 803462cecae1e0e77a3f6fac8a7efccb1fc53ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Wed, 19 Aug 2020 17:02:16 +0200 Subject: [PATCH 3/4] ceph: add encrypted block status container MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This init container displays the status of the encrypted pvc. This can be really useful for debugging purpose. Signed-off-by: Sébastien Han --- pkg/operator/ceph/cluster/osd/spec.go | 35 +++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/operator/ceph/cluster/osd/spec.go b/pkg/operator/ceph/cluster/osd/spec.go index 22fea93cc..56110913a 100644 --- a/pkg/operator/ceph/cluster/osd/spec.go +++ b/pkg/operator/ceph/cluster/osd/spec.go @@ -51,6 +51,7 @@ const ( activatePVCOSDInitContainer = "activate" expandPVCOSDInitContainer = "expand-bluefs" expandEncryptedPVCOSDInitContainer = "expand-encrypted-bluefs" + encryptedPVCStatusOSDInitContainer = "encrypted-block-status" encryptionKeyFileName = "luks_key" // DmcryptBlockType is a portion of the device mapper name for the encrypted OSD on PVC block.db (rocksdb db) DmcryptBlockType = "block-dmcrypt" @@ -349,9 +350,12 @@ func (c *Cluster) makeDeployment(osdProps osdProperties, osd OSDInfo, provisionC if osdProps.onPVC() && osd.CVMode == "raw" { if osdProps.encrypted { - // Add a new init container to open the encrypted disk! + // Open the encrypted disk initContainers = append(initContainers, c.getPVCEncryptionOpenInitContainerActivate(osdProps)...) + // Copy the encrypted block to the osd data location, e,g: /var/lib/ceph/osd/ceph-0/block initContainers = append(initContainers, c.getPVCEncryptionInitContainerActivate(osdDataDirPath, osdProps)...) + // Print the encrypted block status + initContainers = append(initContainers, c.getEncryptedStatusPVCInitContainer(osdDataDirPath, osdProps)) // Resize the encrypted device if necessary, this must be done after the encrypted block is opened initContainers = append(initContainers, c.getExpandEncryptedPVCInitContainer(osdDataDirPath, osdProps)) } else { @@ -843,5 +847,32 @@ func (c *Cluster) getExpandEncryptedPVCInitContainer(mountPath string, osdProps } } - return container +func (c *Cluster) getEncryptedStatusPVCInitContainer(mountPath string, osdProps osdProperties) v1.Container { + /* Command example: + root@rook-ceph-osd-0-59b9947547-w8mdq /]# cryptsetup status set1-data-2-8n462-block-dmcrypt -v + /dev/mapper/set1-data-2-8n462-block-dmcrypt is active and is in use. + type: LUKS1 + cipher: aes-xts-plain64 + keysize: 256 bits + key location: dm-crypt + device: /dev/xvdbv + sector size: 512 + offset: 4096 sectors + size: 20967424 sectors + mode: read/write + flags: discards + Command successful. + */ + + return v1.Container{ + Name: encryptedPVCStatusOSDInitContainer, + Image: c.spec.CephVersion.Image, + Command: []string{ + "cryptsetup", + }, + Args: []string{"--verbose", "status", encryptionDMName(osdProps.pvc.ClaimName, DmcryptBlockType)}, + VolumeMounts: []v1.VolumeMount{getPvcOSDBridgeMountActivate(mountPath, osdProps.pvc.ClaimName)}, + SecurityContext: PrivilegedContext(), + Resources: osdProps.resources, + } } From 321e8e0b5a3d1199258ebea08a96781ae7f52233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Wed, 19 Aug 2020 18:45:31 +0200 Subject: [PATCH 4/4] ceph: add more pvc dep unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added more unit tests scenario for osd on pvc such as: * block/db/wal * block encryption * block/db encryption * block/db/wal encryption Signed-off-by: Sébastien Han --- pkg/operator/ceph/cluster/osd/spec_test.go | 105 ++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/pkg/operator/ceph/cluster/osd/spec_test.go b/pkg/operator/ceph/cluster/osd/spec_test.go index 87103b578..3b261ac58 100644 --- a/pkg/operator/ceph/cluster/osd/spec_test.go +++ b/pkg/operator/ceph/cluster/osd/spec_test.go @@ -205,7 +205,77 @@ func testPodDevices(t *testing.T, dataDir, deviceName string, allDevices bool) { cont = deployment.Spec.Template.Spec.Containers[0] assert.Equal(t, 6, len(cont.VolumeMounts), cont.VolumeMounts) - // Test OSD on PVC with RAW and metadata device + // Test with encrypted OSD on PVC with RAW + osdProp.encrypted = true + deployment, err = c.makeDeployment(osdProp, osd, dataPathMap) + assert.Nil(t, err) + assert.NotNil(t, deployment) + assert.Equal(t, 7, len(deployment.Spec.Template.Spec.InitContainers), deployment.Spec.Template.Spec.InitContainers[2].Name) + assert.Equal(t, "encryption-open", deployment.Spec.Template.Spec.InitContainers[0].Name) + assert.Equal(t, "blkdevmapper-encryption", deployment.Spec.Template.Spec.InitContainers[1].Name) + assert.Equal(t, "encrypted-block-status", deployment.Spec.Template.Spec.InitContainers[2].Name) + assert.Equal(t, "expand-encrypted-bluefs", deployment.Spec.Template.Spec.InitContainers[3].Name) + assert.Equal(t, "activate", deployment.Spec.Template.Spec.InitContainers[4].Name) + assert.Equal(t, "expand-bluefs", deployment.Spec.Template.Spec.InitContainers[5].Name) + assert.Equal(t, "chown-container-data-dir", deployment.Spec.Template.Spec.InitContainers[6].Name) + assert.Equal(t, 1, len(deployment.Spec.Template.Spec.Containers)) + cont = deployment.Spec.Template.Spec.Containers[0] + assert.Equal(t, 7, len(cont.VolumeMounts), cont.VolumeMounts) + osdProp.encrypted = false + + // // Test OSD on PVC with RAW and metadata device + osd = OSDInfo{ + ID: 0, + CVMode: "raw", + } + osdProp.metadataPVC = v1.PersistentVolumeClaimVolumeSource{ClaimName: "mypvc-metadata"} + deployment, err = c.makeDeployment(osdProp, osd, dataPathMap) + assert.Nil(t, err) + assert.NotNil(t, deployment) + assert.Equal(t, 5, len(deployment.Spec.Template.Spec.InitContainers)) + assert.Equal(t, "blkdevmapper", deployment.Spec.Template.Spec.InitContainers[0].Name) + assert.Equal(t, "blkdevmapper-metadata", deployment.Spec.Template.Spec.InitContainers[1].Name) + assert.Equal(t, "activate", deployment.Spec.Template.Spec.InitContainers[2].Name) + assert.Equal(t, "expand-bluefs", deployment.Spec.Template.Spec.InitContainers[3].Name) + assert.Equal(t, "chown-container-data-dir", deployment.Spec.Template.Spec.InitContainers[4].Name) + assert.Equal(t, 1, len(deployment.Spec.Template.Spec.Containers)) + cont = deployment.Spec.Template.Spec.Containers[0] + assert.Equal(t, 6, len(cont.VolumeMounts), cont.VolumeMounts) + blkInitCont = deployment.Spec.Template.Spec.InitContainers[1] + assert.Equal(t, 1, len(blkInitCont.VolumeDevices)) + blkMetaInitCont := deployment.Spec.Template.Spec.InitContainers[2] + assert.Equal(t, 1, len(blkMetaInitCont.VolumeDevices)) + + // // Test encrypted OSD on PVC with RAW and metadata device + osd = OSDInfo{ + ID: 0, + CVMode: "raw", + } + osdProp.encrypted = true + osdProp.metadataPVC = v1.PersistentVolumeClaimVolumeSource{ClaimName: "mypvc-metadata"} + deployment, err = c.makeDeployment(osdProp, osd, dataPathMap) + assert.Nil(t, err) + assert.NotNil(t, deployment) + assert.Equal(t, 9, len(deployment.Spec.Template.Spec.InitContainers)) + assert.Equal(t, "encryption-open", deployment.Spec.Template.Spec.InitContainers[0].Name) + assert.Equal(t, "encryption-open-metadata", deployment.Spec.Template.Spec.InitContainers[1].Name) + assert.Equal(t, "blkdevmapper-encryption", deployment.Spec.Template.Spec.InitContainers[2].Name) + assert.Equal(t, "blkdevmapper-metadata-encryption", deployment.Spec.Template.Spec.InitContainers[3].Name) + assert.Equal(t, "encrypted-block-status", deployment.Spec.Template.Spec.InitContainers[4].Name) + assert.Equal(t, "expand-encrypted-bluefs", deployment.Spec.Template.Spec.InitContainers[5].Name) + assert.Equal(t, "activate", deployment.Spec.Template.Spec.InitContainers[6].Name) + assert.Equal(t, "expand-bluefs", deployment.Spec.Template.Spec.InitContainers[7].Name) + assert.Equal(t, "chown-container-data-dir", deployment.Spec.Template.Spec.InitContainers[8].Name) + assert.Equal(t, 1, len(deployment.Spec.Template.Spec.Containers)) + cont = deployment.Spec.Template.Spec.Containers[0] + assert.Equal(t, 7, len(cont.VolumeMounts), cont.VolumeMounts) + blkInitCont = deployment.Spec.Template.Spec.InitContainers[1] + assert.Equal(t, 1, len(blkInitCont.VolumeDevices)) + blkMetaInitCont = deployment.Spec.Template.Spec.InitContainers[6] + assert.Equal(t, 1, len(blkMetaInitCont.VolumeDevices)) + osdProp.encrypted = false + + // // Test OSD on PVC with RAW / metadata and wal device osd = OSDInfo{ ID: 0, CVMode: "raw", @@ -227,7 +297,38 @@ func testPodDevices(t *testing.T, dataDir, deviceName string, allDevices bool) { assert.Equal(t, 6, len(cont.VolumeMounts), cont.VolumeMounts) blkInitCont = deployment.Spec.Template.Spec.InitContainers[1] assert.Equal(t, 1, len(blkInitCont.VolumeDevices)) - blkMetaInitCont := deployment.Spec.Template.Spec.InitContainers[2] + blkMetaInitCont = deployment.Spec.Template.Spec.InitContainers[2] + assert.Equal(t, 1, len(blkMetaInitCont.VolumeDevices)) + + // // Test encrypted OSD on PVC with RAW / metadata and wal device + osd = OSDInfo{ + ID: 0, + CVMode: "raw", + } + osdProp.encrypted = true + osdProp.metadataPVC = v1.PersistentVolumeClaimVolumeSource{ClaimName: "mypvc-metadata"} + osdProp.walPVC = v1.PersistentVolumeClaimVolumeSource{ClaimName: "mypvc-wal"} + deployment, err = c.makeDeployment(osdProp, osd, dataPathMap) + assert.Nil(t, err) + assert.NotNil(t, deployment) + assert.Equal(t, 11, len(deployment.Spec.Template.Spec.InitContainers)) + assert.Equal(t, "encryption-open", deployment.Spec.Template.Spec.InitContainers[0].Name) + assert.Equal(t, "encryption-open-metadata", deployment.Spec.Template.Spec.InitContainers[1].Name) + assert.Equal(t, "encryption-open-wal", deployment.Spec.Template.Spec.InitContainers[2].Name) + assert.Equal(t, "blkdevmapper-encryption", deployment.Spec.Template.Spec.InitContainers[3].Name) + assert.Equal(t, "blkdevmapper-metadata-encryption", deployment.Spec.Template.Spec.InitContainers[4].Name) + assert.Equal(t, "blkdevmapper-wal-encryption", deployment.Spec.Template.Spec.InitContainers[5].Name) + assert.Equal(t, "encrypted-block-status", deployment.Spec.Template.Spec.InitContainers[6].Name) + assert.Equal(t, "expand-encrypted-bluefs", deployment.Spec.Template.Spec.InitContainers[7].Name) + assert.Equal(t, "activate", deployment.Spec.Template.Spec.InitContainers[8].Name) + assert.Equal(t, "expand-bluefs", deployment.Spec.Template.Spec.InitContainers[9].Name) + assert.Equal(t, "chown-container-data-dir", deployment.Spec.Template.Spec.InitContainers[10].Name) + assert.Equal(t, 1, len(deployment.Spec.Template.Spec.Containers)) + cont = deployment.Spec.Template.Spec.Containers[0] + assert.Equal(t, 7, len(cont.VolumeMounts), cont.VolumeMounts) + blkInitCont = deployment.Spec.Template.Spec.InitContainers[1] + assert.Equal(t, 1, len(blkInitCont.VolumeDevices)) + blkMetaInitCont = deployment.Spec.Template.Spec.InitContainers[8] assert.Equal(t, 1, len(blkMetaInitCont.VolumeDevices)) }