2018-03-28 17:14:06 -04:00
|
|
|
/*
|
2019-10-04 13:35:33 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
2018-03-28 17:14:06 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
package cache
|
2018-03-28 17:14:06 -04:00
|
|
|
|
|
|
|
import (
|
2018-03-29 17:38:26 -04:00
|
|
|
"encoding/json"
|
2018-07-18 14:22:29 -04:00
|
|
|
"errors"
|
2018-03-29 17:38:26 -04:00
|
|
|
"path/filepath"
|
2018-07-03 19:54:10 -04:00
|
|
|
"strings"
|
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
"github.com/minio/minio/cmd/config"
|
2018-07-03 19:54:10 -04:00
|
|
|
"github.com/minio/minio/pkg/ellipses"
|
2018-03-28 17:14:06 -04:00
|
|
|
)
|
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
// Config represents cache config settings
|
|
|
|
type Config struct {
|
2020-02-23 08:33:39 -05:00
|
|
|
Enabled bool `json:"-"`
|
|
|
|
Drives []string `json:"drives"`
|
|
|
|
Expiry int `json:"expiry"`
|
|
|
|
MaxUse int `json:"maxuse"`
|
|
|
|
Quota int `json:"quota"`
|
|
|
|
Exclude []string `json:"exclude"`
|
|
|
|
After int `json:"after"`
|
|
|
|
WatermarkLow int `json:"watermark_low"`
|
|
|
|
WatermarkHigh int `json:"watermark_high"`
|
2020-06-29 16:25:29 -04:00
|
|
|
Range bool `json:"range"`
|
2018-03-29 17:38:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - implements JSON unmarshal interface for unmarshalling
|
|
|
|
// json entries for CacheConfig.
|
2019-10-04 13:35:33 -04:00
|
|
|
func (cfg *Config) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
type Alias Config
|
2018-03-29 17:38:26 -04:00
|
|
|
var _cfg = &struct {
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(cfg),
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(data, _cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-18 14:22:29 -04:00
|
|
|
|
|
|
|
if _cfg.Expiry < 0 {
|
|
|
|
return errors.New("config expiry value should not be negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _cfg.MaxUse < 0 {
|
|
|
|
return errors.New("config max use value should not be null or negative")
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if _cfg.Quota < 0 {
|
|
|
|
return errors.New("config quota value should not be null or negative")
|
|
|
|
}
|
2020-02-03 22:40:01 -05:00
|
|
|
if _cfg.After < 0 {
|
|
|
|
return errors.New("cache after value should not be less than 0")
|
|
|
|
}
|
2020-02-23 08:33:39 -05:00
|
|
|
if _cfg.WatermarkLow < 0 || _cfg.WatermarkLow > 100 {
|
|
|
|
return errors.New("config low watermark value should be between 0 and 100")
|
|
|
|
}
|
|
|
|
if _cfg.WatermarkHigh < 0 || _cfg.WatermarkHigh > 100 {
|
|
|
|
return errors.New("config high watermark value should be between 0 and 100")
|
|
|
|
}
|
|
|
|
if _cfg.WatermarkLow > 0 && (_cfg.WatermarkLow >= _cfg.WatermarkHigh) {
|
|
|
|
return errors.New("config low watermark value should be less than high watermark")
|
|
|
|
}
|
2018-03-29 17:38:26 -04:00
|
|
|
return nil
|
2018-03-28 17:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses given cacheDrivesEnv and returns a list of cache drives.
|
2019-12-03 13:50:20 -05:00
|
|
|
func parseCacheDrives(drives string) ([]string, error) {
|
|
|
|
var drivesSlice []string
|
2018-07-03 19:54:10 -04:00
|
|
|
if len(drives) == 0 {
|
2019-12-03 13:50:20 -05:00
|
|
|
return drivesSlice, nil
|
2018-07-03 19:54:10 -04:00
|
|
|
}
|
2019-12-03 13:50:20 -05:00
|
|
|
|
|
|
|
drivesSlice = strings.Split(drives, cacheDelimiterLegacy)
|
|
|
|
if len(drivesSlice) == 1 && drivesSlice[0] == drives {
|
|
|
|
drivesSlice = strings.Split(drives, cacheDelimiter)
|
|
|
|
}
|
|
|
|
|
2018-07-03 19:54:10 -04:00
|
|
|
var endpoints []string
|
2019-12-03 13:50:20 -05:00
|
|
|
for _, d := range drivesSlice {
|
|
|
|
if len(d) == 0 {
|
|
|
|
return nil, config.ErrInvalidCacheDrivesValue(nil).Msg("cache dir cannot be an empty path")
|
|
|
|
}
|
2018-07-03 19:54:10 -04:00
|
|
|
if ellipses.HasEllipses(d) {
|
|
|
|
s, err := parseCacheDrivePaths(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
endpoints = append(endpoints, s...)
|
|
|
|
} else {
|
|
|
|
endpoints = append(endpoints, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range endpoints {
|
2018-03-29 17:38:26 -04:00
|
|
|
if !filepath.IsAbs(d) {
|
2019-10-04 13:35:33 -04:00
|
|
|
return nil, config.ErrInvalidCacheDrivesValue(nil).Msg("cache dir should be absolute path: %s", d)
|
2018-03-28 17:14:06 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 19:54:10 -04:00
|
|
|
return endpoints, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses all arguments and returns a slice of drive paths following the ellipses pattern.
|
|
|
|
func parseCacheDrivePaths(arg string) (ep []string, err error) {
|
|
|
|
patterns, perr := ellipses.FindEllipsesPatterns(arg)
|
|
|
|
if perr != nil {
|
2019-10-04 13:35:33 -04:00
|
|
|
return []string{}, config.ErrInvalidCacheDrivesValue(nil).Msg(perr.Error())
|
2018-07-03 19:54:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, lbls := range patterns.Expand() {
|
|
|
|
ep = append(ep, strings.Join(lbls, ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep, nil
|
2018-03-28 17:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses given cacheExcludesEnv and returns a list of cache exclude patterns.
|
2019-12-03 13:50:20 -05:00
|
|
|
func parseCacheExcludes(excludes string) ([]string, error) {
|
|
|
|
var excludesSlice []string
|
|
|
|
if len(excludes) == 0 {
|
|
|
|
return excludesSlice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
excludesSlice = strings.Split(excludes, cacheDelimiterLegacy)
|
|
|
|
if len(excludesSlice) == 1 && excludesSlice[0] == excludes {
|
|
|
|
excludesSlice = strings.Split(excludes, cacheDelimiter)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range excludesSlice {
|
2018-03-29 17:38:26 -04:00
|
|
|
if len(e) == 0 {
|
2019-10-04 13:35:33 -04:00
|
|
|
return nil, config.ErrInvalidCacheExcludesValue(nil).Msg("cache exclude path (%s) cannot be empty", e)
|
2018-03-29 17:38:26 -04:00
|
|
|
}
|
2019-10-04 13:35:33 -04:00
|
|
|
if strings.HasPrefix(e, "/") {
|
|
|
|
return nil, config.ErrInvalidCacheExcludesValue(nil).Msg("cache exclude pattern (%s) cannot start with / as prefix", e)
|
2018-03-28 17:14:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 13:50:20 -05:00
|
|
|
|
|
|
|
return excludesSlice, nil
|
2018-03-28 17:14:06 -04:00
|
|
|
}
|