github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/raid0_mount.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  type Raid0Mount struct {
    10  	Type      string   `yaml:"type,omitempty"`
    11  	Iops      int      `yaml:"iops,omitempty"`
    12  	Size      int      `yaml:"size,omitempty"`
    13  	Devices   []string `yaml:"devices,omitempty"`
    14  	Path      string   `yaml:"path,omitempty"`
    15  	CreateTmp bool     `yaml:"createTmp,omitempty"`
    16  }
    17  
    18  func (r Raid0Mount) SystemdMountName() string {
    19  	return strings.Replace(strings.TrimLeft(r.Path, "/"), "/", "-", -1)
    20  }
    21  
    22  func (r Raid0Mount) DeviceList() string {
    23  	return strings.Join(r.Devices, " ")
    24  }
    25  
    26  func (r Raid0Mount) NumDevices() int {
    27  	return len(r.Devices)
    28  }
    29  
    30  func (r Raid0Mount) Validate() error {
    31  	if r.Type == "io1" {
    32  		if r.Iops < 100 || r.Iops > 20000 {
    33  			return fmt.Errorf(`invalid iops "%d" in %+v: iops must be between "100" and "20000"`, r.Iops, r)
    34  		}
    35  	} else {
    36  		if r.Iops != 0 {
    37  			return fmt.Errorf(`invalid iops "%d" for volume type "%s" in %+v: iops must be "0" when type is "standard" or "gp2"`, r.Iops, r.Type, r)
    38  		}
    39  
    40  		if r.Type != "standard" && r.Type != "gp2" {
    41  			return fmt.Errorf(`invalid type "%s" in %+v: type must be one of "standard", "gp2", "io1"`, r.Type, r)
    42  		}
    43  	}
    44  
    45  	if r.Size <= 0 {
    46  		return fmt.Errorf(`invalid size "%d" in %+v: size must be greater than "0"`, r.Size, r)
    47  	}
    48  
    49  	if r.Path == "" {
    50  		return fmt.Errorf(`invalid path "%s" in %v: path cannot be empty`, r.Path, r)
    51  	} else if regexp.MustCompile("^[a-zA-Z0-9/]*$").MatchString(r.Path) != true || strings.HasSuffix(r.Path, "/") || strings.HasPrefix(r.Path, "/") == false || strings.Contains(r.Path, "//") {
    52  		return fmt.Errorf(`invalid path "%s" in %+v`, r.Path, r)
    53  	}
    54  
    55  	for _, device := range r.Devices {
    56  		if strings.Compare(device, "/dev/xvdf") == -1 || strings.Compare(device, "/dev/xvdz") == 1 {
    57  			return fmt.Errorf(`invalid device "%s" in %+v: device must be a value from "/dev/xvdf" to "/dev/xvdz"`, device, r)
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func ValidateRaid0Mounts(volumes []NodeVolumeMount, raid0s []Raid0Mount) error {
    65  	paths := make(map[string]bool)
    66  	devices := make(map[string]bool)
    67  
    68  	// Populate any existing NodeVolumeMount items (assumes they're already Validated themselves).
    69  	for _, volume := range volumes {
    70  		paths[volume.Path] = true
    71  		devices[volume.Device] = true
    72  	}
    73  
    74  	for _, raid0 := range raid0s {
    75  		if err := raid0.Validate(); err != nil {
    76  			return err
    77  		}
    78  		if paths[raid0.Path] == true {
    79  			return fmt.Errorf("duplicate volumeMount or raid0Mount path detected (%s) - values must be unique", raid0.Path)
    80  		}
    81  		paths[raid0.Path] = true
    82  		for _, device := range raid0.Devices {
    83  			if devices[device] == true {
    84  				return fmt.Errorf("duplicate volumeMount or raid0Mount device detected (%s) - values must be unique", device)
    85  			}
    86  			devices[device] = true
    87  		}
    88  	}
    89  
    90  	return nil
    91  }