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: Fix bugs in AI console pages #403

Merged
merged 5 commits into from
Jan 14, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.alibaba.higress.console.controller.dto.PaginatedResponse;
import com.alibaba.higress.console.controller.dto.Response;
import com.alibaba.higress.console.controller.util.ControllerUtil;
import com.alibaba.higress.sdk.constant.HigressConstants;
import com.alibaba.higress.sdk.exception.ValidationException;
import com.alibaba.higress.sdk.model.PaginatedResult;
import com.alibaba.higress.sdk.model.Service;
Expand All @@ -44,7 +43,6 @@
import com.alibaba.higress.sdk.service.ServiceService;
import com.alibaba.higress.sdk.service.WasmPluginInstanceService;
import com.alibaba.higress.sdk.service.WasmPluginService;
import com.alibaba.higress.sdk.service.kubernetes.KubernetesUtil;

/**
* @author CH3CHO
Expand Down Expand Up @@ -154,18 +152,12 @@ public ResponseEntity<Response<WasmPluginInstance>> addOrUpdateRouteInstance(
@PathVariable("routeName") @NotBlank String routeName, @PathVariable("name") @NotBlank String pluginName,
@RequestBody WasmPluginInstance instance) {
validateRouteName(routeName);
if (routeName.endsWith(HigressConstants.INTERNAL_RESOURCE_NAME_SUFFIX)) {
throw new ValidationException("Changing Wasm plugin configuration of an internal route is not allowed.");
}
return addOrUpdateInstance(WasmPluginInstanceScope.ROUTE, routeName, pluginName, instance);
}

@DeleteMapping(value = "/routes/{routeName}/plugin-instances/{name}")
public void deleteRouteInstance(@PathVariable("routeName") @NotBlank String routeName,
@PathVariable("name") @NotBlank String pluginName) {
if (routeName.endsWith(HigressConstants.INTERNAL_RESOURCE_NAME_SUFFIX)) {
throw new ValidationException("Changing Wasm plugin configuration of an internal route is not allowed.");
}
deleteInstance(WasmPluginInstanceScope.ROUTE, routeName, pluginName);
}

Expand All @@ -188,18 +180,12 @@ public ResponseEntity<Response<WasmPluginInstance>> addOrUpdateServiceInstance(
@PathVariable("serviceName") @NotBlank String serviceName, @PathVariable("name") @NotBlank String pluginName,
@RequestBody WasmPluginInstance instance) {
validateServiceName(serviceName);
if (KubernetesUtil.isInternalService(serviceName)) {
throw new ValidationException("Changing Wasm plugin configuration of an internal service is not allowed.");
}
return addOrUpdateInstance(WasmPluginInstanceScope.SERVICE, serviceName, pluginName, instance);
}

@DeleteMapping(value = "/services/{serviceName}/plugin-instances/{name}")
public void deleteServiceInstance(@PathVariable("serviceName") @NotBlank String serviceName,
@PathVariable("name") @NotBlank String pluginName) {
if (serviceName.endsWith(HigressConstants.INTERNAL_RESOURCE_NAME_SUFFIX)) {
throw new ValidationException("Changing Wasm plugin configuration of an internal service is not allowed.");
}
deleteInstance(WasmPluginInstanceScope.SERVICE, serviceName, pluginName);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Copyright (c) 2022-2025 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.higress.console;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Disabled
public class FrontEndI18nResourceChecker {

private static final String FRONTEND_PROJECT_PATH = "";
private static final String I18N_RESOURCE_PATH = "src/locales";
private static final String I18N_RESOURCE_FILE_NAME = "translation.json";
private static final List<String> TS_FILE_EXTENSIONS = Arrays.asList(".ts", ".tsx");

private static final Set<String> IMPLICITLY_USED_RESOURCE_KEYS = Set.of("init.title", "login.title", "aiRoute.edit",
"tlsCertificate.editTlsCertificate", "serviceSource.editServiceSource", "llmProvider.edit",
"plugins.editPlugin", "route.editRoute", "domain.editDomain", "consumer.edit");
private static final List<String> IMPLICITLY_USED_RESOURCE_KEY_PREFIXES =
Arrays.asList("menu.", "request.error.", "serviceSource.types.", "llmProvider.providerTypes.",
"route.factorGroup.required.", "route.keyValueGroup.required.", "plugins.configForm.", "plugins.subTitle.");

private static final String LANG_CN = "zh-CN";
private static final String LANG_EN = "en-US";

private static final List<Pattern> RESOURCE_USAGE_PATTERNS = Arrays.asList(Pattern.compile("\\bt\\('([^']+)'\\)"),
Pattern.compile("t\\(\"([^\"]+)\"\\)"), Pattern.compile("\\bi18nKey=\"([^\"]+)\""));
private static final Pattern BAD_RESOURCE_CONTENT = Pattern.compile("^[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+$");

@Test
public void checkI18nResourceUsages() throws IOException {
Map<String, String> cnResources = loadI18nResources(LANG_CN);
Set<String> usedResources = new TreeSet<>();
Set<String> badResources = new TreeSet<>();
Set<String> unknownResources = new TreeSet<>();
try (Stream<Path> stream = Files.walk(Paths.get(FRONTEND_PROJECT_PATH))) {
stream.filter(p -> {
String s = p.toString();
return !s.contains("node_modules") && TS_FILE_EXTENSIONS.stream().anyMatch(s::endsWith);
}).forEach(p -> {
String content;
try {
content = Files.readString(p, StandardCharsets.UTF_8);
} catch (IOException e) {
System.out.println("Failed to read file: " + p);
return;
}
Set<String> referredResources = getReferredResources(content);
usedResources.addAll(referredResources);
referredResources.forEach(k -> {
if (!cnResources.containsKey(k)) {
unknownResources.add(k);
}
});
});
}
System.out.println("Unused resources:");
for (String key : cnResources.keySet()) {
if (IMPLICITLY_USED_RESOURCE_KEYS.contains(key)) {
continue;
}
if (IMPLICITLY_USED_RESOURCE_KEY_PREFIXES.stream().anyMatch(key::startsWith)) {
continue;
}
if (!usedResources.contains(key)) {
System.out.println(key);
}
String value = cnResources.get(key);
if (BAD_RESOURCE_CONTENT.matcher(value).matches()) {
badResources.add(key);
}
}
System.out.println("-------------------------------------");
System.out.println("Bad resources:");
for (String key : badResources) {
System.out.printf("\"%s\": \"%s\",\n", key, cnResources.get(key));
}
System.out.println("-------------------------------------");
System.out.println("Unknown resources:");
for (String key : unknownResources) {
String lastSegment = key.substring(key.lastIndexOf('.') + 1);
System.out.printf("\"%s\": \"%s\",\n", lastSegment, key);
}
}

@Test
public void checkI18nResourcesAlignment() throws IOException {
Map<String, String> cnResources = loadI18nResources(LANG_CN);
Map<String, String> enResources = loadI18nResources(LANG_EN);
Set<String> commonKeys = new HashSet<>(cnResources.keySet());
commonKeys.retainAll(enResources.keySet());
commonKeys.forEach(k -> {
cnResources.remove(k);
enResources.remove(k);
});
System.out.println("Chinese resources without English translation:");
for (String key : cnResources.keySet()) {
System.out.println(key);
}
System.out.println();
System.out.println("English resources without Chinese translation:");
for (String key : enResources.keySet()) {
System.out.println(key);
}
}

@Test
public void checkUntranslatedEnglishResources() throws IOException {
Map<String, String> enResources = loadI18nResources(LANG_EN);
for (Map.Entry<String, String> entry : enResources.entrySet()) {
if (hasChinese(entry.getValue())) {
System.out.println(entry.getKey());
}
}
}

private boolean hasChinese(String str) {
for (int i = 0, len = str.length(); i < len; ++i) {
char ch = str.charAt(i);
if (Character.UnicodeScript.of(ch) == Character.UnicodeScript.HAN) {
return true;
}
}
return false;
}

private static Set<String> getReferredResources(String content) {
Set<String> result = new HashSet<>();
for (Pattern pattern : RESOURCE_USAGE_PATTERNS) {
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String key = matcher.group(1);
result.add(key);
}
}
return result;
}

private static Map<String, String> loadI18nResources(String language) throws IOException {
Path resourceFilePath = Paths.get(FRONTEND_PROJECT_PATH, I18N_RESOURCE_PATH, language, I18N_RESOURCE_FILE_NAME);
String json = Files.readString(resourceFilePath, StandardCharsets.UTF_8);
JSONObject obj = JSON.parseObject(json);
Map<String, String> resources = new LinkedHashMap<>();
addI18nResources(resources, obj, "");
return resources;
}

private static void addI18nResources(Map<String, String> resources, JSONObject obj, String prefix) {
for (Map.Entry<String, Object> entry : obj.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof JSONObject) {
addI18nResources(resources, (JSONObject)value, prefix + key + ".");
} else {
resources.put(prefix + key, value.toString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public class TlsCertificate implements VersionedDto {

private List<String> domains;

@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss'Z'")
private LocalDateTime validityStart;

@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss'Z'")
private LocalDateTime validityEnd;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public Domain add(Domain domain) {
}
throw new BusinessException("Error occurs when adding a new domain.", e);
}

// Sync the domain configs of routes using the new domain, especially the default domain
syncRouteDomainConfigs(domain.getName());

return kubernetesModelConverter.configMap2Domain(newDomainConfigMap);
}

Expand Down Expand Up @@ -129,20 +133,26 @@ public Domain put(Domain domain) {
"Error occurs when replacing the ConfigMap generated by domain: " + domain.getName(), e);
}

// Sync the domain configs of routes using the domain
syncRouteDomainConfigs(domain.getName());

return kubernetesModelConverter.configMap2Domain(updatedConfigMap);
}

private void syncRouteDomainConfigs(String domainName) {
List<Route> routes;
if (HigressConstants.DEFAULT_DOMAIN.equals(domain.getName())) {
if (HigressConstants.DEFAULT_DOMAIN.equals(domainName)) {
PaginatedResult<Route> routeQueryResult = routeService.list(null);
routes = routeQueryResult.getData().stream().filter(r -> CollectionUtils.isEmpty(r.getDomains()))
.collect(Collectors.toList());
} else {
RoutePageQuery query = new RoutePageQuery();
query.setDomainName(domain.getName());
query.setDomainName(domainName);
PaginatedResult<Route> routeQueryResult = routeService.list(query);
routes = routeQueryResult.getData();
}

// TODO: Switch to the new logic after 2025/03/31
// String domainName = domain.getName();
// if (HigressConstants.DEFAULT_DOMAIN.equals(domainName)) {
// domainName = HigressConstants.DEFAULT_DOMAIN;
// }
Expand All @@ -154,7 +164,5 @@ public Domain put(Domain domain) {
if (CollectionUtils.isNotEmpty(routes)) {
routes.forEach(routeService::update);
}

return kubernetesModelConverter.configMap2Domain(updatedConfigMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public Consumer addOrUpdate(Consumer consumer) {
instance.setInternal(true);
instance.setGlobalTarget();
}
instance.setEnabled(true);
if (config.saveConsumer(instance, consumer)) {
instancesToUpdate.add(instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -1735,7 +1736,7 @@ private static List<String> getCertBoundDomains(String certData) {
}

private static List<String> getCertBoundDomains(X509Certificate certificate) {
List<String> domains = new ArrayList<>();
Set<String> domains = new LinkedHashSet<>();

String subjectDomain = getPrincipleValue(certificate.getSubjectX500Principal(), "CN");
if (StringUtils.isNotEmpty(subjectDomain)) {
Expand Down Expand Up @@ -1764,7 +1765,7 @@ private static List<String> getCertBoundDomains(X509Certificate certificate) {
}
}

return domains;
return new ArrayList<>(domains);
}

private static String getPrincipleValue(X500Principal principal, String type) {
Expand Down
35 changes: 0 additions & 35 deletions frontend/src/components/CardAreaChart/index.module.css

This file was deleted.

Loading
Loading