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

feat: Return null when trying to load a non-existent K8s resource #281

Merged
merged 1 commit into from
Jan 15, 2024
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 @@ -74,7 +74,12 @@ public PaginatedResult<Route> list(RoutePageQuery query) {

@Override
public Route query(String routeName) {
V1Ingress ingress = kubernetesClientService.readIngress(routeName);
V1Ingress ingress;
try {
ingress = kubernetesClientService.readIngress(routeName);
} catch (ApiException e) {
throw new BusinessException("Error occurs when reading the Ingress with name: " + routeName, e);
}
return ingress != null ? kubernetesModelConverter.ingress2Route(ingress) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public PaginatedResult<ServiceSource> list(CommonPageQuery query) {
try {
mcpBridge = kubernetesClientService.readMcpBridge(V1McpBridge.DEFAULT_NAME);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return PaginatedResult.createFromFullList(Collections.emptyList(), query);
}
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
List<ServiceSource> serviceSources = Collections.emptyList();
Expand All @@ -94,13 +91,11 @@ public PaginatedResult<ServiceSource> list(CommonPageQuery query) {

@Override
public ServiceSource addOrUpdate(ServiceSource serviceSource) {
V1McpBridge mcpBridge = null;
V1McpBridge mcpBridge;
try {
mcpBridge = kubernetesClientService.readMcpBridge(V1McpBridge.DEFAULT_NAME);
} catch (ApiException e) {
if (e.getCode() != HttpStatus.NOT_FOUND.value()) {
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
try {
if (null == mcpBridge) {
Expand Down Expand Up @@ -130,21 +125,19 @@ public void delete(String name) {
try {
mcpBridge = kubernetesClientService.readMcpBridge(V1McpBridge.DEFAULT_NAME);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return;
}
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
if (mcpBridge == null) {
return;
}
V1RegistryConfig registry = kubernetesModelConverter.removeV1McpBridgeRegistry(mcpBridge, name);
if (StringUtils.isNotEmpty(registry.getAuthSecretName())) {
try {
kubernetesClientService.deleteSecret(registry.getAuthSecretName());
} catch (ApiException e) {
if (e.getCode() != HttpStatus.NOT_FOUND.value()) {
String message = "Error occurs when deleting the secret associated with ServiceSource named "
+ registry.getName();
throw new BusinessException(message, e);
}
String message =
"Error occurs when deleting the secret associated with ServiceSource named " + registry.getName();
throw new BusinessException(message, e);
}
}
try {
Expand All @@ -160,9 +153,6 @@ public ServiceSource query(String name) throws BusinessException {
try {
mcpBridge = kubernetesClientService.readMcpBridge(V1McpBridge.DEFAULT_NAME);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return null;
}
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
if (null == mcpBridge || CollectionUtils.isEmpty(mcpBridge.getSpec().getRegistries())) {
Expand All @@ -180,13 +170,11 @@ public ServiceSource query(String name) throws BusinessException {

@Override
public ServiceSource add(ServiceSource serviceSource) {
V1McpBridge mcpBridge = null;
V1McpBridge mcpBridge;
try {
mcpBridge = kubernetesClientService.readMcpBridge(V1McpBridge.DEFAULT_NAME);
} catch (ApiException e) {
if (e.getCode() != HttpStatus.NOT_FOUND.value()) {
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
try {
if (null == mcpBridge) {
Expand Down Expand Up @@ -262,17 +250,16 @@ private void syncAuthSecret(ServiceSource serviceSource, V1RegistryConfig regist
try {
secret = kubernetesClientService.readSecret(registry.getAuthSecretName());
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
secret = new V1Secret();
V1ObjectMeta metadata = new V1ObjectMeta();
metadata.setName(registry.getAuthSecretName());
secret.setMetadata(metadata);
secretLost = true;
} else {
String message = "Error occurs when getting the secret associated with ServiceSource named "
+ serviceSource.getName();
throw new BusinessException(message, e);
}
String message = "Error occurs when getting the secret associated with ServiceSource named "
+ serviceSource.getName();
throw new BusinessException(message, e);
}
if (secret == null) {
secret = new V1Secret();
V1ObjectMeta metadata = new V1ObjectMeta();
metadata.setName(registry.getAuthSecretName());
secret.setMetadata(metadata);
secretLost = true;
}
secret.setData(secretData);
try {
Expand Down Expand Up @@ -325,11 +312,7 @@ private ServiceSource convert(V1RegistryConfig registry) {
try {
secret = kubernetesClientService.readSecret(registry.getAuthSecretName());
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
secret = null;
} else {
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
throw new BusinessException("Error occurs when getting McpBridge.", e);
}
if (secret != null && MapUtils.isNotEmpty(secret.getData())) {
authN.setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ public WasmPluginInstance addOrUpdate(WasmPluginInstance instance) {
existedCr = existedCrs.get(0);
}
} catch (ApiException e) {
if (e.getCode() != HttpStatus.NOT_FOUND.value()) {
throw new BusinessException("Error occurs when getting WasmPlugin.", e);
}
throw new BusinessException("Error occurs when getting WasmPlugin.", e);
}

if (instance.getConfigurations() == null && StringUtils.isNotEmpty(instance.getRawConfigurations())) {
Expand Down Expand Up @@ -178,13 +176,11 @@ public WasmPluginInstance addOrUpdate(WasmPluginInstance instance) {

@Override
public void delete(WasmPluginInstanceScope scope, String target, String pluginName) {
List<V1alpha1WasmPlugin> existedCrs = null;
List<V1alpha1WasmPlugin> existedCrs;
try {
existedCrs = kubernetesClientService.listWasmPlugin(pluginName);
} catch (ApiException e) {
if (e.getCode() != HttpStatus.NOT_FOUND.value()) {
throw new BusinessException("Error occurs when getting WasmPlugin.", e);
}
throw new BusinessException("Error occurs when getting WasmPlugin.", e);
}
deletePluginInstances(existedCrs, scope, target);
}
Expand All @@ -195,9 +191,7 @@ public void deleteAll(WasmPluginInstanceScope scope, String target) {
try {
existedCrs = kubernetesClientService.listWasmPlugin();
} catch (ApiException e) {
if (e.getCode() != HttpStatus.NOT_FOUND.value()) {
throw new BusinessException("Error occurs when getting WasmPlugin.", e);
}
throw new BusinessException("Error occurs when getting WasmPlugin.", e);
}
deletePluginInstances(existedCrs, scope, target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,15 @@ public List<V1Ingress> listIngressByDomain(String domainName) {
}
}

public V1Ingress readIngress(String name) {
public V1Ingress readIngress(String name) throws ApiException {
NetworkingV1Api apiInstance = new NetworkingV1Api(client);
try {
return apiInstance.readNamespacedIngress(name, controllerNamespace, null);
} catch (ApiException e) {
log.error("getIngress Status code: " + e.getCode() + "Reason: " + e.getResponseBody() + "Response headers: "
+ e.getResponseHeaders(), e);
return null;
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return null;
}
throw e;
}
}

Expand Down Expand Up @@ -299,7 +300,14 @@ public V1ConfigMap createConfigMap(V1ConfigMap configMap) throws ApiException {

public V1ConfigMap readConfigMap(String name) throws ApiException {
CoreV1Api coreV1Api = new CoreV1Api(client);
return coreV1Api.readNamespacedConfigMap(name, controllerNamespace, null);
try {
return coreV1Api.readNamespacedConfigMap(name, controllerNamespace, null);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return null;
}
throw e;
}
}

public void deleteConfigMap(String name) throws ApiException {
Expand Down Expand Up @@ -341,7 +349,14 @@ public List<V1Secret> listSecret(String type) throws ApiException {

public V1Secret readSecret(String name) throws ApiException {
CoreV1Api coreV1Api = new CoreV1Api(client);
return coreV1Api.readNamespacedSecret(name, controllerNamespace, null);
try {
return coreV1Api.readNamespacedSecret(name, controllerNamespace, null);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return null;
}
throw e;
}
}

public V1Secret createSecret(V1Secret secret) throws ApiException {
Expand Down Expand Up @@ -418,9 +433,16 @@ public void deleteMcpBridge(String name) throws ApiException {

public V1McpBridge readMcpBridge(String name) throws ApiException {
CustomObjectsApi customObjectsApi = new CustomObjectsApi(client);
Object response = customObjectsApi.getNamespacedCustomObject(V1McpBridge.API_GROUP, V1McpBridge.VERSION,
controllerNamespace, V1McpBridge.PLURAL, name);
return client.getJSON().deserialize(client.getJSON().serialize(response), V1McpBridge.class);
try {
Object response = customObjectsApi.getNamespacedCustomObject(V1McpBridge.API_GROUP, V1McpBridge.VERSION,
controllerNamespace, V1McpBridge.PLURAL, name);
return client.getJSON().deserialize(client.getJSON().serialize(response), V1McpBridge.class);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return null;
}
throw e;
}
}

public List<V1alpha1WasmPlugin> listWasmPlugin() throws ApiException {
Expand Down Expand Up @@ -487,9 +509,16 @@ public void deleteWasmPlugin(String name) throws ApiException {

public V1alpha1WasmPlugin readWasmPlugin(String name) throws ApiException {
CustomObjectsApi customObjectsApi = new CustomObjectsApi(client);
Object response = customObjectsApi.getNamespacedCustomObject(V1alpha1WasmPlugin.API_GROUP,
V1alpha1WasmPlugin.VERSION, controllerNamespace, V1alpha1WasmPlugin.PLURAL, name);
return client.getJSON().deserialize(client.getJSON().serialize(response), V1alpha1WasmPlugin.class);
try {
Object response = customObjectsApi.getNamespacedCustomObject(V1alpha1WasmPlugin.API_GROUP,
V1alpha1WasmPlugin.VERSION, controllerNamespace, V1alpha1WasmPlugin.PLURAL, name);
return client.getJSON().deserialize(client.getJSON().serialize(response), V1alpha1WasmPlugin.class);
} catch (ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
return null;
}
throw e;
}
}

private void checkResponseStatus(V1Status status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,10 @@ private void fillIngressTls(V1ObjectMeta metadata, V1IngressSpec spec, Route rou
e);
}

if (configMap == null) {
continue;
}

Domain domain = configMap2Domain(configMap);

if (Domain.EnableHttps.OFF.equals(domain.getEnableHttps())) {
Expand Down
Loading