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

[type:feat]Discovery adapts to namespace transformation #5655

Merged
merged 28 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 22 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
6 changes: 6 additions & 0 deletions db/init/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2432,3 +2432,9 @@ ALTER TABLE `shenyu`.`rule` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT '
ALTER TABLE `shenyu`.`meta_data` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId';

ALTER TABLE `shenyu`.`app_auth` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `enabled`;

ALTER TABLE `shenyu`.`discovery` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `plugin_name`;

ALTER TABLE `shenyu`.`discovery_upstream` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `discovery_handler_id`;

ALTER TABLE `shenyu`.`proxy_selector` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `props`;
8 changes: 7 additions & 1 deletion db/init/ob/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2381,4 +2381,10 @@ ALTER TABLE `shenyu`.`rule` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT '

ALTER TABLE `shenyu`.`meta_data` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId';

ALTER TABLE `shenyu`.`app_auth` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `enabled`;
ALTER TABLE `shenyu`.`app_auth` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `enabled`;

ALTER TABLE `shenyu`.`discovery` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `plugin_name`;

ALTER TABLE `shenyu`.`discovery_upstream` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `discovery_handler_id`;

ALTER TABLE `shenyu`.`proxy_selector` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `props`;
10 changes: 10 additions & 0 deletions db/init/og/create-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2761,3 +2761,13 @@ COMMENT ON COLUMN "public"."meta_data"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."app_auth" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."app_auth"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."discovery" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."discovery_upstream" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."proxy_selector" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."proxy_selector"."namespace_id" IS 'namespaceId';

10 changes: 10 additions & 0 deletions db/init/oracle/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2975,3 +2975,13 @@ COMMENT ON COLUMN meta_data.namespace_id IS 'namespaceId';

ALTER TABLE app_auth ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN app_auth.namespace_id IS 'namespaceId';

ALTER TABLE discovery ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN discovery.namespace_id IS 'namespaceId';

ALTER TABLE discovery_upstream ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN discovery_upstream.namespace_id IS 'namespaceId';

ALTER TABLE proxy_selector ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN proxy_selector.namespace_id IS 'namespaceId';

9 changes: 9 additions & 0 deletions db/init/pg/create-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2878,3 +2878,12 @@ COMMENT ON COLUMN "public"."meta_data"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."app_auth" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."app_auth"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."discovery" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."discovery_upstream" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."proxy_selector" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."proxy_selector"."namespace_id" IS 'namespaceId';
21 changes: 21 additions & 0 deletions db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,24 @@ ALTER TABLE `shenyu`.`app_auth` ADD COLUMN `namespace_id` varchar(50) NULL COMME
UPDATE app_auth
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;
ALTER TABLE `shenyu`.`discovery` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `plugin_name`;

UPDATE discovery
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;

ALTER TABLE `shenyu`.`discovery_upstream` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `discovery_handler_id`;

UPDATE discovery_upstream
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;


ALTER TABLE `shenyu`.`proxy_selector` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `props`;

UPDATE proxy_selector
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;



17 changes: 17 additions & 0 deletions db/upgrade/2.6.1-upgrade-2.7.0-og.sql
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,20 @@ ALTER TABLE "public"."app_auth" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."app_auth"."namespace_id" IS 'namespaceId';

UPDATE "public"."app_auth" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;



ALTER TABLE "public"."discovery" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery"."namespace_id" IS 'namespaceId';

UPDATE "public"."discovery" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

ALTER TABLE "public"."discovery_upstream" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'namespaceId';

UPDATE "public"."discovery_upstream" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

ALTER TABLE "public"."proxy_selector" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."proxy_selector"."namespace_id" IS 'namespaceId';

UPDATE "public"."proxy_selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;
16 changes: 16 additions & 0 deletions db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,19 @@ ALTER TABLE app_auth ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN app_auth.namespace_id IS 'namespaceId';

UPDATE app_auth SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;


ALTER TABLE discovery ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN discovery.namespace_id IS 'namespaceId';

UPDATE discovery SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

ALTER TABLE discovery_upstream ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN discovery_upstream.namespace_id IS 'namespaceId';

UPDATE discovery_upstream SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

ALTER TABLE proxy_selector ADD namespace_id VARCHAR2(50) NULL;
COMMENT ON COLUMN proxy_selector.namespace_id IS 'namespaceId';

UPDATE proxy_selector SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;
16 changes: 16 additions & 0 deletions db/upgrade/2.6.1-upgrade-2.7.0-pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,19 @@ COMMENT ON COLUMN "public"."app_auth"."namespace_id" IS 'namespaceId';

UPDATE "public"."app_auth" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;



ALTER TABLE "public"."discovery" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery"."namespace_id" IS 'namespaceId';

UPDATE "public"."discovery" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

ALTER TABLE "public"."discovery_upstream" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'namespaceId';

UPDATE "public"."discovery_upstream" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

ALTER TABLE "public"."proxy_selector" ADD COLUMN namespace_id VARCHAR(50) NULL;
COMMENT ON COLUMN "public"."proxy_selector"."namespace_id" IS 'namespaceId';

UPDATE "public"."proxy_selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public ShenyuAdminResult typeEnums() {
*
* @param pluginName plugin name
* @param level level
* @param namespaceId namespaceId
* @return {@link org.apache.shenyu.admin.model.vo.DiscoveryVO}
*/
@GetMapping("")
public ShenyuAdminResult discovery(final String pluginName, final String level) {
return ShenyuAdminResult.success(ShenyuResultMessage.SUCCESS, discoveryService.discovery(pluginName, level));
public ShenyuAdminResult discovery(final String pluginName, final String level, final String namespaceId) {
return ShenyuAdminResult.success(ShenyuResultMessage.SUCCESS, discoveryService.discovery(pluginName, level, namespaceId));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.admin.controller;

import org.apache.shenyu.admin.aspect.annotation.RestApi;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.model.dto.ProxySelectorAddDTO;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.page.PageParameter;
Expand All @@ -26,6 +27,7 @@
import org.apache.shenyu.admin.model.vo.ProxySelectorVO;
import org.apache.shenyu.admin.service.ProxySelectorService;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shenyu.admin.validation.annotation.Existed;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -55,14 +57,17 @@ public ProxySelectorController(final ProxySelectorService proxySelectorService)
* @param name name
* @param currentPage currentPage
* @param pageSize pageSize
* @param namespaceId namespaceId
* @return {@linkplain ShenyuAdminResult}
*/
@GetMapping("")
public ShenyuAdminResult queryProxySelector(final String name, @NotNull final Integer currentPage,
@NotNull final Integer pageSize) {
@NotNull final Integer pageSize,
@Existed(message = "namespace is not existed",
provider = NamespaceMapper.class) final String namespaceId) {

CommonPager<ProxySelectorVO> commonPager = proxySelectorService
.listByPage(new ProxySelectorQuery(name, new PageParameter(currentPage, pageSize)));
.listByPage(new ProxySelectorQuery(name, new PageParameter(currentPage, pageSize), namespaceId));
return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, commonPager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public void changeUpstream(final ProxySelectorDTO proxySelectorDTO, final List<D
discoverySyncData.setPluginName(proxySelectorDTO.getPluginName());
discoverySyncData.setSelectorId(proxySelectorDTO.getId());
discoverySyncData.setSelectorName(proxySelectorDTO.getName());
discoverySyncData.setNamespaceId(proxySelectorDTO.getNamespaceId());
List<DiscoveryUpstreamData> upstreamDataList = upstreamDTOS.stream().map(DiscoveryTransfer.INSTANCE::mapToData).collect(Collectors.toList());
discoverySyncData.setUpstreamDataList(upstreamDataList);
DataChangedEvent dataChangedEvent = new DataChangedEvent(ConfigGroupEnum.DISCOVER_UPSTREAM, DataEventTypeEnum.UPDATE, Collections.singletonList(discoverySyncData));
Expand Down Expand Up @@ -181,6 +182,7 @@ public void fetchAll(final DiscoveryHandlerDTO discoveryHandlerDTO, final ProxyS
discoverySyncData.setSelectorName(proxySelectorDTO.getName());
discoverySyncData.setPluginName(proxySelectorDTO.getPluginName());
discoverySyncData.setUpstreamDataList(discoveryUpstreamDataList);
discoverySyncData.setNamespaceId(proxySelectorDTO.getNamespaceId());
DataChangedEvent dataChangedEvent = new DataChangedEvent(ConfigGroupEnum.DISCOVER_UPSTREAM, DataEventTypeEnum.UPDATE, Collections.singletonList(discoverySyncData));
eventPublisher.publishEvent(dataChangedEvent);
}
Expand Down Expand Up @@ -209,6 +211,7 @@ public DataChangedEventListener getDiscoveryDataChangedEventListener(final Disco
discoverySyncData.setPluginName(proxySelectorDTO.getPluginName());
discoverySyncData.setSelectorName(proxySelectorDTO.getName());
discoverySyncData.setSelectorId(proxySelectorDTO.getId());
discoverySyncData.setNamespaceId(proxySelectorDTO.getNamespaceId());
return new DiscoveryDataChangedEventSyncListener(eventPublisher, discoveryUpstreamMapper,
new CustomDiscoveryUpstreamParser(customMap), discoveryHandlerDTO.getId(), discoverySyncData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.Objects;
import java.util.stream.Collectors;

import static org.apache.shenyu.common.constant.Constants.SYS_DEFAULT_NAMESPACE_ID;

/**
* DiscoveryHandler.
*/
Expand Down Expand Up @@ -134,6 +136,14 @@ private void fillFullyDiscoverySyncData(final DiscoverySyncData discoverySyncDat

private DiscoverySyncData buildProxySelectorData(final String value) {
List<DiscoveryUpstreamData> discoveryUpstreamDTOS = keyValueParser.parseValue(value);
discoveryUpstreamDTOS.forEach(discoveryUpstreamData -> {
if (StringUtils.isBlank(discoveryUpstreamData.getNamespaceId())) {
discoveryUpstreamData.setNamespaceId(SYS_DEFAULT_NAMESPACE_ID);
}
});
discoveryUpstreamDTOS = discoveryUpstreamDTOS.stream()
.filter(upstreamData -> contextInfo.getNamespaceId().equals(upstreamData.getNamespaceId()))
.collect(Collectors.toList());
discoveryUpstreamDTOS.forEach(discoveryUpstreamData -> {
discoveryUpstreamData.setDiscoveryHandlerId(discoveryHandlerId);
if (StringUtils.isBlank(discoveryUpstreamData.getProtocol())) {
Expand All @@ -145,6 +155,7 @@ private DiscoverySyncData buildProxySelectorData(final String value) {
data.setSelectorId(contextInfo.getSelectorId());
data.setSelectorName(contextInfo.getSelectorName());
data.setPluginName(contextInfo.getPluginName());
data.setNamespaceId(contextInfo.getNamespaceId());
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public interface DiscoveryMapper extends ExistProvider {
*
* @param pluginName plugin name
* @param level level
* @param namespaceId namespaceId
* @return {@linkplain DiscoveryDO}
*/
DiscoveryDO selectByPluginNameAndLevel(@Param("pluginName") String pluginName, @Param("level") String level);
DiscoveryDO selectByPluginNameAndLevel(@Param("pluginName") String pluginName, @Param("level") String level, @Param("namespaceId") String namespaceId);
xcsnx marked this conversation as resolved.
Show resolved Hide resolved

/**
* insert discovery.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package org.apache.shenyu.admin.model.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.validation.annotation.Existed;

import java.io.Serializable;
import java.util.Objects;

Expand Down Expand Up @@ -66,6 +70,13 @@ public class DiscoveryDTO implements Serializable {
@NotNull(message = "props not null")
private String props;

/**
* namespaceId.
*/
@NotBlank
@Existed(message = "namespaceId is not existed", provider = NamespaceMapper.class)
private String namespaceId;

/**
* discoveryHandler.
*/
Expand Down Expand Up @@ -218,6 +229,24 @@ public void setDiscoveryHandler(final DiscoveryHandlerDTO discoveryHandler) {
this.discoveryHandler = discoveryHandler;
}

/**
* get namespaceId.
*
* @return namespaceId
*/
public String getNamespaceId() {
return namespaceId;
}

/**
* set namespaceId.
*
* @param namespaceId namespaceId
*/
public void setNamespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
}

/**
* Get discovery rel.
* @return discovery rel
Expand Down Expand Up @@ -248,11 +277,12 @@ public boolean equals(final Object o) {
&& Objects.equals(serverList, that.serverList) && Objects.equals(props, that.props)
&& Objects.equals(level, that.level) && Objects.equals(pluginName, that.pluginName)
&& Objects.equals(discoveryHandler, that.discoveryHandler)
&& Objects.equals(discoveryRel, that.discoveryRel);
&& Objects.equals(discoveryRel, that.discoveryRel)
&& Objects.equals(namespaceId, that.namespaceId);
}

@Override
public int hashCode() {
return Objects.hash(id, name, type, serverList, props, level, pluginName, discoveryHandler, discoveryRel);
return Objects.hash(id, name, type, serverList, props, level, pluginName, discoveryHandler, discoveryRel, namespaceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.admin.model.dto;

import org.apache.shenyu.admin.mapper.DiscoveryUpstreamMapper;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.validation.annotation.Existed;

import jakarta.validation.constraints.NotBlank;
Expand All @@ -44,6 +45,13 @@ public class DiscoveryUpstreamDTO implements Serializable {
@NotBlank(message = "discoveryHandlerId不能为空")
private String discoveryHandlerId;

/**
* namespaceId.
*/
@NotBlank
@Existed(message = "namespaceId is not existed", provider = NamespaceMapper.class)
private String namespaceId;

/**
* protocol.
*/
Expand Down Expand Up @@ -84,7 +92,6 @@ public class DiscoveryUpstreamDTO implements Serializable {
*/
private Timestamp dateUpdated;


/**
* getId.
*
Expand Down Expand Up @@ -259,4 +266,22 @@ public Timestamp getDateUpdated() {
public void setDateUpdated(final Timestamp dateUpdated) {
this.dateUpdated = dateUpdated;
}

/**
* get namespaceId.
*
* @return namespaceId
*/
public String getNamespaceId() {
return namespaceId;
}

/**
* set namespaceId.
*
* @param namespaceId namespaceId
*/
public void setNamespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
}
}
Loading
Loading