From 926f858a13645f8f0a37249ef031dcd4385f63f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=84=E6=BD=AD?= Date: Thu, 12 Jan 2023 11:20:24 +0800 Subject: [PATCH] support multi destination (#119) --- pkg/ingress/kube/annotations/destination.go | 5 +++-- pkg/ingress/kube/common/tool.go | 7 +++++-- pkg/ingress/kube/ingress/controller.go | 6 +++++- pkg/ingress/kube/ingressv1/controller.go | 5 +++++ samples/nacos-discovery/canary.yaml | 21 +++++++++++++++++++ .../nacos-discovery/multi-destination.yaml | 21 +++++++++++++++++++ 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 samples/nacos-discovery/canary.yaml create mode 100644 samples/nacos-discovery/multi-destination.yaml diff --git a/pkg/ingress/kube/annotations/destination.go b/pkg/ingress/kube/annotations/destination.go index d416c6e8b..e69d9c5e9 100644 --- a/pkg/ingress/kube/annotations/destination.go +++ b/pkg/ingress/kube/annotations/destination.go @@ -32,6 +32,7 @@ var _ Parser = destination{} type DestinationConfig struct { McpDestination []*networking.HTTPRouteDestination + WeightSum int64 } type destination struct{} @@ -99,11 +100,11 @@ func (a destination) Parse(annotations Annotations, config *Ingress, globalConte destinations = append(destinations, dest) } if weightSum != 100 { - IngressLog.Errorf("destination has invalid weight sum %d within ingress %s/%s", weightSum, config.Namespace, config.Name) - return nil + IngressLog.Warnf("destination has invalid weight sum %d within ingress %s/%s", weightSum, config.Namespace, config.Name) } config.Destination = &DestinationConfig{ McpDestination: destinations, + WeightSum: weightSum, } return nil } diff --git a/pkg/ingress/kube/common/tool.go b/pkg/ingress/kube/common/tool.go index 92f066f16..ea8fe54ba 100644 --- a/pkg/ingress/kube/common/tool.go +++ b/pkg/ingress/kube/common/tool.go @@ -295,12 +295,15 @@ func SplitServiceFQDN(fqdn string) (string, string, bool) { func ConvertBackendService(routeDestination *networking.HTTPRouteDestination) model.BackendService { parts := strings.Split(routeDestination.Destination.Host, ".") - return model.BackendService{ + service := model.BackendService{ Namespace: parts[1], Name: parts[0], - Port: routeDestination.Destination.Port.Number, Weight: routeDestination.Weight, } + if routeDestination.Destination.Port != nil { + service.Port = routeDestination.Destination.Port.Number + } + return service } func getLoadBalancerIp(svc *v1.Service) []string { diff --git a/pkg/ingress/kube/ingress/controller.go b/pkg/ingress/kube/ingress/controller.go index 0732c2b96..e2ccbcad6 100644 --- a/pkg/ingress/kube/ingress/controller.go +++ b/pkg/ingress/kube/ingress/controller.go @@ -575,6 +575,10 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra destinationConfig := wrapper.AnnotationsConfig.Destination wrapperHttpRoute.HTTPRoute.Route, event = c.backendToRouteDestination(&httpPath.Backend, cfg.Namespace, ingressRouteBuilder, destinationConfig) + if destinationConfig != nil { + wrapperHttpRoute.WeightTotal = int32(destinationConfig.WeightSum) + } + if ingressRouteBuilder.Event != common.Normal { event = ingressRouteBuilder.Event } @@ -961,7 +965,7 @@ func (c *controller) createServiceKey(service *ingress.IngressBackend, namespace } func isCanaryRoute(canary, route *common.WrapperHTTPRoute) bool { - return !strings.HasSuffix(route.HTTPRoute.Name, "-canary") && canary.OriginPath == route.OriginPath && + return !route.WrapperConfig.AnnotationsConfig.IsCanary() && canary.OriginPath == route.OriginPath && canary.OriginPathType == route.OriginPathType } diff --git a/pkg/ingress/kube/ingressv1/controller.go b/pkg/ingress/kube/ingressv1/controller.go index d63af1c9a..a468adb6f 100644 --- a/pkg/ingress/kube/ingressv1/controller.go +++ b/pkg/ingress/kube/ingressv1/controller.go @@ -570,6 +570,11 @@ func (c *controller) ConvertHTTPRoute(convertOptions *common.ConvertOptions, wra var event common.Event destinationConfig := wrapper.AnnotationsConfig.Destination wrapperHttpRoute.HTTPRoute.Route, event = c.backendToRouteDestination(&httpPath.Backend, cfg.Namespace, ingressRouteBuilder, destinationConfig) + + if destinationConfig != nil { + wrapperHttpRoute.WeightTotal = int32(destinationConfig.WeightSum) + } + if ingressRouteBuilder.Event != common.Normal { event = ingressRouteBuilder.Event } diff --git a/samples/nacos-discovery/canary.yaml b/samples/nacos-discovery/canary.yaml new file mode 100644 index 000000000..dc6842564 --- /dev/null +++ b/samples/nacos-discovery/canary.yaml @@ -0,0 +1,21 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + annotations: + higress.io/destination: service-provider-gray.DEFAULT-GROUP.public.nacos + nginx.ingress.kubernetes.io/canary: 'true' + nginx.ingress.kubernetes.io/canary-by-header: x-user-id + nginx.ingress.kubernetes.io/canary-by-header-value: '100' + name: echo-gray + namespace: default +spec: + rules: + - http: + paths: + - backend: + resource: + apiGroup: networking.higress.io + kind: McpBridge + name: default + path: /echo + pathType: Prefix diff --git a/samples/nacos-discovery/multi-destination.yaml b/samples/nacos-discovery/multi-destination.yaml new file mode 100644 index 000000000..85dbdf5c0 --- /dev/null +++ b/samples/nacos-discovery/multi-destination.yaml @@ -0,0 +1,21 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + annotations: + higress.io/destination: | + 33% service-provider.DEFAULT-GROUP.public.nacos + 33% service-provider-gray.DEFAULT-GROUP.public.nacos + 34% bar-service.default.svc.cluster.local:5678 + name: echo + namespace: default +spec: + rules: + - http: + paths: + - backend: + resource: + apiGroup: networking.higress.io + kind: McpBridge + name: default + path: /echo + pathType: Prefix