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 (
|
|
|
|
"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"`
|
2019-01-18 15:27:48 +00:00
|
|
|
|
|
|
|
MonValueFromSecret string `json:"monValueFromSecret"`
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateNonEmptyField(field, fieldName string) error {
|
|
|
|
if field == "" {
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("parameter '%s' cannot be empty", fieldName)
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *volumeOptions) validate() error {
|
|
|
|
if err := validateNonEmptyField(o.Monitors, "monitors"); err != nil {
|
2019-01-18 15:27:48 +00:00
|
|
|
if err = validateNonEmptyField(o.MonValueFromSecret, "monValueFromSecret"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateNonEmptyField(o.RootPath, "rootPath"); err != nil {
|
|
|
|
if !o.ProvisionVolume {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if o.ProvisionVolume {
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("non-empty field rootPath is in conflict with provisionVolume=true")
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-01-16 13:03:38 +00:00
|
|
|
opt, ok := options[optionLabel]
|
|
|
|
if !ok {
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("missing required field %s", optionLabel)
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
2019-01-16 13:03:38 +00:00
|
|
|
|
|
|
|
*dest = opt
|
|
|
|
return nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 13:11:51 +00:00
|
|
|
func validateMounter(m string) error {
|
|
|
|
switch m {
|
2019-01-16 13:03:38 +00:00
|
|
|
case volumeMounterFuse:
|
|
|
|
case volumeMounterKernel:
|
2018-03-22 13:11:51 +00:00
|
|
|
default:
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("unknown mounter '%s'. Valid options are 'fuse' and 'kernel'", m)
|
2018-03-22 13:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 14:21:03 +00:00
|
|
|
func newVolumeOptions(volOptions, secret map[string]string) (*volumeOptions, error) {
|
2018-04-13 12:21:15 +00:00
|
|
|
var (
|
2019-01-29 05:49:16 +00:00
|
|
|
opts volumeOptions
|
|
|
|
err error
|
2018-04-13 12:21:15 +00:00
|
|
|
)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-01-21 14:21:03 +00:00
|
|
|
// extract mon from secret first
|
|
|
|
if err = extractOption(&opts.MonValueFromSecret, "monValueFromSecret", volOptions); err == nil {
|
2019-01-28 13:59:16 +00:00
|
|
|
mon := ""
|
|
|
|
if mon, err = getMonValFromSecret(secret); err == nil && len(mon) > 0 {
|
2019-01-21 14:21:03 +00:00
|
|
|
opts.Monitors = mon
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(opts.Monitors) == 0 {
|
|
|
|
// if not set in secret, get it from parameter
|
|
|
|
if err = extractOption(&opts.Monitors, "monitors", volOptions); err != nil {
|
|
|
|
return nil, fmt.Errorf("either monitors or monValueFromSecret should be set")
|
2019-01-18 15:27:48 +00:00
|
|
|
}
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|
2019-01-29 05:49:16 +00:00
|
|
|
|
|
|
|
if err = extractNewVolOpt(&opts, volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = opts.validate(); err != nil {
|
2018-03-09 16:05:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-01-29 05:49:16 +00:00
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractNewVolOpt(opts *volumeOptions, volOpt map[string]string) error {
|
|
|
|
var (
|
|
|
|
provisionVolumeBool string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = extractOption(&provisionVolumeBool, "provisionVolume", volOpt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if opts.ProvisionVolume, err = strconv.ParseBool(provisionVolumeBool); err != nil {
|
2019-02-04 05:14:37 +00:00
|
|
|
return 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 {
|
2019-01-29 05:49:16 +00:00
|
|
|
if err = extractOption(&opts.Pool, "pool", volOpt); err != nil {
|
|
|
|
return err
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-29 05:49:16 +00:00
|
|
|
if err = extractOption(&opts.RootPath, "rootPath", volOpt); err != nil {
|
|
|
|
return err
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
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
|
2019-01-28 19:55:10 +00:00
|
|
|
// nolint
|
|
|
|
// (skip errcheck and gosec as this is optional)
|
2019-01-29 05:49:16 +00:00
|
|
|
extractOption(&opts.Mounter, "mounter", volOpt)
|
|
|
|
return nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|