2018-03-05 11:59:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
2018-03-22 13:11:51 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-04-13 12:21:15 +00:00
|
|
|
"strconv"
|
2018-03-22 13:11:51 +00:00
|
|
|
)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
type volumeOptions struct {
|
2018-03-20 14:51:27 +00:00
|
|
|
Monitors string `json:"monitors"`
|
2018-04-13 12:21:15 +00:00
|
|
|
Pool string `json:"pool"`
|
2018-03-20 14:51:27 +00:00
|
|
|
RootPath string `json:"rootPath"`
|
2018-04-13 12:21:15 +00:00
|
|
|
|
|
|
|
Mounter string `json:"mounter"`
|
|
|
|
ProvisionVolume bool `json:"provisionVolume"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNonEmptyField(field, fieldName string) error {
|
|
|
|
if field == "" {
|
|
|
|
return fmt.Errorf("Parameter '%s' cannot be empty", fieldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *volumeOptions) validate() error {
|
|
|
|
if err := validateNonEmptyField(o.Monitors, "monitors"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateNonEmptyField(o.RootPath, "rootPath"); err != nil {
|
|
|
|
if !o.ProvisionVolume {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if o.ProvisionVolume {
|
|
|
|
return fmt.Errorf("Non-empty field rootPath is in conflict with provisionVolume=true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.ProvisionVolume {
|
|
|
|
if err := validateNonEmptyField(o.Pool, "pool"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.Mounter != "" {
|
|
|
|
if err := validateMounter(o.Mounter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractOption(dest *string, optionLabel string, options map[string]string) error {
|
|
|
|
if opt, ok := options[optionLabel]; !ok {
|
2018-04-13 12:21:15 +00:00
|
|
|
return errors.New("Missing required field " + optionLabel)
|
2018-03-05 11:59:47 +00:00
|
|
|
} else {
|
|
|
|
*dest = opt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:11:51 +00:00
|
|
|
func validateMounter(m string) error {
|
|
|
|
switch m {
|
|
|
|
case volumeMounter_fuse:
|
|
|
|
case volumeMounter_kernel:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown mounter '%s'. Valid options are 'fuse' and 'kernel'", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
func newVolumeOptions(volOptions map[string]string) (*volumeOptions, error) {
|
2018-04-13 12:21:15 +00:00
|
|
|
var (
|
|
|
|
opts volumeOptions
|
|
|
|
provisionVolumeBool string
|
|
|
|
err error
|
|
|
|
)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if err = extractOption(&opts.Monitors, "monitors", volOptions); err != nil {
|
2018-03-09 16:05:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if err = extractOption(&provisionVolumeBool, "provisionVolume", volOptions); err != nil {
|
2018-03-09 16:05:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if opts.ProvisionVolume, err = strconv.ParseBool(provisionVolumeBool); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse provisionVolume: %v", err)
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if opts.ProvisionVolume {
|
|
|
|
if err = extractOption(&opts.Pool, "pool", volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = extractOption(&opts.RootPath, "rootPath", volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-22 13:11:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
// This field is optional, don't check for its presence
|
|
|
|
extractOption(&opts.Mounter, "mounter", volOptions)
|
|
|
|
|
|
|
|
if err = opts.validate(); err != nil {
|
2018-03-22 13:11:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-09 16:05:19 +00:00
|
|
|
return &opts, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|