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

fix: process services implicit inject #1741

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions apiserver/paasng/paasng/platform/bkapp_model/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def apply_to(self, model_res: crd.BkAppResource, module: Module):
)

# Set the annotation to inform operator what the image pull secret name is
model_res.metadata.annotations[
IMAGE_CREDENTIALS_REF_ANNO_KEY
] = f"{generate_bkapp_name(module)}--dockerconfigjson"
model_res.metadata.annotations[IMAGE_CREDENTIALS_REF_ANNO_KEY] = (
f"{generate_bkapp_name(module)}--dockerconfigjson"
)


class BuildConfigManifestConstructor(ManifestConstructor):
Expand Down Expand Up @@ -566,12 +566,20 @@ def apply_proc_svc_if_implicit_needed(model_res: crd.BkAppResource, env: ModuleE

:param model_res: The bkapp model resource object.
:param env: The environment object.

# 说明: 现阶段通过设置注解 bkapp.paas.bk.tencent.com/proc-services-feature-enabled, 在 operator 侧完成对应功能.
# TODO 后续处理: 当 implicit_needed 为 True 时, 创建并注入 process services 配置, 并且统一设置注解值为 true
"""
flag, _ = ProcessServicesFlag.objects.get_or_create(app_environment=env, defaults={"implicit_needed": False})
if not flag.implicit_needed:
model_res.set_proc_services_annotation("true")
else:
model_res.set_proc_services_annotation("false")
model_res.set_proc_services_annotation("true")
if flag.implicit_needed:
for process in model_res.spec.processes:
if process.name != "web":
service = crd.ProcService(name=process.name, targetPort=5000, protocol="TCP", port=80)
songzxc789 marked this conversation as resolved.
Show resolved Hide resolved
if not process.services:
songzxc789 marked this conversation as resolved.
Show resolved Hide resolved
process.services = []
process.services.append(service)
else:
service = crd.ProcService(
name=process.name, targetPort=5000, protocol="TCP", port=80, exposedType=crd.ExposedType()
)
if not process.services:
process.services = []
process.services.append(service)
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ def blank_resource() -> crd.BkAppResource:
)


@pytest.fixture()
def resource_with_processes() -> crd.BkAppResource:
"""A resource object have processes spec."""
return crd.BkAppResource(
apiVersion=ApiVersion.V1ALPHA2,
metadata=ObjectMetadata(name="a-blank-resource"),
spec=crd.BkAppSpec(
processes=[
crd.BkAppProcess(name="worker"),
crd.BkAppProcess(name="web"),
]
),
)


@pytest.fixture()
def local_service(bk_app):
"""A local service object."""
Expand Down Expand Up @@ -547,7 +562,11 @@ def test_apply_proc_svc_if_implicit_needed_is_false(blank_resource, bk_stag_env)
assert blank_resource.get_proc_services_annotation() == "true"


def test_apply_proc_svc_if_implicit_needed_is_true(blank_resource, bk_stag_env):
def test_apply_proc_svc_if_implicit_needed_is_true(resource_with_processes, bk_stag_env):
ProcessServicesFlag.objects.create(app_environment=bk_stag_env, implicit_needed=True)
apply_proc_svc_if_implicit_needed(blank_resource, bk_stag_env)
assert blank_resource.get_proc_services_annotation() == "false"
apply_proc_svc_if_implicit_needed(resource_with_processes, bk_stag_env)
assert resource_with_processes.get_proc_services_annotation() == "true"
assert resource_with_processes.spec.processes[0].services[0].name == "worker"
assert resource_with_processes.spec.processes[0].services[0].exposedType is None
assert resource_with_processes.spec.processes[1].services[0].name == "web"
assert resource_with_processes.spec.processes[1].services[0].exposedType == crd.ExposedType()