forked from GoogleCloudPlatform/gcsfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_parser.go
159 lines (137 loc) · 4.96 KB
/
yaml_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2021 Google Inc. All Rights Reserved.
//
// 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 config
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/Tulsishah/gcsfuse-tulsishah7/v2/internal/util"
"gopkg.in/yaml.v3"
)
type LogSeverity string
const (
TRACE LogSeverity = "TRACE"
DEBUG LogSeverity = "DEBUG"
INFO LogSeverity = "INFO"
WARNING LogSeverity = "WARNING"
ERROR LogSeverity = "ERROR"
OFF LogSeverity = "OFF"
parseConfigFileErrMsgFormat = "error parsing config file: %v"
MetadataCacheTtlSecsInvalidValueError = "the value of ttl-secs for metadata-cache can't be less than -1"
MetadataCacheTtlSecsTooHighError = "the value of ttl-secs in metadata-cache is too high to be supported. Max is 9223372036"
TypeCacheMaxSizeMBInvalidValueError = "the value of type-cache-max-size-mb for metadata-cache can't be less than -1"
StatCacheMaxSizeMBInvalidValueError = "the value of stat-cache-max-size-mb for metadata-cache can't be less than -1"
StatCacheMaxSizeMBTooHighError = "the value of stat-cache-max-size-mb for metadata-cache is too high! Max supported: 17592186044415"
MaxSupportedStatCacheMaxSizeMB = util.MaxMiBsInUint64
)
func IsValidLogSeverity(severity LogSeverity) bool {
switch severity {
case
TRACE,
DEBUG,
INFO,
WARNING,
ERROR,
OFF:
return true
}
return false
}
func IsValidLogRotateConfig(config LogRotateConfig) error {
if config.MaxFileSizeMB <= 0 {
return fmt.Errorf("max-file-size-mb should be atleast 1")
}
if config.BackupFileCount < 0 {
return fmt.Errorf("backup-file-count should be 0 (to retain all backup files) or a positive value")
}
return nil
}
func (fileCacheConfig *FileCacheConfig) validate() error {
if fileCacheConfig.MaxSizeMB < -1 {
return fmt.Errorf("the value of max-size-mb for file-cache can't be less than -1")
}
return nil
}
func (metadataCacheConfig *MetadataCacheConfig) validate() error {
if metadataCacheConfig.TtlInSeconds != TtlInSecsUnsetSentinel {
if metadataCacheConfig.TtlInSeconds < -1 {
return fmt.Errorf(MetadataCacheTtlSecsInvalidValueError)
}
if metadataCacheConfig.TtlInSeconds > MaxSupportedTtlInSeconds {
return fmt.Errorf(MetadataCacheTtlSecsTooHighError)
}
}
if metadataCacheConfig.TypeCacheMaxSizeMB < -1 {
return fmt.Errorf(TypeCacheMaxSizeMBInvalidValueError)
}
if metadataCacheConfig.StatCacheMaxSizeMB != StatCacheMaxSizeMBUnsetSentinel {
if metadataCacheConfig.StatCacheMaxSizeMB < -1 {
return fmt.Errorf(StatCacheMaxSizeMBInvalidValueError)
}
if metadataCacheConfig.StatCacheMaxSizeMB > int64(MaxSupportedStatCacheMaxSizeMB) {
return fmt.Errorf(StatCacheMaxSizeMBTooHighError)
}
}
return nil
}
func (grpcClientConfig *GrpcClientConfig) validate() error {
if grpcClientConfig.ConnPoolSize < 1 {
return fmt.Errorf("the value of conn-pool-size can't be less than 1")
}
return nil
}
func ParseConfigFile(fileName string) (mountConfig *MountConfig, err error) {
mountConfig = NewMountConfig()
if fileName == "" {
return
}
buf, err := os.ReadFile(fileName)
if err != nil {
err = fmt.Errorf("error reading config file: %w", err)
return
}
// Ensure error is thrown when unexpected configs are passed in config file.
// Ref: https://github.com/go-yaml/yaml/issues/602#issuecomment-623485602
decoder := yaml.NewDecoder(bytes.NewReader(buf))
decoder.KnownFields(true)
if err = decoder.Decode(mountConfig); err != nil {
// Decode returns EOF in case of empty config file.
if err == io.EOF {
return mountConfig, nil
}
return mountConfig, fmt.Errorf(parseConfigFileErrMsgFormat, err)
}
// convert log severity to upper-case
mountConfig.LogConfig.Severity = LogSeverity(strings.ToUpper(string(mountConfig.LogConfig.Severity)))
if !IsValidLogSeverity(mountConfig.LogConfig.Severity) {
err = fmt.Errorf("error parsing config file: log severity should be one of [trace, debug, info, warning, error, off]")
return
}
if err = IsValidLogRotateConfig(mountConfig.LogConfig.LogRotateConfig); err != nil {
err = fmt.Errorf(parseConfigFileErrMsgFormat, err)
return
}
if err = mountConfig.FileCacheConfig.validate(); err != nil {
return mountConfig, fmt.Errorf("error parsing file-cache configs: %w", err)
}
if err = mountConfig.MetadataCacheConfig.validate(); err != nil {
return mountConfig, fmt.Errorf("error parsing metadata-cache configs: %w", err)
}
if err = mountConfig.GrpcClientConfig.validate(); err != nil {
return mountConfig, fmt.Errorf("error parsing grpc-config: %w", err)
}
return
}