/
utils.go
246 lines (212 loc) · 7.38 KB
/
utils.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
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 common
import (
"encoding/base64"
"fmt"
"math/rand"
"regexp"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/schema"
"github.com/PaddlePaddle/PaddleFlow/pkg/fs/common"
)
const (
RegPatternQueueName = "^[a-z0-9][a-z0-9-]{0,8}[a-z0-9]$"
RegPatternUserName = "^[A-Za-z0-9-]{4,16}$"
RegPatternRunName = "^[A-Za-z0-9_][A-Za-z0-9-_]{1,49}[A-Za-z0-9_]$"
RegPatternResource = "^[1-9][0-9]*([numkMGTPE]|Ki|Mi|Gi|Ti|Pi|Ei)?$"
RegPatternPipelineName = "^[A-Za-z0-9_][A-Za-z0-9-_]{1,49}[A-Za-z0-9_]$"
RegPatternClusterName = "^[A-Za-z0-9_][A-Za-z0-9-_]{0,253}[A-Za-z0-9_]$"
// DNS1123LabelMaxLength is a label's max length in DNS (RFC 1123)
DNS1123LabelMaxLength = 63
DNS1123LabelFmt = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
dns1123LabelErrMsg = "a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-'," +
" and must start and end with an alphanumeric character"
HDFS = "hdfs"
HDFSWithKerberos = "hdfsWithKerberos"
S3 = "s3"
Local = "local"
SFTP = "sftp"
Mock = "mock"
CFS = "cfs"
IPDomainOrIPDomainPortPattern = "^([a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+)" +
"(:([1-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]))?$"
TypeSplit = 0
HDFSSplit = 3
S3Split = 3
S3EndpointSplit = 2
S3SplitLen = 4
LocalSplit = 2
CFSSplit = 3
ServerAddressSplit = 2
S3AddressLength = 3
IDSliceLen = 3
FsPrefix = "fs-"
UserRoot = "root"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func IsRootUser(userName string) bool {
return strings.EqualFold(userName, "root")
}
func IsValidQueueStatus(status string) bool {
if strings.EqualFold(status, schema.StatusQueueCreating) || strings.EqualFold(status, schema.StatusQueueOpen) ||
strings.EqualFold(status, schema.StatusQueueClosing) || strings.EqualFold(status, schema.StatusQueueClosed) ||
strings.EqualFold(status, schema.StatusQueueUnavailable) {
return true
}
log.Errorf("Not valid queue status. status:%s", status)
return false
}
// check string is slice or not
func StringInSlice(s string, strSlice []string) bool {
for _, str := range strSlice {
if s == str {
return true
}
}
return false
}
func RemoveDuplicateStr(strSlice []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func SplitString(str, sep string) []string {
var result []string
strList := strings.Split(str, sep)
for _, s := range strList {
result = append(result, strings.TrimSpace(s))
}
return result
}
func ID(userName, fsName string) string {
return FsPrefix + userName + "-" + fsName
}
// InformationFromURL get fs system information from url
func InformationFromURL(url string, properties map[string]string) (fileSystemType, serverAddress, subPath string) {
fileSystemType = strings.Split(url, ":")[TypeSplit]
serverAddress = ""
subPath = ""
urlSplit := strings.Split(url, "/")
switch fileSystemType {
case Local, Mock:
serverAddress = ""
subPath = "/" + SubPathFromUrl(urlSplit, LocalSplit)
case HDFS:
serverAddress = urlSplit[ServerAddressSplit]
if properties == nil {
properties = make(map[string]string)
}
properties[common.NameNodeAddress] = serverAddress
subPath = "/" + SubPathFromUrl(urlSplit, HDFSSplit)
if properties[common.KeyTabData] != "" {
fileSystemType = HDFSWithKerberos
}
case SFTP:
serverAddress = urlSplit[ServerAddressSplit]
subPath = "/" + SubPathFromUrl(urlSplit, HDFSSplit)
case S3:
serverAddress = properties[common.Endpoint]
subPath = "/" + SubPathFromUrl(urlSplit, S3Split)
case CFS:
serverAddress = urlSplit[ServerAddressSplit]
subPath = "/" + SubPathFromUrl(urlSplit, CFSSplit)
default:
return
}
return
}
func SubPathFromUrl(urlSplit []string, split int) string {
temp := urlSplit[split:]
return strings.Join(temp, "/")
}
func CheckKerberosProperties(properties map[string]string) error {
if properties[common.Principal] == "" {
return InvalidField(common.Principal, fmt.Sprintf("kerberos hdfs, %s must be provided", common.Principal))
}
if properties[common.KeyTabData] == "" {
return InvalidField(common.KeyTabData, fmt.Sprintf("kerberos hdfs, %s must be provided", common.KeyTabData))
}
if properties[common.DataTransferProtection] == "" {
return InvalidField(common.DataTransferProtection, fmt.Sprintf("kerberos hdfs, %s must be provided", common.DataTransferProtection))
}
_, err := base64.StdEncoding.DecodeString(properties[common.KeyTabData])
if err != nil {
log.Errorf("Decode keyTab data failed, %v", err)
return err
}
if KDCStr, ok := properties[common.Kdc]; ok {
for _, kdc := range strings.Split(KDCStr, ",") {
if matched, err := regexp.MatchString(IPDomainOrIPDomainPortPattern, kdc); err != nil || !matched {
return InvalidField(common.Kdc,
"kerberos.kdc should be ip or ip:port format")
}
}
} else {
return InvalidField(common.Kdc,
"for kerberos hdfs, kerberos.Kdc must be provided")
}
nameNodePrincipal, ok := properties[common.NameNodePrincipal]
if !ok && nameNodePrincipal == "" {
return InvalidField(common.NameNodePrincipal,
"for kerberos hdfs, kerberos.namenode.principal must be provided")
}
if realm, ok := properties[common.Realm]; ok {
if strings.Contains(nameNodePrincipal, "@") &&
!strings.HasSuffix(nameNodePrincipal, "@"+realm) {
return InvalidField(common.Realm, fmt.Sprintf("nameNodePrincipal has not suffix realm[%s]", realm))
}
} else {
return InvalidField(common.Realm, fmt.Sprintf("kerberos hdfs, %s must be provided", common.Realm))
}
return nil
}
func CheckFsNested(path1, path2 string) bool {
path1 = strings.TrimRight(path1, "/")
path2 = strings.TrimRight(path2, "/")
path1Arr := strings.Split(path1, "/")
path2Arr := strings.Split(path2, "/")
minIndex := len(path1Arr)
if len(path1Arr) > len(path2Arr) {
minIndex = len(path2Arr)
}
for index := 0; index < minIndex; index++ {
if path1Arr[index] != path2Arr[index] {
return false
}
}
return true
}
// IsDNS1123Label tests for a string that conforms to the definition of a label in
// DNS (RFC 1123).
func IsDNS1123Label(value string) []string {
var errs []string
var dns1123LabelRegexp = regexp.MustCompile("^" + DNS1123LabelFmt + "$")
if len(value) > DNS1123LabelMaxLength {
errs = append(errs, fmt.Sprintf("must be no more than %d characters", DNS1123LabelMaxLength))
}
if !dns1123LabelRegexp.MatchString(value) {
errs = append(errs, dns1123LabelErrMsg+" (regex used for validation is '"+DNS1123LabelFmt+"')")
}
return errs
}