support virtualservice multi port

Signed-off-by: zackzhangkai <zhangkaiamm@gmail.com>
This commit is contained in:
zackzhangkai
2021-03-08 22:44:19 +08:00
parent 6f719e0af6
commit 676062d760
4 changed files with 222 additions and 58 deletions

View File

@@ -45,6 +45,9 @@ var (
applicationName = "bookinfo"
namespace = metav1.NamespaceDefault
subsets = []string{"v1", "v2"}
httpPort = 80
grpcPort = 81
mysqlPort = 82
)
type fixture struct {
@@ -123,7 +126,7 @@ func newVirtualService(name string, host string, labels map[string]string) *v1al
return &vr
}
func newService(name string, labels map[string]string, selector map[string]string, protocol string, port int) *v1.Service {
func newService(name string, labels map[string]string, selector map[string]string) *v1.Service {
svc := v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
@@ -134,9 +137,21 @@ func newService(name string, labels map[string]string, selector map[string]strin
Ports: []v1.ServicePort{
{
Protocol: v1.ProtocolTCP,
Port: int32(port),
Name: fmt.Sprintf("%s-aaa", protocol),
TargetPort: intstr.FromInt(port),
Port: int32(httpPort),
Name: "HTTP-1",
TargetPort: intstr.FromInt(httpPort),
},
{
Protocol: v1.ProtocolTCP,
Port: int32(grpcPort),
Name: "grpc-1",
TargetPort: intstr.FromInt(grpcPort),
},
{
Protocol: v1.ProtocolTCP,
Port: int32(mysqlPort),
Name: "mysql-1",
TargetPort: intstr.FromInt(mysqlPort),
},
},
Selector: selector,
@@ -299,7 +314,7 @@ func (f *fixture) run_(serviceKey string, expectedVS *v1alpha3.VirtualService, s
func TestInitialStrategyCreate(t *testing.T) {
f := newFixture(t)
svc := newService("foo", NewLabels().WithApplication(applicationName).WithApp(serviceName), NewLabels().WithApplication(serviceName).WithApp(applicationName), "http", 80)
svc := newService("foo", NewLabels().WithApplication(applicationName).WithApp(serviceName), NewLabels().WithApplication(serviceName).WithApp(applicationName))
dr := newDestinationRule(svc.Name, toHost(svc), NewLabels().WithApp("foo").WithApplication(applicationName), subsets[0])
svc.Annotations = NewLabels().WithServiceMeshEnabled(true)
@@ -310,21 +325,46 @@ func TestInitialStrategyCreate(t *testing.T) {
vs := newVirtualService(svc.Name, "foo", NewLabels().WithApplication("bookinfo").WithApp(svc.Name))
vs.Annotations = make(map[string]string)
vs.Spec.Http = []*apiv1alpha3.HTTPRoute{
{
Route: []*apiv1alpha3.HTTPRouteDestination{
{
Destination: &apiv1alpha3.Destination{
Host: svc.Name,
Subset: "v1",
Port: &apiv1alpha3.PortSelector{
Number: uint32(svc.Spec.Ports[0].Port),
for _, port := range svc.Spec.Ports {
if servicemesh.SupportHttpProtocol(port.Name) {
httpRoute := apiv1alpha3.HTTPRoute{
Route: []*apiv1alpha3.HTTPRouteDestination{
{
Destination: &apiv1alpha3.Destination{
Host: svc.Name,
Subset: "v1",
Port: &apiv1alpha3.PortSelector{
Number: uint32(port.Port),
},
},
Weight: 100,
},
Weight: 100,
},
},
},
Match: []*apiv1alpha3.HTTPMatchRequest{
{Port: uint32(port.Port)},
},
}
vs.Spec.Http = append(vs.Spec.Http, &httpRoute)
} else {
tcpRoute := apiv1alpha3.TCPRoute{
Route: []*apiv1alpha3.RouteDestination{
{
Destination: &apiv1alpha3.Destination{
Host: svc.Name,
Subset: "v1",
Port: &apiv1alpha3.PortSelector{
Number: uint32(port.Port),
},
},
Weight: 100,
},
},
Match: []*apiv1alpha3.L4MatchAttributes{
{Port: uint32(port.Port)},
},
}
vs.Spec.Tcp = append(vs.Spec.Tcp, &tcpRoute)
}
}
key, err := cache.MetaNamespaceKeyFunc(svc)
@@ -354,7 +394,7 @@ func runStrategy(t *testing.T, svc *v1.Service, dr *v1alpha3.DestinationRule, st
func TestStrategies(t *testing.T) {
svc := newService(serviceName, NewLabels().WithApplication(applicationName).WithApp(serviceName), NewLabels().WithApplication(applicationName).WithApp(serviceName), "http", 80)
svc := newService(serviceName, NewLabels().WithApplication(applicationName).WithApp(serviceName), NewLabels().WithApplication(applicationName).WithApp(serviceName))
defaultDr := newDestinationRule(svc.Name, toHost(svc), NewLabels().WithApp(serviceName).WithApplication(applicationName), subsets...)
svc.Annotations = NewLabels().WithServiceMeshEnabled(true)
defaultStrategy := &v1alpha2.Strategy{
@@ -395,6 +435,9 @@ func TestStrategies(t *testing.T) {
Weight: 20,
},
},
Match: []*apiv1alpha3.HTTPMatchRequest{
{Port: 0},
},
},
},
},
@@ -412,7 +455,7 @@ func TestStrategies(t *testing.T) {
Host: svc.Name,
Subset: "v1",
Port: &apiv1alpha3.PortSelector{
Number: uint32(svc.Spec.Ports[0].Port),
Number: uint32(httpPort),
},
},
Weight: 80,
@@ -422,12 +465,38 @@ func TestStrategies(t *testing.T) {
Host: svc.Name,
Subset: "v2",
Port: &apiv1alpha3.PortSelector{
Number: uint32(svc.Spec.Ports[0].Port),
Number: uint32(httpPort),
},
},
Weight: 20,
},
},
Match: []*apiv1alpha3.HTTPMatchRequest{{Port: uint32(httpPort)}},
},
{
Route: []*apiv1alpha3.HTTPRouteDestination{
{
Destination: &apiv1alpha3.Destination{
Host: svc.Name,
Subset: "v1",
Port: &apiv1alpha3.PortSelector{
Number: uint32(grpcPort),
},
},
Weight: 80,
},
{
Destination: &apiv1alpha3.Destination{
Host: svc.Name,
Subset: "v2",
Port: &apiv1alpha3.PortSelector{
Number: uint32(grpcPort),
},
},
Weight: 20,
},
},
Match: []*apiv1alpha3.HTTPMatchRequest{{Port: uint32(grpcPort)}},
},
}
@@ -443,6 +512,8 @@ func TestStrategies(t *testing.T) {
expected := defaultExpected.DeepCopy()
expected.Spec.Http[0].Route[0].Weight = 0
expected.Spec.Http[0].Route[1].Weight = 100
expected.Spec.Http[1].Route[0].Weight = 0
expected.Spec.Http[1].Route[1].Weight = 100
runStrategy(t, svc, defaultDr, strategy, expected)
})
@@ -454,6 +525,7 @@ func TestStrategies(t *testing.T) {
expected.Spec.Http[0].Route[0].Weight = 100
expected.Spec.Http[0].Route[0].Destination.Subset = "v2"
expected.Spec.Http[0].Route = expected.Spec.Http[0].Route[:1]
expected.Spec.Http = expected.Spec.Http[:1]
runStrategy(t, svc, defaultDr, strategy, expected)
})
@@ -483,6 +555,20 @@ func TestStrategies(t *testing.T) {
Uri: &apiv1alpha3.StringMatch{
MatchType: &apiv1alpha3.StringMatch_Prefix{Prefix: "/apis"},
},
Port: expected.Spec.Http[0].Route[0].Destination.Port.Number,
},
}
expected.Spec.Http[1].Match = []*apiv1alpha3.HTTPMatchRequest{
{
Headers: map[string]*apiv1alpha3.StringMatch{
"X-USER": {
MatchType: &apiv1alpha3.StringMatch_Regex{Regex: "users"},
},
},
Uri: &apiv1alpha3.StringMatch{
MatchType: &apiv1alpha3.StringMatch_Prefix{Prefix: "/apis"},
},
Port: expected.Spec.Http[1].Route[0].Destination.Port.Number,
},
}
runStrategy(t, svc, defaultDr, strategy, expected)