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

fix issue #629 #693

Merged
merged 10 commits into from
Sep 23, 2022
Merged
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 apiserver/httpserver/i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
400216 = "exist usergroup" #UserGroupExisted
400217 = "exist auth strategy rule" #AuthStrategyRuleExisted
400218 = "some sub-account existed in owner" #SubAccountExisted
400219 = "some config group existed in namespace" #NamespaceExistedConfigGroups
400301 = "not found service" #NotFoundService
400302 = "not found routing" #NotFoundRouting
400303 = "not found instances" #NotFoundInstance
Expand Down
1 change: 1 addition & 0 deletions apiserver/httpserver/i18n/zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
400216 = "用户组已存在" #UserGroupExisted
400217 = "鉴权策略规则已存在" #AuthStrategyRuleExisted
400218 = "某些子账号已属当前拥有人" #SubAccountExisted
400219 = "当前命名空间存在配置分组,请先删除配置分组,再删除命名空间" #NamespaceExistedConfigGroups
400301 = "服务未找到" #NotFoundService
400302 = "路由未找到" #NotFoundRouting
400303 = "示例未找到" #NotFoundInstance
Expand Down
3 changes: 3 additions & 0 deletions common/api/v1/codeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const (
NamespaceExistedCircuitBreakers uint32 = 400212
ServiceSubscribedByMeshes uint32 = 400213
ServiceExistedFluxRateLimits uint32 = 400214
NamespaceExistedConfigGroups uint32 = 400219

NotFoundService uint32 = 400301
NotFoundRouting uint32 = 400302
Expand Down Expand Up @@ -342,6 +343,8 @@ var code2info = map[uint32]string{
SubAccountExisted: "some sub-account existed in owner",
InvalidUserID: "invalid user-id",
TokenNotExisted: "token not existed",

NamespaceExistedConfigGroups: "some config group existed in namespace",
}

// code to info
Expand Down
22 changes: 22 additions & 0 deletions namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ func (s *Server) DeleteNamespace(ctx context.Context, req *api.Namespace) *api.R
return api.NewNamespaceResponse(api.NamespaceExistedCircuitBreakers, req)
}

// 判断属于该命名空间的服务是否都已经被删除
total, err = s.getConfigGroupCountWithNamespace(namespace.Name)
if err != nil {
log.Error("get config group count with namespace err",
zap.String("request-id", requestID),
zap.String("err", err.Error()))
return api.NewNamespaceResponse(api.StoreLayerException, req)
}
if total != 0 {
log.Error("the removed namespace has remain config-group", zap.String("request-id", requestID))
return api.NewNamespaceResponse(api.NamespaceExistedConfigGroups, req)
}

// 存储层操作
if err := tx.DeleteNamespace(namespace.Name); err != nil {
log.Error(err.Error(), zap.String("request-id", requestID))
Expand Down Expand Up @@ -383,6 +396,15 @@ func (s *Server) getServicesCountWithNamespace(namespace string) (uint32, error)
return total, nil
}

// 根据命名空间查询配置分组总数
func (s *Server) getConfigGroupCountWithNamespace(namespace string) (uint32, error) {
total, _, err := s.storage.QueryConfigFileGroups(namespace, "", 0, 1)
if err != nil {
return 0, err
}
return total, nil
}

// 根据命名空间查询熔断规则数量
func (s *Server) getCircuitBreakerCountWithNamespace(namespace string) (uint32, error) {
filter := map[string]string{"namespace": namespace}
Expand Down
4 changes: 2 additions & 2 deletions test/http/circuitbreaker_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (c *Client) CreateCircuitBreakers(circuitBreakers []*api.CircuitBreaker) (*
ret, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
return ret, err
}

return checkCreateCircuitBreakersResponse(ret, circuitBreakers)
Expand All @@ -118,7 +118,7 @@ func (c *Client) CreateCircuitBreakerVersions(circuitBreakers []*api.CircuitBrea
ret, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
return ret, err
}

return checkCreateCircuitBreakersResponse(ret, circuitBreakers)
Expand Down
3 changes: 2 additions & 1 deletion test/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ func GetBatchWriteResponse(response *http.Response) (*api.BatchWriteResponse, er
fmt.Printf("%+v\n", ret)
} else {
fmt.Printf("%v\n", checkErr)
ret = nil
}

// 检查回复
if response.StatusCode != 200 {
return nil, errors.New("invalid http code")
return ret, fmt.Errorf("invalid http code : %d, ret code : %d", response.StatusCode, ret.GetCode().GetValue())
}

if checkErr == nil {
Expand Down
4 changes: 2 additions & 2 deletions test/http/config_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func checkCreateConfigResponse(ret *api.ConfigResponse) (

switch {
case ret.GetCode().GetValue() != api.ExecuteSuccess:
return nil, errors.New("invalid batch code")
return nil, errors.New(ret.GetInfo().GetValue())
}

return ret, nil
Expand All @@ -292,7 +292,7 @@ func checkQueryConfigResponse(ret *api.ConfigBatchQueryResponse) (

switch {
case ret.GetCode().GetValue() != api.ExecuteSuccess:
return nil, errors.New("invalid batch code")
return nil, errors.New(ret.GetInfo().GetValue())
}

return ret, nil
Expand Down
2 changes: 1 addition & 1 deletion test/http/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *Client) CreateInstances(instances []*api.Instance) (*api.BatchWriteResp
ret, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
return ret, err
}

return checkCreateInstancesResponse(ret, instances)
Expand Down
29 changes: 28 additions & 1 deletion test/http/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Client) CreateNamespaces(namespaces []*api.Namespace) (*api.BatchWriteR
ret, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
return ret, err
}

return checkCreateNamespacesResponse(ret, namespaces)
Expand Down Expand Up @@ -108,6 +108,33 @@ func (c *Client) DeleteNamespaces(namespaces []*api.Namespace) error {
return nil
}

// DeleteNamespaces 删除命名空间
func (c *Client) DeleteNamespacesGetResp(namespaces []*api.Namespace) (*api.BatchWriteResponse, error) {
fmt.Printf("\ndelete namespaces\n")

url := fmt.Sprintf("http://%v/naming/%v/namespaces/delete", c.Address, c.Version)

body, err := JSONFromNamespaces(namespaces)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
}

response, err := c.SendRequest("POST", url, body)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
}

resp, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return resp, err
}

return resp, nil
}

// UpdateNamesapces 更新命名空间
func (c *Client) UpdateNamesapces(namespaces []*api.Namespace) error {
fmt.Printf("\nupdate namespaces\n")
Expand Down
2 changes: 1 addition & 1 deletion test/http/ratelimit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Client) CreateRateLimits(rateLimits []*api.Rule) (*api.BatchWriteRespon
ret, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
return ret, err
}

return checkCreateRateLimitsResponse(ret, rateLimits)
Expand Down
2 changes: 1 addition & 1 deletion test/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Client) CreateServices(services []*api.Service) (*api.BatchWriteRespons
ret, err := GetBatchWriteResponse(response)
if err != nil {
fmt.Printf("%v\n", err)
return nil, err
return ret, err
}

return checkCreateServicesResponse(ret, services)
Expand Down
89 changes: 89 additions & 0 deletions test/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,96 @@ func TestCountNamespaceService(t *testing.T) {
t.Logf("TestNamespaceServiceCnt success")

// 开始清理所有的数据
}

// TestDeleteNamespaceWhenHaveService 当命名空间下还有服务时进行删除命名空间的操作
func TestDeleteNamespaceWhenHaveService(t *testing.T) {
t.Log("test namepsace delete when has service")
client := http.NewClient(httpserverAddress, httpserverVersion)

namespaces := resource.CreateNamespaces()

// 创建命名空间
_, err := client.CreateNamespaces(namespaces)
if err != nil {
t.Fatalf("create namespaces fail: %s", err.Error())
}
t.Log("create namepsaces success")

services := resource.CreateServices(namespaces[0])
if _, err = client.CreateServices(services); err != nil {
t.Fatal(err)
}

resp, err := client.DeleteNamespacesGetResp(namespaces)
if resp != nil {
if resp.GetCode().GetValue() != v1.NamespaceExistedServices {
t.Fatalf("delete namespace need return code:NamespaceExistedServices, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
}
}
if err != nil && resp == nil {
t.Fatalf("delete namespaces fail: %s", err.Error())
}

// 删除 service
if err := client.DeleteServices(services); err != nil {
t.Fatalf("delete service fail: %+v", err)
}

resp, err = client.DeleteNamespacesGetResp(namespaces)
if resp != nil {
if resp.GetCode().GetValue() != v1.ExecuteSuccess {
t.Fatalf("delete namespace need return code:ExecuteSuccess, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
}
}
if err != nil && resp == nil {
t.Fatalf("delete namespaces fail: %s", err.Error())
}
}

// TestDeleteNamespaceWhenHaveConfigGroup 当命名空间下还有配置时进行删除命名空间的操作
func TestDeleteNamespaceWhenHaveConfigGroup(t *testing.T) {
t.Log("test namepsace interface")
client := http.NewClient(httpserverAddress, httpserverVersion)

namespaces := resource.CreateNamespaces()

// 创建命名空间
_, err := client.CreateNamespaces(namespaces)
if err != nil {
t.Fatalf("create namespaces fail: %s", err.Error())
}
t.Log("create namepsaces success")

groups := resource.MockConfigGroups(namespaces[0])
if _, err = client.CreateConfigGroup(groups[0]); err != nil {
t.Fatal(err)
}

resp, err := client.DeleteNamespacesGetResp(namespaces)
if resp != nil {
if resp.GetCode().GetValue() != v1.NamespaceExistedConfigGroups {
t.Fatalf("delete namespace need return code:NamespaceExistedConfigGroups, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
}
}
if err != nil && resp == nil {
t.Fatalf("delete namespaces fail: %s", err.Error())
}

// 删除配置分组
if _, err := client.DeleteConfigGroup(groups[0]); err != nil {
t.Fatalf("delete service fail: %+v", err)
}

resp, err = client.DeleteNamespacesGetResp(namespaces)
if resp != nil {
if resp.GetCode().GetValue() != v1.ExecuteSuccess {
t.Fatalf("delete namespace need return code:ExecuteSuccess, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
}
}
if err != nil && resp == nil {
t.Fatalf("delete namespaces fail: %s", err.Error())
}
}

func createServiceAndInstance(t *testing.T, expectRes *map[string]model.NamespaceServiceCount, client *http.Client, namespace *v1.Namespace) ([]*v1.Service, []*v1.Instance) {
Expand Down