Skip to content

Commit

Permalink
feat: Job 支持多租户 TencentBlueKing#3369
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Jan 8, 2025
1 parent d565ebe commit 6b248d1
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@
import com.tencent.bk.job.common.constant.TenantIdConstants;
import com.tencent.bk.job.common.esb.config.AppProperties;
import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties;
import com.tencent.bk.job.common.esb.metrics.EsbMetricTags;
import com.tencent.bk.job.common.esb.model.BkApiAuthorization;
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.esb.model.OpenApiRequestInfo;
import com.tencent.bk.job.common.esb.model.OpenApiResponse;
import com.tencent.bk.job.common.esb.sdk.BkApiV2Client;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.exception.InternalUserManageException;
import com.tencent.bk.job.common.model.dto.BkUserDTO;
import com.tencent.bk.job.common.paas.model.EsbListUsersResult;
import com.tencent.bk.job.common.paas.model.GetUserListReq;
import com.tencent.bk.job.common.paas.model.OpenApiTenant;
import com.tencent.bk.job.common.tenant.TenantEnvService;
import com.tencent.bk.job.common.util.http.HttpHelperFactory;
Expand All @@ -50,11 +45,8 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.message.BasicHeader;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
Expand All @@ -68,12 +60,8 @@
@Slf4j
public class UserMgrApiClient extends BkApiV2Client {

private static final String API_GET_USER_LIST = "/api/c/compapi/v2/usermanage/list_users/";

private final BkApiAuthorization authorization;

private final TenantEnvService tenantEnvService;

public UserMgrApiClient(BkApiGatewayProperties bkApiGatewayProperties,
AppProperties appProperties,
MeterRegistry meterRegistry,
Expand All @@ -86,69 +74,12 @@ public UserMgrApiClient(BkApiGatewayProperties bkApiGatewayProperties,
);
this.authorization = BkApiAuthorization.appAuthorization(appProperties.getCode(),
appProperties.getSecret());
this.tenantEnvService = tenantEnvService;
}

public List<BkUserDTO> getAllUserList(String tenantId) {
String fields = "id,username,display_name,logo";
List<EsbListUsersResult> esbUserList;
try {
GetUserListReq req = buildGetUserListReq(fields);

HttpMetricUtil.setHttpMetricName(USER_MANAGE_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(
Tag.of(EsbMetricTags.KEY_API_NAME, API_GET_USER_LIST)
);
EsbResp<List<EsbListUsersResult>> esbResp = doRequest(
OpenApiRequestInfo.builder()
.method(HttpMethodEnum.GET)
.uri(API_GET_USER_LIST)
.queryParams(req.toUrlParams())
// .addHeader(new BasicHeader(JobCommonHeaders.BK_TENANT_ID, tenantId))
.authorization(authorization)
.build(),
new TypeReference<EsbResp<List<EsbListUsersResult>>>() {
}
);
esbUserList = esbResp.getData();
} catch (Exception e) {
String errorMsg = "Get " + API_GET_USER_LIST + " error";
log.error(errorMsg, e);
throw new InternalUserManageException(errorMsg, e, ErrorCode.BK_USER_MANAGE_API_ERROR);
} finally {
HttpMetricUtil.clearHttpMetric();
}
return convert(esbUserList);
return Collections.emptyList();
}

private GetUserListReq buildGetUserListReq(String fields) {
GetUserListReq req = new GetUserListReq();
if (StringUtils.isNotBlank(fields)) {
req.setFields(fields);
}
req.setPage(0L);
req.setPageSize(0L);
req.setNoPage(true);
return req;
}

private List<BkUserDTO> convert(List<EsbListUsersResult> esbUserList) {
if (CollectionUtils.isEmpty(esbUserList)) {
return Collections.emptyList();
}
List<BkUserDTO> userList = new ArrayList<>();
for (EsbListUsersResult esbUser : esbUserList) {
BkUserDTO user = new BkUserDTO();
user.setId(esbUser.getId());
user.setUsername(esbUser.getUsername());
user.setDisplayName(esbUser.getDisplayName());
user.setLogo(esbUser.getLogo());
userList.add(user);
}
return userList;
}


/**
* 获取全量租户
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes
}

@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@Configuration
public class JobAssembleConfiguration {
@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes
}

@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes
}

@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes
}

@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes
}

@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package com.tencent.bk.job.manage.config;

import com.tencent.bk.job.common.service.AppCacheService;
import com.tencent.bk.job.common.service.AppScopeMappingService;
import com.tencent.bk.job.common.web.interceptor.BasicAppInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -34,7 +33,7 @@
public class JobManageConfiguration {

@Bean
public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) {
public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) {
return new BasicAppInterceptor(appCacheService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public UserDAOImpl(@Qualifier("job-manage-dsl-context") DSLContext dslContext) {
@Override
public void saveUser(BkUserDTO user) {
dslContext
.insertInto(T_USER,
.insertInto(
T_USER,
T_USER.USERNAME,
T_USER.DISPLAY_NAME,
T_USER.TENANT_ID,
Expand All @@ -62,7 +63,8 @@ public int deleteUser(String username) {

@Override
public List<BkUserDTO> listTenantUsers(String tenantId) {
Result<Record> result = dslContext.select(ALL_FIELDS)
Result<Record> result = dslContext
.select(ALL_FIELDS)
.from(T_USER)
.where(T_USER.TENANT_ID.eq(tenantId))
.fetch();
Expand All @@ -82,7 +84,8 @@ private BkUserDTO extract(Record record) {

@Override
public List<BkUserDTO> listUsersByDisplayNamePrefix(String tenantId, String prefixStr, Long limit) {
Result<Record> result = dslContext.select(ALL_FIELDS)
Result<Record> result = dslContext
.select(ALL_FIELDS)
.from(T_USER)
.where(T_USER.TENANT_ID.eq(tenantId))
.and(T_USER.DISPLAY_NAME.startsWith(prefixStr))
Expand All @@ -95,7 +98,8 @@ public List<BkUserDTO> listUsersByDisplayNamePrefix(String tenantId, String pref

@Override
public List<BkUserDTO> listUsersByUsernames(String tenantId, Collection<String> usernames) {
Result<Record> result = dslContext.select(ALL_FIELDS)
Result<Record> result = dslContext
.select(ALL_FIELDS)
.from(T_USER)
.where(T_USER.TENANT_ID.eq(tenantId))
.and(T_USER.USERNAME.in(usernames))
Expand All @@ -108,7 +112,8 @@ public List<BkUserDTO> listUsersByUsernames(String tenantId, Collection<String>

@Override
public List<String> listExistUserName(String tenantId, Collection<String> usernames) {
Result<Record> result = dslContext.select(ALL_FIELDS)
Result<Record> result = dslContext
.select(ALL_FIELDS)
.from(T_USER)
.where(T_USER.TENANT_ID.eq(tenantId))
.and(T_USER.USERNAME.in(usernames))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.util.Set;

/**
* 用户服务
* 用户信息缓存服务
*/
public interface UserService {
public interface UserCacheService {
void saveUser(BkUserDTO user);

int deleteUser(String username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.tencent.bk.job.common.model.dto.BkUserDTO;
import com.tencent.bk.job.common.mysql.JobTransactional;
import com.tencent.bk.job.manage.dao.UserDAO;
import com.tencent.bk.job.manage.service.UserService;
import com.tencent.bk.job.manage.service.UserCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

Expand All @@ -13,11 +13,11 @@

@Slf4j
@Service
public class UserServiceImpl implements UserService {
public class UserCacheServiceImpl implements UserCacheService {

private final UserDAO userDAO;

public UserServiceImpl(UserDAO userDAO) {
public UserCacheServiceImpl(UserDAO userDAO) {
this.userDAO = userDAO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.tencent.bk.job.manage.service.impl.notify;

import com.tencent.bk.job.manage.metrics.MetricsConstants;
import com.tencent.bk.job.manage.service.UserService;
import com.tencent.bk.job.manage.service.UserCacheService;
import com.tencent.bk.job.manage.service.impl.WatchableSendMsgService;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
Expand All @@ -47,15 +47,15 @@ public class NotifySendService {
//发通知专用线程池
private final ThreadPoolExecutor notifySendExecutor;
private final WatchableSendMsgService watchableSendMsgService;
private final UserService userService;
private final UserCacheService userCacheService;

@Autowired
public NotifySendService(WatchableSendMsgService watchableSendMsgService,
UserService userService,
UserCacheService userCacheService,
@Qualifier("notifySendExecutor") ThreadPoolExecutor notifySendExecutor,
MeterRegistry meterRegistry) {
this.watchableSendMsgService = watchableSendMsgService;
this.userService = userService;
this.userCacheService = userCacheService;
this.notifySendExecutor = notifySendExecutor;
measureNotifySendExecutor(meterRegistry);
}
Expand Down Expand Up @@ -90,7 +90,7 @@ private SendNotifyTask buildSendTask(Long appId,
.title(title)
.content(content)
.build();
task.bindService(watchableSendMsgService, userService);
task.bindService(watchableSendMsgService, userCacheService);
return task;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.tencent.bk.job.manage.model.web.request.notify.NotifyBlackUsersReq;
import com.tencent.bk.job.manage.model.web.vo.notify.NotifyBlackUserInfoVO;
import com.tencent.bk.job.manage.model.web.vo.notify.UserVO;
import com.tencent.bk.job.manage.service.UserService;
import com.tencent.bk.job.manage.service.UserCacheService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -49,13 +49,13 @@
public class NotifyUserService {

private final NotifyBlackUserInfoDAO notifyBlackUserInfoDAO;
private final UserService userService;
private final UserCacheService userCacheService;

@Autowired
public NotifyUserService(NotifyBlackUserInfoDAO notifyBlackUserInfoDAO,
UserService userService) {
UserCacheService userCacheService) {
this.notifyBlackUserInfoDAO = notifyBlackUserInfoDAO;
this.userService = userService;
this.userCacheService = userCacheService;
}

public List<NotifyBlackUserInfoVO> listNotifyBlackUsers(Integer start, Integer pageSize) {
Expand Down Expand Up @@ -149,9 +149,9 @@ private List<BkUserDTO> searchUserByPrefix(String prefixStr) {
while (userNames.contains("")) {
userNames.remove("");
}
return userService.listUsersByUsernames(null, userNames);
return userCacheService.listUsersByUsernames(null, userNames);
} else {
return userService.listUsersByDisplayNamePrefix(null, prefixStr, -1L);
return userCacheService.listUsersByDisplayNamePrefix(null, prefixStr, -1L);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package com.tencent.bk.job.manage.service.impl.notify;

import com.tencent.bk.job.common.util.ThreadUtils;
import com.tencent.bk.job.manage.service.UserService;
import com.tencent.bk.job.manage.service.UserCacheService;
import com.tencent.bk.job.manage.service.impl.WatchableSendMsgService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -45,7 +45,7 @@ public class SendNotifyTask implements Runnable {
private final int NOTIFY_MAX_RETRY_COUNT = 1;

private WatchableSendMsgService watchableSendMsgService;
private UserService userService;
private UserCacheService userCacheService;

private final Long appId;
private final long createTimeMillis;
Expand All @@ -58,9 +58,9 @@ public class SendNotifyTask implements Runnable {
private Set<String> validReceivers;

public void bindService(WatchableSendMsgService watchableSendMsgService,
UserService userService) {
UserCacheService userCacheService) {
this.watchableSendMsgService = watchableSendMsgService;
this.userService = userService;
this.userCacheService = userCacheService;
}

@Override
Expand Down Expand Up @@ -88,7 +88,7 @@ public void run() {

private void pickValidReceivers() {
// TODO 需要加入租户 ID
List<String> existUserNameList = userService.listExistUserName(null, receivers);
List<String> existUserNameList = userCacheService.listExistUserName(null, receivers);
validReceivers = new HashSet<>(existUserNameList);
}

Expand Down
Loading

0 comments on commit 6b248d1

Please sign in to comment.