Skip to content
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

chore(refactor): the translation of upstream #1295

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 2 additions & 5 deletions pkg/providers/apisix/apisix_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,14 @@ func (c *apisixUpstreamController) sync(ctx context.Context, ev *types.Event) er
c.recordStatus(au, utils.ResourceSyncAborted, err, metav1.ConditionFalse, au.GetGeneration())
return err
}
var newUps *apisixv1.Upstream
newUps := apisixv1.NewDefaultUpstream()
if au.Spec != nil && ev.Type != types.EventDelete {
cfg, ok := portLevelSettings[port.Port]
if !ok {
cfg = au.Spec.ApisixUpstreamConfig
}
// FIXME Same ApisixUpstreamConfig might be translated multiple times.
newUps, err = c.translator.TranslateUpstreamConfigV2(&cfg)
if err != nil {
if err = c.translator.TranslateUpstreamConfigV2(&cfg, newUps); err != nil {
log.Errorw("ApisixUpstream conversion cannot be completed, or the format is incorrect",
zap.Any("object", au),
zap.Error(err),
Expand All @@ -295,8 +294,6 @@ func (c *apisixUpstreamController) sync(ctx context.Context, ev *types.Event) er
c.recordStatus(au, utils.ResourceSyncAborted, err, metav1.ConditionFalse, au.GetGeneration())
return err
}
} else {
newUps = apisixv1.NewDefaultUpstream()
}

newUps.Metadata = ups.Metadata
Expand Down
6 changes: 1 addition & 5 deletions pkg/providers/apisix/translation/apisix_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ func (t *translator) translateTrafficSplitPlugin(ctx *translation.TranslateConte
)

for _, backend := range backends {
svcClusterIP, svcPort, err := t.GetServiceClusterIPAndPort(&backend, ns)
if err != nil {
return nil, err
}
ups, err := t.translateService(ns, backend.ServiceName, backend.Subset, backend.ResolveGranularity, svcClusterIP, svcPort)
ups, err := t.TranslateUpstream(ns, backend.ServiceName, backend.Subset, backend.ResolveGranularity, backend.ServicePort)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/apisix/translation/apisix_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func TestTranslateTrafficSplitPluginBadCases(t *testing.T) {
cfg, err = tr.translateTrafficSplitPlugin(ctx, ar1.Namespace, 30, backends)
assert.Nil(t, cfg)
assert.NotNil(t, err)
assert.Equal(t, "service.spec.ports: port not defined", err.Error())
assert.Contains(t, err.Error(), "service.Spec.Ports: port port-not-found not found")

backends[1].ServicePort.StrVal = "port2"
backends[1].ResolveGranularity = "service"
Expand Down
Loading