Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/common/constants/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
ScopeService = `service`
SideProvider = `provider`
SideConsumer = `consumer`
RuleConfigGroup = `dubbo`

ConfiguratorRuleDotSuffix = ".configurators"
ConfiguratorsSuffix = "configurators"
Expand Down
12 changes: 10 additions & 2 deletions pkg/console/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,12 @@ func UpInsertServiceArgumentRouteConfig(ctx consolectx.Context, req model.BaseSe
logger.Errorf("get service condition rule %s failed, cause: %v", serviceConditionRuleName, err)
return err
}
shouldCreate := conditionRouteRes == nil
if conditionRouteRes == nil {
conditionRouteRes = meshresource.NewConditionRouteResourceWithAttributes(serviceConditionRuleName, req.Mesh)
conditionRouteRes.Spec.Conditions = make([]string, 0)
} else if conditionRouteRes.Spec == nil {
conditionRouteRes.Spec = &meshproto.ConditionRoute{Conditions: make([]string, 0)}
}
conditions := slice.Filter(conditionRouteRes.Spec.Conditions, func(index int, condition string) bool {
return !isArgumentRoute(condition)
Expand All @@ -751,8 +754,13 @@ func UpInsertServiceArgumentRouteConfig(ctx consolectx.Context, req model.BaseSe
Scope: constants.ScopeService,
Conditions: conditions,
}
if err = UpdateConditionRule(ctx, conditionRouteRes); err != nil {
logger.Errorf("create service condition rule %s failed, cause: %v", serviceConditionRuleName, err)
if shouldCreate {
err = CreateConditionRule(ctx, conditionRouteRes)
} else {
err = UpdateConditionRule(ctx, conditionRouteRes)
}
if err != nil {
logger.Errorf("upsert service condition rule %s failed, cause: %v", serviceConditionRuleName, err)
return err
}
return nil
Expand Down
128 changes: 128 additions & 0 deletions pkg/console/service/service_argument_route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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 service

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/apache/dubbo-admin/pkg/common/constants"
"github.com/apache/dubbo-admin/pkg/console/model"
meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
"github.com/apache/dubbo-admin/pkg/core/versioning"
)

func TestUpInsertServiceArgumentRouteConfigCreatesMissingConditionRule(t *testing.T) {
ctx := setupRollbackTestEnv(t)
req := model.BaseServiceReq{
ServiceName: "org.apache.demo.DemoService",
Version: "1.0.0",
Group: "demo",
}

err := UpInsertServiceArgumentRouteConfig(ctx, req, model.ServiceArgumentRoute{
Routes: []model.ServiceArgument{
{
Method: "sayHello",
Conditions: []model.RouteCondition{
{Index: "0", Relation: constants.Equal, Value: "foo"},
},
Destinations: []model.Destination{
{
Conditions: []model.DestinationCondition{
{Tag: "region", Relation: constants.Equal, Value: "hangzhou"},
},
},
},
},
},
})
require.NoError(t, err)

ruleName := "org.apache.demo.DemoService:1.0.0:demo.condition-router"
current, exists, err := ctx.rm.GetByKey(meshresource.ConditionRouteKind, coremodel.BuildResourceKey("", ruleName))
require.NoError(t, err)
require.True(t, exists)
conditionRule := current.(*meshresource.ConditionRouteResource)
require.NotNil(t, conditionRule.Spec)
assert.Equal(t, constants.ConfiguratorVersionV3, conditionRule.Spec.ConfigVersion)
assert.Equal(t, constants.ScopeService, conditionRule.Spec.Scope)
assert.Equal(t, "org.apache.demo.DemoService", conditionRule.Spec.Key)
assert.Equal(t, []string{"method=sayHello & arguments[0]=foo => region=hangzhou"}, conditionRule.Spec.Conditions)

versions, err := ListRuleVersions(ctx, RuleRef{Kind: meshresource.ConditionRouteKind, Name: ruleName})
require.NoError(t, err)
require.Len(t, versions.Items, 1)
assert.Equal(t, versioning.OperationCreate, versions.Items[0].Operation)
}

func TestUpInsertServiceArgumentRouteConfigUpdatesExistingConditionRule(t *testing.T) {
ctx := setupRollbackTestEnv(t)
req := model.BaseServiceReq{ServiceName: "org.apache.demo.DemoService"}
ruleName := "org.apache.demo.DemoService::.condition-router"
require.NoError(t, CreateConditionRule(ctx, conditionRule(ruleName, "=>region=$region")))

err := UpInsertServiceArgumentRouteConfig(ctx, req, model.ServiceArgumentRoute{
Routes: []model.ServiceArgument{
{
Method: "sayHello",
Conditions: []model.RouteCondition{
{Index: "0", Relation: constants.Equal, Value: "bar"},
},
},
},
})
require.NoError(t, err)

current, exists, err := ctx.rm.GetByKey(meshresource.ConditionRouteKind, coremodel.BuildResourceKey("", ruleName))
require.NoError(t, err)
require.True(t, exists)
conditionRule := current.(*meshresource.ConditionRouteResource)
assert.Equal(t, []string{"=>region=$region", "method=sayHello & arguments[0]=bar"}, conditionRule.Spec.Conditions)

versions, err := ListRuleVersions(ctx, RuleRef{Kind: meshresource.ConditionRouteKind, Name: ruleName})
require.NoError(t, err)
require.Len(t, versions.Items, 2)
assert.Equal(t, versioning.OperationUpdate, versions.Items[0].Operation)
}

func TestUpInsertServiceArgumentRouteConfigHandlesExistingRuleWithoutSpec(t *testing.T) {
ctx := setupRollbackTestEnv(t)
req := model.BaseServiceReq{ServiceName: "org.apache.demo.DemoService"}
ruleName := "org.apache.demo.DemoService::.condition-router"
res := meshresource.NewConditionRouteResourceWithAttributes(ruleName, "")
res.Spec = nil
require.NoError(t, ctx.stores[meshresource.ConditionRouteKind].Add(res))

err := UpInsertServiceArgumentRouteConfig(ctx, req, model.ServiceArgumentRoute{
Routes: []model.ServiceArgument{
{Method: "sayHello"},
},
})
require.NoError(t, err)

current, exists, err := ctx.rm.GetByKey(meshresource.ConditionRouteKind, coremodel.BuildResourceKey("", ruleName))
require.NoError(t, err)
require.True(t, exists)
conditionRule := current.(*meshresource.ConditionRouteResource)
require.NotNil(t, conditionRule.Spec)
assert.Equal(t, []string{"method=sayHello"}, conditionRule.Spec.Conditions)
}
22 changes: 16 additions & 6 deletions pkg/discovery/zk/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ func toDeleteMappingResource(mesh, nodePath string) coremodel.Resource {
}

func toUpsertZKConfigResource(mesh, nodePath, nodeData string) coremodel.Resource {
paths := strings.Split(nodePath, constants.PathSeparator)
if len(paths) != 4 {
configName, ok := zkConfigName(nodePath)
if !ok {
return nil
}
configName := paths[3]
res := meshresource.NewZKConfigResourceWithAttributes(configName, mesh)
res.Spec = &meshproto.ZKConfig{
NodeName: configName,
Expand All @@ -130,18 +129,29 @@ func toUpsertZKConfigResource(mesh, nodePath, nodeData string) coremodel.Resourc
}

func toDeleteZKConfigResource(mesh, nodePath string) coremodel.Resource {
paths := strings.Split(nodePath, constants.PathSeparator)
if len(paths) != 4 {
configName, ok := zkConfigName(nodePath)
if !ok {
return nil
}
configName := paths[3]
res := meshresource.NewZKConfigResourceWithAttributes(configName, mesh)
res.Spec = &meshproto.ZKConfig{
NodeName: configName,
}
return res
}

func zkConfigName(nodePath string) (string, bool) {
paths := strings.Split(nodePath, constants.PathSeparator)
switch len(paths) {
case 4:
return paths[3], paths[3] != ""
case 5:
return paths[4], paths[3] != "" && paths[4] != ""
default:
return "", false
}
}

func toUpsertZKMetadataResource(mesh, nodePath, nodeData string) coremodel.Resource {
paths := strings.Split(nodePath, constants.PathSeparator)
if len(paths) < 5 {
Expand Down
65 changes: 65 additions & 0 deletions pkg/discovery/zk/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 zk

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestZKConfigNameSupportsLegacyAndGroupedConfigPaths(t *testing.T) {
tests := []struct {
name string
nodePath string
wantName string
wantOK bool
}{
{
name: "legacy direct config key",
nodePath: "/dubbo/config/demo-provider.condition-router",
wantName: "demo-provider.condition-router",
wantOK: true,
},
{
name: "dubbo-go grouped config key",
nodePath: "/dubbo/config/dubbo/org.apache.demo.DemoService:1.0.0:demo.condition-router",
wantName: "org.apache.demo.DemoService:1.0.0:demo.condition-router",
wantOK: true,
},
{
name: "config root",
nodePath: "/dubbo/config",
wantOK: false,
},
{
name: "group root",
nodePath: "/dubbo/config/dubbo",
wantOK: true,
wantName: "dubbo",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotName, gotOK := zkConfigName(tt.nodePath)
assert.Equal(t, tt.wantOK, gotOK)
assert.Equal(t, tt.wantName, gotName)
})
}
}
Loading
Loading