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: Job 支持多租户 #3369 #3447

Open
wants to merge 14 commits into
base: feat/tenant
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
package com.tencent.bk.job.common.cc.config;

import com.tencent.bk.job.common.WatchableThreadPoolExecutor;
import com.tencent.bk.job.common.cc.mock.MockBizCmdbClient;
import com.tencent.bk.job.common.cc.mock.MockTenantSetCmdbClient;
import com.tencent.bk.job.common.cc.sdk.BizCmdbClient;
import com.tencent.bk.job.common.cc.sdk.BizSetCmdbClient;
import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient;
import com.tencent.bk.job.common.cc.sdk.IBizSetCmdbClient;
import com.tencent.bk.job.common.cc.sdk.ITenantSetCmdbClient;
import com.tencent.bk.job.common.cc.sdk.TenantSetCmdbClient;
import com.tencent.bk.job.common.esb.config.AppProperties;
import com.tencent.bk.job.common.esb.config.BkApiAutoConfiguration;
import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties;
Expand Down Expand Up @@ -89,14 +95,15 @@ public ThreadPoolExecutor cmdbLongTermThreadPoolExecutor(CmdbConfig cmdbConfig,

@Bean
@Primary
public BizCmdbClient bizCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
ThreadPoolExecutor cmdbThreadPoolExecutor,
ThreadPoolExecutor cmdbLongTermThreadPoolExecutor,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
@ConditionalOnMockCmdbApiDisabled
public IBizCmdbClient bizCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
ThreadPoolExecutor cmdbThreadPoolExecutor,
ThreadPoolExecutor cmdbLongTermThreadPoolExecutor,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
return new BizCmdbClient(
appProperties,
bkApiGatewayProperties,
Expand All @@ -111,14 +118,15 @@ public BizCmdbClient bizCmdbClient(AppProperties appProperties,
}

@Bean("cnBizCmdbClient")
public BizCmdbClient cnBizCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
ThreadPoolExecutor cmdbThreadPoolExecutor,
ThreadPoolExecutor cmdbLongTermThreadPoolExecutor,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
@ConditionalOnMockCmdbApiDisabled
public IBizCmdbClient cnBizCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
ThreadPoolExecutor cmdbThreadPoolExecutor,
ThreadPoolExecutor cmdbLongTermThreadPoolExecutor,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
return new BizCmdbClient(
appProperties,
bkApiGatewayProperties,
Expand All @@ -133,12 +141,12 @@ public BizCmdbClient cnBizCmdbClient(AppProperties appProperties,
}

@Bean
public BizSetCmdbClient bizSetCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
public IBizSetCmdbClient bizSetCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
return new BizSetCmdbClient(
appProperties,
bkApiGatewayProperties,
Expand All @@ -149,4 +157,49 @@ public BizSetCmdbClient bizSetCmdbClient(AppProperties appProperties,
);
}

@Bean
@ConditionalOnMockCmdbApiDisabled
public ITenantSetCmdbClient tenantSetCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
return new TenantSetCmdbClient(
appProperties,
bkApiGatewayProperties,
cmdbConfig,
flowControllerProvider.getIfAvailable(),
meterRegistry,
tenantEnvService
);
}

@Bean
@ConditionalOnMockCmdbApiEnabled
public IBizCmdbClient mockedBizCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
ThreadPoolExecutor cmdbThreadPoolExecutor,
ThreadPoolExecutor cmdbLongTermThreadPoolExecutor,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
return new MockBizCmdbClient(
appProperties,
bkApiGatewayProperties,
cmdbConfig,
cmdbThreadPoolExecutor,
cmdbLongTermThreadPoolExecutor,
meterRegistry,
flowControllerProvider,
tenantEnvService
);
}

@Bean
@ConditionalOnMockCmdbApiEnabled
public ITenantSetCmdbClient mockedTenantSetCmdbClient() {
return new MockTenantSetCmdbClient();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.cc.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ConditionalOnProperty(
value = "mockApi.cmdb.enabled",
havingValue = "false",
matchIfMissing = true
)
public @interface ConditionalOnMockCmdbApiDisabled {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.cc.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ConditionalOnProperty(
value = "mockApi.cmdb.enabled",
havingValue = "true"
)
public @interface ConditionalOnMockCmdbApiEnabled {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.cc.mock;

import com.tencent.bk.job.common.cc.config.CmdbConfig;
import com.tencent.bk.job.common.cc.model.CcInstanceDTO;
import com.tencent.bk.job.common.cc.model.InstanceTopologyDTO;
import com.tencent.bk.job.common.cc.sdk.BizCmdbClient;
import com.tencent.bk.job.common.cc.sdk.IBizCmdbClient;
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.constants.EsbLang;
import com.tencent.bk.job.common.model.dto.ApplicationHostDTO;
import com.tencent.bk.job.common.tenant.TenantEnvService;
import com.tencent.bk.job.common.util.FlowController;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.experimental.Delegate;
import org.springframework.beans.factory.ObjectProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

public class MockBizCmdbClient implements IBizCmdbClient {

@Delegate
private final IBizCmdbClient proxy;

public MockBizCmdbClient(AppProperties appProperties,
BkApiGatewayProperties bkApiGatewayProperties,
CmdbConfig cmdbConfig,
ThreadPoolExecutor cmdbThreadPoolExecutor,
ThreadPoolExecutor cmdbLongTermThreadPoolExecutor,
MeterRegistry meterRegistry,
ObjectProvider<FlowController> flowControllerProvider,
TenantEnvService tenantEnvService) {
this.proxy = new BizCmdbClient(appProperties,
bkApiGatewayProperties,
cmdbConfig,
EsbLang.EN,
cmdbThreadPoolExecutor,
cmdbLongTermThreadPoolExecutor,
flowControllerProvider.getIfAvailable(),
meterRegistry,
tenantEnvService
);
}

private InstanceTopologyDTO mockInstanceTopologyDTO() {
InstanceTopologyDTO bizNode = new InstanceTopologyDTO();
bizNode.setObjectId("biz");
bizNode.setInstanceId(1L);
bizNode.setInstanceName("蓝鲸");

List<InstanceTopologyDTO> moduleList = new ArrayList<>();
InstanceTopologyDTO idleMachineModule = new InstanceTopologyDTO();
idleMachineModule.setObjectId("module");
idleMachineModule.setInstanceId(2L);
idleMachineModule.setInstanceName("空闲机");
moduleList.add(idleMachineModule);
InstanceTopologyDTO faultMachineModule = new InstanceTopologyDTO();
faultMachineModule.setObjectId("module");
faultMachineModule.setInstanceId(3L);
faultMachineModule.setInstanceName("故障机");
moduleList.add(faultMachineModule);
InstanceTopologyDTO recycleMachineModule = new InstanceTopologyDTO();
recycleMachineModule.setObjectId("module");
recycleMachineModule.setInstanceId(4L);
recycleMachineModule.setInstanceName("待回收");
moduleList.add(recycleMachineModule);

List<InstanceTopologyDTO> setList = new ArrayList<>();
InstanceTopologyDTO idlePoolSet = new InstanceTopologyDTO();
idlePoolSet.setObjectId("set");
idlePoolSet.setInstanceId(2L);
idlePoolSet.setInstanceName("空闲机池");
idlePoolSet.setChild(moduleList);

setList.add(idlePoolSet);
idlePoolSet.setChild(setList);
return idlePoolSet;
}

@Override
public InstanceTopologyDTO getBizInstTopology(long bizId) {
return mockInstanceTopologyDTO();
}

@Override
public InstanceTopologyDTO getBizInstCompleteTopology(String tenantId, long bizId) {
return mockInstanceTopologyDTO();
}

@Override
public List<ApplicationHostDTO> getHosts(long bizId, List<CcInstanceDTO> ccInstList) {
ApplicationHostDTO host = new ApplicationHostDTO();
host.setHostId(1L);
host.setBizId(1L);
host.setIp("127.0.0.1");
host.setHostName("MockHost1");
host.setGseAgentStatus(0);
host.setAgentId("MockAgentId1");
host.setCloudAreaId(0L);
host.setCloudAreaName("MockCloudArea1");
return null;
}
}
Loading