Skip to content

Commit

Permalink
feature: 支持bk_agent_id (close TencentBlueKing#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 committed Mar 17, 2022
1 parent 6d2a473 commit 6654796
Show file tree
Hide file tree
Showing 23 changed files with 650 additions and 246 deletions.
8 changes: 7 additions & 1 deletion apps/backend/components/collections/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,10 @@ def _execute(self, data, parent_data, common_data: PluginCommonData):
gse_op_params = {
"meta": {"namespace": constants.GSE_NAMESPACE, "name": meta_name},
"op_type": op_type,
# GSE 1.0 目标对象
"hosts": [{"ip": host.inner_ip, "bk_cloud_id": host.bk_cloud_id}],
# GSE 2.0 目标对象
"agent_id_list": [host.bk_agent_id],
# 此字段是节点管理自用,仅用于标识,不会被GSE使用
"nodeman_spec": {
"process_status_id": process_status.id,
Expand Down Expand Up @@ -1227,7 +1230,10 @@ def _schedule(self, data, parent_data, callback_data=None):
subscription_instance = group_id_instance_map.get(process_status.group_id)

proc_name = self.get_plugin_meta_name(plugin, process_status)
gse_proc_key = f"{host.bk_cloud_id}:{host.inner_ip}:{constants.GSE_NAMESPACE}:{proc_name}"
if host.bk_agent_id:
gse_proc_key = f"{host.bk_agent_id}:{constants.GSE_NAMESPACE}:{proc_name}"
else:
gse_proc_key = f"{host.bk_cloud_id}:{host.inner_ip}:{constants.GSE_NAMESPACE}:{proc_name}"
proc_operate_result = result["data"].get(gse_proc_key)
if not proc_operate_result:
self.move_insts_to_failed(
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/subscription/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_host_object_attribute(bk_biz_id):
def list_biz_hosts(bk_biz_id, condition, func, split_params=False):
biz_custom_property = []
kwargs = {
"fields": constants.LIST_BIZ_HOSTS_KWARGS,
"fields": constants.CC_HOST_FIELDS,
}
if bk_biz_id:
biz_custom_property = get_host_object_attribute(bk_biz_id)
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/subscription/render_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_host_detail_by_template(bk_obj_id, template_info_list: list, bk_biz_id:
if not template_info_list:
return []

fields = constants.FIND_HOST_BY_TEMPLATE_FIELD
fields = constants.CC_HOST_FIELDS

if bk_obj_id == models.Subscription.NodeType.SERVICE_TEMPLATE:
# 服务模板
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/subscription/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def get_host_detail_by_template(bk_obj_id, template_info_list: list, bk_biz_id:
if not template_info_list:
return []

fields = constants.FIND_HOST_BY_TEMPLATE_FIELD
fields = constants.CC_HOST_FIELDS

if bk_obj_id == models.Subscription.NodeType.SERVICE_TEMPLATE:
# 服务模板
Expand Down
136 changes: 136 additions & 0 deletions apps/mock_data/api_mkd/gse/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,142 @@
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.
"""
import copy

from apps.mock_data.common_unit import host
from apps.node_man import constants

# 操作类接口一般返回的是 task_id
OP_RESULT = {"task_id": "GSETASK:S:202111161138323563236795:143"}
GSE_PROCESS_VERSION = "1.7.17"
GSE_PROCESS_NAME = "test_process"

GET_AGENT_ALIVE_STATUS_DATA = {
f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}": {
"ip": host.DEFAULT_IP,
"bk_cloud_id": constants.DEFAULT_CLOUD,
"bk_agent_alive": constants.BkAgentStatus.ALIVE.value,
}
}

GET_AGENT_NOT_ALIVE_STATUS_DATA = {
f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}": {
"ip": host.DEFAULT_IP,
"bk_cloud_id": constants.DEFAULT_CLOUD,
"bk_agent_alive": constants.BkAgentStatus.NOT_ALIVE.value,
}
}

GET_AGENT_INFO_DATA = {
f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}": {
"ip": host.DEFAULT_IP,
"version": GSE_PROCESS_VERSION,
"bk_cloud_id": 0,
"parent_ip": host.DEFAULT_IP,
"parent_port": 50000,
}
}

GET_PROC_OPERATE_RESULT = {
f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}:{constants.GSE_NAMESPACE}:sub_870_host_1_host_exp002": {
"content": '{\n "value" : [\n {\n "funcID" : "",\n'
' "instanceID" : "",\n '
' "procName" : "host_exp002",\n "result" : "success",\n'
' "setupPath" : "/usr/local/gse/external_plugins/sub_870'
'_host_1/host_exp002"\n }\n ]\n}\n',
"error_code": 0,
"error_msg": "success",
}
}

GET_AGENT_STATE_LIST_DATA = [
{
"bk_agent_id": f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}",
"bk_cloud_id": constants.DEFAULT_CLOUD,
"version": GSE_PROCESS_VERSION,
"run_mode": 0,
"status_code": constants.GseAgentStatusCode.RUNNING.value,
}
]

GET_AGENT_NOT_ALIVE_STATE_LIST_DATA = [
{
"bk_agent_id": f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}",
"bk_cloud_id": constants.DEFAULT_CLOUD,
"version": GSE_PROCESS_VERSION,
"run_mode": 0,
"status_code": constants.GseAgentStatusCode.STOPPED.value,
}
]

GET_AGENT_INFO_LIST_DATA = [
{
"code": 0,
"message": "OK",
"bk_agent_id": f"{constants.DEFAULT_CLOUD}:{host.DEFAULT_IP}",
"bk_cloud_id": constants.DEFAULT_CLOUD,
"bk_host_ip": host.DEFAULT_IP,
"bk_os_type": "linux",
"report_time": 1632830304777,
"parent_ip": host.DEFAULT_IP,
"parent_port": 0,
"version": GSE_PROCESS_VERSION,
"cpu_rate": 12.34,
"mem_rate": 56.78,
"start_time": 1632830300,
"run_mode": 0,
"status_code": constants.GseAgentStatusCode.RUNNING.value,
"status": "running",
"last_status_code": constants.GseAgentStatusCode.RUNNING.value,
"last_status": "running",
"remark": "",
}
]

GET_PROC_STATUS_DATA = {
"proc_infos": [
{
# 需要根据不同参数补充 hosts 或者 bk_agent_id, 详见 mock_get_proc_status
"status": constants.GseProcessStatusCode.STOPPED.value,
"version": GSE_PROCESS_VERSION,
"isauto": constants.GseProcessAutoCode.AUTO.value,
"meta": {
"name": GSE_PROCESS_NAME,
"namespace": constants.GSE_NAMESPACE,
"labels": {"proc_name": GSE_PROCESS_NAME},
},
}
]
}


def mock_get_agent_info_list(params):
agent_info_list = copy.deepcopy(GET_AGENT_INFO_LIST_DATA)
for index, agent_info in enumerate(agent_info_list):
agent_info["bk_agent_id"] = params["agent_id_list"][index]
return agent_info_list


def mock_get_agent_state_list(params):
agent_state_list = copy.deepcopy(GET_AGENT_STATE_LIST_DATA)
for index, agent_state in enumerate(agent_state_list):
agent_state["bk_agent_id"] = params["agent_id_list"][index]
return agent_state_list


def mock_get_proc_status(params):
pro_status_data = copy.deepcopy(GET_PROC_STATUS_DATA)
hosts_param = params["hosts"][0]
for index, host_info in enumerate(params["hosts"]):
if f"{hosts_param['bk_cloud_id']}:{hosts_param['ip']}" == hosts_param["agent_id"]:
pro_status_data["proc_infos"][index]["host"] = {
"ip": hosts_param["ip"],
"bk_cloud_id": hosts_param["bk_cloud_id"],
}
else:
pro_status_data["proc_infos"][index]["bk_agent_id"] = hosts_param["agent_id"]
# 添加返回一个不存在的key来模拟额外返回的case
not_exist_proc_info = copy.deepcopy(pro_status_data["proc_infos"][0])
not_exist_proc_info["bk_agent_id"] = "not_exist_proc_info_agent_id"
pro_status_data["proc_infos"].append(not_exist_proc_info)
return pro_status_data
45 changes: 39 additions & 6 deletions apps/mock_data/api_mkd/gse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,50 @@
"""

from ... import utils
from . import unit


class GseApiMockClient(utils.BaseMockClient):
DEFAULT_OPERATE_PROC_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.RETURN_VALUE.value, return_obj=unit.OP_RESULT
)
DEFAULT_GET_OPERATE_RESULT_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.RETURN_VALUE.value, return_obj=unit.GET_PROC_OPERATE_RESULT
)
DEFAULT_GET_AGENT_INFO_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.RETURN_VALUE.value, return_obj=unit.GET_AGENT_INFO_DATA
)
DEFAULT_GET_AGENT_STATUS_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.RETURN_VALUE.value, return_obj=unit.GET_AGENT_ALIVE_STATUS_DATA
)
GET_AGENT_NOT_ALIVE_STATUS_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.RETURN_VALUE.value, return_obj=unit.GET_AGENT_NOT_ALIVE_STATUS_DATA
)
DEFAULT_GET_AGENT_INFO_LIST_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.SIDE_EFFECT.value, return_obj=unit.mock_get_agent_info_list
)
DEFAULT_GET_AGENT_STATE_LIST_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.SIDE_EFFECT.value, return_obj=unit.mock_get_agent_state_list
)
GET_AGENT_NOT_ALIVE_STATE_LIST_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.RETURN_VALUE.value, return_obj=unit.GET_AGENT_NOT_ALIVE_STATE_LIST_DATA
)
DEFAULT_GET_PROC_STATUS_RETURN = utils.MockReturn(
return_type=utils.MockReturnType.SIDE_EFFECT.value, return_obj=unit.mock_get_proc_status
)

def __init__(
self,
operate_proc_return=None,
operate_proc_multi_return=None,
get_proc_operate_result_return=None,
get_proc_status_return=None,
operate_proc_return=DEFAULT_OPERATE_PROC_RETURN,
operate_proc_multi_return=DEFAULT_OPERATE_PROC_RETURN,
get_proc_operate_result_return=DEFAULT_GET_OPERATE_RESULT_RETURN,
get_proc_status_return=DEFAULT_GET_PROC_STATUS_RETURN,
sync_proc_status_return=None,
update_proc_info_return=None,
get_agent_info_return=None,
get_agent_status_return=None,
get_agent_info_return=DEFAULT_GET_AGENT_INFO_RETURN,
get_agent_status_return=DEFAULT_GET_AGENT_STATUS_RETURN,
get_agent_info_list_return=DEFAULT_GET_AGENT_INFO_LIST_RETURN,
get_agent_state_list_return=DEFAULT_GET_AGENT_STATE_LIST_RETURN,
):
super().__init__()
self.operate_proc = self.generate_magic_mock(mock_return_obj=operate_proc_return)
Expand All @@ -33,3 +64,5 @@ def __init__(
self.update_proc_info = self.generate_magic_mock(mock_return_obj=update_proc_info_return)
self.get_agent_info = self.generate_magic_mock(mock_return_obj=get_agent_info_return)
self.get_agent_status = self.generate_magic_mock(mock_return_obj=get_agent_status_return)
self.get_agent_info_list = self.generate_magic_mock(mock_return_obj=get_agent_info_list_return)
self.get_agent_state_list = self.generate_magic_mock(mock_return_obj=get_agent_state_list_return)
4 changes: 2 additions & 2 deletions apps/mock_data/common_unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"""


from . import gse, host, job, plugin, subscription
from . import host, job, plugin, subscription

__all__ = ["host", "job", "plugin", "subscription", "gse"]
__all__ = ["host", "job", "plugin", "subscription"]
33 changes: 0 additions & 33 deletions apps/mock_data/common_unit/gse.py

This file was deleted.

17 changes: 16 additions & 1 deletion apps/mock_data/common_unit/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
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.
"""
import copy

from apps.node_man import constants, models

from .. import utils

BK_AGENT_ID = "xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx"

DEFAULT_HOST_ID = 1

DEFAULT_IP = "127.0.0.1"

DEFAULT_IPv6 = "ABCD:EF01:2345:6789:ABCD:EF01:2345:6789"

PROXY_INNER_IP = "1.1.1.1"

AP_MODEL_DATA = {
Expand Down Expand Up @@ -91,13 +97,22 @@
"os_type": constants.OsType.LINUX,
"cpu_arch": constants.CpuType.x86_64,
"node_type": constants.NodeType.AGENT,
"node_from": constants.NodeFrom.NODE_MAN,
"node_from": constants.NodeFrom.CMDB,
"ap_id": constants.DEFAULT_AP_ID,
"upstream_nodes": [],
"is_manual": False,
"extra_data": {"bt_speed_limit": None, "peer_exchange_switch_for_agent": 1},
}

HOST_MODEL_DATA_WITH_AGENT_ID = copy.deepcopy(HOST_MODEL_DATA)
HOST_MODEL_DATA_WITH_AGENT_ID.update(
**{
"bk_agent_id": BK_AGENT_ID,
"inner_ipv6": DEFAULT_IPv6,
"outer_ipv6": DEFAULT_IPv6,
}
)

IDENTITY_MODEL_DATA = {
"bk_host_id": DEFAULT_HOST_ID,
"auth_type": constants.AuthType.PASSWORD,
Expand Down
Loading

0 comments on commit 6654796

Please sign in to comment.