From 3573752921cb6ba42cdfb1a6a4fdc8f0295fa24e Mon Sep 17 00:00:00 2001 From: Nathan Mittler Date: Tue, 30 Jul 2019 15:24:01 -0700 Subject: [PATCH] Moving protocol to subpackage (#15925) This is part of the cleanup of pkg/config. Planning on moving everything here to subpackages. --- .../serviceentry/converter/instance_test.go | 41 ++++--- .../transforms/serviceentry/create_test.go | 5 +- galley/pkg/runtime/conversions/ingress.go | 5 +- .../serviceentry/converter/instance_test.go | 41 ++++--- .../serviceentry/handler_bench_test.go | 6 +- .../projections/serviceentry/handler_test.go | 9 +- istioctl/pkg/validate/validate.go | 10 +- pilot/pkg/config/kube/ingress/conversion.go | 10 +- pilot/pkg/model/config_test.go | 19 +-- pilot/pkg/model/gateway.go | 17 +-- pilot/pkg/model/push_context.go | 3 +- pilot/pkg/model/service.go | 7 +- pilot/pkg/model/validation_test.go | 19 +-- pilot/pkg/networking/core/v1alpha3/cluster.go | 9 +- .../networking/core/v1alpha3/cluster_test.go | 12 +- pilot/pkg/networking/core/v1alpha3/gateway.go | 17 +-- .../pkg/networking/core/v1alpha3/httproute.go | 3 +- .../core/v1alpha3/httproute_test.go | 6 +- .../pkg/networking/core/v1alpha3/listener.go | 19 +-- .../core/v1alpha3/listener_builder_test.go | 14 +-- .../networking/core/v1alpha3/listener_test.go | 56 ++++----- .../loadbalancer/loadbalancer_test.go | 6 +- .../networking/core/v1alpha3/networkfilter.go | 8 +- .../core/v1alpha3/route/route_test.go | 3 +- pilot/pkg/networking/plugin/plugin.go | 14 +-- pilot/pkg/proxy/envoy/v2/eds.go | 3 +- pilot/pkg/proxy/envoy/v2/eds_sh_test.go | 3 +- pilot/pkg/proxy/envoy/v2/eds_test.go | 21 ++-- pilot/pkg/proxy/envoy/v2/mem.go | 7 +- pilot/pkg/proxy/envoy/v2/xds_test.go | 33 ++--- .../aggregate/controller_test.go | 5 +- .../pkg/serviceregistry/consul/conversion.go | 11 +- .../serviceregistry/consul/conversion_test.go | 32 ++--- .../serviceregistry/external/conversion.go | 3 +- .../external/conversion_test.go | 7 +- .../kube/controller/controller_test.go | 39 +++--- pilot/pkg/serviceregistry/kube/conversion.go | 16 +-- .../serviceregistry/kube/conversion_test.go | 57 +++++---- pilot/pkg/serviceregistry/memory/discovery.go | 21 ++-- pkg/config/kube/conversion.go | 17 +-- .../{protocol.go => protocol/instance.go} | 116 ++++++++++-------- .../instance_test.go} | 76 +++++++----- pkg/config/server.go | 13 +- pkg/config/validation.go | 23 ++-- pkg/test/conformance/stage.go | 2 +- pkg/test/echo/cmd/server/main.go | 6 +- pkg/test/echo/server/endpoint/instance.go | 6 +- pkg/test/echo/server/instance.go | 12 +- .../framework/components/echo/common/call.go | 8 +- .../components/echo/common/config.go | 6 +- .../components/echo/common/envoy_test.go | 16 +-- .../components/echo/common/portgen.go | 28 ++--- .../components/echo/docker/instance.go | 6 +- .../framework/components/echo/docker/port.go | 13 +- .../framework/components/echo/docker/util.go | 11 -- pkg/test/framework/components/echo/echo.go | 4 +- .../components/echo/kube/instance.go | 14 +-- pkg/test/framework/components/mixer/native.go | 2 +- .../conformance/conformance_test.go | 4 +- tests/integration/pilot/locality/main_test.go | 4 +- .../pilot/outboundtrafficpolicy/helper.go | 6 +- tests/integration/pilot/ping_test.go | 6 +- .../pilot/traffic_shifting_test.go | 13 +- tests/integration/security/rbac/v1_test.go | 4 +- tests/integration/security/util/framework.go | 8 +- 65 files changed, 536 insertions(+), 505 deletions(-) rename pkg/config/{protocol.go => protocol/instance.go} (50%) rename pkg/config/{protocol_test.go => protocol/instance_test.go} (52%) diff --git a/galley/pkg/config/processor/transforms/serviceentry/converter/instance_test.go b/galley/pkg/config/processor/transforms/serviceentry/converter/instance_test.go index d438900972b0..4813d07eb859 100644 --- a/galley/pkg/config/processor/transforms/serviceentry/converter/instance_test.go +++ b/galley/pkg/config/processor/transforms/serviceentry/converter/instance_test.go @@ -28,6 +28,7 @@ import ( "istio.io/istio/galley/pkg/config/processor/transforms/serviceentry/pod" "istio.io/istio/galley/pkg/config/resource" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" coreV1 "k8s.io/api/core/v1" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -195,27 +196,27 @@ func TestServicePorts(t *testing.T) { cases := []struct { name string proto coreV1.Protocol - out config.Protocol + out protocol.Instance }{ - {"", coreV1.ProtocolTCP, config.ProtocolTCP}, - {"http", coreV1.ProtocolTCP, config.ProtocolHTTP}, - {"http-test", coreV1.ProtocolTCP, config.ProtocolHTTP}, - {"http", coreV1.ProtocolUDP, config.ProtocolUDP}, - {"httptest", coreV1.ProtocolTCP, config.ProtocolTCP}, - {"https", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"https-test", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"http2", coreV1.ProtocolTCP, config.ProtocolHTTP2}, - {"http2-test", coreV1.ProtocolTCP, config.ProtocolHTTP2}, - {"grpc", coreV1.ProtocolTCP, config.ProtocolGRPC}, - {"grpc-test", coreV1.ProtocolTCP, config.ProtocolGRPC}, - {"grpc-web", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"grpc-web-test", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"mongo", coreV1.ProtocolTCP, config.ProtocolMongo}, - {"mongo-test", coreV1.ProtocolTCP, config.ProtocolMongo}, - {"redis", coreV1.ProtocolTCP, config.ProtocolRedis}, - {"redis-test", coreV1.ProtocolTCP, config.ProtocolRedis}, - {"mysql", coreV1.ProtocolTCP, config.ProtocolMySQL}, - {"mysql-test", coreV1.ProtocolTCP, config.ProtocolMySQL}, + {"", coreV1.ProtocolTCP, protocol.TCP}, + {"http", coreV1.ProtocolTCP, protocol.HTTP}, + {"http-test", coreV1.ProtocolTCP, protocol.HTTP}, + {"http", coreV1.ProtocolUDP, protocol.UDP}, + {"httptest", coreV1.ProtocolTCP, protocol.TCP}, + {"https", coreV1.ProtocolTCP, protocol.HTTPS}, + {"https-test", coreV1.ProtocolTCP, protocol.HTTPS}, + {"http2", coreV1.ProtocolTCP, protocol.HTTP2}, + {"http2-test", coreV1.ProtocolTCP, protocol.HTTP2}, + {"grpc", coreV1.ProtocolTCP, protocol.GRPC}, + {"grpc-test", coreV1.ProtocolTCP, protocol.GRPC}, + {"grpc-web", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"grpc-web-test", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"mongo", coreV1.ProtocolTCP, protocol.Mongo}, + {"mongo-test", coreV1.ProtocolTCP, protocol.Mongo}, + {"redis", coreV1.ProtocolTCP, protocol.Redis}, + {"redis-test", coreV1.ProtocolTCP, protocol.Redis}, + {"mysql", coreV1.ProtocolTCP, protocol.MySQL}, + {"mysql-test", coreV1.ProtocolTCP, protocol.MySQL}, } ip := "10.0.0.1" diff --git a/galley/pkg/config/processor/transforms/serviceentry/create_test.go b/galley/pkg/config/processor/transforms/serviceentry/create_test.go index 2de215f4c6b2..d1dc7f89308c 100644 --- a/galley/pkg/config/processor/transforms/serviceentry/create_test.go +++ b/galley/pkg/config/processor/transforms/serviceentry/create_test.go @@ -28,7 +28,6 @@ import ( mcp "istio.io/api/mcp/v1alpha1" networking "istio.io/api/networking/v1alpha3" - "istio.io/istio/galley/pkg/config/collection" "istio.io/istio/galley/pkg/config/event" "istio.io/istio/galley/pkg/config/meshcfg" @@ -41,7 +40,7 @@ import ( "istio.io/istio/galley/pkg/config/processor/transforms/serviceentry/pod" "istio.io/istio/galley/pkg/config/resource" "istio.io/istio/galley/pkg/config/testing/fixtures" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/mcp/snapshot" coreV1 "k8s.io/api/core/v1" @@ -1060,7 +1059,7 @@ func (b *serviceEntryBuilder) Build() *networking.ServiceEntry { { Name: "http", Number: 80, - Protocol: string(config.ProtocolHTTP), + Protocol: string(protocol.HTTP), }, }, SubjectAltNames: expectedSubjectAltNames(ns, b.serviceAccounts), diff --git a/galley/pkg/runtime/conversions/ingress.go b/galley/pkg/runtime/conversions/ingress.go index 732134b55fae..ab1710991d0f 100644 --- a/galley/pkg/runtime/conversions/ingress.go +++ b/galley/pkg/runtime/conversions/ingress.go @@ -26,6 +26,7 @@ import ( "istio.io/istio/galley/pkg/metadata" "istio.io/istio/galley/pkg/runtime/resource" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/pkg/log" ingress "k8s.io/api/extensions/v1beta1" @@ -199,7 +200,7 @@ func IngressToGateway(key resource.VersionedKey, meta resource.Metadata, i *ingr gateway.Servers = append(gateway.Servers, &v1alpha3.Server{ Port: &v1alpha3.Port{ Number: 443, - Protocol: string(config.ProtocolHTTPS), + Protocol: string(protocol.HTTPS), Name: fmt.Sprintf("https-443-i-%s-%s", name, namespace), }, Hosts: tls.Hosts, @@ -220,7 +221,7 @@ func IngressToGateway(key resource.VersionedKey, meta resource.Metadata, i *ingr gateway.Servers = append(gateway.Servers, &v1alpha3.Server{ Port: &v1alpha3.Port{ Number: 80, - Protocol: string(config.ProtocolHTTP), + Protocol: string(protocol.HTTP), Name: fmt.Sprintf("http-80-i-%s-%s", name, namespace), }, Hosts: []string{"*"}, diff --git a/galley/pkg/runtime/projections/serviceentry/converter/instance_test.go b/galley/pkg/runtime/projections/serviceentry/converter/instance_test.go index 9b046a9970c1..9c7fda81de71 100644 --- a/galley/pkg/runtime/projections/serviceentry/converter/instance_test.go +++ b/galley/pkg/runtime/projections/serviceentry/converter/instance_test.go @@ -30,6 +30,7 @@ import ( "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/protocol" coreV1 "k8s.io/api/core/v1" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -373,27 +374,27 @@ func TestServicePorts(t *testing.T) { cases := []struct { name string proto coreV1.Protocol - out config.Protocol + out protocol.Instance }{ - {"", coreV1.ProtocolTCP, config.ProtocolTCP}, - {"http", coreV1.ProtocolTCP, config.ProtocolHTTP}, - {"http-test", coreV1.ProtocolTCP, config.ProtocolHTTP}, - {"http", coreV1.ProtocolUDP, config.ProtocolUDP}, - {"httptest", coreV1.ProtocolTCP, config.ProtocolTCP}, - {"https", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"https-test", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"http2", coreV1.ProtocolTCP, config.ProtocolHTTP2}, - {"http2-test", coreV1.ProtocolTCP, config.ProtocolHTTP2}, - {"grpc", coreV1.ProtocolTCP, config.ProtocolGRPC}, - {"grpc-test", coreV1.ProtocolTCP, config.ProtocolGRPC}, - {"grpc-web", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"grpc-web-test", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"mongo", coreV1.ProtocolTCP, config.ProtocolMongo}, - {"mongo-test", coreV1.ProtocolTCP, config.ProtocolMongo}, - {"redis", coreV1.ProtocolTCP, config.ProtocolRedis}, - {"redis-test", coreV1.ProtocolTCP, config.ProtocolRedis}, - {"mysql", coreV1.ProtocolTCP, config.ProtocolMySQL}, - {"mysql-test", coreV1.ProtocolTCP, config.ProtocolMySQL}, + {"", coreV1.ProtocolTCP, protocol.TCP}, + {"http", coreV1.ProtocolTCP, protocol.HTTP}, + {"http-test", coreV1.ProtocolTCP, protocol.HTTP}, + {"http", coreV1.ProtocolUDP, protocol.UDP}, + {"httptest", coreV1.ProtocolTCP, protocol.TCP}, + {"https", coreV1.ProtocolTCP, protocol.HTTPS}, + {"https-test", coreV1.ProtocolTCP, protocol.HTTPS}, + {"http2", coreV1.ProtocolTCP, protocol.HTTP2}, + {"http2-test", coreV1.ProtocolTCP, protocol.HTTP2}, + {"grpc", coreV1.ProtocolTCP, protocol.GRPC}, + {"grpc-test", coreV1.ProtocolTCP, protocol.GRPC}, + {"grpc-web", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"grpc-web-test", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"mongo", coreV1.ProtocolTCP, protocol.Mongo}, + {"mongo-test", coreV1.ProtocolTCP, protocol.Mongo}, + {"redis", coreV1.ProtocolTCP, protocol.Redis}, + {"redis-test", coreV1.ProtocolTCP, protocol.Redis}, + {"mysql", coreV1.ProtocolTCP, protocol.MySQL}, + {"mysql-test", coreV1.ProtocolTCP, protocol.MySQL}, } ip := "10.0.0.1" diff --git a/galley/pkg/runtime/projections/serviceentry/handler_bench_test.go b/galley/pkg/runtime/projections/serviceentry/handler_bench_test.go index e5c06da752cd..c40e36e8e36f 100644 --- a/galley/pkg/runtime/projections/serviceentry/handler_bench_test.go +++ b/galley/pkg/runtime/projections/serviceentry/handler_bench_test.go @@ -18,14 +18,14 @@ import ( "strconv" "testing" + coreV1 "k8s.io/api/core/v1" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "istio.io/istio/galley/pkg/metadata" "istio.io/istio/galley/pkg/runtime/processing" "istio.io/istio/galley/pkg/runtime/projections/serviceentry" "istio.io/istio/galley/pkg/runtime/projections/serviceentry/pod" "istio.io/istio/galley/pkg/runtime/resource" - - coreV1 "k8s.io/api/core/v1" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) var ( diff --git a/galley/pkg/runtime/projections/serviceentry/handler_test.go b/galley/pkg/runtime/projections/serviceentry/handler_test.go index 6a138804865c..9b12e28f0b0c 100644 --- a/galley/pkg/runtime/projections/serviceentry/handler_test.go +++ b/galley/pkg/runtime/projections/serviceentry/handler_test.go @@ -24,6 +24,8 @@ import ( "time" "github.com/gogo/protobuf/types" + coreV1 "k8s.io/api/core/v1" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" "istio.io/api/annotation" mcp "istio.io/api/mcp/v1alpha1" @@ -33,10 +35,7 @@ import ( "istio.io/istio/galley/pkg/runtime/projections/serviceentry" "istio.io/istio/galley/pkg/runtime/projections/serviceentry/pod" "istio.io/istio/galley/pkg/runtime/resource" - "istio.io/istio/pkg/config" - - coreV1 "k8s.io/api/core/v1" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "istio.io/istio/pkg/config/protocol" ) const ( @@ -945,7 +944,7 @@ func (b *serviceEntryBuilder) Build() *networking.ServiceEntry { { Name: "http", Number: 80, - Protocol: string(config.ProtocolHTTP), + Protocol: string(protocol.HTTP), }, }, SubjectAltNames: expectedSubjectAltNames(ns, b.serviceAccounts), diff --git a/istioctl/pkg/validate/validate.go b/istioctl/pkg/validate/validate.go index 122900c0f65e..90d798764e66 100644 --- a/istioctl/pkg/validate/validate.go +++ b/istioctl/pkg/validate/validate.go @@ -21,9 +21,9 @@ import ( "os" "strings" - "github.com/hashicorp/go-multierror" + multierror "github.com/hashicorp/go-multierror" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" mixercrd "istio.io/istio/mixer/pkg/config/crd" mixerstore "istio.io/istio/mixer/pkg/config/store" @@ -32,7 +32,7 @@ import ( "istio.io/istio/pilot/pkg/config/kube/crd" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/serviceregistry/kube/controller" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/pkg/log" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -328,8 +328,8 @@ func servicePortPrefixed(n string) bool { if i >= 0 { n = n[:i] } - protocol := config.ParseProtocol(n) - return protocol == config.ProtocolUnsupported + p := protocol.Parse(n) + return p == protocol.Unsupported } func handleNamespace(istioNamespace string) string { if istioNamespace == "" { diff --git a/pilot/pkg/config/kube/ingress/conversion.go b/pilot/pkg/config/kube/ingress/conversion.go index 811b63f5646c..a03b0ba1e621 100644 --- a/pilot/pkg/config/kube/ingress/conversion.go +++ b/pilot/pkg/config/kube/ingress/conversion.go @@ -21,16 +21,16 @@ import ( "strings" "github.com/hashicorp/go-multierror" + "k8s.io/api/extensions/v1beta1" + "k8s.io/apimachinery/pkg/util/intstr" meshconfig "istio.io/api/mesh/v1alpha1" networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/serviceregistry/kube" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/pkg/log" - - "k8s.io/api/extensions/v1beta1" - "k8s.io/apimachinery/pkg/util/intstr" ) // EncodeIngressRuleName encodes an ingress rule name for a given ingress resource name, @@ -81,7 +81,7 @@ func ConvertIngressV1alpha3(ingress v1beta1.Ingress, domainSuffix string) model. gateway.Servers = append(gateway.Servers, &networking.Server{ Port: &networking.Port{ Number: 443, - Protocol: string(config.ProtocolHTTPS), + Protocol: string(protocol.HTTPS), Name: fmt.Sprintf("https-443-ingress-%s-%s", ingress.Name, ingress.Namespace), }, Hosts: tls.Hosts, @@ -102,7 +102,7 @@ func ConvertIngressV1alpha3(ingress v1beta1.Ingress, domainSuffix string) model. gateway.Servers = append(gateway.Servers, &networking.Server{ Port: &networking.Port{ Number: 80, - Protocol: string(config.ProtocolHTTP), + Protocol: string(protocol.HTTP), Name: fmt.Sprintf("http-80-ingress-%s-%s", ingress.Name, ingress.Namespace), }, Hosts: []string{"*"}, diff --git a/pilot/pkg/model/config_test.go b/pilot/pkg/model/config_test.go index 5209f0d89009..951550a81239 100644 --- a/pilot/pkg/model/config_test.go +++ b/pilot/pkg/model/config_test.go @@ -33,6 +33,7 @@ import ( "istio.io/istio/pilot/pkg/model" mock_config "istio.io/istio/pilot/test/mock" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // getByMessageName finds a schema by message name if it is available @@ -96,8 +97,8 @@ func TestEventString(t *testing.T) { func TestPortList(t *testing.T) { pl := model.PortList{ - {Name: "http", Port: 80, Protocol: config.ProtocolHTTP}, - {Name: "http-alt", Port: 8080, Protocol: config.ProtocolHTTP}, + {Name: "http", Port: 80, Protocol: protocol.HTTP}, + {Name: "http-alt", Port: 8080, Protocol: protocol.HTTP}, } gotNames := pl.GetNames() @@ -130,7 +131,7 @@ func TestServiceKey(t *testing.T) { // Verify Service.Key() delegates to ServiceKey() { want := "hostname|http|a=b,c=d" - port := &model.Port{Name: "http", Port: 80, Protocol: config.ProtocolHTTP} + port := &model.Port{Name: "http", Port: 80, Protocol: protocol.HTTP} labels := config.Labels{"a": "b", "c": "d"} got := svc.Key(port, labels) if !reflect.DeepEqual(got, want) { @@ -145,19 +146,19 @@ func TestServiceKey(t *testing.T) { }{ { port: model.PortList{ - {Name: "http", Port: 80, Protocol: config.ProtocolHTTP}, - {Name: "http-alt", Port: 8080, Protocol: config.ProtocolHTTP}, + {Name: "http", Port: 80, Protocol: protocol.HTTP}, + {Name: "http-alt", Port: 8080, Protocol: protocol.HTTP}, }, labels: config.LabelsCollection{{"a": "b", "c": "d"}}, want: "hostname|http,http-alt|a=b,c=d", }, { - port: model.PortList{{Name: "http", Port: 80, Protocol: config.ProtocolHTTP}}, + port: model.PortList{{Name: "http", Port: 80, Protocol: protocol.HTTP}}, labels: config.LabelsCollection{{"a": "b", "c": "d"}}, want: "hostname|http|a=b,c=d", }, { - port: model.PortList{{Port: 80, Protocol: config.ProtocolHTTP}}, + port: model.PortList{{Port: 80, Protocol: protocol.HTTP}}, labels: config.LabelsCollection{{"a": "b", "c": "d"}}, want: "hostname||a=b,c=d", }, @@ -167,12 +168,12 @@ func TestServiceKey(t *testing.T) { want: "hostname||a=b,c=d", }, { - port: model.PortList{{Name: "http", Port: 80, Protocol: config.ProtocolHTTP}}, + port: model.PortList{{Name: "http", Port: 80, Protocol: protocol.HTTP}}, labels: config.LabelsCollection{nil}, want: "hostname|http", }, { - port: model.PortList{{Name: "http", Port: 80, Protocol: config.ProtocolHTTP}}, + port: model.PortList{{Name: "http", Port: 80, Protocol: protocol.HTTP}}, labels: config.LabelsCollection{}, want: "hostname|http", }, diff --git a/pilot/pkg/model/gateway.go b/pilot/pkg/model/gateway.go index c10fe5f8b39e..d4a45a2d6f6e 100644 --- a/pilot/pkg/model/gateway.go +++ b/pilot/pkg/model/gateway.go @@ -22,6 +22,7 @@ import ( networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/monitoring" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // MergedGateway describes a set of gateways for a workload merged into a single logical gateway. @@ -90,7 +91,7 @@ func MergeGateways(gateways ...Config) *MergedGateway { sanitizeServerHostNamespace(s, gatewayConfig.Namespace) gatewayNameForServer[s] = gatewayName log.Debugf("MergeGateways: gateway %q processing server %v", gatewayName, s.Hosts) - protocol := config.ParseProtocol(s.Port.Protocol) + p := protocol.Parse(s.Port.Protocol) if s.Tls != nil { // Envoy will reject config that has multiple filter chain matches with the same matching rules @@ -113,11 +114,11 @@ func MergeGateways(gateways ...Config) *MergedGateway { // for each server (as each server ends up as a separate http connection manager due to filter chain match // 3. No for everything else. - if p, exists := plaintextServers[s.Port.Number]; exists { - currentProto := config.ParseProtocol(p[0].Port.Protocol) - if currentProto != protocol || !protocol.IsHTTP() { + if server, exists := plaintextServers[s.Port.Number]; exists { + currentProto := protocol.Parse(server[0].Port.Protocol) + if currentProto != p || !p.IsHTTP() { log.Debugf("skipping server on gateway %s port %s.%d.%s: conflict with existing server %s.%d.%s", - gatewayConfig.Name, s.Port.Name, s.Port.Number, s.Port.Protocol, p[0].Port.Name, p[0].Port.Number, p[0].Port.Protocol) + gatewayConfig.Name, s.Port.Name, s.Port.Number, s.Port.Protocol, server[0].Port.Name, server[0].Port.Number, server[0].Port.Protocol) recordRejectedConfig(gatewayName) continue } @@ -245,12 +246,12 @@ func checkDuplicates(hosts []string, knownHosts map[string]struct{}) []string { // different ports, the optimization (one RDS instead of two) could quickly become useless the moment the set of // hosts on the two servers start differing -- necessitating the need for two different RDS routes. func gatewayRDSRouteName(server *networking.Server, cfg Config) string { - protocol := config.ParseProtocol(server.Port.Protocol) - if protocol.IsHTTP() { + p := protocol.Parse(server.Port.Protocol) + if p.IsHTTP() { return fmt.Sprintf("http.%d", server.Port.Number) } - if protocol == config.ProtocolHTTPS && server.Tls != nil && !config.IsPassThroughServer(server) { + if p == protocol.HTTPS && server.Tls != nil && !config.IsPassThroughServer(server) { return fmt.Sprintf("https.%d.%s.%s.%s", server.Port.Number, server.Port.Name, cfg.Name, cfg.Namespace) } diff --git a/pilot/pkg/model/push_context.go b/pilot/pkg/model/push_context.go index 012086302add..c2fb8be266fb 100644 --- a/pilot/pkg/model/push_context.go +++ b/pilot/pkg/model/push_context.go @@ -22,6 +22,7 @@ import ( networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/monitoring" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // PushContext tracks the status of a push - metrics and errors. @@ -641,7 +642,7 @@ func (ps *PushContext) initServiceAccounts(env *Environment, services []*Service for _, svc := range services { ps.ServiceAccounts[svc.Hostname] = map[int][]string{} for _, port := range svc.Ports { - if port.Protocol == config.ProtocolUDP { + if port.Protocol == protocol.UDP { continue } ps.ServiceAccounts[svc.Hostname][port.Port] = env.GetIstioServiceAccounts(svc, []int{port.Port}) diff --git a/pilot/pkg/model/service.go b/pilot/pkg/model/service.go index b8ec7eb5e88f..c566f9518484 100644 --- a/pilot/pkg/model/service.go +++ b/pilot/pkg/model/service.go @@ -35,6 +35,7 @@ import ( authn "istio.io/api/authentication/v1alpha1" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // Service describes an Istio service (e.g., catalog.mystore.com:8080) @@ -126,7 +127,7 @@ type Port struct { Port int `json:"port"` // Protocol to be used for the port. - Protocol config.Protocol `json:"protocol,omitempty"` + Protocol protocol.Instance `json:"protocol,omitempty"` } // PortList is a set of ports @@ -471,8 +472,8 @@ func (ports PortList) Get(name string) (*Port, bool) { // GetByPort retrieves a port declaration by port value func (ports PortList) GetByPort(num int) (*Port, bool) { for _, port := range ports { - if port.Port == num && port.Protocol != config.ProtocolUDP && - port.Protocol != config.ProtocolUnsupported { + if port.Port == num && port.Protocol != protocol.UDP && + port.Protocol != protocol.Unsupported { return port, true } } diff --git a/pilot/pkg/model/validation_test.go b/pilot/pkg/model/validation_test.go index 457fd81c931b..1930d988be4d 100644 --- a/pilot/pkg/model/validation_test.go +++ b/pilot/pkg/model/validation_test.go @@ -23,6 +23,7 @@ import ( "istio.io/istio/pilot/pkg/model/test" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) const ( @@ -160,15 +161,15 @@ var ( endpoint1 = NetworkEndpoint{ Address: "192.168.1.1", Port: 10001, - ServicePort: &Port{Name: "http", Port: 81, Protocol: config.ProtocolHTTP}, + ServicePort: &Port{Name: "http", Port: 81, Protocol: protocol.HTTP}, } service1 = &Service{ Hostname: "one.service.com", Address: "192.168.3.1", // VIP Ports: PortList{ - &Port{Name: "http", Port: 81, Protocol: config.ProtocolHTTP}, - &Port{Name: "http-alt", Port: 8081, Protocol: config.ProtocolHTTP}, + &Port{Name: "http", Port: 81, Protocol: protocol.HTTP}, + &Port{Name: "http-alt", Port: 8081, Protocol: protocol.HTTP}, }, } ) @@ -235,7 +236,7 @@ func TestServiceInstanceValidate(t *testing.T) { ServicePort: &Port{ Name: "http", Port: service1.Ports[1].Port + 1, - Protocol: config.ProtocolGRPC, + Protocol: protocol.GRPC, }, }, }, @@ -251,13 +252,13 @@ func TestServiceInstanceValidate(t *testing.T) { func TestServiceValidate(t *testing.T) { ports := PortList{ - {Name: "http", Port: 80, Protocol: config.ProtocolHTTP}, - {Name: "http-alt", Port: 8080, Protocol: config.ProtocolHTTP}, + {Name: "http", Port: 80, Protocol: protocol.HTTP}, + {Name: "http-alt", Port: 8080, Protocol: protocol.HTTP}, } badPorts := PortList{ - {Port: 80, Protocol: config.ProtocolHTTP}, - {Name: "http-alt^", Port: 8080, Protocol: config.ProtocolHTTP}, - {Name: "http", Port: -80, Protocol: config.ProtocolHTTP}, + {Port: 80, Protocol: protocol.HTTP}, + {Name: "http-alt^", Port: 8080, Protocol: protocol.HTTP}, + {Name: "http", Port: -80, Protocol: protocol.HTTP}, } address := "192.168.1.1" diff --git a/pilot/pkg/networking/core/v1alpha3/cluster.go b/pilot/pkg/networking/core/v1alpha3/cluster.go index b34ee6a1a3ff..9b9d790b2419 100644 --- a/pilot/pkg/networking/core/v1alpha3/cluster.go +++ b/pilot/pkg/networking/core/v1alpha3/cluster.go @@ -39,6 +39,7 @@ import ( "istio.io/istio/pilot/pkg/networking/util" authn_model "istio.io/istio/pilot/pkg/security/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) const ( @@ -148,7 +149,7 @@ func (configgen *ConfigGeneratorImpl) buildOutboundClusters(env *model.Environme for _, service := range push.Services(proxy) { destRule := push.DestinationRule(proxy, service) for _, port := range service.Ports { - if port.Protocol == config.ProtocolUDP { + if port.Protocol == protocol.UDP { continue } inputParams.Service = service @@ -253,7 +254,7 @@ func (configgen *ConfigGeneratorImpl) buildOutboundSniDnatClusters(env *model.En for _, service := range push.Services(proxy) { destRule := push.DestinationRule(proxy, service) for _, port := range service.Ports { - if port.Protocol == config.ProtocolUDP { + if port.Protocol == protocol.UDP { continue } lbEndpoints := buildLocalityLbEndpoints(env, networkView, service, port.Port, nil) @@ -476,7 +477,7 @@ func (configgen *ConfigGeneratorImpl) buildInboundClusters(env *model.Environmen // as inbound|portNumber|portName|Hostname listenPort := &model.Port{ Port: int(ingressListener.Port.Number), - Protocol: config.ParseProtocol(ingressListener.Port.Protocol), + Protocol: protocol.Parse(ingressListener.Port.Protocol), Name: ingressListener.Port.Name, } @@ -888,7 +889,7 @@ func applyLoadBalancer(cluster *apiv2.Cluster, lb *networking.LoadBalancerSettin } // Redis protocol must be defaulted with MAGLEV to benefit from client side sharding. - if features.EnableRedisFilter.Get() && port != nil && port.Protocol == config.ProtocolRedis { + if features.EnableRedisFilter.Get() && port != nil && port.Protocol == protocol.Redis { cluster.LbPolicy = apiv2.Cluster_MAGLEV return } diff --git a/pilot/pkg/networking/core/v1alpha3/cluster_test.go b/pilot/pkg/networking/core/v1alpha3/cluster_test.go index adf596b063a1..dad9a9d6d884 100644 --- a/pilot/pkg/networking/core/v1alpha3/cluster_test.go +++ b/pilot/pkg/networking/core/v1alpha3/cluster_test.go @@ -22,8 +22,6 @@ import ( "testing" "time" - "istio.io/istio/pilot/pkg/features" - apiv2 "github.com/envoyproxy/go-control-plane/envoy/api/v2" "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" "github.com/gogo/protobuf/proto" @@ -32,10 +30,12 @@ import ( meshconfig "istio.io/api/mesh/v1alpha1" networking "istio.io/api/networking/v1alpha3" + "istio.io/istio/pilot/pkg/features" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/networking/core/v1alpha3/fakes" "istio.io/istio/pilot/pkg/networking/plugin" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) type ConfigType int @@ -188,7 +188,7 @@ func buildTestClustersWithProxyMetadata(serviceHostname string, serviceResolutio servicePort := &model.Port{ Name: "default", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, } service := &model.Service{ Hostname: config.Hostname(serviceHostname), @@ -812,7 +812,7 @@ func TestBuildLocalityLbEndpoints(t *testing.T) { servicePort := &model.Port{ Name: "default", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, } service := &model.Service{ Hostname: config.Hostname("*.example.org"), @@ -953,7 +953,7 @@ func TestRedisProtocolWithPassThroughResolution(t *testing.T) { servicePort := &model.Port{ Name: "redis-port", Port: 6379, - Protocol: config.ProtocolRedis, + Protocol: protocol.Redis, } service := &model.Service{ Hostname: config.Hostname("redis.com"), @@ -991,7 +991,7 @@ func TestRedisProtocolCluster(t *testing.T) { servicePort := &model.Port{ Name: "redis-port", Port: 6379, - Protocol: config.ProtocolRedis, + Protocol: protocol.Redis, } service := &model.Service{ Hostname: config.Hostname("redis.com"), diff --git a/pilot/pkg/networking/core/v1alpha3/gateway.go b/pilot/pkg/networking/core/v1alpha3/gateway.go index 9959873f5871..b0d9e3d394b2 100644 --- a/pilot/pkg/networking/core/v1alpha3/gateway.go +++ b/pilot/pkg/networking/core/v1alpha3/gateway.go @@ -25,7 +25,7 @@ import ( "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener" "github.com/envoyproxy/go-control-plane/envoy/api/v2/route" http_conn "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2" - "github.com/hashicorp/go-multierror" + multierror "github.com/hashicorp/go-multierror" networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/features" @@ -35,6 +35,7 @@ import ( "istio.io/istio/pilot/pkg/networking/util" authn_model "istio.io/istio/pilot/pkg/security/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/proto" "istio.io/pkg/log" ) @@ -72,9 +73,9 @@ func (configgen *ConfigGeneratorImpl) buildGatewayListeners(env *model.Environme bindToPort: true, } - protocol := config.ParseProtocol(servers[0].Port.Protocol) - listenerProtocol := plugin.ModelProtocolToListenerProtocol(protocol) - if protocol.IsHTTP() { + p := protocol.Parse(servers[0].Port.Protocol) + listenerProtocol := plugin.ModelProtocolToListenerProtocol(p) + if p.IsHTTP() { // We have a list of HTTP servers on this port. Build a single listener for the server port. // We only need to look at the first server in the list as the merge logic // ensures that all servers are of same type. @@ -144,7 +145,7 @@ func (configgen *ConfigGeneratorImpl) buildGatewayListeners(env *model.Environme Port: &model.Port{ Name: servers[0].Port.Name, Port: int(portNumber), - Protocol: protocol, + Protocol: p, }, } for _, p := range configgen.Plugins { @@ -327,7 +328,7 @@ func (configgen *ConfigGeneratorImpl) buildGatewayHTTPRouteConfig(env *model.Env func (configgen *ConfigGeneratorImpl) createGatewayHTTPFilterChainOpts( node *model.Proxy, server *networking.Server, routeName string) *filterChainOpts { - serverProto := config.ParseProtocol(server.Port.Protocol) + serverProto := protocol.Parse(server.Port.Protocol) httpProtoOpts := &core.Http1ProtocolOptions{} @@ -552,7 +553,7 @@ func buildGatewayNetworkFiltersFromTCPRoutes(node *model.Proxy, env *model.Envir port := &model.Port{ Name: server.Port.Name, Port: int(server.Port.Number), - Protocol: config.ParseProtocol(server.Port.Protocol), + Protocol: protocol.Parse(server.Port.Protocol), } gatewayServerHosts := make(map[config.Hostname]bool, len(server.Hosts)) @@ -594,7 +595,7 @@ func buildGatewayNetworkFiltersFromTLSRoutes(node *model.Proxy, env *model.Envir port := &model.Port{ Name: server.Port.Name, Port: int(server.Port.Number), - Protocol: config.ParseProtocol(server.Port.Protocol), + Protocol: protocol.Parse(server.Port.Protocol), } gatewayServerHosts := make(map[config.Hostname]bool, len(server.Hosts)) diff --git a/pilot/pkg/networking/core/v1alpha3/httproute.go b/pilot/pkg/networking/core/v1alpha3/httproute.go index 26373c2f08bc..8c6baea70b13 100644 --- a/pilot/pkg/networking/core/v1alpha3/httproute.go +++ b/pilot/pkg/networking/core/v1alpha3/httproute.go @@ -30,6 +30,7 @@ import ( "istio.io/istio/pilot/pkg/networking/plugin" "istio.io/istio/pilot/pkg/networking/util" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/proto" "istio.io/pkg/log" ) @@ -275,7 +276,7 @@ func (configgen *ConfigGeneratorImpl) buildSidecarOutboundHTTPRouteConfig(env *m Port: &model.Port{ Name: "", Port: listenerPort, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, } diff --git a/pilot/pkg/networking/core/v1alpha3/httproute_test.go b/pilot/pkg/networking/core/v1alpha3/httproute_test.go index 5dc16b27c1a5..107189820ab1 100644 --- a/pilot/pkg/networking/core/v1alpha3/httproute_test.go +++ b/pilot/pkg/networking/core/v1alpha3/httproute_test.go @@ -21,13 +21,13 @@ import ( "sort" "testing" - "istio.io/istio/pilot/pkg/features" - meshconfig "istio.io/api/mesh/v1alpha1" networking "istio.io/api/networking/v1alpha3" + "istio.io/istio/pilot/pkg/features" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/networking/plugin" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) func TestGenerateVirtualHostDomains(t *testing.T) { @@ -618,7 +618,7 @@ func buildHTTPService(hostname string, visibility config.Visibility, ip, namespa Ports = append(Ports, &model.Port{ Name: fmt.Sprintf("http-%d", p), Port: p, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }) } diff --git a/pilot/pkg/networking/core/v1alpha3/listener.go b/pilot/pkg/networking/core/v1alpha3/listener.go index 462b5005758b..5577f43cd301 100644 --- a/pilot/pkg/networking/core/v1alpha3/listener.go +++ b/pilot/pkg/networking/core/v1alpha3/listener.go @@ -45,6 +45,7 @@ import ( "istio.io/istio/pilot/pkg/networking/util" authn_model "istio.io/istio/pilot/pkg/security/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/proto" "istio.io/pkg/log" ) @@ -313,7 +314,7 @@ func (configgen *ConfigGeneratorImpl) buildSidecarInboundListeners(env *model.En listenPort := &model.Port{ Port: int(ingressListener.Port.Number), - Protocol: config.ParseProtocol(ingressListener.Port.Protocol), + Protocol: protocol.Parse(ingressListener.Port.Protocol), Name: ingressListener.Port.Name, } @@ -401,7 +402,7 @@ func (configgen *ConfigGeneratorImpl) buildSidecarInboundHTTPListenerOptsForPort // See https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld#configure-the-proxy if pluginParams.ServiceInstance.Endpoint.ServicePort.Protocol.IsHTTP2() { httpOpts.connectionManager.Http2ProtocolOptions = &core.Http2ProtocolOptions{} - if pluginParams.ServiceInstance.Endpoint.ServicePort.Protocol == config.ProtocolGRPCWeb { + if pluginParams.ServiceInstance.Endpoint.ServicePort.Protocol == protocol.GRPCWeb { httpOpts.addGRPCWebFilter = true } } @@ -521,7 +522,7 @@ type outboundListenerEntry struct { locked bool } -func protocolName(p config.Protocol) string { +func protocolName(p protocol.Instance) string { switch plugin.ModelProtocolToListenerProtocol(p) { case plugin.ListenerProtocolHTTP: return "HTTP" @@ -536,10 +537,10 @@ type outboundListenerConflict struct { metric monitoring.Metric node *model.Proxy listenerName string - currentProtocol config.Protocol + currentProtocol protocol.Instance currentServices []*model.Service newHostname config.Hostname - newProtocol config.Protocol + newProtocol protocol.Instance } func (c outboundListenerConflict) addMetric(push *model.PushContext) { @@ -637,7 +638,7 @@ func (configgen *ConfigGeneratorImpl) buildSidecarOutboundListeners(env *model.E listenPort := &model.Port{ Port: int(egressListener.IstioListener.Port.Number), - Protocol: config.ParseProtocol(egressListener.IstioListener.Port.Protocol), + Protocol: protocol.Parse(egressListener.IstioListener.Port.Protocol), Name: egressListener.IstioListener.Port.Name, } @@ -1281,7 +1282,7 @@ func (configgen *ConfigGeneratorImpl) onVirtualOutboundListener(env *model.Envir mesh := env.Mesh redirectPort := &model.Port{ Port: int(mesh.ProxyListenPort), - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, } if len(ipTablesListener.FilterChains) < 1 || len(ipTablesListener.FilterChains[0].Filters) < 1 { @@ -1367,8 +1368,8 @@ func buildSidecarInboundMgmtListeners(node *model.Proxy, env *model.Environment, // assumes that inbound connections/requests are sent to the endpoint address for _, mPort := range managementPorts { switch mPort.Protocol { - case config.ProtocolHTTP, config.ProtocolHTTP2, config.ProtocolGRPC, config.ProtocolGRPCWeb, config.ProtocolTCP, - config.ProtocolHTTPS, config.ProtocolTLS, config.ProtocolMongo, config.ProtocolRedis, config.ProtocolMySQL: + case protocol.HTTP, protocol.HTTP2, protocol.GRPC, protocol.GRPCWeb, protocol.TCP, + protocol.HTTPS, protocol.TLS, protocol.Mongo, protocol.Redis, protocol.MySQL: instance := &model.ServiceInstance{ Endpoint: model.NetworkEndpoint{ diff --git a/pilot/pkg/networking/core/v1alpha3/listener_builder_test.go b/pilot/pkg/networking/core/v1alpha3/listener_builder_test.go index e0a798b1a0cc..edc8f5cf3df4 100644 --- a/pilot/pkg/networking/core/v1alpha3/listener_builder_test.go +++ b/pilot/pkg/networking/core/v1alpha3/listener_builder_test.go @@ -20,7 +20,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/networking/plugin" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) type LdsEnv struct { @@ -56,7 +56,7 @@ func TestListenerBuilder(t *testing.T) { // prepare t.Helper() ldsEnv := getDefaultLdsEnv() - service := buildService("test.com", wildcardIP, config.ProtocolHTTP, tnow) + service := buildService("test.com", wildcardIP, protocol.HTTP, tnow) services := []*model.Service{service} env := buildListenerEnv(services) @@ -82,10 +82,10 @@ func TestListenerBuilder(t *testing.T) { if len(listeners) != 1 { t.Fatalf("expected %d listeners, found %d", 1, len(listeners)) } - protocol := service.Ports[0].Protocol - if protocol != config.ProtocolHTTP && isHTTPListener(listeners[0]) { + p := service.Ports[0].Protocol + if p != protocol.HTTP && isHTTPListener(listeners[0]) { t.Fatal("expected TCP listener, found HTTP") - } else if protocol == config.ProtocolHTTP && !isHTTPListener(listeners[0]) { + } else if p == protocol.HTTP && !isHTTPListener(listeners[0]) { t.Fatal("expected HTTP listener, found TCP") } verifyInboundHTTPListenerServerName(t, listeners[0]) @@ -100,7 +100,7 @@ func TestVirtualListenerBuilder(t *testing.T) { // prepare t.Helper() ldsEnv := getDefaultLdsEnv() - service := buildService("test.com", wildcardIP, config.ProtocolHTTP, tnow) + service := buildService("test.com", wildcardIP, protocol.HTTP, tnow) services := []*model.Service{service} env := buildListenerEnv(services) @@ -148,7 +148,7 @@ func TestVirtualInboundListenerBuilder(t *testing.T) { // prepare t.Helper() ldsEnv := getDefaultLdsEnv() - service := buildService("test.com", wildcardIP, config.ProtocolHTTP, tnow) + service := buildService("test.com", wildcardIP, protocol.HTTP, tnow) services := []*model.Service{service} env := buildListenerEnv(services) diff --git a/pilot/pkg/networking/core/v1alpha3/listener_test.go b/pilot/pkg/networking/core/v1alpha3/listener_test.go index 7d00180c9fc2..f7e587f05421 100644 --- a/pilot/pkg/networking/core/v1alpha3/listener_test.go +++ b/pilot/pkg/networking/core/v1alpha3/listener_test.go @@ -29,13 +29,13 @@ import ( "github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/types" - "istio.io/istio/pilot/pkg/features" - networking "istio.io/api/networking/v1alpha3" + "istio.io/istio/pilot/pkg/features" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/networking/core/v1alpha3/fakes" "istio.io/istio/pilot/pkg/networking/plugin" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) const ( @@ -114,33 +114,33 @@ func TestOutboundListenerConflict_HTTPWithCurrentTCP(t *testing.T) { // The oldest service port is TCP. We should encounter conflicts when attempting to add the HTTP ports. Purposely // storing the services out of time order to test that it's being sorted properly. testOutboundListenerConflict(t, - buildService("test1.com", wildcardIP, config.ProtocolHTTP, tnow.Add(1*time.Second)), - buildService("test2.com", wildcardIP, config.ProtocolTCP, tnow), - buildService("test3.com", wildcardIP, config.ProtocolHTTP, tnow.Add(2*time.Second))) + buildService("test1.com", wildcardIP, protocol.HTTP, tnow.Add(1*time.Second)), + buildService("test2.com", wildcardIP, protocol.TCP, tnow), + buildService("test3.com", wildcardIP, protocol.HTTP, tnow.Add(2*time.Second))) } func TestOutboundListenerConflict_TCPWithCurrentHTTP(t *testing.T) { // The oldest service port is HTTP. We should encounter conflicts when attempting to add the TCP ports. Purposely // storing the services out of time order to test that it's being sorted properly. testOutboundListenerConflict(t, - buildService("test1.com", wildcardIP, config.ProtocolTCP, tnow.Add(1*time.Second)), - buildService("test2.com", wildcardIP, config.ProtocolHTTP, tnow), - buildService("test3.com", wildcardIP, config.ProtocolTCP, tnow.Add(2*time.Second))) + buildService("test1.com", wildcardIP, protocol.TCP, tnow.Add(1*time.Second)), + buildService("test2.com", wildcardIP, protocol.HTTP, tnow), + buildService("test3.com", wildcardIP, protocol.TCP, tnow.Add(2*time.Second))) } func TestOutboundListenerConflict_Unordered(t *testing.T) { // Ensure that the order is preserved when all the times match. The first service in the list wins. testOutboundListenerConflict(t, - buildService("test1.com", wildcardIP, config.ProtocolHTTP, tzero), - buildService("test2.com", wildcardIP, config.ProtocolTCP, tzero), - buildService("test3.com", wildcardIP, config.ProtocolTCP, tzero)) + buildService("test1.com", wildcardIP, protocol.HTTP, tzero), + buildService("test2.com", wildcardIP, protocol.TCP, tzero), + buildService("test3.com", wildcardIP, protocol.TCP, tzero)) } func TestOutboundListenerConflict_TCPWithCurrentTCP(t *testing.T) { services := []*model.Service{ - buildService("test1.com", "1.2.3.4", config.ProtocolTCP, tnow.Add(1*time.Second)), - buildService("test2.com", "1.2.3.4", config.ProtocolTCP, tnow), - buildService("test3.com", "1.2.3.4", config.ProtocolTCP, tnow.Add(2*time.Second)), + buildService("test1.com", "1.2.3.4", protocol.TCP, tnow.Add(1*time.Second)), + buildService("test2.com", "1.2.3.4", protocol.TCP, tnow), + buildService("test3.com", "1.2.3.4", protocol.TCP, tnow.Add(2*time.Second)), } p := &fakePlugin{} listeners := buildOutboundListeners(p, nil, nil, services...) @@ -155,9 +155,9 @@ func TestOutboundListenerConflict_TCPWithCurrentTCP(t *testing.T) { oldestService := getOldestService(services...) oldestProtocol := oldestService.Ports[0].Protocol - if oldestProtocol != config.ProtocolHTTP && isHTTPListener(listeners[0]) { + if oldestProtocol != protocol.HTTP && isHTTPListener(listeners[0]) { t.Fatal("expected TCP listener, found HTTP") - } else if oldestProtocol == config.ProtocolHTTP && !isHTTPListener(listeners[0]) { + } else if oldestProtocol == protocol.HTTP && !isHTTPListener(listeners[0]) { t.Fatal("expected HTTP listener, found TCP") } @@ -194,7 +194,7 @@ func TestOutboundListenerTCPWithVS(t *testing.T) { tt.expectedChains = append([]string{"1.1.1.1"}, tt.expectedChains...) } services := []*model.Service{ - buildService("test.com", tt.CIDR, config.ProtocolTCP, tnow), + buildService("test.com", tt.CIDR, protocol.TCP, tnow), } p := &fakePlugin{} @@ -230,10 +230,10 @@ func TestInboundListenerConfig_HTTP(t *testing.T) { for _, p := range []*model.Proxy{&proxy, &proxyHTTP10} { // Add a service and verify it's config testInboundListenerConfig(t, p, - buildService("test.com", wildcardIP, config.ProtocolHTTP, tnow)) + buildService("test.com", wildcardIP, protocol.HTTP, tnow)) testInboundListenerConfigWithoutServices(t, p) testInboundListenerConfigWithSidecar(t, p, - buildService("test.com", wildcardIP, config.ProtocolHTTP, tnow)) + buildService("test.com", wildcardIP, protocol.HTTP, tnow)) testInboundListenerConfigWithSidecarWithoutServices(t, p) } } @@ -241,9 +241,9 @@ func TestInboundListenerConfig_HTTP(t *testing.T) { func TestOutboundListenerConfig_WithSidecar(t *testing.T) { // Add a service and verify it's config services := []*model.Service{ - buildService("test1.com", wildcardIP, config.ProtocolHTTP, tnow.Add(1*time.Second)), - buildService("test2.com", wildcardIP, config.ProtocolTCP, tnow), - buildService("test3.com", wildcardIP, config.ProtocolHTTP, tnow.Add(2*time.Second))} + buildService("test1.com", wildcardIP, protocol.HTTP, tnow.Add(1*time.Second)), + buildService("test2.com", wildcardIP, protocol.TCP, tnow), + buildService("test3.com", wildcardIP, protocol.HTTP, tnow.Add(2*time.Second))} testOutboundListenerConfigWithSidecar(t, services...) testOutboundListenerConfigWithSidecarWithCaptureModeNone(t, services...) testOutboundListenerConfigWithSidecarWithUseRemoteAddress(t, services...) @@ -297,9 +297,9 @@ func testOutboundListenerConflict(t *testing.T, services ...*model.Service) { } oldestProtocol := oldestService.Ports[0].Protocol - if oldestProtocol != config.ProtocolHTTP && isHTTPListener(listeners[0]) { + if oldestProtocol != protocol.HTTP && isHTTPListener(listeners[0]) { t.Fatal("expected TCP listener, found HTTP") - } else if oldestProtocol == config.ProtocolHTTP && !isHTTPListener(listeners[0]) { + } else if oldestProtocol == protocol.HTTP && !isHTTPListener(listeners[0]) { t.Fatal("expected HTTP listener, found TCP") } @@ -321,9 +321,9 @@ func testInboundListenerConfig(t *testing.T, proxy *model.Proxy, services ...*mo t.Fatalf("expected %d listeners, found %d", 1, len(listeners)) } oldestProtocol := oldestService.Ports[0].Protocol - if oldestProtocol != config.ProtocolHTTP && isHTTPListener(listeners[0]) { + if oldestProtocol != protocol.HTTP && isHTTPListener(listeners[0]) { t.Fatal("expected TCP listener, found HTTP") - } else if oldestProtocol == config.ProtocolHTTP && !isHTTPListener(listeners[0]) { + } else if oldestProtocol == protocol.HTTP && !isHTTPListener(listeners[0]) { t.Fatal("expected HTTP listener, found TCP") } verifyInboundHTTPListenerServerName(t, listeners[0]) @@ -432,7 +432,7 @@ func testOutboundListenerConfigWithSidecar(t *testing.T, services ...*model.Serv { Port: &networking.Port{ Number: 3306, - Protocol: string(config.ProtocolMySQL), + Protocol: string(protocol.MySQL), Name: "MySQL", }, Bind: "8.8.8.8", @@ -895,7 +895,7 @@ func findListenerByPort(listeners []*xdsapi.Listener, port uint32) *xdsapi.Liste return nil } -func buildService(hostname string, ip string, protocol config.Protocol, creationTime time.Time) *model.Service { +func buildService(hostname string, ip string, protocol protocol.Instance, creationTime time.Time) *model.Service { return &model.Service{ CreationTime: creationTime, Hostname: config.Hostname(hostname), diff --git a/pilot/pkg/networking/core/v1alpha3/loadbalancer/loadbalancer_test.go b/pilot/pkg/networking/core/v1alpha3/loadbalancer/loadbalancer_test.go index fa651647a14c..f20ad8c00049 100644 --- a/pilot/pkg/networking/core/v1alpha3/loadbalancer/loadbalancer_test.go +++ b/pilot/pkg/networking/core/v1alpha3/loadbalancer/loadbalancer_test.go @@ -28,7 +28,7 @@ import ( networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/networking/core/v1alpha3/fakes" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) func TestApplyLocalitySetting(t *testing.T) { @@ -167,7 +167,7 @@ func buildEnvForClustersWithDistribute(distribute []*meshconfig.LocalityLoadBala &model.Port{ Name: "default", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, }, @@ -225,7 +225,7 @@ func buildEnvForClustersWithFailover() *model.Environment { &model.Port{ Name: "default", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, }, diff --git a/pilot/pkg/networking/core/v1alpha3/networkfilter.go b/pilot/pkg/networking/core/v1alpha3/networkfilter.go index 519a55291cdc..55ce951a9d03 100644 --- a/pilot/pkg/networking/core/v1alpha3/networkfilter.go +++ b/pilot/pkg/networking/core/v1alpha3/networkfilter.go @@ -19,7 +19,6 @@ import ( "time" "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" - "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener" accesslogconfig "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v2" accesslog "github.com/envoyproxy/go-control-plane/envoy/config/filter/accesslog/v2" @@ -35,6 +34,7 @@ import ( istio_route "istio.io/istio/pilot/pkg/networking/core/v1alpha3/route" "istio.io/istio/pilot/pkg/networking/util" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) const tcpEnvoyAccesslogName string = "tcp_envoy_accesslog" @@ -184,16 +184,16 @@ func buildOutboundNetworkFiltersWithWeightedClusters(env *model.Environment, nod func buildNetworkFiltersStack(node *model.Proxy, port *model.Port, tcpFilter *listener.Filter, statPrefix string, clusterName string) []listener.Filter { filterstack := make([]listener.Filter, 0) switch port.Protocol { - case config.ProtocolMongo: + case protocol.Mongo: filterstack = append(filterstack, buildMongoFilter(statPrefix, util.IsXDSMarshalingToAnyEnabled(node)), *tcpFilter) - case config.ProtocolRedis: + case protocol.Redis: if util.IsProxyVersionGE11(node) && features.EnableRedisFilter.Get() { // redis filter has route config, it is a terminating filter, no need append tcp filter. filterstack = append(filterstack, buildRedisFilter(statPrefix, clusterName, util.IsXDSMarshalingToAnyEnabled(node))) } else { filterstack = append(filterstack, *tcpFilter) } - case config.ProtocolMySQL: + case protocol.MySQL: if util.IsProxyVersionGE11(node) && features.EnableMysqlFilter.Get() { filterstack = append(filterstack, buildMySQLFilter(statPrefix, util.IsXDSMarshalingToAnyEnabled(node))) } diff --git a/pilot/pkg/networking/core/v1alpha3/route/route_test.go b/pilot/pkg/networking/core/v1alpha3/route/route_test.go index 6b73e606eaa8..3c9d7763a8de 100644 --- a/pilot/pkg/networking/core/v1alpha3/route/route_test.go +++ b/pilot/pkg/networking/core/v1alpha3/route/route_test.go @@ -26,6 +26,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/networking/core/v1alpha3/route" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) func TestBuildHTTPRoutes(t *testing.T) { @@ -38,7 +39,7 @@ func TestBuildHTTPRoutes(t *testing.T) { &model.Port{ Name: "default", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, }, diff --git a/pilot/pkg/networking/plugin/plugin.go b/pilot/pkg/networking/plugin/plugin.go index 1a976ff8da91..a19746a83565 100644 --- a/pilot/pkg/networking/plugin/plugin.go +++ b/pilot/pkg/networking/plugin/plugin.go @@ -22,7 +22,7 @@ import ( networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/model" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // ListenerProtocol is the protocol associated with the listener. @@ -46,13 +46,13 @@ const ( Mixer = "mixer" ) -// ModelProtocolToListenerProtocol converts from a config.Protocol to its corresponding plugin.ListenerProtocol -func ModelProtocolToListenerProtocol(protocol config.Protocol) ListenerProtocol { - switch protocol { - case config.ProtocolHTTP, config.ProtocolHTTP2, config.ProtocolGRPC, config.ProtocolGRPCWeb: +// ModelProtocolToListenerProtocol converts from a config.Instance to its corresponding plugin.ListenerProtocol +func ModelProtocolToListenerProtocol(p protocol.Instance) ListenerProtocol { + switch p { + case protocol.HTTP, protocol.HTTP2, protocol.GRPC, protocol.GRPCWeb: return ListenerProtocolHTTP - case config.ProtocolTCP, config.ProtocolHTTPS, config.ProtocolTLS, - config.ProtocolMongo, config.ProtocolRedis, config.ProtocolMySQL: + case protocol.TCP, protocol.HTTPS, protocol.TLS, + protocol.Mongo, protocol.Redis, protocol.MySQL: return ListenerProtocolTCP default: return ListenerProtocolUnknown diff --git a/pilot/pkg/proxy/envoy/v2/eds.go b/pilot/pkg/proxy/envoy/v2/eds.go index ce8dd2302847..9a8e66ecbbb1 100644 --- a/pilot/pkg/proxy/envoy/v2/eds.go +++ b/pilot/pkg/proxy/envoy/v2/eds.go @@ -34,6 +34,7 @@ import ( "istio.io/istio/pilot/pkg/serviceregistry" "istio.io/istio/pilot/pkg/serviceregistry/aggregate" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // EDS returns the list of endpoints (IP:port and in future labels) associated with a real @@ -294,7 +295,7 @@ func (s *DiscoveryServer) updateServiceShards(push *model.PushContext) error { entries := []*model.IstioEndpoint{} for _, port := range svc.Ports { - if port.Protocol == config.ProtocolUDP { + if port.Protocol == protocol.UDP { continue } diff --git a/pilot/pkg/proxy/envoy/v2/eds_sh_test.go b/pilot/pkg/proxy/envoy/v2/eds_sh_test.go index 01e010778e2a..4a6a7b2c166c 100644 --- a/pilot/pkg/proxy/envoy/v2/eds_sh_test.go +++ b/pilot/pkg/proxy/envoy/v2/eds_sh_test.go @@ -32,6 +32,7 @@ import ( "istio.io/istio/pilot/pkg/serviceregistry" "istio.io/istio/pilot/pkg/serviceregistry/aggregate" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/env" "istio.io/istio/tests/util" ) @@ -278,7 +279,7 @@ func initRegistry(server *bootstrap.Server, clusterNum int, gatewaysIP []string, ServicePort: &model.Port{ Name: "http-main", Port: 1080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Network: id, Locality: "az", diff --git a/pilot/pkg/proxy/envoy/v2/eds_test.go b/pilot/pkg/proxy/envoy/v2/eds_test.go index 0950c02d09de..9a9714e8a934 100644 --- a/pilot/pkg/proxy/envoy/v2/eds_test.go +++ b/pilot/pkg/proxy/envoy/v2/eds_test.go @@ -37,6 +37,7 @@ import ( v2 "istio.io/istio/pilot/pkg/proxy/envoy/v2" "istio.io/istio/pkg/adsc" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/env" "istio.io/istio/tests/util" ) @@ -178,7 +179,7 @@ func addTestClientEndpoints(server *bootstrap.Server) { { Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, }) @@ -189,7 +190,7 @@ func addTestClientEndpoints(server *bootstrap.Server) { ServicePort: &model.Port{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: asdcLocality, }, @@ -201,7 +202,7 @@ func addTestClientEndpoints(server *bootstrap.Server) { ServicePort: &model.Port{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: asdc2Locality, }, @@ -562,7 +563,7 @@ func addUdsEndpoint(server *bootstrap.Server) { { Name: "grpc", Port: 0, - Protocol: config.ProtocolGRPC, + Protocol: protocol.GRPC, }, }, MeshExternal: true, @@ -576,7 +577,7 @@ func addUdsEndpoint(server *bootstrap.Server) { ServicePort: &model.Port{ Name: "grpc", Port: 0, - Protocol: config.ProtocolGRPC, + Protocol: protocol.GRPC, }, Locality: "localhost", }, @@ -593,7 +594,7 @@ func addLocalityEndpoints(server *bootstrap.Server, hostname config.Hostname) { { Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, }) @@ -614,7 +615,7 @@ func addLocalityEndpoints(server *bootstrap.Server, hostname config.Hostname) { ServicePort: &model.Port{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: locality, }, @@ -630,12 +631,12 @@ func addOverlappingEndpoints(server *bootstrap.Server) { { Name: "dns", Port: 53, - Protocol: config.ProtocolUDP, + Protocol: protocol.UDP, }, { Name: "tcp-dns", Port: 53, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, }) @@ -646,7 +647,7 @@ func addOverlappingEndpoints(server *bootstrap.Server) { ServicePort: &model.Port{ Name: "tcp-dns", Port: 53, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, }) diff --git a/pilot/pkg/proxy/envoy/v2/mem.go b/pilot/pkg/proxy/envoy/v2/mem.go index b84614addf01..b075a65ce3ae 100644 --- a/pilot/pkg/proxy/envoy/v2/mem.go +++ b/pilot/pkg/proxy/envoy/v2/mem.go @@ -21,6 +21,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/spiffe" ) @@ -117,7 +118,7 @@ func (sd *MemServiceDiscovery) AddHTTPService(name, vip string, port int) { { Name: "http-main", Port: port, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, }) @@ -161,7 +162,7 @@ func (sd *MemServiceDiscovery) AddEndpoint(service config.Hostname, servicePortN ServicePort: &model.Port{ Name: servicePortName, Port: servicePort, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, } @@ -210,7 +211,7 @@ func (sd *MemServiceDiscovery) SetEndpoints(service string, namespace string, en ServicePort: &model.Port{ Name: e.ServicePortName, Port: p.Port, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: e.Locality, LbWeight: e.LbWeight, diff --git a/pilot/pkg/proxy/envoy/v2/xds_test.go b/pilot/pkg/proxy/envoy/v2/xds_test.go index 198ed3d4b3da..b11191355c60 100644 --- a/pilot/pkg/proxy/envoy/v2/xds_test.go +++ b/pilot/pkg/proxy/envoy/v2/xds_test.go @@ -31,6 +31,7 @@ import ( "istio.io/istio/pilot/pkg/bootstrap" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/env" "istio.io/istio/tests/util" ) @@ -160,7 +161,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) ServicePort: &model.Port{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: "az", }, @@ -175,7 +176,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) { Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }}, }) server.EnvoyXdsServer.MemRegistry.AddInstance("local.default.svc.cluster.local", &model.ServiceInstance{ @@ -185,7 +186,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) ServicePort: &model.Port{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: "az", }, @@ -206,7 +207,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) ServicePort: &model.Port{ Name: "http-main", Port: 1080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: "az", }, @@ -219,7 +220,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) ServicePort: &model.Port{ Name: "http-main", Port: 1080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: "az", }, @@ -234,12 +235,12 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) { Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "https", Port: 443, - Protocol: config.ProtocolHTTPS, + Protocol: protocol.HTTPS, }, }, }) @@ -250,7 +251,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) ServicePort: &model.Port{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, Locality: "az", }, @@ -263,7 +264,7 @@ func initLocalPilotTestEnv(t *testing.T) (*bootstrap.Server, util.TearDownFunc) ServicePort: &model.Port{ Name: "https", Port: 443, - Protocol: config.ProtocolHTTPS, + Protocol: protocol.HTTPS, }, Locality: "az", }, @@ -295,31 +296,31 @@ func testPorts(base int) []*model.Port { { Name: "http", Port: base + 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "http-status", Port: base + 81, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "custom", Port: base + 90, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, { Name: "mongo", Port: base + 100, - Protocol: config.ProtocolMongo, + Protocol: protocol.Mongo, }, { Name: "redis", Port: base + 110, - Protocol: config.ProtocolRedis, + Protocol: protocol.Redis, }, { Name: "mysql", Port: base + 120, - Protocol: config.ProtocolMySQL, + Protocol: protocol.MySQL, }, { Name: "h2port", Port: base + 66, - Protocol: config.ProtocolGRPC, + Protocol: protocol.GRPC, }} } diff --git a/pilot/pkg/serviceregistry/aggregate/controller_test.go b/pilot/pkg/serviceregistry/aggregate/controller_test.go index 7d6b8f16b101..5acc11acaea8 100644 --- a/pilot/pkg/serviceregistry/aggregate/controller_test.go +++ b/pilot/pkg/serviceregistry/aggregate/controller_test.go @@ -24,6 +24,7 @@ import ( "istio.io/istio/pilot/pkg/serviceregistry" "istio.io/istio/pilot/pkg/serviceregistry/memory" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) // MockController specifies a mock Controller for testing @@ -432,11 +433,11 @@ func TestManagementPorts(t *testing.T) { expected := model.PortList{{ Name: "http", Port: 3333, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "custom", Port: 9999, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }} // Get management ports from mockAdapter1 diff --git a/pilot/pkg/serviceregistry/consul/conversion.go b/pilot/pkg/serviceregistry/consul/conversion.go index 5ebae72797be..1bf36cddff36 100644 --- a/pilot/pkg/serviceregistry/consul/conversion.go +++ b/pilot/pkg/serviceregistry/consul/conversion.go @@ -22,6 +22,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/pkg/log" ) @@ -162,11 +163,11 @@ func parseHostname(hostname config.Hostname) (name string, err error) { return } -func convertProtocol(name string) config.Protocol { - protocol := config.ParseProtocol(name) - if protocol == config.ProtocolUnsupported { +func convertProtocol(name string) protocol.Instance { + p := protocol.Parse(name) + if p == protocol.Unsupported { log.Warnf("unsupported protocol value: %s", name) - return config.ProtocolTCP + return protocol.TCP } - return protocol + return p } diff --git a/pilot/pkg/serviceregistry/consul/conversion_test.go b/pilot/pkg/serviceregistry/consul/conversion_test.go index 76a2e35adfbb..1d5802339a4e 100644 --- a/pilot/pkg/serviceregistry/consul/conversion_test.go +++ b/pilot/pkg/serviceregistry/consul/conversion_test.go @@ -20,22 +20,22 @@ import ( "github.com/hashicorp/consul/api" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) var ( protocols = []struct { name string port int - out config.Protocol + out protocol.Instance }{ - {"tcp", 80, config.ProtocolTCP}, - {"http", 81, config.ProtocolHTTP}, - {"https", 443, config.ProtocolHTTPS}, - {"http2", 83, config.ProtocolHTTP2}, - {"grpc", 84, config.ProtocolGRPC}, - {"udp", 85, config.ProtocolUDP}, - {"", 86, config.ProtocolTCP}, + {"tcp", 80, protocol.TCP}, + {"http", 81, protocol.HTTP}, + {"https", 443, protocol.HTTPS}, + {"http2", 83, protocol.HTTP2}, + {"grpc", 84, protocol.GRPC}, + {"udp", 85, protocol.UDP}, + {"", 86, protocol.TCP}, } goodLabels = []string{ @@ -73,7 +73,7 @@ func TestConvertLabels(t *testing.T) { func TestConvertInstance(t *testing.T) { ip := "172.19.0.11" port := 9080 - protocol := "udp" + p := "udp" name := "productpage" tagKey1 := "version" tagVal1 := "v1" @@ -92,17 +92,17 @@ func TestConvertInstance(t *testing.T) { ServiceAddress: ip, ServicePort: port, Datacenter: dc, - ServiceMeta: map[string]string{protocolTagName: protocol}, + ServiceMeta: map[string]string{protocolTagName: p}, } out := convertInstance(&consulServiceInst) - if out.Endpoint.ServicePort.Protocol != config.ProtocolUDP { - t.Errorf("convertInstance() => %v, want %v", out.Endpoint.ServicePort.Protocol, config.ProtocolUDP) + if out.Endpoint.ServicePort.Protocol != protocol.UDP { + t.Errorf("convertInstance() => %v, want %v", out.Endpoint.ServicePort.Protocol, protocol.UDP) } - if out.Endpoint.ServicePort.Name != protocol { - t.Errorf("convertInstance() => %v, want %v", out.Endpoint.ServicePort.Name, protocol) + if out.Endpoint.ServicePort.Name != p { + t.Errorf("convertInstance() => %v, want %v", out.Endpoint.ServicePort.Name, p) } if out.Endpoint.ServicePort.Port != port { @@ -138,7 +138,7 @@ func TestConvertInstance(t *testing.T) { t.Errorf("convertInstance() incorrect # of service ports => %q, want %q", len(out.Service.Ports), 1) } - if out.Service.Ports[0].Port != port || out.Service.Ports[0].Name != protocol { + if out.Service.Ports[0].Port != port || out.Service.Ports[0].Name != p { t.Errorf("convertInstance() incorrect service port => %q", out.Service.Ports[0]) } diff --git a/pilot/pkg/serviceregistry/external/conversion.go b/pilot/pkg/serviceregistry/external/conversion.go index 148b9c45c8ad..4c49be5260d7 100644 --- a/pilot/pkg/serviceregistry/external/conversion.go +++ b/pilot/pkg/serviceregistry/external/conversion.go @@ -21,13 +21,14 @@ import ( networking "istio.io/api/networking/v1alpha3" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) func convertPort(port *networking.Port) *model.Port { return &model.Port{ Name: port.Name, Port: int(port.Number), - Protocol: config.ParseProtocol(port.Protocol), + Protocol: protocol.Parse(port.Protocol), } } diff --git a/pilot/pkg/serviceregistry/external/conversion_test.go b/pilot/pkg/serviceregistry/external/conversion_test.go index d59efdf1a87d..4855093db6a8 100644 --- a/pilot/pkg/serviceregistry/external/conversion_test.go +++ b/pilot/pkg/serviceregistry/external/conversion_test.go @@ -24,6 +24,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/test/util" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) var GlobalTime = time.Now() @@ -272,13 +273,13 @@ var udsLocal = &model.Config{ }, } -func convertPortNameToProtocol(name string) config.Protocol { +func convertPortNameToProtocol(name string) protocol.Instance { prefix := name i := strings.Index(name, "-") if i >= 0 { prefix = name[:i] } - return config.ParseProtocol(prefix) + return protocol.Parse(prefix) } func makeService(hostname config.Hostname, configNamespace, address string, ports map[string]int, external bool, resolution model.Resolution) *model.Service { @@ -335,7 +336,7 @@ func makeInstance(cfg *model.Config, address string, port int, ServicePort: &model.Port{ Name: svcPort.Name, Port: int(svcPort.Number), - Protocol: config.ParseProtocol(svcPort.Protocol), + Protocol: protocol.Parse(svcPort.Protocol), }, }, Labels: config.Labels(labels), diff --git a/pilot/pkg/serviceregistry/kube/controller/controller_test.go b/pilot/pkg/serviceregistry/kube/controller/controller_test.go index 06358c7d7bac..b71dffc50dd4 100644 --- a/pilot/pkg/serviceregistry/kube/controller/controller_test.go +++ b/pilot/pkg/serviceregistry/kube/controller/controller_test.go @@ -23,21 +23,22 @@ import ( "testing" "time" + coreV1 "k8s.io/api/core/v1" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/fake" + "istio.io/api/annotation" meshconfig "istio.io/api/mesh/v1alpha1" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pilot/pkg/serviceregistry/kube" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/spiffe" "istio.io/istio/pkg/test" "istio.io/istio/pkg/test/env" "istio.io/pkg/log" - - coreV1 "k8s.io/api/core/v1" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/intstr" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/kubernetes/fake" ) func makeClient(t *testing.T) kubernetes.Interface { @@ -205,7 +206,7 @@ func TestServices(t *testing.T) { for _, item := range out { if item.Hostname == hostname && len(item.Ports) == 1 && - item.Ports[0].Protocol == config.ProtocolHTTP { + item.Ports[0].Protocol == protocol.HTTP { return true } } @@ -572,7 +573,7 @@ func TestWorkloadHealthCheckInfo(t *testing.T) { Port: &model.Port{ Name: "mgmt-8080", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, { @@ -580,7 +581,7 @@ func TestWorkloadHealthCheckInfo(t *testing.T) { Port: &model.Port{ Name: "mgmt-9090", Port: 9090, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, } @@ -693,12 +694,12 @@ func TestManagementPorts(t *testing.T) { { Name: "mgmt-8080", Port: 8080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "mgmt-9090", Port: 9090, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, } @@ -740,7 +741,7 @@ func TestController_Service(t *testing.T) { &model.Port{ Name: "test-port", Port: 8080, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, }, @@ -751,7 +752,7 @@ func TestController_Service(t *testing.T) { &model.Port{ Name: "test-port", Port: 8081, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, }, @@ -762,7 +763,7 @@ func TestController_Service(t *testing.T) { &model.Port{ Name: "test-port", Port: 8082, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, }, @@ -773,7 +774,7 @@ func TestController_Service(t *testing.T) { &model.Port{ Name: "test-port", Port: 8083, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, }, @@ -818,7 +819,7 @@ func TestController_ExternalNameService(t *testing.T) { &model.Port{ Name: "test-port", Port: 8080, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, MeshExternal: true, @@ -830,7 +831,7 @@ func TestController_ExternalNameService(t *testing.T) { &model.Port{ Name: "test-port", Port: 8081, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, MeshExternal: true, @@ -842,7 +843,7 @@ func TestController_ExternalNameService(t *testing.T) { &model.Port{ Name: "test-port", Port: 8082, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, MeshExternal: true, @@ -854,7 +855,7 @@ func TestController_ExternalNameService(t *testing.T) { &model.Port{ Name: "test-port", Port: 8083, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, }, MeshExternal: true, diff --git a/pilot/pkg/serviceregistry/kube/conversion.go b/pilot/pkg/serviceregistry/kube/conversion.go index 34fbc6ab40c3..ae4634d01f8e 100644 --- a/pilot/pkg/serviceregistry/kube/conversion.go +++ b/pilot/pkg/serviceregistry/kube/conversion.go @@ -23,15 +23,15 @@ import ( "strings" "github.com/hashicorp/go-multierror" + coreV1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/intstr" "istio.io/api/annotation" "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/config" "istio.io/istio/pkg/config/kube" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/spiffe" - - coreV1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/util/intstr" ) const ( @@ -184,17 +184,17 @@ func ConvertProbePort(c *coreV1.Container, handler *coreV1.Handler) (*model.Port return nil, nil } - var protocol config.Protocol + var p protocol.Instance var portVal intstr.IntOrString // Only two types of handler is allowed by Kubernetes (HTTPGet or TCPSocket) switch { case handler.HTTPGet != nil: portVal = handler.HTTPGet.Port - protocol = config.ProtocolHTTP + p = protocol.HTTP case handler.TCPSocket != nil: portVal = handler.TCPSocket.Port - protocol = config.ProtocolTCP + p = protocol.TCP default: return nil, nil } @@ -205,7 +205,7 @@ func ConvertProbePort(c *coreV1.Container, handler *coreV1.Handler) (*model.Port return &model.Port{ Name: managementPortPrefix + strconv.Itoa(port), Port: port, - Protocol: protocol, + Protocol: p, }, nil case intstr.String: for _, named := range c.Ports { @@ -214,7 +214,7 @@ func ConvertProbePort(c *coreV1.Container, handler *coreV1.Handler) (*model.Port return &model.Port{ Name: managementPortPrefix + strconv.Itoa(port), Port: port, - Protocol: protocol, + Protocol: p, }, nil } } diff --git a/pilot/pkg/serviceregistry/kube/conversion_test.go b/pilot/pkg/serviceregistry/kube/conversion_test.go index 2e871e254ae3..50022d44d272 100644 --- a/pilot/pkg/serviceregistry/kube/conversion_test.go +++ b/pilot/pkg/serviceregistry/kube/conversion_test.go @@ -23,8 +23,8 @@ import ( "istio.io/api/annotation" "istio.io/istio/pilot/pkg/model" - "istio.io/istio/pkg/config" "istio.io/istio/pkg/config/kube" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/spiffe" coreV1 "k8s.io/api/core/v1" @@ -41,28 +41,28 @@ func TestConvertProtocol(t *testing.T) { type protocolCase struct { name string proto coreV1.Protocol - out config.Protocol + out protocol.Instance } protocols := []protocolCase{ - {"", coreV1.ProtocolTCP, config.ProtocolTCP}, - {"http", coreV1.ProtocolTCP, config.ProtocolHTTP}, - {"http-test", coreV1.ProtocolTCP, config.ProtocolHTTP}, - {"http", coreV1.ProtocolUDP, config.ProtocolUDP}, - {"httptest", coreV1.ProtocolTCP, config.ProtocolTCP}, - {"https", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"https-test", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"http2", coreV1.ProtocolTCP, config.ProtocolHTTP2}, - {"http2-test", coreV1.ProtocolTCP, config.ProtocolHTTP2}, - {"grpc", coreV1.ProtocolTCP, config.ProtocolGRPC}, - {"grpc-test", coreV1.ProtocolTCP, config.ProtocolGRPC}, - {"grpc-web", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"grpc-web-test", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"mongo", coreV1.ProtocolTCP, config.ProtocolMongo}, - {"mongo-test", coreV1.ProtocolTCP, config.ProtocolMongo}, - {"redis", coreV1.ProtocolTCP, config.ProtocolRedis}, - {"redis-test", coreV1.ProtocolTCP, config.ProtocolRedis}, - {"mysql", coreV1.ProtocolTCP, config.ProtocolMySQL}, - {"mysql-test", coreV1.ProtocolTCP, config.ProtocolMySQL}, + {"", coreV1.ProtocolTCP, protocol.TCP}, + {"http", coreV1.ProtocolTCP, protocol.HTTP}, + {"http-test", coreV1.ProtocolTCP, protocol.HTTP}, + {"http", coreV1.ProtocolUDP, protocol.UDP}, + {"httptest", coreV1.ProtocolTCP, protocol.TCP}, + {"https", coreV1.ProtocolTCP, protocol.HTTPS}, + {"https-test", coreV1.ProtocolTCP, protocol.HTTPS}, + {"http2", coreV1.ProtocolTCP, protocol.HTTP2}, + {"http2-test", coreV1.ProtocolTCP, protocol.HTTP2}, + {"grpc", coreV1.ProtocolTCP, protocol.GRPC}, + {"grpc-test", coreV1.ProtocolTCP, protocol.GRPC}, + {"grpc-web", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"grpc-web-test", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"mongo", coreV1.ProtocolTCP, protocol.Mongo}, + {"mongo-test", coreV1.ProtocolTCP, protocol.Mongo}, + {"redis", coreV1.ProtocolTCP, protocol.Redis}, + {"redis-test", coreV1.ProtocolTCP, protocol.Redis}, + {"mysql", coreV1.ProtocolTCP, protocol.MySQL}, + {"mysql-test", coreV1.ProtocolTCP, protocol.MySQL}, } // Create the list of cases for all of the names in both upper and lowercase. @@ -95,12 +95,12 @@ func BenchmarkConvertProtocol(b *testing.B) { cases := []struct { name string proto coreV1.Protocol - out config.Protocol + out protocol.Instance }{ - {"grpc-web-lowercase", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"GRPC-WEB-mixedcase", coreV1.ProtocolTCP, config.ProtocolGRPCWeb}, - {"https-lowercase", coreV1.ProtocolTCP, config.ProtocolHTTPS}, - {"HTTPS-mixedcase", coreV1.ProtocolTCP, config.ProtocolHTTPS}, + {"grpc-web-lowercase", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"GRPC-WEB-mixedcase", coreV1.ProtocolTCP, protocol.GRPCWeb}, + {"https-lowercase", coreV1.ProtocolTCP, protocol.HTTPS}, + {"HTTPS-mixedcase", coreV1.ProtocolTCP, protocol.HTTPS}, } for _, c := range cases { @@ -318,7 +318,6 @@ func TestExternalClusterLocalServiceConversion(t *testing.T) { if !service.External() { t.Fatal("ExternalName service (even if .cluster.local) should be external") - t.Fatalf("ExternalName service (even if .cluster.local) should be external") } if service.Hostname != ServiceHostname(serviceName, namespace, domainSuffix) { @@ -391,12 +390,12 @@ func TestProbesToPortsConversion(t *testing.T) { { Name: "mgmt-3306", Port: 3306, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, { Name: "mgmt-9080", Port: 9080, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, } diff --git a/pilot/pkg/serviceregistry/memory/discovery.go b/pilot/pkg/serviceregistry/memory/discovery.go index 5a2133a71dc3..82203a1b9025 100644 --- a/pilot/pkg/serviceregistry/memory/discovery.go +++ b/pilot/pkg/serviceregistry/memory/discovery.go @@ -21,6 +21,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/spiffe" ) @@ -47,27 +48,27 @@ func MakeService(hostname config.Hostname, address string) *model.Service { { Name: PortHTTPName, Port: 80, // target port 80 - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "http-status", Port: 81, // target port 1081 - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "custom", Port: 90, // target port 1090 - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, { Name: "mongo", Port: 100, // target port 1100 - Protocol: config.ProtocolMongo, + Protocol: protocol.Mongo, }, { Name: "redis", Port: 110, // target port 1110 - Protocol: config.ProtocolRedis, + Protocol: protocol.Redis, }, { Name: "mysql", Port: 120, // target port 1120 - Protocol: config.ProtocolMySQL, + Protocol: protocol.MySQL, }, }, } @@ -83,7 +84,7 @@ func MakeExternalHTTPService(hostname config.Hostname, isMeshExternal bool, addr Ports: []*model.Port{{ Name: "http", Port: 80, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }}, } } @@ -98,7 +99,7 @@ func MakeExternalHTTPSService(hostname config.Hostname, isMeshExternal bool, add Ports: []*model.Port{{ Name: "https", Port: 443, - Protocol: config.ProtocolHTTPS, + Protocol: protocol.HTTPS, }}, } } @@ -245,11 +246,11 @@ func (sd *ServiceDiscovery) ManagementPorts(addr string) model.PortList { return model.PortList{{ Name: "http", Port: 3333, - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "custom", Port: 9999, - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }} } diff --git a/pkg/config/kube/conversion.go b/pkg/config/kube/conversion.go index e591efe33d17..b7774f603ed9 100644 --- a/pkg/config/kube/conversion.go +++ b/pkg/config/kube/conversion.go @@ -27,6 +27,7 @@ import ( "strings" "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" coreV1 "k8s.io/api/core/v1" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -52,27 +53,27 @@ func ParseHostname(hostname config.Hostname) (name string, namespace string, err return } -var grpcWeb = string(config.ProtocolGRPCWeb) +var grpcWeb = string(protocol.GRPCWeb) var grpcWebLen = len(grpcWeb) // ConvertProtocol from k8s protocol and port name -func ConvertProtocol(name string, proto coreV1.Protocol) config.Protocol { - out := config.ProtocolTCP +func ConvertProtocol(name string, proto coreV1.Protocol) protocol.Instance { + out := protocol.TCP switch proto { case coreV1.ProtocolUDP: - out = config.ProtocolUDP + out = protocol.UDP case coreV1.ProtocolTCP: if len(name) >= grpcWebLen && strings.EqualFold(name[:grpcWebLen], grpcWeb) { - out = config.ProtocolGRPCWeb + out = protocol.GRPCWeb break } i := strings.IndexByte(name, '-') if i >= 0 { name = name[:i] } - protocol := config.ParseProtocol(name) - if protocol != config.ProtocolUDP && protocol != config.ProtocolUnsupported { - out = protocol + p := protocol.Parse(name) + if p != protocol.UDP && p != protocol.Unsupported { + out = p } } return out diff --git a/pkg/config/protocol.go b/pkg/config/protocol/instance.go similarity index 50% rename from pkg/config/protocol.go rename to pkg/config/protocol/instance.go index 0197ca6e0d54..c02fef996057 100644 --- a/pkg/config/protocol.go +++ b/pkg/config/protocol/instance.go @@ -20,78 +20,78 @@ // generate the configuration files for the Layer 7 proxy sidecar. The proxy // code is specific to individual proxy implementations -package config +package protocol import "strings" -// Protocol defines network protocols for ports -type Protocol string +// Instance defines network protocols for ports +type Instance string const ( - // ProtocolGRPC declares that the port carries gRPC traffic. - ProtocolGRPC Protocol = "GRPC" - // ProtocolGRPCWeb declares that the port carries gRPC traffic. - ProtocolGRPCWeb Protocol = "GRPC-Web" - // ProtocolHTTP declares that the port carries HTTP/1.1 traffic. + // GRPC declares that the port carries gRPC traffic. + GRPC Instance = "GRPC" + // GRPCWeb declares that the port carries gRPC traffic. + GRPCWeb Instance = "GRPC-Web" + // HTTP declares that the port carries HTTP/1.1 traffic. // Note that HTTP/1.0 or earlier may not be supported by the proxy. - ProtocolHTTP Protocol = "HTTP" - // ProtocolHTTP2 declares that the port carries HTTP/2 traffic. - ProtocolHTTP2 Protocol = "HTTP2" - // ProtocolHTTPS declares that the port carries HTTPS traffic. - ProtocolHTTPS Protocol = "HTTPS" - // ProtocolTCP declares the the port uses TCP. + HTTP Instance = "HTTP" + // HTTP2 declares that the port carries HTTP/2 traffic. + HTTP2 Instance = "HTTP2" + // HTTPS declares that the port carries HTTPS traffic. + HTTPS Instance = "HTTPS" + // TCP declares the the port uses TCP. // This is the default protocol for a service port. - ProtocolTCP Protocol = "TCP" - // ProtocolTLS declares that the port carries TLS traffic. + TCP Instance = "TCP" + // TLS declares that the port carries TLS traffic. // TLS traffic is assumed to contain SNI as part of the handshake. - ProtocolTLS Protocol = "TLS" - // ProtocolUDP declares that the port uses UDP. + TLS Instance = "TLS" + // UDP declares that the port uses UDP. // Note that UDP protocol is not currently supported by the proxy. - ProtocolUDP Protocol = "UDP" - // ProtocolMongo declares that the port carries MongoDB traffic. - ProtocolMongo Protocol = "Mongo" - // ProtocolRedis declares that the port carries Redis traffic. - ProtocolRedis Protocol = "Redis" - // ProtocolMySQL declares that the port carries MySQL traffic. - ProtocolMySQL Protocol = "MySQL" - // ProtocolUnsupported - value to signify that the protocol is unsupported. - ProtocolUnsupported Protocol = "UnsupportedProtocol" + UDP Instance = "UDP" + // Mongo declares that the port carries MongoDB traffic. + Mongo Instance = "Mongo" + // Redis declares that the port carries Redis traffic. + Redis Instance = "Redis" + // MySQL declares that the port carries MySQL traffic. + MySQL Instance = "MySQL" + // Unsupported - value to signify that the protocol is unsupported. + Unsupported Instance = "UnsupportedProtocol" ) -// ParseProtocol from string ignoring case -func ParseProtocol(s string) Protocol { +// Parse from string ignoring case +func Parse(s string) Instance { switch strings.ToLower(s) { case "tcp": - return ProtocolTCP + return TCP case "udp": - return ProtocolUDP + return UDP case "grpc": - return ProtocolGRPC + return GRPC case "grpc-web": - return ProtocolGRPCWeb + return GRPCWeb case "http": - return ProtocolHTTP + return HTTP case "http2": - return ProtocolHTTP2 + return HTTP2 case "https": - return ProtocolHTTPS + return HTTPS case "tls": - return ProtocolTLS + return TLS case "mongo": - return ProtocolMongo + return Mongo case "redis": - return ProtocolRedis + return Redis case "mysql": - return ProtocolMySQL + return MySQL } - return ProtocolUnsupported + return Unsupported } // IsHTTP2 is true for protocols that use HTTP/2 as transport protocol -func (p Protocol) IsHTTP2() bool { - switch p { - case ProtocolHTTP2, ProtocolGRPC, ProtocolGRPCWeb: +func (i Instance) IsHTTP2() bool { + switch i { + case HTTP2, GRPC, GRPCWeb: return true default: return false @@ -99,9 +99,9 @@ func (p Protocol) IsHTTP2() bool { } // IsHTTP is true for protocols that use HTTP as transport protocol -func (p Protocol) IsHTTP() bool { - switch p { - case ProtocolHTTP, ProtocolHTTP2, ProtocolGRPC, ProtocolGRPCWeb: +func (i Instance) IsHTTP() bool { + switch i { + case HTTP, HTTP2, GRPC, GRPCWeb: return true default: return false @@ -109,9 +109,9 @@ func (p Protocol) IsHTTP() bool { } // IsTCP is true for protocols that use TCP as transport protocol -func (p Protocol) IsTCP() bool { - switch p { - case ProtocolTCP, ProtocolHTTPS, ProtocolTLS, ProtocolMongo, ProtocolRedis, ProtocolMySQL: +func (i Instance) IsTCP() bool { + switch i { + case TCP, HTTPS, TLS, Mongo, Redis, MySQL: return true default: return false @@ -119,9 +119,19 @@ func (p Protocol) IsTCP() bool { } // IsTLS is true for protocols on top of TLS (e.g. HTTPS) -func (p Protocol) IsTLS() bool { - switch p { - case ProtocolHTTPS, ProtocolTLS: +func (i Instance) IsTLS() bool { + switch i { + case HTTPS, TLS: + return true + default: + return false + } +} + +// IsGRPC is true for GRCP protocols. +func (i Instance) IsGRPC() bool { + switch i { + case GRPC, GRPCWeb: return true default: return false diff --git a/pkg/config/protocol_test.go b/pkg/config/protocol/instance_test.go similarity index 52% rename from pkg/config/protocol_test.go rename to pkg/config/protocol/instance_test.go index 71aa15893c35..3ac256397ec2 100644 --- a/pkg/config/protocol_test.go +++ b/pkg/config/protocol/instance_test.go @@ -20,53 +20,63 @@ // generate the configuration files for the Layer 7 proxy sidecar. The proxy // code is specific to individual proxy implementations -package config +package protocol_test -import "testing" +import ( + "testing" -func TestHTTPProtocol(t *testing.T) { - if ProtocolUDP.IsHTTP() { + "istio.io/istio/pkg/config/protocol" +) + +func TestIsHTTP(t *testing.T) { + if protocol.UDP.IsHTTP() { t.Errorf("UDP is not HTTP protocol") } - if !ProtocolGRPC.IsHTTP() { + if !protocol.GRPC.IsHTTP() { t.Errorf("gRPC is HTTP protocol") } } -func TestParseProtocol(t *testing.T) { +func TestParse(t *testing.T) { var testPairs = []struct { name string - out Protocol + out protocol.Instance }{ - {"tcp", ProtocolTCP}, - {"http", ProtocolHTTP}, - {"HTTP", ProtocolHTTP}, - {"Http", ProtocolHTTP}, - {"https", ProtocolHTTPS}, - {"http2", ProtocolHTTP2}, - {"grpc", ProtocolGRPC}, - {"grpc-web", ProtocolGRPCWeb}, - {"gRPC-Web", ProtocolGRPCWeb}, - {"grpc-Web", ProtocolGRPCWeb}, - {"udp", ProtocolUDP}, - {"Mongo", ProtocolMongo}, - {"mongo", ProtocolMongo}, - {"MONGO", ProtocolMongo}, - {"Redis", ProtocolRedis}, - {"redis", ProtocolRedis}, - {"REDIS", ProtocolRedis}, - {"Mysql", ProtocolMySQL}, - {"mysql", ProtocolMySQL}, - {"MYSQL", ProtocolMySQL}, - {"MySQL", ProtocolMySQL}, - {"", ProtocolUnsupported}, - {"SMTP", ProtocolUnsupported}, + {"tcp", protocol.TCP}, + {"http", protocol.HTTP}, + {"HTTP", protocol.HTTP}, + {"Http", protocol.HTTP}, + {"https", protocol.HTTPS}, + {"http2", protocol.HTTP2}, + {"grpc", protocol.GRPC}, + {"grpc-web", protocol.GRPCWeb}, + {"gRPC-Web", protocol.GRPCWeb}, + {"grpc-Web", protocol.GRPCWeb}, + {"udp", protocol.UDP}, + {"Mongo", protocol.Mongo}, + {"mongo", protocol.Mongo}, + {"MONGO", protocol.Mongo}, + {"Redis", protocol.Redis}, + {"redis", protocol.Redis}, + {"REDIS", protocol.Redis}, + {"Mysql", protocol.MySQL}, + {"mysql", protocol.MySQL}, + {"MYSQL", protocol.MySQL}, + {"MySQL", protocol.MySQL}, + {"", protocol.Unsupported}, + {"SMTP", protocol.Unsupported}, } for _, testPair := range testPairs { - out := ParseProtocol(testPair.name) - if out != testPair.out { - t.Errorf("ParseProtocol(%q) => %q, want %q", testPair.name, out, testPair.out) + testName := testPair.name + if testName == "" { + testName = "[empty]" } + t.Run(testName, func(t *testing.T) { + out := protocol.Parse(testPair.name) + if out != testPair.out { + t.Fatalf("Parse(%q) => %q, want %q", testPair.name, out, testPair.out) + } + }) } } diff --git a/pkg/config/server.go b/pkg/config/server.go index 2410502be26a..53606590230e 100644 --- a/pkg/config/server.go +++ b/pkg/config/server.go @@ -22,11 +22,14 @@ package config -import "istio.io/api/networking/v1alpha3" +import ( + "istio.io/api/networking/v1alpha3" + "istio.io/istio/pkg/config/protocol" +) // IsTLSServer returns true if this server is non HTTP, with some TLS settings for termination/passthrough func IsTLSServer(server *v1alpha3.Server) bool { - if server.Tls != nil && !ParseProtocol(server.Port.Protocol).IsHTTP() { + if server.Tls != nil && !protocol.Parse(server.Port.Protocol).IsHTTP() { return true } return false @@ -34,12 +37,12 @@ func IsTLSServer(server *v1alpha3.Server) bool { // IsHTTPServer returns true if this server is using HTTP or HTTPS with termination func IsHTTPServer(server *v1alpha3.Server) bool { - protocol := ParseProtocol(server.Port.Protocol) - if protocol.IsHTTP() { + p := protocol.Parse(server.Port.Protocol) + if p.IsHTTP() { return true } - if protocol == ProtocolHTTPS && server.Tls != nil && !IsPassThroughServer(server) { + if p == protocol.HTTPS && server.Tls != nil && !IsPassThroughServer(server) { return true } diff --git a/pkg/config/validation.go b/pkg/config/validation.go index 24300525ac3f..2f45050d922a 100644 --- a/pkg/config/validation.go +++ b/pkg/config/validation.go @@ -28,7 +28,7 @@ import ( xdsUtil "github.com/envoyproxy/go-control-plane/pkg/util" "github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/types" - "github.com/hashicorp/go-multierror" + multierror "github.com/hashicorp/go-multierror" authn "istio.io/api/authentication/v1alpha1" meshconfig "istio.io/api/mesh/v1alpha1" @@ -36,6 +36,7 @@ import ( mccpb "istio.io/api/mixer/v1/config/client" networking "istio.io/api/networking/v1alpha3" rbac "istio.io/api/rbac/v1alpha1" + "istio.io/istio/pkg/config/protocol" "istio.io/pkg/log" ) @@ -359,12 +360,12 @@ func validateServer(server *networking.Server) (errs error) { // If port is HTTPS or TLS, make sure that server has TLS options if portErr == nil { - protocol := ParseProtocol(server.Port.Protocol) - if protocol.IsTLS() && server.Tls == nil { + p := protocol.Parse(server.Port.Protocol) + if p.IsTLS() && server.Tls == nil { errs = appendErrors(errs, fmt.Errorf("server must have TLS settings for HTTPS/TLS protocols")) - } else if !protocol.IsTLS() && server.Tls != nil { + } else if !p.IsTLS() && server.Tls != nil { // only tls redirect is allowed if this is a HTTP server - if protocol.IsHTTP() { + if p.IsHTTP() { if !IsPassThroughServer(server) || server.Tls.CaCertificates != "" || server.Tls.PrivateKey != "" || server.Tls.ServerCertificate != "" { errs = appendErrors(errs, fmt.Errorf("server cannot have TLS settings for plain text HTTP ports")) @@ -381,7 +382,7 @@ func validateServerPort(port *networking.Port) (errs error) { if port == nil { return appendErrors(errs, fmt.Errorf("port is required")) } - if ParseProtocol(port.Protocol) == ProtocolUnsupported { + if protocol.Parse(port.Protocol) == protocol.Unsupported { errs = appendErrors(errs, fmt.Errorf("invalid protocol %q, supported protocols are HTTP, HTTP2, GRPC, MONGO, REDIS, MYSQL, TCP", port.Protocol)) } if port.Number > 0 { @@ -2417,8 +2418,8 @@ func ValidateServiceEntry(_, _ string, config proto.Message) (errs error) { if serviceEntry.Resolution != networking.ServiceEntry_NONE && len(serviceEntry.Hosts) > 1 { canDifferentiate := true for _, port := range serviceEntry.Ports { - protocol := ParseProtocol(port.Protocol) - if !protocol.IsHTTP() && !protocol.IsTLS() { + p := protocol.Parse(port.Protocol) + if !p.IsHTTP() && !p.IsTLS() { canDifferentiate = false break } @@ -2447,9 +2448,9 @@ func validatePortName(name string) error { return nil } -func validateProtocol(protocol string) error { - if ParseProtocol(protocol) == ProtocolUnsupported { - return fmt.Errorf("unsupported protocol: %s", protocol) +func validateProtocol(protocolStr string) error { + if protocol.Parse(protocolStr) == protocol.Unsupported { + return fmt.Errorf("unsupported protocol: %s", protocolStr) } return nil } diff --git a/pkg/test/conformance/stage.go b/pkg/test/conformance/stage.go index 0a65ca1cbbb8..dd6ec978a6d1 100644 --- a/pkg/test/conformance/stage.go +++ b/pkg/test/conformance/stage.go @@ -48,7 +48,7 @@ type Traffic struct { type Port struct { Name string - Protocol string // config.Protocol + Protocol string // protocol.Instance ServicePort uint16 } diff --git a/pkg/test/echo/cmd/server/main.go b/pkg/test/echo/cmd/server/main.go index 5ed0049f4b18..c01641851291 100644 --- a/pkg/test/echo/cmd/server/main.go +++ b/pkg/test/echo/cmd/server/main.go @@ -24,7 +24,7 @@ import ( "istio.io/istio/pilot/pkg/model" "istio.io/istio/pkg/cmd" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/server" "istio.io/pkg/log" ) @@ -51,7 +51,7 @@ var ( for i, p := range httpPorts { ports[portIndex] = &model.Port{ Name: "http-" + strconv.Itoa(i), - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, Port: p, } portIndex++ @@ -59,7 +59,7 @@ var ( for i, p := range grpcPorts { ports[portIndex] = &model.Port{ Name: "grpc-" + strconv.Itoa(i), - Protocol: config.ProtocolGRPC, + Protocol: protocol.GRPC, Port: p, } portIndex++ diff --git a/pkg/test/echo/server/endpoint/instance.go b/pkg/test/echo/server/endpoint/instance.go index 19c639dd42d9..ac52c09e10c1 100644 --- a/pkg/test/echo/server/endpoint/instance.go +++ b/pkg/test/echo/server/endpoint/instance.go @@ -19,7 +19,7 @@ import ( "io" "istio.io/istio/pilot/pkg/model" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/common" ) @@ -50,9 +50,9 @@ type Instance interface { func New(cfg Config) (Instance, error) { if cfg.Port != nil { switch cfg.Port.Protocol { - case config.ProtocolTCP, config.ProtocolHTTP, config.ProtocolHTTPS: + case protocol.TCP, protocol.HTTP, protocol.HTTPS: return newHTTP(cfg), nil - case config.ProtocolHTTP2, config.ProtocolGRPC: + case protocol.HTTP2, protocol.GRPC: return newGRPC(cfg), nil default: return nil, fmt.Errorf("unsupported protocol: %s", cfg.Port.Protocol) diff --git a/pkg/test/echo/server/instance.go b/pkg/test/echo/server/instance.go index ec36383f6d7f..fb4e53e4eedf 100644 --- a/pkg/test/echo/server/instance.go +++ b/pkg/test/echo/server/instance.go @@ -23,7 +23,7 @@ import ( "github.com/hashicorp/go-multierror" "istio.io/istio/pilot/pkg/model" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/common" "istio.io/istio/pkg/test/echo/server/endpoint" "istio.io/pkg/log" @@ -144,11 +144,11 @@ func (s *Instance) waitUntilReady() error { func (s *Instance) validate() error { for _, port := range s.Ports { switch port.Protocol { - case config.ProtocolTCP: - case config.ProtocolHTTP: - case config.ProtocolHTTPS: - case config.ProtocolHTTP2: - case config.ProtocolGRPC: + case protocol.TCP: + case protocol.HTTP: + case protocol.HTTPS: + case protocol.HTTP2: + case protocol.GRPC: default: return fmt.Errorf("protocol %v not currently supported", port.Protocol) } diff --git a/pkg/test/framework/components/echo/common/call.go b/pkg/test/framework/components/echo/common/call.go index b00abfb959bd..3f3283c209bd 100644 --- a/pkg/test/framework/components/echo/common/call.go +++ b/pkg/test/framework/components/echo/common/call.go @@ -23,7 +23,7 @@ import ( "reflect" "strconv" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/client" "istio.io/istio/pkg/test/echo/common" "istio.io/istio/pkg/test/echo/common/scheme" @@ -155,11 +155,11 @@ func fillInCallOptions(opts *echo.CallOptions) error { func schemeForPort(port *echo.Port) (scheme.Instance, error) { switch port.Protocol { - case config.ProtocolGRPC, config.ProtocolGRPCWeb, config.ProtocolHTTP2: + case protocol.GRPC, protocol.GRPCWeb, protocol.HTTP2: return scheme.GRPC, nil - case config.ProtocolHTTP, config.ProtocolTCP: + case protocol.HTTP, protocol.TCP: return scheme.HTTP, nil - case config.ProtocolHTTPS, config.ProtocolTLS: + case protocol.HTTPS, protocol.TLS: return scheme.HTTPS, nil default: return "", fmt.Errorf("failed creating call for port %s: unsupported protocol %s", diff --git a/pkg/test/framework/components/echo/common/config.go b/pkg/test/framework/components/echo/common/config.go index ec53dac9ad5e..dab3dc01e4ce 100644 --- a/pkg/test/framework/components/echo/common/config.go +++ b/pkg/test/framework/components/echo/common/config.go @@ -18,7 +18,7 @@ import ( "fmt" "strings" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/framework/components/echo" "istio.io/istio/pkg/test/framework/components/namespace" "istio.io/istio/pkg/test/framework/resource" @@ -93,7 +93,7 @@ func FillInDefaults(ctx resource.Context, defaultDomain string, c *echo.Config) } // GetPortForProtocol returns the first port found with the given protocol, or nil if none was found. -func GetPortForProtocol(c *echo.Config, protocol config.Protocol) *echo.Port { +func GetPortForProtocol(c *echo.Config, protocol protocol.Instance) *echo.Port { for _, p := range c.Ports { if p.Protocol == protocol { return &p @@ -103,7 +103,7 @@ func GetPortForProtocol(c *echo.Config, protocol config.Protocol) *echo.Port { } // AddPortIfMissing adds a port for the given protocol if none was found. -func AddPortIfMissing(c *echo.Config, protocol config.Protocol) { +func AddPortIfMissing(c *echo.Config, protocol protocol.Instance) { if GetPortForProtocol(c, protocol) == nil { c.Ports = append([]echo.Port{ { diff --git a/pkg/test/framework/components/echo/common/envoy_test.go b/pkg/test/framework/components/echo/common/envoy_test.go index db4bd114a90c..9078eabf0265 100644 --- a/pkg/test/framework/components/echo/common/envoy_test.go +++ b/pkg/test/framework/components/echo/common/envoy_test.go @@ -24,7 +24,7 @@ import ( envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" "github.com/gogo/protobuf/jsonpb" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test" "istio.io/istio/pkg/test/echo/client" "istio.io/istio/pkg/test/echo/proto" @@ -47,7 +47,7 @@ func TestCheckOutboundConfig(t *testing.T) { cfgs := []testConfig{ { - protocol: config.ProtocolHTTP, + protocol: protocol.HTTP, service: "b", namespace: "apps-1-99281", domain: "cluster.local", @@ -55,7 +55,7 @@ func TestCheckOutboundConfig(t *testing.T) { address: "10.43.241.185", }, { - protocol: config.ProtocolHTTP, + protocol: protocol.HTTP, service: "b", namespace: "apps-1-99281", domain: "cluster.local", @@ -63,7 +63,7 @@ func TestCheckOutboundConfig(t *testing.T) { address: "10.43.241.185", }, { - protocol: config.ProtocolTCP, + protocol: protocol.TCP, service: "b", namespace: "apps-1-99281", domain: "cluster.local", @@ -71,7 +71,7 @@ func TestCheckOutboundConfig(t *testing.T) { address: "10.43.241.185", }, { - protocol: config.ProtocolHTTPS, + protocol: protocol.HTTPS, service: "b", namespace: "apps-1-99281", domain: "cluster.local", @@ -79,7 +79,7 @@ func TestCheckOutboundConfig(t *testing.T) { address: "10.43.241.185", }, { - protocol: config.ProtocolHTTP2, + protocol: protocol.HTTP2, service: "b", namespace: "apps-1-99281", domain: "cluster.local", @@ -87,7 +87,7 @@ func TestCheckOutboundConfig(t *testing.T) { address: "10.43.241.185", }, { - protocol: config.ProtocolGRPC, + protocol: protocol.GRPC, service: "b", namespace: "apps-1-99281", domain: "cluster.local", @@ -111,7 +111,7 @@ var _ echo.Instance = &testConfig{} var _ echo.Workload = &testConfig{} type testConfig struct { - protocol config.Protocol + protocol protocol.Instance servicePort int address string service string diff --git a/pkg/test/framework/components/echo/common/portgen.go b/pkg/test/framework/components/echo/common/portgen.go index 79150dcc7bee..4cfd08412800 100644 --- a/pkg/test/framework/components/echo/common/portgen.go +++ b/pkg/test/framework/components/echo/common/portgen.go @@ -17,7 +17,7 @@ package common import ( "math" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" ) const ( @@ -44,23 +44,23 @@ func newPortGenerators() *portGenerators { // portGenerator is a utility that generates reasonable default port values // for a given protocol. type portGenerator struct { - next map[config.Protocol]int + next map[protocol.Instance]int used map[int]struct{} } func newPortGenerator() *portGenerator { return &portGenerator{ - next: map[config.Protocol]int{ - config.ProtocolHTTP: httpBase, - config.ProtocolHTTPS: httpsBase, - config.ProtocolTLS: httpsBase, - config.ProtocolTCP: tcpBase, - config.ProtocolGRPCWeb: grpcBase, - config.ProtocolGRPC: grpcBase, - config.ProtocolMongo: tcpBase, - config.ProtocolMySQL: tcpBase, - config.ProtocolRedis: tcpBase, - config.ProtocolUDP: tcpBase, + next: map[protocol.Instance]int{ + protocol.HTTP: httpBase, + protocol.HTTPS: httpsBase, + protocol.TLS: httpsBase, + protocol.TCP: tcpBase, + protocol.GRPCWeb: grpcBase, + protocol.GRPC: grpcBase, + protocol.Mongo: tcpBase, + protocol.MySQL: tcpBase, + protocol.Redis: tcpBase, + protocol.UDP: tcpBase, }, used: make(map[int]struct{}), } @@ -80,7 +80,7 @@ func (g *portGenerator) IsUsed(port int) bool { } // Next assigns the next port for the given protocol. -func (g *portGenerator) Next(protocol config.Protocol) int { +func (g *portGenerator) Next(protocol protocol.Instance) int { for { v := g.next[protocol] diff --git a/pkg/test/framework/components/echo/docker/instance.go b/pkg/test/framework/components/echo/docker/instance.go index 3730132fb275..237ded0d6e55 100644 --- a/pkg/test/framework/components/echo/docker/instance.go +++ b/pkg/test/framework/components/echo/docker/instance.go @@ -23,7 +23,7 @@ import ( "github.com/hashicorp/go-multierror" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test" appEcho "istio.io/istio/pkg/test/echo/client" "istio.io/istio/pkg/test/framework/components/echo" @@ -56,8 +56,8 @@ func newInstance(ctx resource.Context, cfg echo.Config) (out *instance, err erro e := ctx.Environment().(*native.Environment) // Fill in defaults for any missing values. - common.AddPortIfMissing(&cfg, config.ProtocolGRPC) - common.AddPortIfMissing(&cfg, config.ProtocolHTTP) + common.AddPortIfMissing(&cfg, protocol.GRPC) + common.AddPortIfMissing(&cfg, protocol.HTTP) if err = common.FillInDefaults(ctx, e.Domain, &cfg); err != nil { return nil, err } diff --git a/pkg/test/framework/components/echo/docker/port.go b/pkg/test/framework/components/echo/docker/port.go index 5b9d0be08214..6ef39f01d207 100644 --- a/pkg/test/framework/components/echo/docker/port.go +++ b/pkg/test/framework/components/echo/docker/port.go @@ -19,7 +19,7 @@ import ( "strconv" "strings" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/docker" "istio.io/istio/pkg/test/framework/components/echo" "istio.io/istio/pkg/test/util/reserveport" @@ -54,9 +54,9 @@ func newPortMap(portMgr reserveport.PortManager, cfg echo.Config) (*portMap, err }) switch p.Protocol { - case config.ProtocolHTTP: + case protocol.HTTP: hasHTTP = true - case config.ProtocolGRPC, config.ProtocolGRPCWeb: + case protocol.GRPC, protocol.GRPCWeb: hasGRPC = true } } @@ -76,7 +76,7 @@ func (m *portMap) toEchoArgs() []string { echoArgs := make([]string, 0) for _, port := range m.ports { portNumber := port.containerPort.ServicePort - if isGRPC(port.containerPort.Protocol) { + if port.containerPort.Protocol.IsGRPC() { echoArgs = append(echoArgs, "--grpc", strconv.Itoa(portNumber)) } else { echoArgs = append(echoArgs, "--port", strconv.Itoa(portNumber)) @@ -108,7 +108,7 @@ func (m *portMap) toDocker() docker.PortMap { func (m *portMap) http() port { for _, port := range m.ports { - if port.containerPort.Protocol == config.ProtocolHTTP { + if port.containerPort.Protocol == protocol.HTTP { return port } } @@ -117,8 +117,7 @@ func (m *portMap) http() port { func (m *portMap) grpc() port { for _, port := range m.ports { - switch port.containerPort.Protocol { - case config.ProtocolGRPC, config.ProtocolGRPCWeb: + if port.containerPort.Protocol.IsGRPC() { return port } } diff --git a/pkg/test/framework/components/echo/docker/util.go b/pkg/test/framework/components/echo/docker/util.go index a3d12ad7fffc..71c227c1f0a2 100644 --- a/pkg/test/framework/components/echo/docker/util.go +++ b/pkg/test/framework/components/echo/docker/util.go @@ -17,8 +17,6 @@ package docker import ( "fmt" "net" - - "istio.io/istio/pkg/config" ) const ( @@ -42,12 +40,3 @@ func getIP() (net.IP, error) { } return nil, fmt.Errorf("unable to find IPv4 address in: %v", addrs) } - -func isGRPC(p config.Protocol) bool { - switch p { - case config.ProtocolGRPC, config.ProtocolGRPCWeb: - return true - default: - return false - } -} diff --git a/pkg/test/framework/components/echo/echo.go b/pkg/test/framework/components/echo/echo.go index f1c18250034c..cb3a8f1dfa65 100644 --- a/pkg/test/framework/components/echo/echo.go +++ b/pkg/test/framework/components/echo/echo.go @@ -19,7 +19,7 @@ import ( envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test" "istio.io/istio/pkg/test/echo/client" "istio.io/istio/pkg/test/echo/proto" @@ -85,7 +85,7 @@ type Port struct { Name string // Protocol to be used for the port. - Protocol config.Protocol + Protocol protocol.Instance // ServicePort number where the service can be reached. Does not necessarily // map to the corresponding port numbers for the instances behind the diff --git a/pkg/test/framework/components/echo/kube/instance.go b/pkg/test/framework/components/echo/kube/instance.go index e4f048b368ec..2aadb99e6a54 100644 --- a/pkg/test/framework/components/echo/kube/instance.go +++ b/pkg/test/framework/components/echo/kube/instance.go @@ -24,7 +24,7 @@ import ( "github.com/hashicorp/go-multierror" "istio.io/istio/pilot/pkg/model" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test" appEcho "istio.io/istio/pkg/test/echo/client" "istio.io/istio/pkg/test/framework/components/echo" @@ -58,7 +58,7 @@ type instance struct { func newInstance(ctx resource.Context, cfg echo.Config) (out *instance, err error) { // Fill in defaults for any missing values. - common.AddPortIfMissing(&cfg, config.ProtocolGRPC) + common.AddPortIfMissing(&cfg, protocol.GRPC) if err = common.FillInDefaults(ctx, defaultDomain, &cfg); err != nil { return nil, err } @@ -78,7 +78,7 @@ func newInstance(ctx resource.Context, cfg echo.Config) (out *instance, err erro c.id = ctx.TrackResource(c) // Save the GRPC port. - grpcPort := common.GetPortForProtocol(&cfg, config.ProtocolGRPC) + grpcPort := common.GetPortForProtocol(&cfg, protocol.GRPC) if grpcPort == nil { return nil, errors.New("unable fo find GRPC command port") } @@ -132,9 +132,9 @@ func getContainerPorts(ports []echo.Port) model.PortList { containerPorts = append(containerPorts, cport) switch p.Protocol { - case config.ProtocolGRPC: + case protocol.GRPC: continue - case config.ProtocolHTTP: + case protocol.HTTP: if p.InstancePort == httpReadinessPort { readyPort = cport } @@ -149,14 +149,14 @@ func getContainerPorts(ports []echo.Port) model.PortList { if readyPort == nil { containerPorts = append(containerPorts, &model.Port{ Name: "http-readiness-port", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, Port: httpReadinessPort, }) } if healthPort == nil { containerPorts = append(containerPorts, &model.Port{ Name: "tcp-health-port", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, Port: tcpHealthPort, }) } diff --git a/pkg/test/framework/components/mixer/native.go b/pkg/test/framework/components/mixer/native.go index 941f85ab1632..0b70a28e6923 100644 --- a/pkg/test/framework/components/mixer/native.go +++ b/pkg/test/framework/components/mixer/native.go @@ -135,7 +135,7 @@ func newNative(ctx resource.Context, config Config) (Instance, error) { //_, err = env.ServiceManager.Create(localServiceName, "", model.PortList{ // &model.Port{ // Name: grpcPortName, - // Protocol: config.ProtocolGRPC, + // Protocol: protocol.GRPC, // Port: port, // }, //}) diff --git a/tests/integration/conformance/conformance_test.go b/tests/integration/conformance/conformance_test.go index 7f27646ffadd..cd749fcea858 100644 --- a/tests/integration/conformance/conformance_test.go +++ b/tests/integration/conformance/conformance_test.go @@ -27,7 +27,7 @@ import ( envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" "gopkg.in/yaml.v2" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test" "istio.io/istio/pkg/test/conformance" "istio.io/istio/pkg/test/conformance/constraint" @@ -229,7 +229,7 @@ func validateTraffic(t framework.TestContext, pil pilot.Instance, gal galley.Ins for _, p := range svc.Ports { ports = append(ports, echo.Port{ Name: p.Name, - Protocol: config.Protocol(p.Protocol), + Protocol: protocol.Instance(p.Protocol), ServicePort: int(p.ServicePort), }) } diff --git a/tests/integration/pilot/locality/main_test.go b/tests/integration/pilot/locality/main_test.go index 6c58151d3520..b38037ef8857 100644 --- a/tests/integration/pilot/locality/main_test.go +++ b/tests/integration/pilot/locality/main_test.go @@ -26,7 +26,7 @@ import ( envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test" "istio.io/istio/pkg/test/framework" "istio.io/istio/pkg/test/framework/components/echo" @@ -157,7 +157,7 @@ func echoConfig(ns namespace.Instance, name string) echo.Config { Ports: []echo.Port{ { Name: "http", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, ServicePort: 80, }, }, diff --git a/tests/integration/pilot/outboundtrafficpolicy/helper.go b/tests/integration/pilot/outboundtrafficpolicy/helper.go index 3d755625f79f..1a5b3d28f33f 100644 --- a/tests/integration/pilot/outboundtrafficpolicy/helper.go +++ b/tests/integration/pilot/outboundtrafficpolicy/helper.go @@ -24,7 +24,7 @@ import ( envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/common/scheme" "istio.io/istio/pkg/test/framework" "istio.io/istio/pkg/test/framework/components/echo" @@ -126,11 +126,11 @@ func RunExternalRequestTest(expected map[string][]string, t *testing.T) { Ports: []echo.Port{ { Name: "http", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "https", - Protocol: config.ProtocolHTTPS, + Protocol: protocol.HTTPS, }, }, }).BuildOrFail(t) diff --git a/tests/integration/pilot/ping_test.go b/tests/integration/pilot/ping_test.go index 95809550c2ee..269a6ebde293 100644 --- a/tests/integration/pilot/ping_test.go +++ b/tests/integration/pilot/ping_test.go @@ -20,7 +20,7 @@ import ( "testing" "time" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/common/scheme" "istio.io/istio/pkg/test/framework" "istio.io/istio/pkg/test/framework/components/echo" @@ -47,11 +47,11 @@ func doTest(t *testing.T, ctx framework.TestContext) { ports := []echo.Port{ { Name: "http", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "tcp", - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, } diff --git a/tests/integration/pilot/traffic_shifting_test.go b/tests/integration/pilot/traffic_shifting_test.go index 1ad14296ed0b..1322ceb1440f 100644 --- a/tests/integration/pilot/traffic_shifting_test.go +++ b/tests/integration/pilot/traffic_shifting_test.go @@ -21,21 +21,18 @@ import ( "sync" "testing" + envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" "github.com/hashicorp/go-multierror" - "istio.io/istio/pkg/test/framework/components/environment" - + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/client" - "istio.io/istio/pkg/test/util/structpath" - - envoyAdmin "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" - - "istio.io/istio/pkg/config" "istio.io/istio/pkg/test/framework" "istio.io/istio/pkg/test/framework/components/echo" "istio.io/istio/pkg/test/framework/components/echo/echoboot" + "istio.io/istio/pkg/test/framework/components/environment" "istio.io/istio/pkg/test/framework/components/namespace" "istio.io/istio/pkg/test/util/file" + "istio.io/istio/pkg/test/util/structpath" "istio.io/istio/pkg/test/util/tmpl" ) @@ -175,7 +172,7 @@ func echoConfig(ns namespace.Instance, name string) echo.Config { Ports: []echo.Port{ { Name: "http", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, }, Galley: g, diff --git a/tests/integration/security/rbac/v1_test.go b/tests/integration/security/rbac/v1_test.go index 902fc14e8a0f..783e99ce3103 100644 --- a/tests/integration/security/rbac/v1_test.go +++ b/tests/integration/security/rbac/v1_test.go @@ -17,7 +17,7 @@ package rbac import ( "testing" - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/echo/common/scheme" "istio.io/istio/pkg/test/framework" "istio.io/istio/pkg/test/framework/components/echo" @@ -245,7 +245,7 @@ func TestV1_Path(t *testing.T) { ports := []echo.Port{ { Name: "http", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, ServicePort: 80, }, } diff --git a/tests/integration/security/util/framework.go b/tests/integration/security/util/framework.go index 119542d97195..27f25d50bcc3 100644 --- a/tests/integration/security/util/framework.go +++ b/tests/integration/security/util/framework.go @@ -15,7 +15,7 @@ package util import ( - "istio.io/istio/pkg/config" + "istio.io/istio/pkg/config/protocol" "istio.io/istio/pkg/test/framework/components/echo" "istio.io/istio/pkg/test/framework/components/galley" "istio.io/istio/pkg/test/framework/components/namespace" @@ -32,15 +32,15 @@ func EchoConfig(name string, ns namespace.Instance, headless bool, annos echo.An Ports: []echo.Port{ { Name: "http", - Protocol: config.ProtocolHTTP, + Protocol: protocol.HTTP, }, { Name: "tcp", - Protocol: config.ProtocolTCP, + Protocol: protocol.TCP, }, { Name: "grpc", - Protocol: config.ProtocolGRPC, + Protocol: protocol.GRPC, }, }, Galley: g,