Skip to content

Commit

Permalink
Merge branch 'master' into fix_sql_error
Browse files Browse the repository at this point in the history
  • Loading branch information
moremind authored Oct 15, 2024
2 parents 361a063 + 2df54be commit c05faea
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 0 deletions.
1 change: 1 addition & 0 deletions shenyu-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>shenyu-registry-apollo</module>
<module>shenyu-registry-eureka</module>
<module>shenyu-registry-polaris</module>
<module>shenyu-registry-kubernetes</module>
</modules>

</project>
5 changes: 5 additions & 0 deletions shenyu-registry/shenyu-registry-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<artifactId>shenyu-registry-eureka</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-registry-kubernetes</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
43 changes: 43 additions & 0 deletions shenyu-registry/shenyu-registry-kubernetes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-registry</artifactId>
<version>2.7.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>shenyu-registry-kubernetes</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-registry-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shenyu.registry.kubernetes;

import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* kubernetes client.
*/
public class KubernetesClient {

private RestTemplate rest;

private KubernetesConfig kubernetesConfig;

public KubernetesClient(final KubernetesConfig kubernetesConfig) {
this.kubernetesConfig = kubernetesConfig;
this.rest = new RestTemplate();
}

/**
* get all serviceInstance.
* @param serviceId service identifier
* @return list of serviceInstance
*/
public List<KubernetesInstance> selectInstances(final String serviceId) {
List<KubernetesInstance> response = Collections.emptyList();
KubernetesInstance[] responseBody = (KubernetesInstance[]) this.rest.getForEntity(this.kubernetesConfig.getDiscoveryServerUrl() + "/apps/" + serviceId,
KubernetesInstance[].class, new Object[0]).getBody();
if (responseBody != null && responseBody.length > 0) {
response = (List) Arrays.stream(responseBody).filter(this::matchNamespaces).collect(Collectors.toList());
}

return response;
}

private boolean matchNamespaces(final KubernetesInstance kubernetesInstance) {
return CollectionUtils.isEmpty(this.kubernetesConfig.getNamespaces()) ? true : this.kubernetesConfig.getNamespaces().contains(kubernetesInstance.getNamespace());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shenyu.registry.kubernetes;

import java.util.ArrayList;
import java.util.List;

public class KubernetesConfig {

private String discoveryServerUrl;

private boolean enabled = true;

private List<String> namespaces = new ArrayList();

public KubernetesConfig() {
}

/**
* get discoveryServer url.
* @return discoveryServer url.
*/
public String getDiscoveryServerUrl() {
return this.discoveryServerUrl;
}

/**
* set discoveryServer url.
* @param discoveryServerUrl discoveryServer url.
*/
public void setDiscoveryServerUrl(final String discoveryServerUrl) {
this.discoveryServerUrl = discoveryServerUrl;
}

/**
* get enable status.
* @return enable status.
*/
public boolean isEnabled() {
return this.enabled;
}

/**
* set registry enable status.
* @param enabled enable status.
*/
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}

/**
* get namespaces.
* @return namespaces.
*/
List<String> getNamespaces() {
return this.namespaces;
}

/**
* set kubernetes namespace.
* @param namespaces list of namespace.
*/
public void setNamespaces(final List<String> namespaces) {
this.namespaces = namespaces;
}
}
Loading

0 comments on commit c05faea

Please sign in to comment.