-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: traffic split #308
feat: traffic split #308
Changes from 1 commit
d08cfec
c618397
d0d5d8b
3ac2766
884d34f
8381fb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package translation | ||
|
||
import ( | ||
configv2alpha1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1" | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
) | ||
|
||
func (t *translator) translateTrafficSplitPlugin(ar *configv2alpha1.ApisixRoute, defaultBackendWeight int, | ||
backends []*configv2alpha1.ApisixRouteHTTPBackend) ([]*apisixv1.Upstream, *apisixv1.TrafficSplitConfig, error) { | ||
var ( | ||
upstreams []*apisixv1.Upstream | ||
wups []apisixv1.TrafficSplitConfigRuleWeightedUpstream | ||
) | ||
|
||
for _, backend := range backends { | ||
svcClusterIP, svcPort, err := t.getServiceClusterIPAndPort(backend, ar) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
ups, err := t.translateUpstream(ar.Namespace, backend.ServiceName, backend.ResolveGranularity, svcClusterIP, svcPort) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
upstreams = append(upstreams, ups) | ||
|
||
weight := _defaultWeight | ||
if backend.Weight != 0 { | ||
weight = backend.Weight | ||
} | ||
wups = append(wups, apisixv1.TrafficSplitConfigRuleWeightedUpstream{ | ||
UpstreamID: ups.ID, | ||
Weight: weight, | ||
}) | ||
} | ||
|
||
// Finally append the default upstream in the route. | ||
wups = append(wups, apisixv1.TrafficSplitConfigRuleWeightedUpstream{ | ||
Weight: defaultBackendWeight, | ||
}) | ||
|
||
tsCfg := &apisixv1.TrafficSplitConfig{ | ||
Rules: []apisixv1.TrafficSplitConfigRule{ | ||
{ | ||
WeightedUpstreams: wups, | ||
}, | ||
}, | ||
} | ||
return upstreams, tsCfg, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package translation | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/apache/apisix-ingress-controller/pkg/id" | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
|
||
"go.uber.org/zap" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
configv2alpha1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1" | ||
"github.com/apache/apisix-ingress-controller/pkg/log" | ||
) | ||
|
||
func (t *translator) getServiceClusterIPAndPort(backend *configv2alpha1.ApisixRouteHTTPBackend, ar *configv2alpha1.ApisixRoute) (string, int32, error) { | ||
svc, err := t.ServiceLister.Services(ar.Namespace).Get(backend.ServiceName) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
svcPort := int32(-1) | ||
loop: | ||
for _, port := range svc.Spec.Ports { | ||
switch backend.ServicePort.Type { | ||
case intstr.Int: | ||
if backend.ServicePort.IntVal == port.Port { | ||
svcPort = port.Port | ||
break loop | ||
} | ||
case intstr.String: | ||
if backend.ServicePort.StrVal == port.Name { | ||
svcPort = port.Port | ||
break loop | ||
} | ||
} | ||
} | ||
if svcPort == -1 { | ||
log.Errorw("ApisixRoute refers to non-existent Service port", | ||
zap.Any("ApisixRoute", ar), | ||
zap.String("port", backend.ServicePort.String()), | ||
) | ||
return "", 0, err | ||
} | ||
|
||
if backend.ResolveGranularity == "service" && svc.Spec.ClusterIP == "" { | ||
log.Errorw("ApisixRoute refers to a headless service but want to use the service level resolve granularity", | ||
zap.Any("ApisixRoute", ar), | ||
zap.Any("service", svc), | ||
) | ||
return "", 0, errors.New("conflict headless service and backend resolve granularity") | ||
} | ||
return svc.Spec.ClusterIP, svcPort, nil | ||
} | ||
|
||
func (t *translator) translateUpstream(namespace, svcName, svcResolveGranularity, svcClusterIP string, svcPort int32) (*apisixv1.Upstream, error) { | ||
ups, err := t.TranslateUpstream(namespace, svcName, svcPort) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if svcResolveGranularity == "service" { | ||
ups.Nodes = []apisixv1.UpstreamNode{ | ||
{ | ||
IP: svcClusterIP, | ||
Port: int(svcPort), | ||
Weight: _defaultWeight, | ||
}, | ||
} | ||
} | ||
ups.FullName = apisixv1.ComposeUpstreamName(namespace, svcName, svcPort) | ||
ups.Name = ups.FullName | ||
ups.ID = id.GenID(ups.FullName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If upstream.ID is nil, what is the behavior of the controller ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update of Upstream will fail in APISIX side since the necessary ID is not provided by the caller. |
||
return ups, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confused here,
backend
will be covered if backends is not empty ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, already check the exclusive relationship before this, when
backends
not empty,backend
isnil
.