github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/volumes.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package libvirttools
    18  
    19  import (
    20  	libvirtxml "github.com/libvirt/libvirt-go-xml"
    21  	digest "github.com/opencontainers/go-digest"
    22  
    23  	"github.com/Mirantis/virtlet/pkg/fs"
    24  	"github.com/Mirantis/virtlet/pkg/metadata/types"
    25  	"github.com/Mirantis/virtlet/pkg/utils"
    26  	"github.com/Mirantis/virtlet/pkg/virt"
    27  )
    28  
    29  // ImageManager describes an image info provider.
    30  type ImageManager interface {
    31  	// GetImagePathDigestAndVirtualSize returns the path, image
    32  	// digest ("sha256:...") and the size in bytes for the
    33  	// specified image.
    34  	GetImagePathDigestAndVirtualSize(ref string) (string, digest.Digest, uint64, error)
    35  	// FilesystemStats returns filesystem statistics for the specified image.
    36  	FilesystemStats() (*types.FilesystemStats, error)
    37  	// BytesUsedBy returns the size of the specified file.
    38  	BytesUsedBy(path string) (uint64, error)
    39  }
    40  
    41  type volumeOwner interface {
    42  	StoragePool() (virt.StoragePool, error)
    43  	DomainConnection() virt.DomainConnection
    44  	StorageConnection() virt.StorageConnection
    45  	ImageManager() ImageManager
    46  	RawDevices() []string
    47  	KubeletRootDir() string
    48  	VolumePoolName() string
    49  	FileSystem() fs.FileSystem
    50  	SharedFilesystemPath() string
    51  	Commander() utils.Commander
    52  }
    53  
    54  // VMVolumeSource is a function that provides `VMVolume`s for VMs
    55  type VMVolumeSource func(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error)
    56  
    57  // VMVolume describes a volume provider.
    58  type VMVolume interface {
    59  	IsDisk() bool
    60  	UUID() string
    61  	Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error)
    62  	WriteImage(diskPathMap) error
    63  	Teardown() error
    64  }
    65  
    66  type volumeBase struct {
    67  	config *types.VMConfig
    68  	owner  volumeOwner
    69  }
    70  
    71  func (v *volumeBase) WriteImage(diskPathMap) error { return nil }
    72  func (v *volumeBase) Teardown() error              { return nil }
    73  
    74  // CombineVMVolumeSources returns a function which will pass VM configuration
    75  // to all listed volumes sources combining returned by them `VMVolume`s.
    76  func CombineVMVolumeSources(srcs ...VMVolumeSource) VMVolumeSource {
    77  	return func(config *types.VMConfig, owner volumeOwner) ([]VMVolume, error) {
    78  		var vols []VMVolume
    79  		for _, src := range srcs {
    80  			vs, err := src(config, owner)
    81  			if err != nil {
    82  				return nil, err
    83  			}
    84  			vols = append(vols, vs...)
    85  		}
    86  		return vols, nil
    87  	}
    88  }