From 88708e7b2930ce32bae72a87e3b94136d955b646 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Wed, 1 Jul 2020 21:36:26 -0400 Subject: [PATCH] pkg/internal/kubebuilder/validation: use k8s.io/validation instead --- pkg/internal/kubebuilder/validation/dns.go | 114 ------------------ .../kubebuilder/validation/project.go | 4 +- pkg/plugin/v1/init.go | 2 +- 3 files changed, 4 insertions(+), 116 deletions(-) delete mode 100644 pkg/internal/kubebuilder/validation/dns.go diff --git a/pkg/internal/kubebuilder/validation/dns.go b/pkg/internal/kubebuilder/validation/dns.go deleted file mode 100644 index 8168eac7..00000000 --- a/pkg/internal/kubebuilder/validation/dns.go +++ /dev/null @@ -1,114 +0,0 @@ -/* -Copyright 2020 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 validation - -import ( - "fmt" - "regexp" -) - -// This file's code was modified from "k8s.io/apimachinery/pkg/util/validation" -// to avoid package dependencies. In case of additional functionality from -// "k8s.io/apimachinery" is needed, re-consider whether to add the dependency. - -const ( - dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?" - dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*" - dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?" -) - -type dnsValidationConfig struct { - format string - maxLen int - re *regexp.Regexp - errMsg string - examples []string -} - -var dns1123LabelConfig = dnsValidationConfig{ - format: dns1123LabelFmt, - maxLen: 56, // = 63 - len("-system") - re: regexp.MustCompile("^" + dns1123LabelFmt + "$"), - errMsg: "a DNS-1123 label must consist of lower case alphanumeric characters or '-'", - examples: []string{"example.com"}, -} - -var dns1123SubdomainConfig = dnsValidationConfig{ - format: dns1123SubdomainFmt, - maxLen: 253, // a subdomain's max length in DNS (RFC 1123). - re: regexp.MustCompile("^" + dns1123SubdomainFmt + "$"), - errMsg: "a DNS-1123 subdomain must consist of lower case alphanumeric characters, " + - "'-' or '.', and must start and end with an alphanumeric character", - examples: []string{"my-name", "abc-123"}, -} - -var dns1035LabelConfig = dnsValidationConfig{ - format: dns1035LabelFmt, - maxLen: 63, // a label's max length in DNS (RFC 1035). - re: regexp.MustCompile("^" + dns1035LabelFmt + "$"), - errMsg: "a DNS-1035 label must consist of lower case alphanumeric characters or '-', " + - "start with an alphabetic character, and end with an alphanumeric character", - examples: []string{"my-name", "123-abc"}, -} - -func (c dnsValidationConfig) check(value string) (errs []string) { - if len(value) > c.maxLen { - errs = append(errs, maxLenError(c.maxLen)) - } - if !c.re.MatchString(value) { - errs = append(errs, regexError(c.errMsg, c.format, c.examples...)) - } - return errs -} - -// IsDNS1123Subdomain tests for a string that conforms to the definition of a -// subdomain in DNS (RFC 1123). -func IsDNS1123Subdomain(value string) []string { - return dns1123SubdomainConfig.check(value) -} - -// IsDNS1123Label tests for a string that conforms to the definition of a label in DNS (RFC 1123). -func IsDNS1123Label(value string) []string { - return dns1123LabelConfig.check(value) -} - -// IsDNS1035Label tests for a string that conforms to the definition of a label in DNS (RFC 1035). -func IsDNS1035Label(value string) []string { - return dns1035LabelConfig.check(value) -} - -// maxLenError returns a string explanation of a "string too long" validation -// failure. -func maxLenError(length int) string { - return fmt.Sprintf("must be no more than %d characters", length) -} - -// regexError returns a string explanation of a regex validation failure. -func regexError(msg string, fmt string, examples ...string) string { - if len(examples) == 0 { - return msg + " (regex used for validation is '" + fmt + "')" - } - msg += " (e.g. " - for i := range examples { - if i > 0 { - msg += " or " - } - msg += "'" + examples[i] + "', " - } - msg += "regex used for validation is '" + fmt + "')" - return msg -} diff --git a/pkg/internal/kubebuilder/validation/project.go b/pkg/internal/kubebuilder/validation/project.go index 3afe3e98..a851e48c 100644 --- a/pkg/internal/kubebuilder/validation/project.go +++ b/pkg/internal/kubebuilder/validation/project.go @@ -19,6 +19,8 @@ package validation import ( "errors" "regexp" + + "k8s.io/apimachinery/pkg/util/validation" ) // projectVersionFmt defines the project version format from a project config. @@ -32,7 +34,7 @@ func ValidateProjectVersion(version string) error { return errors.New("project version is empty") } if !projectVersionRe.MatchString(version) { - return errors.New(regexError("invalid value for project version", projectVersionFmt)) + return errors.New(validation.RegexError("invalid value for project version", projectVersionFmt)) } return nil } diff --git a/pkg/plugin/v1/init.go b/pkg/plugin/v1/init.go index c49520d7..ca2c56e9 100644 --- a/pkg/plugin/v1/init.go +++ b/pkg/plugin/v1/init.go @@ -23,12 +23,12 @@ import ( "strings" "github.com/spf13/pflag" + "k8s.io/apimachinery/pkg/util/validation" "sigs.k8s.io/kubebuilder/pkg/model/config" "sigs.k8s.io/kubebuilder/pkg/plugin" "sigs.k8s.io/kubebuilder/pkg/plugin/scaffold" "github.com/joelanford/helm-operator/pkg/internal/kubebuilder/cmdutil" - "github.com/joelanford/helm-operator/pkg/internal/kubebuilder/validation" "github.com/joelanford/helm-operator/pkg/plugin/internal/chartutil" "github.com/joelanford/helm-operator/pkg/plugin/v1/scaffolds" )