Skip to content

Commit

Permalink
Moving config validation code to subpackage (istio#16199)
Browse files Browse the repository at this point in the history
This is part of the cleanup of pkg/config. Planning on moving everything here to subpackages.
  • Loading branch information
nmittler authored and istio-testing committed Aug 12, 2019
1 parent f419cd5 commit bcf04fc
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"istio.io/istio/galley/pkg/runtime/projections/serviceentry/converter"
"istio.io/istio/galley/pkg/runtime/projections/serviceentry/pod"
"istio.io/istio/galley/pkg/runtime/resource"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/protocol"
"istio.io/istio/pkg/config/validation"

coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestServiceResolution(t *testing.T) {
case networking.ServiceEntry_DNS:
g.Expect(len(actual.Addresses)).To(Equal(0))
for _, host := range actual.Hosts {
g.Expect(config.ValidateFQDN(host)).To(BeNil())
g.Expect(validation.ValidateFQDN(host)).To(BeNil())

}
case networking.ServiceEntry_STATIC:
Expand Down
5 changes: 3 additions & 2 deletions istioctl/cmd/convert_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
"istio.io/istio/istioctl/pkg/convert"
"istio.io/istio/pilot/pkg/config/kube/crd"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/validation"

"istio.io/pkg/log"
)

Expand Down Expand Up @@ -157,7 +158,7 @@ func validateConfigs(configs []model.Config) error {
var errs error
for _, cfg := range configs {
if cfg.Type == model.VirtualService.Type {
if err := config.ValidateVirtualService(cfg.Name, cfg.Namespace, cfg.Spec); err != nil {
if err := validation.ValidateVirtualService(cfg.Name, cfg.Namespace, cfg.Spec); err != nil {
errs = multierror.Append(err, errs)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pilot/cmd/pilot-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import (
"istio.io/istio/pilot/pkg/proxy/envoy"
"istio.io/istio/pilot/pkg/serviceregistry"
"istio.io/istio/pkg/cmd"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/mesh"
"istio.io/istio/pkg/config/validation"
"istio.io/istio/pkg/spiffe"
"istio.io/istio/pkg/util/protomarshal"
)
Expand Down Expand Up @@ -278,7 +278,7 @@ var (
}
}

if err := config.ValidateProxyConfig(&proxyConfig); err != nil {
if err := validation.ValidateProxyConfig(&proxyConfig); err != nil {
return err
}

Expand Down
36 changes: 18 additions & 18 deletions pilot/pkg/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
networking "istio.io/api/networking/v1alpha3"

"istio.io/istio/galley/pkg/metadata"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/host"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/validation"
testConfig "istio.io/istio/pkg/test/config"
)

Expand Down Expand Up @@ -222,7 +222,7 @@ type ProtoSchema struct {

// Validate configuration as a protobuf message assuming the object is an
// instance of the expected message type
Validate config.ValidationFunc
Validate validation.ValidateFunc

// MCP collection for this configuration resource schema
Collection string
Expand Down Expand Up @@ -335,7 +335,7 @@ var (
Group: "networking",
Version: "v1alpha3",
MessageName: "istio.networking.v1alpha3.VirtualService",
Validate: config.ValidateVirtualService,
Validate: validation.ValidateVirtualService,
Collection: metadata.IstioNetworkingV1alpha3Virtualservices.Collection.String(),
}

Expand All @@ -346,7 +346,7 @@ var (
Group: "networking",
Version: "v1alpha3",
MessageName: "istio.networking.v1alpha3.Gateway",
Validate: config.ValidateGateway,
Validate: validation.ValidateGateway,
Collection: metadata.IstioNetworkingV1alpha3Gateways.Collection.String(),
}

Expand All @@ -357,7 +357,7 @@ var (
Group: "networking",
Version: "v1alpha3",
MessageName: "istio.networking.v1alpha3.ServiceEntry",
Validate: config.ValidateServiceEntry,
Validate: validation.ValidateServiceEntry,
Collection: metadata.IstioNetworkingV1alpha3Serviceentries.Collection.String(),
}

Expand All @@ -368,7 +368,7 @@ var (
Group: "networking",
Version: "v1alpha3",
MessageName: "istio.networking.v1alpha3.DestinationRule",
Validate: config.ValidateDestinationRule,
Validate: validation.ValidateDestinationRule,
Collection: metadata.IstioNetworkingV1alpha3Destinationrules.Collection.String(),
}

Expand All @@ -379,7 +379,7 @@ var (
Group: "networking",
Version: "v1alpha3",
MessageName: "istio.networking.v1alpha3.EnvoyFilter",
Validate: config.ValidateEnvoyFilter,
Validate: validation.ValidateEnvoyFilter,
Collection: metadata.IstioNetworkingV1alpha3Envoyfilters.Collection.String(),
}

Expand All @@ -390,7 +390,7 @@ var (
Group: "networking",
Version: "v1alpha3",
MessageName: "istio.networking.v1alpha3.Sidecar",
Validate: config.ValidateSidecar,
Validate: validation.ValidateSidecar,
Collection: metadata.IstioNetworkingV1alpha3Sidecars.Collection.String(),
}

Expand All @@ -401,7 +401,7 @@ var (
Group: "config",
Version: istioAPIVersion,
MessageName: "istio.mixer.v1.config.client.HTTPAPISpec",
Validate: config.ValidateHTTPAPISpec,
Validate: validation.ValidateHTTPAPISpec,
Collection: metadata.IstioConfigV1alpha2Httpapispecs.Collection.String(),
}

Expand All @@ -412,7 +412,7 @@ var (
Group: "config",
Version: istioAPIVersion,
MessageName: "istio.mixer.v1.config.client.HTTPAPISpecBinding",
Validate: config.ValidateHTTPAPISpecBinding,
Validate: validation.ValidateHTTPAPISpecBinding,
Collection: metadata.IstioConfigV1alpha2Httpapispecbindings.Collection.String(),
}

Expand All @@ -423,7 +423,7 @@ var (
Group: "config",
Version: istioAPIVersion,
MessageName: "istio.mixer.v1.config.client.QuotaSpec",
Validate: config.ValidateQuotaSpec,
Validate: validation.ValidateQuotaSpec,
Collection: metadata.IstioMixerV1ConfigClientQuotaspecs.Collection.String(),
}

Expand All @@ -434,7 +434,7 @@ var (
Group: "config",
Version: istioAPIVersion,
MessageName: "istio.mixer.v1.config.client.QuotaSpecBinding",
Validate: config.ValidateQuotaSpecBinding,
Validate: validation.ValidateQuotaSpecBinding,
Collection: metadata.IstioMixerV1ConfigClientQuotaspecbindings.Collection.String(),
}

Expand All @@ -446,7 +446,7 @@ var (
Group: "authentication",
Version: "v1alpha1",
MessageName: "istio.authentication.v1alpha1.Policy",
Validate: config.ValidateAuthenticationPolicy,
Validate: validation.ValidateAuthenticationPolicy,
Collection: metadata.IstioAuthenticationV1alpha1Policies.Collection.String(),
}

Expand All @@ -459,7 +459,7 @@ var (
Group: "authentication",
Version: "v1alpha1",
MessageName: "istio.authentication.v1alpha1.Policy",
Validate: config.ValidateAuthenticationPolicy,
Validate: validation.ValidateAuthenticationPolicy,
Collection: metadata.IstioAuthenticationV1alpha1Meshpolicies.Collection.String(),
}

Expand All @@ -470,7 +470,7 @@ var (
Group: "rbac",
Version: "v1alpha1",
MessageName: "istio.rbac.v1alpha1.ServiceRole",
Validate: config.ValidateServiceRole,
Validate: validation.ValidateServiceRole,
Collection: metadata.IstioRbacV1alpha1Serviceroles.Collection.String(),
}

Expand All @@ -482,7 +482,7 @@ var (
Group: "rbac",
Version: "v1alpha1",
MessageName: "istio.rbac.v1alpha1.ServiceRoleBinding",
Validate: config.ValidateServiceRoleBinding,
Validate: validation.ValidateServiceRoleBinding,
Collection: metadata.IstioRbacV1alpha1Servicerolebindings.Collection.String(),
}

Expand All @@ -495,7 +495,7 @@ var (
Group: "rbac",
Version: "v1alpha1",
MessageName: "istio.rbac.v1alpha1.RbacConfig",
Validate: config.ValidateRbacConfig,
Validate: validation.ValidateRbacConfig,
Collection: metadata.IstioRbacV1alpha1Rbacconfigs.Collection.String(),
}

Expand All @@ -507,7 +507,7 @@ var (
Group: "rbac",
Version: "v1alpha1",
MessageName: "istio.rbac.v1alpha1.RbacConfig",
Validate: config.ValidateClusterRbacConfig,
Validate: validation.ValidateClusterRbacConfig,
Collection: metadata.IstioRbacV1alpha1Clusterrbacconfigs.Collection.String(),
}

Expand Down
10 changes: 5 additions & 5 deletions pilot/pkg/model/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/hashicorp/go-multierror"

"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/validation"
)

// UnixAddressPrefix is the prefix used to indicate an address is for a Unix Domain socket. It is used in
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *Service) Validate() error {
} else if !labels.IsDNS1123Label(port.Name) {
errs = multierror.Append(errs, fmt.Errorf("invalid name: %q", port.Name))
}
if err := config.ValidatePort(port.Port); err != nil {
if err := validation.ValidatePort(port.Port); err != nil {
errs = multierror.Append(errs,
fmt.Errorf("invalid service port value %d for %q: %v", port.Port, port.Name, err))
}
Expand All @@ -116,7 +116,7 @@ func (instance *ServiceInstance) Validate() error {
errs = multierror.Append(errs, err)
}

if err := config.ValidatePort(instance.Endpoint.Port); err != nil {
if err := validation.ValidatePort(instance.Endpoint.Port); err != nil {
errs = multierror.Append(errs, err)
}

Expand Down Expand Up @@ -149,12 +149,12 @@ func ValidateNetworkEndpointAddress(n *NetworkEndpoint) error {
case AddressFamilyTCP:
ipAddr := net.ParseIP(n.Address) // Typically it is an IP address
if ipAddr == nil {
if err := config.ValidateFQDN(n.Address); err != nil { // Otherwise could be an FQDN
if err := validation.ValidateFQDN(n.Address); err != nil { // Otherwise could be an FQDN
return errors.New("invalid address " + n.Address)
}
}
case AddressFamilyUnix:
return config.ValidateUnixAddress(n.Address)
return validation.ValidateUnixAddress(n.Address)
default:
panic(fmt.Sprintf("unhandled Family %v", n.Family))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

meshconfig "istio.io/api/mesh/v1alpha1"

"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/validation"
"istio.io/istio/pkg/util/protomarshal"
)

Expand Down Expand Up @@ -111,7 +111,7 @@ func ApplyMeshConfigDefaults(yaml string) (*meshconfig.MeshConfig, error) {
}
}

if err := config.ValidateMeshConfig(&out); err != nil {
if err := validation.ValidateMeshConfig(&out); err != nil {
return nil, err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/config/mesh/mesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ import (

meshconfig "istio.io/api/mesh/v1alpha1"

"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/mesh"
"istio.io/istio/pkg/config/validation"
)

func TestDefaultProxyConfig(t *testing.T) {
proxyConfig := mesh.DefaultProxyConfig()
if err := config.ValidateProxyConfig(&proxyConfig); err != nil {
if err := validation.ValidateProxyConfig(&proxyConfig); err != nil {
t.Errorf("validation of default proxy config failed with %v", err)
}
}

func TestDefaultMeshConfig(t *testing.T) {
m := mesh.DefaultMeshConfig()
if err := config.ValidateMeshConfig(&m); err != nil {
if err := validation.ValidateMeshConfig(&m); err != nil {
t.Errorf("validation of default mesh config failed with %v", err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package config
package validation

import (
"errors"
Expand Down Expand Up @@ -94,8 +94,8 @@ var supportedMethods = map[string]bool{
http.MethodTrace: true,
}

// ValidationFunc defines a validation func for an API proto.
type ValidationFunc func(name, namespace string, config proto.Message) error
// ValidateFunc defines a validation func for an API proto.
type ValidateFunc func(name, namespace string, config proto.Message) error

// ValidatePort checks that the network port is in range
func ValidatePort(port int) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package config
package validation

import (
"fmt"
Expand Down

0 comments on commit bcf04fc

Please sign in to comment.