|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package translator |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + |
| 23 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 24 | + gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 25 | + |
| 26 | + adctypes "github.com/apache/apisix-ingress-controller/api/adc" |
| 27 | + apiv2 "github.com/apache/apisix-ingress-controller/api/v2" |
| 28 | + "github.com/apache/apisix-ingress-controller/internal/controller/label" |
| 29 | + "github.com/apache/apisix-ingress-controller/internal/id" |
| 30 | + "github.com/apache/apisix-ingress-controller/internal/provider" |
| 31 | + "github.com/apache/apisix-ingress-controller/internal/types" |
| 32 | +) |
| 33 | + |
| 34 | +func (t *Translator) TranslateTLSRoute(tctx *provider.TranslateContext, tlsRoute *gatewayv1alpha2.TLSRoute) (*TranslateResult, error) { |
| 35 | + result := &TranslateResult{} |
| 36 | + rules := tlsRoute.Spec.Rules |
| 37 | + labels := label.GenLabel(tlsRoute) |
| 38 | + hosts := make([]string, 0, len(tlsRoute.Spec.Hostnames)) |
| 39 | + for _, hostname := range tlsRoute.Spec.Hostnames { |
| 40 | + hosts = append(hosts, string(hostname)) |
| 41 | + } |
| 42 | + for ruleIndex, rule := range rules { |
| 43 | + service := adctypes.NewDefaultService() |
| 44 | + service.Labels = labels |
| 45 | + service.Name = adctypes.ComposeServiceNameWithStream(tlsRoute.Namespace, tlsRoute.Name, fmt.Sprintf("%d", ruleIndex), "TLS") |
| 46 | + service.ID = id.GenID(service.Name) |
| 47 | + var ( |
| 48 | + upstreams = make([]*adctypes.Upstream, 0) |
| 49 | + weightedUpstreams = make([]adctypes.TrafficSplitConfigRuleWeightedUpstream, 0) |
| 50 | + ) |
| 51 | + for _, backend := range rule.BackendRefs { |
| 52 | + if backend.Namespace == nil { |
| 53 | + namespace := gatewayv1.Namespace(tlsRoute.Namespace) |
| 54 | + backend.Namespace = &namespace |
| 55 | + } |
| 56 | + upstream := newDefaultUpstreamWithoutScheme() |
| 57 | + upNodes, err := t.translateBackendRef(tctx, backend, DefaultEndpointFilter) |
| 58 | + if err != nil { |
| 59 | + continue |
| 60 | + } |
| 61 | + if len(upNodes) == 0 { |
| 62 | + continue |
| 63 | + } |
| 64 | + // TODO: Confirm BackendTrafficPolicy attachment with e2e test case. |
| 65 | + t.AttachBackendTrafficPolicyToUpstream(backend, tctx.BackendTrafficPolicies, upstream) |
| 66 | + upstream.Nodes = upNodes |
| 67 | + var ( |
| 68 | + kind string |
| 69 | + port int32 |
| 70 | + ) |
| 71 | + if backend.Kind == nil { |
| 72 | + kind = types.KindService |
| 73 | + } else { |
| 74 | + kind = string(*backend.Kind) |
| 75 | + } |
| 76 | + if backend.Port != nil { |
| 77 | + port = int32(*backend.Port) |
| 78 | + } |
| 79 | + namespace := string(*backend.Namespace) |
| 80 | + name := string(backend.Name) |
| 81 | + upstreamName := adctypes.ComposeUpstreamNameForBackendRef(kind, namespace, name, port) |
| 82 | + upstream.Name = upstreamName |
| 83 | + upstream.ID = id.GenID(upstreamName) |
| 84 | + upstreams = append(upstreams, upstream) |
| 85 | + } |
| 86 | + |
| 87 | + // Handle multiple backends with traffic-split plugin |
| 88 | + if len(upstreams) == 0 { |
| 89 | + // Create a default upstream if no valid backends |
| 90 | + upstream := adctypes.NewDefaultUpstream() |
| 91 | + service.Upstream = upstream |
| 92 | + } else if len(upstreams) == 1 { |
| 93 | + // Single backend - use directly as service upstream |
| 94 | + service.Upstream = upstreams[0] |
| 95 | + // remove the id and name of the service.upstream, adc schema does not need id and name for it |
| 96 | + service.Upstream.ID = "" |
| 97 | + service.Upstream.Name = "" |
| 98 | + } else { |
| 99 | + // Multiple backends - use traffic-split plugin |
| 100 | + service.Upstream = upstreams[0] |
| 101 | + // remove the id and name of the service.upstream, adc schema does not need id and name for it |
| 102 | + service.Upstream.ID = "" |
| 103 | + service.Upstream.Name = "" |
| 104 | + |
| 105 | + upstreams = upstreams[1:] |
| 106 | + |
| 107 | + if len(upstreams) > 0 { |
| 108 | + service.Upstreams = upstreams |
| 109 | + } |
| 110 | + |
| 111 | + // Set weight in traffic-split for the default upstream |
| 112 | + weight := apiv2.DefaultWeight |
| 113 | + if rule.BackendRefs[0].Weight != nil { |
| 114 | + weight = int(*rule.BackendRefs[0].Weight) |
| 115 | + } |
| 116 | + weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{ |
| 117 | + Weight: weight, |
| 118 | + }) |
| 119 | + |
| 120 | + // Set other upstreams in traffic-split using upstream_id |
| 121 | + for i, upstream := range upstreams { |
| 122 | + weight := apiv2.DefaultWeight |
| 123 | + // get weight from the backend refs starting from the second backend |
| 124 | + if i+1 < len(rule.BackendRefs) && rule.BackendRefs[i+1].Weight != nil { |
| 125 | + weight = int(*rule.BackendRefs[i+1].Weight) |
| 126 | + } |
| 127 | + weightedUpstreams = append(weightedUpstreams, adctypes.TrafficSplitConfigRuleWeightedUpstream{ |
| 128 | + UpstreamID: upstream.ID, |
| 129 | + Weight: weight, |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + if len(weightedUpstreams) > 0 { |
| 134 | + if service.Plugins == nil { |
| 135 | + service.Plugins = make(map[string]any) |
| 136 | + } |
| 137 | + service.Plugins["traffic-split"] = &adctypes.TrafficSplitConfig{ |
| 138 | + Rules: []adctypes.TrafficSplitConfigRule{ |
| 139 | + { |
| 140 | + WeightedUpstreams: weightedUpstreams, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + for _, host := range hosts { |
| 148 | + streamRoute := adctypes.NewDefaultStreamRoute() |
| 149 | + streamRouteName := adctypes.ComposeStreamRouteName(tlsRoute.Namespace, tlsRoute.Name, fmt.Sprintf("%d", ruleIndex), "TLS") |
| 150 | + streamRoute.Name = streamRouteName |
| 151 | + streamRoute.ID = id.GenID(streamRouteName) |
| 152 | + streamRoute.SNI = host |
| 153 | + streamRoute.Labels = labels |
| 154 | + service.StreamRoutes = append(service.StreamRoutes, streamRoute) |
| 155 | + } |
| 156 | + result.Services = append(result.Services, service) |
| 157 | + } |
| 158 | + return result, nil |
| 159 | +} |
0 commit comments