disks

package
v1.2.20210816 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package disks defines the logic for managing a set of per-user persistent disks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

A Manager provides functionality for getting the disk for a user.

The Manager maintains a 1:1 mapping between user's a disks, so that any given user never has more than 1 disk assigned to them.

The Manager is also responsible for creating the disk for a user when the user does not already have one, and will initialize the disk by formatting it and (optinally) running a specified initialization script over the disk.

Internall, the Manager uses a snapshot of an already-initialized disk in order to speed up the process of disk creation.

func NewManager

func NewManager(ctx context.Context, computeService *compute.Service, project, zone, snapshotName, diskInitScript string, sizeGB int64, vmNetwork string) (*Manager, error)

NewManager creates a new Manager in the given project/zone combination.

The disks created by the Manager will be generated from a disk snapshot, so that they do not have to be formatted once attached to a VM.

The name of the disk snapshot is provided by the `snapshotName` argument. If that snapshot already exists, then it is simply reused. Otherwise, this method will create it and then format/initialize it using a temporary VM.

If non-empty, then the `diskInitScript` value is run on the initial disk used to generate the snapshot. This allows callers to perform arbitrary customizations that apply to every disk returned by the pool.

The `diskInitScript` runs as root from the top-level directory of the formatted disk.

The `vmNetwork` parameter is used to specify the name of the network in which the disk-formatting VM will reside. This VM does not need to accept inbound connections, so you should provide the most locked-down network you have.

func (*Manager) GetDiskName

func (d *Manager) GetDiskName(userEmail string) string

GetDiskName returns the name of the persistent disk for the given user.

This does not imply that the specified disk exists, only what the name of it must be if it does exist.

In order to make the user email fit the naming restrictions of the Google Compute Engine API (while still preventing collisions), we take the sha256 hash of the email.

The resulting hash is 256 bits long, but we need to fit it into a 64 character string of lower case letters, numbers, and hyphens.

To do that, we encode the hash as base32, which always results in exactly 52 alphanumeric characters followed by 4 padding characters (=) at the end. We simply truncate the padding and lowercase the alphanumeric characters to get 52 characters (out of a total 64 allowed) for the disk name.

Finally, we add a "user-" prefix and "-disk" suffix to ensure the name does not start or end with a number.

Example:

User Email: user@example.com
Disk Name: user-mj4q529i7cgq071uig7ha3lpn32k4m3v3avths71pgfvoni7aka0-disk

func (*Manager) GetForUser

func (d *Manager) GetForUser(ctx context.Context, userEmail string) (*compute.Disk, error)

GetForUser gets the persistent disk for the specified user, creating it if it does not already exist.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL