Date: Mon, 30 Oct 2023 17:55:04 +0800
Subject: [PATCH 04/81] Summer ospp#10389 (#11208)
* Notify IstioConfigChangeEvent by parsing config tags
* Parse Configs and Create Virtual Service
* function complete
* Style Fixed
* Delete Personal Info
* add some comments and regex process func
* Nacos Istio LDS & RDS xds protocol
* reset configs
---
.../model/event/IstioConfigChangeEvent.java | 59 ++++
.../service/ConfigOperationService.java | 8 +
.../config/server/utils/ConfigTagUtil.java | 74 +++++
.../main/resources/META-INF/logback/nacos.xml | 1 -
.../alibaba/nacos/istio/api/ApiConstants.java | 5 +
.../nacos/istio/api/ApiGeneratorFactory.java | 4 +
.../istio/common/IstioConfigProcessor.java | 137 ++++++++
.../NacosServiceInfoResourceWatcher.java | 2 +-
.../nacos/istio/model/DestinationRule.java | 144 +++++++++
.../nacos/istio/model/VirtualService.java | 303 ++++++++++++++++++
.../nacos/istio/server/IstioServer.java | 3 +-
.../alibaba/nacos/istio/xds/LdsGenerator.java | 245 ++++++++++++++
.../nacos/istio/xds/NacosXdsService.java | 66 +++-
.../alibaba/nacos/istio/xds/RdsGenerator.java | 246 ++++++++++++++
14 files changed, 1291 insertions(+), 6 deletions(-)
create mode 100644 config/src/main/java/com/alibaba/nacos/config/server/model/event/IstioConfigChangeEvent.java
create mode 100644 config/src/main/java/com/alibaba/nacos/config/server/utils/ConfigTagUtil.java
create mode 100644 istio/src/main/java/com/alibaba/nacos/istio/common/IstioConfigProcessor.java
create mode 100644 istio/src/main/java/com/alibaba/nacos/istio/model/DestinationRule.java
create mode 100644 istio/src/main/java/com/alibaba/nacos/istio/model/VirtualService.java
create mode 100644 istio/src/main/java/com/alibaba/nacos/istio/xds/LdsGenerator.java
create mode 100644 istio/src/main/java/com/alibaba/nacos/istio/xds/RdsGenerator.java
diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/event/IstioConfigChangeEvent.java b/config/src/main/java/com/alibaba/nacos/config/server/model/event/IstioConfigChangeEvent.java
new file mode 100644
index 00000000000..218ae03eac0
--- /dev/null
+++ b/config/src/main/java/com/alibaba/nacos/config/server/model/event/IstioConfigChangeEvent.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed 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 com.alibaba.nacos.config.server.model.event;
+
+public class IstioConfigChangeEvent extends ConfigDataChangeEvent {
+
+ public final String content;
+
+ public final String type;
+
+ public IstioConfigChangeEvent(String dataId, String group, long gmtModified, String content, String type) {
+ super(dataId, group, gmtModified);
+ this.content = content;
+ this.type = type;
+ }
+
+ public IstioConfigChangeEvent(boolean isBeta, String dataId, String group, String tenant, long gmtModified,
+ String content, String type) {
+ super(isBeta, dataId, group, tenant, gmtModified);
+ this.content = content;
+ this.type = type;
+ }
+
+ public IstioConfigChangeEvent(boolean isBeta, String dataId, String group, long gmtModified, String content,
+ String type) {
+ super(isBeta, dataId, group, gmtModified);
+ this.content = content;
+ this.type = type;
+ }
+
+ public IstioConfigChangeEvent(boolean isBeta, String dataId, String group, String tenant, String tag,
+ long gmtModified, String content, String type) {
+ super(isBeta, dataId, group, tenant, tag, gmtModified);
+ this.content = content;
+ this.type = type;
+ }
+
+ public IstioConfigChangeEvent(String dataId, String group, String tenant, boolean isBatch, long gmtModified,
+ String content, String type) {
+ super(dataId, group, tenant, isBatch, gmtModified);
+ this.content = content;
+ this.type = type;
+ }
+
+}
diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigOperationService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigOperationService.java
index 265732b0c33..a5530cd129a 100644
--- a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigOperationService.java
+++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigOperationService.java
@@ -25,11 +25,13 @@
import com.alibaba.nacos.config.server.model.ConfigOperateResult;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.model.ConfigRequestInfo;
+import com.alibaba.nacos.config.server.model.event.IstioConfigChangeEvent;
import com.alibaba.nacos.config.server.model.form.ConfigForm;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoBetaPersistService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoTagPersistService;
import com.alibaba.nacos.config.server.service.trace.ConfigTraceService;
+import com.alibaba.nacos.config.server.utils.ConfigTagUtil;
import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.sys.utils.InetUtils;
@@ -102,6 +104,12 @@ public Boolean publishConfig(ConfigForm configForm, ConfigRequestInfo configRequ
ConfigChangePublisher.notifyConfigChange(
new ConfigDataChangeEvent(false, configForm.getDataId(), configForm.getGroup(),
configForm.getNamespaceId(), configOperateResult.getLastModified()));
+ if (ConfigTagUtil.isIstio(configForm.getConfigTags())) {
+ ConfigChangePublisher.notifyConfigChange(
+ new IstioConfigChangeEvent(configForm.getDataId(), configForm.getGroup(),
+ configOperateResult.getLastModified(), configForm.getContent(),
+ ConfigTagUtil.getIstioType(configForm.getConfigTags())));
+ }
} else {
persistEvent = ConfigTraceService.PERSISTENCE_EVENT_TAG + "-" + configForm.getTag();
configOperateResult = configInfoTagPersistService.insertOrUpdateTag(configInfo, configForm.getTag(),
diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/ConfigTagUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/ConfigTagUtil.java
new file mode 100644
index 00000000000..0fda7e843dc
--- /dev/null
+++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/ConfigTagUtil.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed 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 com.alibaba.nacos.config.server.utils;
+
+import java.util.Arrays;
+
+public class ConfigTagUtil {
+
+ public static final String VIRTUAL_SERVICE = "virtual-service";
+
+ public static final String DESTINATION_RULE = "destination-rule";
+
+ private static final String TAGS_DELIMITER = ",";
+
+ private static final String HYPHEN = "-";
+
+ /**
+ * Checks if config tags contains "virtual-service" or "destination-rule".
+ * @param configTags the tags to check
+ * @return {@code true} if the config tags contains "virtual-service" or "destination-rule".
+ * @throws IllegalArgumentException if configTags is null.
+ */
+ public static boolean isIstio(String configTags) {
+ if (configTags == null) {
+ throw new IllegalArgumentException("configTags cannot be null.");
+ }
+
+ if (configTags.isEmpty()) {
+ return false;
+ }
+
+ return Arrays.stream(configTags.split(TAGS_DELIMITER))
+ .map(tag -> tag.trim().replaceAll(HYPHEN, ""))
+ .anyMatch(tag -> tag.equalsIgnoreCase(VIRTUAL_SERVICE.replaceAll(HYPHEN, ""))
+ || tag.equalsIgnoreCase(DESTINATION_RULE.replaceAll(HYPHEN, "")));
+ }
+
+ /**
+ * Gets the type of Istio from the config tags.
+ * @param configTags the tags to check
+ * @return the type of Istio if it is found, {@code null} otherwise.
+ * @throws IllegalArgumentException if configTags is null.
+ */
+ public static String getIstioType(String configTags) {
+ if (configTags == null) {
+ throw new IllegalArgumentException("configTags cannot be null.");
+ }
+
+ if (configTags.isEmpty()) {
+ return null;
+ }
+
+ return Arrays.stream(configTags.split(TAGS_DELIMITER))
+ .map(tag -> tag.trim().replaceAll(HYPHEN, ""))
+ .filter(tag -> tag.equalsIgnoreCase(VIRTUAL_SERVICE.replaceAll(HYPHEN, ""))
+ || tag.equalsIgnoreCase(DESTINATION_RULE.replaceAll(HYPHEN, "")))
+ .findFirst()
+ .orElse(null);
+ }
+}
diff --git a/core/src/main/resources/META-INF/logback/nacos.xml b/core/src/main/resources/META-INF/logback/nacos.xml
index ca543a8bdd1..1296290bad8 100644
--- a/core/src/main/resources/META-INF/logback/nacos.xml
+++ b/core/src/main/resources/META-INF/logback/nacos.xml
@@ -275,7 +275,6 @@