mirror of
https://github.com/vee1e/kubeedge.git
synced 2026-09-02 10:47:47 +00:00
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
/*
|
|
Copyright 2019 The KubeEdge 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 validation
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"regexp"
|
|
)
|
|
|
|
// Regexps
|
|
var (
|
|
regexpImageRepo = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:-]*/[A-Za-z0-9][A-Za-z0-9._-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)*(@sha256:[A-Fa-f0-9]{64}|:[A-Za-z0-9_][A-Za-z0-9._-]{0,127})?$`)
|
|
|
|
regexpVersion = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-zA-Z0-9_.]+(?:-[a-zA-Z0-9_.]+)*)?$`)
|
|
)
|
|
|
|
// IsValidIP tests that the argument is a valid IP address.
|
|
func IsValidIP(value string) []string {
|
|
if net.ParseIP(value) == nil {
|
|
return []string{"must be a valid IP address, (e.g. 10.9.8.7)"}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsValidPortNum tests that the argument is a valid, non-zero port number.
|
|
func IsValidPortNum(port int) []string {
|
|
if 1 <= port && port <= 65535 {
|
|
return nil
|
|
}
|
|
return []string{InclusiveRangeError(1, 65535)}
|
|
}
|
|
|
|
// InclusiveRangeError returns a string explanation of a numeric "must be
|
|
// between" validation failure.
|
|
func InclusiveRangeError(lo, hi int) string {
|
|
return fmt.Sprintf(`must be between %d and %d, inclusive`, lo, hi)
|
|
}
|
|
|
|
func ValidateImageRepo(image string) bool {
|
|
return regexpImageRepo.MatchString(image)
|
|
}
|
|
|
|
func ValidateVersion(version string) bool {
|
|
return regexpVersion.MatchString(version)
|
|
}
|