Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VirtletRootVolumeSize annotation #742

Merged
merged 7 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/volumes.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ CD-ROM and all the flexvolume types that Virtlet supports.
finding the device inside the virtual machine. Note that both
mechanisms are Linux-specific.

## Specifying the root volume size

You can set the size of the root volume of a Virtlet VM by using
`VirtletRootVolumeSize` annotation. The specified size must be
greater than the QCOW2 volume size, otherwise it will be ignored.
Here's an example:
```yaml
metadata:
name: my-vm
annotations:
kubernetes.io/target-runtime: virtlet.cloud
VirtletRootVolumeSize: 4Gi
```
This sets the root volume size to 4 GiB unless QCOW2 image size is
larger than 4 GiB, in which case the QCOW2 volume size is used.
The annotation uses the standard Kubernetes quantity specification
format, for more info, see [here](https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-memory).

## Flexvolume driver

Virtlet uses custom
Expand Down
2 changes: 2 additions & 0 deletions examples/cirros-vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ metadata:
# inject ssh keys via cloud-init
VirtletSSHKeys: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCaJEcFDXEK2ZbX0ZLS1EIYFZRbDAcRfuVjpstSc0De8+sV1aiu+dePxdkuDRwqFtCyk6dEZkssjOkBXtri00MECLkir6FcH3kKOJtbJ6vy3uaJc9w1ERo+wyl6SkAh/+JTJkp7QRXj8oylW5E20LsbnA/dIwWzAF51PPwF7A7FtNg9DnwPqMkxFo1Th/buOMKbP5ZA1mmNNtmzbMpMfJATvVyiv3ccsSJKOiyQr6UG+j7sc/7jMVz5Xk34Vd0l8GwcB0334MchHckmqDB142h/NCWTr8oLakDNvkfC1YneAfAO41hDkUbxPtVBG5M/o7P4fxoqiHEX+ZLfRxDtHB53 me@localhost
# set root volume size
VirtletRootVolumeSize: 1Gi
spec:
# This nodeAffinity specification tells Kubernetes to run this
# pod only on the nodes that have extraRuntime=virtlet label.
Expand Down
2 changes: 2 additions & 0 deletions examples/ubuntu-vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ metadata:
kubernetes.io/target-runtime: virtlet.cloud
VirtletSSHKeys: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCaJEcFDXEK2ZbX0ZLS1EIYFZRbDAcRfuVjpstSc0De8+sV1aiu+dePxdkuDRwqFtCyk6dEZkssjOkBXtri00MECLkir6FcH3kKOJtbJ6vy3uaJc9w1ERo+wyl6SkAh/+JTJkp7QRXj8oylW5E20LsbnA/dIwWzAF51PPwF7A7FtNg9DnwPqMkxFo1Th/buOMKbP5ZA1mmNNtmzbMpMfJATvVyiv3ccsSJKOiyQr6UG+j7sc/7jMVz5Xk34Vd0l8GwcB0334MchHckmqDB142h/NCWTr8oLakDNvkfC1YneAfAO41hDkUbxPtVBG5M/o7P4fxoqiHEX+ZLfRxDtHB53 me@localhost
# set root volume size
VirtletRootVolumeSize: 4Gi
spec:
affinity:
nodeAffinity:
Expand Down
3 changes: 3 additions & 0 deletions pkg/libvirttools/TestContainerLifecycle.out.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
CPUSetting: null
DiskDriver: scsi
MetaData: null
RootVolumeSize: 0
SSHKeys: null
UserData: null
UserDataOverwrite: false
Expand Down Expand Up @@ -169,6 +170,7 @@
CPUSetting: null
DiskDriver: scsi
MetaData: null
RootVolumeSize: 0
SSHKeys: null
UserData: null
UserDataOverwrite: false
Expand Down Expand Up @@ -217,6 +219,7 @@
CPUSetting: null
DiskDriver: scsi
MetaData: null
RootVolumeSize: 0
SSHKeys: null
UserData: null
UserDataOverwrite: false
Expand Down
1 change: 1 addition & 0 deletions pkg/libvirttools/TestDomainForcedShutdown.out.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
CPUSetting: null
DiskDriver: scsi
MetaData: null
RootVolumeSize: 0
SSHKeys: null
UserData: null
UserDataOverwrite: false
Expand Down
16 changes: 16 additions & 0 deletions pkg/libvirttools/TestRootVolumeSize__default__zero_.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: 'image: GetImagePathAndVirtualSize'
value: rootfs image name
- name: 'volumes: CreateStorageVol'
value: |-
<volume type="file">
<name>virtlet_root_77f29a0e-46af-4188-a6af-9ff8b8a65224</name>
<allocation unit="b">0</allocation>
<capacity unit="b">424242</capacity>
<target>
<format type="qcow2"></format>
</target>
<backingStore>
<path>/fake/volume/path</path>
<format type="qcow2"></format>
</backingStore>
</volume>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: 'image: GetImagePathAndVirtualSize'
value: rootfs image name
- name: 'volumes: CreateStorageVol'
value: |-
<volume type="file">
<name>virtlet_root_77f29a0e-46af-4188-a6af-9ff8b8a65224</name>
<allocation unit="b">0</allocation>
<capacity unit="b">424252</capacity>
<target>
<format type="qcow2"></format>
</target>
<backingStore>
<path>/fake/volume/path</path>
<format type="qcow2"></format>
</backingStore>
</volume>
16 changes: 16 additions & 0 deletions pkg/libvirttools/TestRootVolumeSize__negative.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: 'image: GetImagePathAndVirtualSize'
value: rootfs image name
- name: 'volumes: CreateStorageVol'
value: |-
<volume type="file">
<name>virtlet_root_77f29a0e-46af-4188-a6af-9ff8b8a65224</name>
<allocation unit="b">0</allocation>
<capacity unit="b">424242</capacity>
<target>
<format type="qcow2"></format>
</target>
<backingStore>
<path>/fake/volume/path</path>
<format type="qcow2"></format>
</backingStore>
</volume>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: 'image: GetImagePathAndVirtualSize'
value: rootfs image name
- name: 'volumes: CreateStorageVol'
value: |-
<volume type="file">
<name>virtlet_root_77f29a0e-46af-4188-a6af-9ff8b8a65224</name>
<allocation unit="b">0</allocation>
<capacity unit="b">424242</capacity>
<target>
<format type="qcow2"></format>
</target>
<backingStore>
<path>/fake/volume/path</path>
<format type="qcow2"></format>
</backingStore>
</volume>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: 'image: GetImagePathAndVirtualSize'
value: rootfs image name
- name: 'volumes: CreateStorageVol'
value: |-
<volume type="file">
<name>virtlet_root_77f29a0e-46af-4188-a6af-9ff8b8a65224</name>
<allocation unit="b">0</allocation>
<capacity unit="b">424242</capacity>
<target>
<format type="qcow2"></format>
</target>
<backingStore>
<path>/fake/volume/path</path>
<format type="qcow2"></format>
</backingStore>
</volume>
5 changes: 5 additions & 0 deletions pkg/libvirttools/root_volumesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (v *rootVolume) createVolume() (virt.StorageVolume, error) {
return nil, err
}

if v.config.ParsedAnnotations != nil && v.config.ParsedAnnotations.RootVolumeSize > 0 &&
uint64(v.config.ParsedAnnotations.RootVolumeSize) > virtualSize {
virtualSize = uint64(v.config.ParsedAnnotations.RootVolumeSize)
}

storagePool, err := v.owner.StoragePool()
if err != nil {
return nil, err
Expand Down
88 changes: 79 additions & 9 deletions pkg/libvirttools/root_volumesource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
)

const (
testUUID = "77f29a0e-46af-4188-a6af-9ff8b8a65224"
testUUID = "77f29a0e-46af-4188-a6af-9ff8b8a65224"
fakeImageVirtualSize = 424242
)

type FakeImageManager struct {
Expand All @@ -46,7 +47,7 @@ func NewFakeImageManager(rec testutils.Recorder) *FakeImageManager {

func (im *FakeImageManager) GetImagePathAndVirtualSize(imageName string) (string, uint64, error) {
im.rec.Rec("GetImagePathAndVirtualSize", imageName)
return "/fake/volume/path", 424242, nil
return "/fake/volume/path", fakeImageVirtualSize, nil
}

func TestRootVolumeNaming(t *testing.T) {
Expand All @@ -63,20 +64,17 @@ func TestRootVolumeNaming(t *testing.T) {
}
}

func TestRootVolumeLifeCycle(t *testing.T) {
func getRootVolumeForTest(t *testing.T, vmConfig *types.VMConfig) (*rootVolume, *testutils.TopLevelRecorder, *fake.FakeStoragePool) {
rec := testutils.NewToplevelRecorder()

volumesPoolPath := "/fake/volumes/pool"
expectedRootVolumePath := volumesPoolPath + "/virtlet_root_" + testUUID
spool := fake.NewFakeStoragePool(rec.Child("volumes"), &libvirtxml.StoragePool{
Name: "volumes",
Target: &libvirtxml.StoragePoolTarget{Path: volumesPoolPath},
})

im := NewFakeImageManager(rec.Child("image"))

volumes, err := GetRootVolume(
&types.VMConfig{DomainUUID: testUUID, Image: "rootfs image name"},
vmConfig,
newFakeVolumeOwner(spool, im),
)
if err != nil {
Expand All @@ -87,11 +85,83 @@ func TestRootVolumeLifeCycle(t *testing.T) {
t.Fatalf("GetRootVolumes returned non single number of volumes: %d", len(volumes))
}

rootVol := volumes[0]
return volumes[0].(*rootVolume), rec, spool
}

func TestRootVolumeSize(t *testing.T) {
for _, tc := range []struct {
name string
specifiedRootVolumeSize int64
expectedVolumeSize int64
}{
{
name: "default (zero)",
specifiedRootVolumeSize: 0,
expectedVolumeSize: fakeImageVirtualSize,
},
{
name: "negative",
specifiedRootVolumeSize: -1,
expectedVolumeSize: fakeImageVirtualSize,
},
{
name: "smaller than fakeImageVirtualSize",
specifiedRootVolumeSize: fakeImageVirtualSize - 10,
expectedVolumeSize: fakeImageVirtualSize,
},
{
name: "same as fakeImageVirtualSize",
specifiedRootVolumeSize: fakeImageVirtualSize,
expectedVolumeSize: fakeImageVirtualSize,
},
{
name: "greater than fakeImageVirtualSize",
specifiedRootVolumeSize: fakeImageVirtualSize + 10,
expectedVolumeSize: fakeImageVirtualSize + 10,
},
} {
t.Run(tc.name, func(t *testing.T) {
rootVol, rec, spool := getRootVolumeForTest(t, &types.VMConfig{
DomainUUID: testUUID,
Image: "rootfs image name",
ParsedAnnotations: &types.VirtletAnnotations{
RootVolumeSize: tc.specifiedRootVolumeSize,
},
})

_, err := rootVol.Setup()
if err != nil {
t.Fatalf("Setup returned an error: %v", err)
}

virtVol, err := spool.LookupVolumeByName(rootVol.volumeName())
if err != nil {
t.Fatalf("couldn't find volume %q", rootVol.volumeName())
}

size, err := virtVol.Size()
if err != nil {
t.Fatalf("couldn't get virt volume size: %v", err)
}

if int64(size) != tc.expectedVolumeSize {
t.Errorf("bad volume size %d instead of %d", size, tc.expectedVolumeSize)
}
gm.Verify(t, gm.NewYamlVerifier(rec.Content()))
})
}
}

func TestRootVolumeLifeCycle(t *testing.T) {
expectedRootVolumePath := "/fake/volumes/pool/virtlet_root_" + testUUID
rootVol, rec, _ := getRootVolumeForTest(t, &types.VMConfig{
DomainUUID: testUUID,
Image: "rootfs image name",
})

vol, err := rootVol.Setup()
if err != nil {
t.Errorf("Setup returned an error: %v", err)
t.Fatalf("Setup returned an error: %v", err)
}

if vol.Source.File == nil {
Expand Down
19 changes: 18 additions & 1 deletion pkg/metadata/types/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/ghodss/yaml"
libvirtxml "github.com/libvirt/libvirt-go-xml"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/Mirantis/virtlet/pkg/utils"
)
Expand All @@ -37,6 +38,7 @@ const (
cloudInitUserDataScriptKeyName = "VirtletCloudInitUserDataScript"
cloudInitImageType = "VirtletCloudInitImageType"
cpuModel = "VirtletCPUModel"
rootVolumeSizeKeyName = "VirtletRootVolumeSize"
libvirtCPUSetting = "VirtletLibvirtCPUSetting"
sshKeysKeyName = "VirtletSSHKeys"
// CloudInitUserDataSourceKeyName is the name of user data source key in the pod annotations.
Expand Down Expand Up @@ -93,6 +95,11 @@ type VirtletAnnotations struct {
DiskDriver DiskDriverName
// CPUSetting directly specifies the cpu to use for libvirt.
CPUSetting *libvirtxml.DomainCPU
// Root volume size in bytes. Defaults to 0 which means using
// the size of QCOW2 image). If the value is less then the
// size of the QCOW2 image, the size of the QCOW2 image is
// used instead.
RootVolumeSize int64
}

// ExternalDataLoader is a function that loads external data that's specified
Expand Down Expand Up @@ -183,7 +190,7 @@ func (va *VirtletAnnotations) parsePodAnnotations(ns string, podAnnotations map[
if vcpuCountStr, found := podAnnotations[vcpuCountAnnotationKeyName]; found {
var err error
if va.VCPUCount, err = strconv.Atoi(vcpuCountStr); err != nil {
return fmt.Errorf("error parsing cpu count for VM pod %q: %v", vcpuCountStr, err)
return fmt.Errorf("error parsing cpu count for VM pod: %q: %v", vcpuCountStr, err)
}
}

Expand Down Expand Up @@ -223,5 +230,15 @@ func (va *VirtletAnnotations) parsePodAnnotations(ns string, podAnnotations map[
va.CDImageType = CloudInitImageType(strings.ToLower(podAnnotations[cloudInitImageType]))
va.DiskDriver = DiskDriverName(podAnnotations[diskDriverKeyName])

if rootVolumeSizeStr, found := podAnnotations[rootVolumeSizeKeyName]; found {
if q, err := resource.ParseQuantity(rootVolumeSizeStr); err != nil {
return fmt.Errorf("error parsing the root volume size for VM pod: %q: %v", rootVolumeSizeStr, err)
} else if size, ok := q.AsInt64(); ok {
va.RootVolumeSize = size
} else {
return fmt.Errorf("bad root volume size %q", rootVolumeSizeStr)
}
}

return nil
}
10 changes: 10 additions & 0 deletions pkg/metadata/types/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func TestVirtletAnnotations(t *testing.T) {
CDImageType: "nocloud",
},
},
{
name: "root volume size",
annotations: map[string]string{"VirtletRootVolumeSize": "1Gi"},
va: &VirtletAnnotations{
VCPUCount: 1,
DiskDriver: "scsi",
CDImageType: "nocloud",
RootVolumeSize: 1073741824,
},
},
{
name: "cloud-init yaml and ssh keys",
annotations: map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func itShouldHaveNetworkConnectivity(podIface func() *framework.PodInterface, ss
}

It("Should have default route"+suffix, func() {
Expect(framework.RunSimple(ssh(), "ip r")).To(SatisfyAll(
Expect(framework.RunSimple(ssh(), "/sbin/ip r")).To(SatisfyAll(
ContainSubstring("default via"),
ContainSubstring("src "+podIface().Pod.Status.PodIP),
))
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package e2e

const (
defaultVMImageLocation = "download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img"
defaultVMImageLocation = "download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img"
DefaultSSHUser = "cirros"

SshPublicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCaJEcFDXEK2ZbX0ZLS1EIYFZRbDAcRfuVjpstSc0De8+sV1aiu+deP" +
Expand Down
Loading