Skip to content

Commit

Permalink
Merge pull request #1 from xiaodaweic/master
Browse files Browse the repository at this point in the history
Application架构添加K8S环境支持,能够自动获取K8S环境服务
  • Loading branch information
xiaoailiang authored Aug 20, 2021
2 parents aecbd87 + aa9f74c commit 745083c
Show file tree
Hide file tree
Showing 14 changed files with 505 additions and 4 deletions.
43 changes: 43 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# kdiff3 ignore
*.orig

# maven ignore
target/

# eclipse ignore
.settings/
.project
.classpath
classes/**

# idea ignore
.idea/
*.ipr
*.iml
*.iws

# temp ignore
*.log
*.cache
*.diff
*.patch
*.tmp

# system ignore
.DS_Store
Thumbs.db

# package ignore (optional)
# *.jar
# *.war
# *.zip
# *.tar
# *.tar.gz

# embedded tomcat
tomcat.8080/**

# pods ignore
Pods/

bistoury-*.tar.gz
57 changes: 57 additions & 0 deletions bistoury-application/bistoury-application-k8s/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bistoury-application</artifactId>
<groupId>qunar.tc.bistoury</groupId>
<version>2.0.7</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>bistoury-application-k8s</artifactId>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>qunar.tc.bistoury</groupId>
<artifactId>bistoury-application-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>13.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>qunar.tc.bistoury</groupId>
<artifactId>bistoury-common</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${server_java_source_version}</source>
<target>${server_java_target_version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package qunar.tc.bistoury.application.k8s.service;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qunar.tc.bistoury.application.api.AdminAppService;
import qunar.tc.bistoury.application.api.ApplicationService;
import qunar.tc.bistoury.application.api.pojo.Application;
import qunar.tc.bistoury.common.NamedThreadFactory;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* @author xkrivzooh
* @since 2019/8/14
*/
@Service
public class AdminAppServiceImpl implements AdminAppService {

private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("refresh-all-app"));

private volatile ImmutableSet<String> adminApps = ImmutableSet.of();

@Autowired
private ApplicationService applicationService;

@PostConstruct
public void init() {
refreshAllApp();

SCHEDULED_EXECUTOR_SERVICE.scheduleWithFixedDelay(this::refreshAllApp, 10, 10, TimeUnit.MINUTES);
}

private void refreshAllApp() {
List<Application> allApplications = applicationService.getAllApplications();
adminApps = ImmutableSet.copyOf(Lists.transform(allApplications, Application::getCode));
}

@Override
public List<String> searchApps(String keyInput, int size) {
final String key = Strings.nullToEmpty(keyInput).toLowerCase();
ImmutableSet<String> adminApps = this.adminApps;
if (Strings.isNullOrEmpty(key)) {
if (adminApps.size() <= size) {
return adminApps.asList();
}
return adminApps.asList().subList(0, size);
}

int needAddSize = size;
List<String> matchApps = new ArrayList<>(size);
if (adminApps.contains(key)) {
matchApps.add(key);
needAddSize--;
}

adminApps.stream()
.filter((app) -> app.contains(key) && !app.equals(key))
.limit(needAddSize)
.forEach(matchApps::add);
return matchApps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2019 Qunar, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package qunar.tc.bistoury.application.k8s.service;


import org.springframework.stereotype.Service;
import qunar.tc.bistoury.application.api.AppServerService;
import qunar.tc.bistoury.application.api.pojo.AppServer;
import qunar.tc.bistoury.application.k8s.util.K8SUtils;

import java.util.List;

/**
* @author leix.xie
* @date 2019/7/2 15:14
* @describe
*/
@Service
public class AppServerServiceImpl implements AppServerService {

@Override
public List<AppServer> getAppServerByAppCode(final String appCode) {
return K8SUtils.getAppserverByAppCode(appCode);
}

@Override
public int changeAutoJMapHistoEnable(final String serverId, final boolean enable, String loginUser) {
return 0;
}

@Override
public int changeAutoJStackEnable(final String serverId, final boolean enable, String loginUser) {
return 0;
}

@Override
public int deleteAppServerByServerId(final String serverId, String loginUser) {
return 0;
}

@Override
public int saveAppServer(AppServer appServer, String loginUser) {
return 0;
}

@Override
public AppServer getAppServerByIp(String ip) {
return K8SUtils.getAppserverByIp(ip);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2019 Qunar, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package qunar.tc.bistoury.application.k8s.service;


import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import qunar.tc.bistoury.application.api.AppService;
import qunar.tc.bistoury.application.api.pojo.Application;
import qunar.tc.bistoury.application.k8s.util.K8SUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author zhenyu.nie created on 2018 2018/10/31 13:56
*/
@Service
public class AppServiceImpl implements AppService {


@Override
public Set<String> getApps(String userCode) {
Set<String> set = new HashSet<>();
List<Application> applications = K8SUtils.getAllAppOrServer(K8SUtils.APPLICATION);
applications.forEach(application -> {
set.add(application.getName());
});
return set;
}

@Override
public Application getAppInfo(String appCode) {
Set<Application> set = new HashSet<>();
List<Application> applications = K8SUtils.getAllAppOrServer(K8SUtils.APPLICATION);
applications.forEach(application -> {
if (StringUtils.equals(appCode, application.getCode())) {
set.add(application);
}
});
return set.iterator().next();
}

@Override
public boolean checkUserPermission(final String appCode, final String usercode) {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package qunar.tc.bistoury.application.k8s.service;

import org.springframework.stereotype.Service;
import qunar.tc.bistoury.application.api.ApplicationService;
import qunar.tc.bistoury.application.api.pojo.Application;
import qunar.tc.bistoury.application.k8s.util.K8SUtils;

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

/**
* @author xkrivzooh
* @since 2019/8/14
*/
@Service
public class ApplicationServiceImpl implements ApplicationService {

@Override
public List<Application> getAllApplications() {
return K8SUtils.getAllAppOrServer(K8SUtils.APPLICATION);
}

@Override
public List<Application> getAllApplications(String userCode) {
return K8SUtils.getAllAppOrServer(K8SUtils.APPLICATION);
}

@Override
public List<String> getAppOwner(String appCode) {
return new ArrayList<>();
}

@Override

public int save(Application application, String loginUser, boolean admin) {
return 0;
}

}
Loading

0 comments on commit 745083c

Please sign in to comment.