Skip to content

Commit

Permalink
fix: missing update deployment processes fields in ImageReleaseMgr (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgetx authored Oct 21, 2024
1 parent c7b4a65 commit 3a231ef
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 113 deletions.
10 changes: 0 additions & 10 deletions apiserver/paasng/paasng/platform/bkapp_model/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,3 @@ def sync_hooks(module: Module, hooks: HookList):

# Remove existing data that is not touched.
module.deploy_hooks.filter(id__in=existing_index.values()).delete()


def sync_to_bkapp_model(module, processes: List[ProcessTmpl], hooks: Optional[HookList] = None):
"""保存应用描述文件记录的信息到 bkapp_models
- Processes
- Hooks
"""
ModuleProcessSpecManager(module).sync_from_desc(processes=processes)
if hooks is not None:
sync_hooks(module, hooks)
26 changes: 19 additions & 7 deletions apiserver/paasng/paasng/platform/bkapp_model/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
LOG_COLLECTOR_TYPE_ANNO_KEY,
MODULE_NAME_ANNO_KEY,
PA_SITE_ID_ANNO_KEY,
PROC_SERVICES_ENABLED_ANNOTATION_KEY,
USE_CNB_ANNO_KEY,
WLAPP_NAME_ANNO_KEY,
ApiVersion,
Expand All @@ -58,6 +57,7 @@
DomainResolution,
ModuleProcessSpec,
ObservabilityConfig,
ProcessServicesFlag,
ProcessSpecEnvOverlay,
SvcDiscConfig,
)
Expand All @@ -67,7 +67,6 @@
merge_env_vars_overlay,
override_env_vars_overlay,
)
from paasng.platform.declarative.models import DeploymentDescription
from paasng.platform.engine.configurations.config_var import get_env_variables
from paasng.platform.engine.constants import AppEnvName, ConfigVarEnvName, RuntimeType
from paasng.platform.engine.models import Deployment
Expand Down Expand Up @@ -500,11 +499,8 @@ def get_bkapp_resource_for_deploy(
# such as: if log collector type is set to "ELK", the operator should mount app logs to host path
model_res.metadata.annotations[LOG_COLLECTOR_TYPE_ANNO_KEY] = get_log_collector_type(env)

# 设置 bkapp.paas.bk.tencent.com/proc-services-feature-enabled 注解值
proc_svc_enabled = "true"
if deployment and (desc_obj := DeploymentDescription.objects.filter(deployment=deployment).first()):
proc_svc_enabled = desc_obj.runtime.get(PROC_SERVICES_ENABLED_ANNOTATION_KEY, "true")
model_res.set_proc_services_annotation(proc_svc_enabled)
# 由于 bkapp 新增了 process services 配置特性,部分旧模块需要平台创建 process services 并注入到 model_res 中
apply_proc_svc_if_implicit_needed(model_res, env)

# Apply other changes to the resource
apply_env_annots(model_res, env, deploy_id=deploy_id)
Expand Down Expand Up @@ -563,3 +559,19 @@ def apply_builtin_env_vars(model_res: crd.BkAppResource, env: ModuleEnvironment)
if not overlay:
overlay = model_res.spec.envOverlay = crd.EnvOverlay(envVariables=[])
overlay.envVariables = override_env_vars_overlay(overlay.envVariables or [], builtin_env_vars_overlay)


def apply_proc_svc_if_implicit_needed(model_res: crd.BkAppResource, env: ModuleEnvironment):
"""如果 implicit_needed flag 为 True, 则创建 process services, 并注入到 model_res 中; 否则不做任何操作.
: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")
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 3.2.25 on 2024-10-18 13:31

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('applications', '0012_application_is_ai_agent_app'),
('bkapp_model', '0015_auto_20240913_1509'),
]

operations = [
migrations.CreateModel(
name='ProcessServicesFlag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('region', models.CharField(help_text='部署区域', max_length=32)),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
(
'implicit_needed',
models.BooleanField(default=False, verbose_name='是否隐式需要 process services 配置'),
),
(
'app_environment',
models.OneToOneField(
db_constraint=False,
on_delete=django.db.models.deletion.CASCADE,
to='applications.applicationenvironment',
),
),
],
options={
'abstract': False,
},
),
]
10 changes: 9 additions & 1 deletion apiserver/paasng/paasng/platform/bkapp_model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from django.utils.translation import gettext_lazy as _

from paas_wl.utils.models import AuditedModel, TimestampedModel
from paasng.platform.applications.models import Application, ModuleEnvironment
from paasng.platform.applications.models import Application, ApplicationEnvironment, ModuleEnvironment
from paasng.platform.bkapp_model.entities import (
AutoscalingConfig,
HostAlias,
Expand Down Expand Up @@ -187,6 +187,14 @@ class Meta:
unique_together = ("proc_spec", "environment_name")


class ProcessServicesFlag(TimestampedModel):
"""ProcessServicesFlag 主要用途是标记是否隐式需要 process services 配置"""

app_environment = models.OneToOneField(ApplicationEnvironment, on_delete=models.CASCADE, db_constraint=False)
# 非 3 版本的 app_desc.yaml/Procfile, 由于不支持用户显式配置 process services, 因此设计 implicit_needed 字段来标记是否需要平台隐式创建
implicit_needed = models.BooleanField("是否隐式需要 process services 配置", default=False)


class ModuleDeployHookManager(models.Manager):
"""ModuleDeployHook RelatedManager, should be used by `module.deploy_hooks`"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from django.db.transaction import atomic

from paas_wl.bk_app.cnative.specs.constants import PROC_SERVICES_ENABLED_ANNOTATION_KEY
from paas_wl.bk_app.monitoring.app_monitor.shim import upsert_app_monitor
from paas_wl.bk_app.processes.constants import ProbeType
from paasng.platform.applications.constants import ApplicationType
Expand Down Expand Up @@ -69,7 +68,7 @@ def perform_action(

self.handle_desc(desc)

return DeployHandleResult(use_cnb=desc.spec_version == AppSpecVersion.VER_3)
return DeployHandleResult(desc.spec_version)

def handle_desc(self, desc: DeploymentDesc):
"""Handle the description object, which was read from the app description file."""
Expand Down Expand Up @@ -150,18 +149,10 @@ def _handle_desc_normal_style(self, desc: DeploymentDesc, desc_obj: DeploymentDe

def _save_desc_obj(self, desc: DeploymentDesc) -> DeploymentDescription:
"""Save the raw description data, return the object created."""
runtime = {"source_dir": desc.source_dir}

# specVersion: 3 ,默认开启 proc services 特性; 旧版本不启用
if desc.spec_version == AppSpecVersion.VER_3:
runtime[PROC_SERVICES_ENABLED_ANNOTATION_KEY] = "true"
else:
runtime[PROC_SERVICES_ENABLED_ANNOTATION_KEY] = "false"

deploy_desc, _ = DeploymentDescription.objects.update_or_create(
deployment=self.deployment,
defaults={
"runtime": runtime,
"runtime": {"source_dir": desc.source_dir},
"spec": desc.spec,
# TODO: store desc.bk_monitor to DeploymentDescription
},
Expand Down Expand Up @@ -220,7 +211,7 @@ def handle_procfile_procs(deployment: Deployment, procfile_procs: List[ProcfileP
# Save the process configs to both the module's spec and current deployment object.
ModuleProcessSpecManager(module).sync_from_desc(processes=list(proc_tmpls.values()))
deployment.update_fields(processes=proc_tmpls)
return DeployHandleResult(use_cnb=False)
return DeployHandleResult()


def sanitize_bkapp_spec_to_dict(spec: v1alpha2.BkAppSpec) -> Dict:
Expand Down
8 changes: 6 additions & 2 deletions apiserver/paasng/paasng/platform/declarative/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
from typing import Optional

from attrs import define

from paasng.platform.declarative.constants import AppSpecVersion


@define
class DeployHandleResult:
"""部署阶段处理应用描述文件的结果类。
:param use_cnb: 是否使用了 v3 版本 CNB 模式,对 S-Mart 应用有意义
:param: spec_version 应用描述文件的版本. None 表示没有有效的spec version, 如仅提供了 Procfile 的应用
"""

use_cnb: bool
spec_version: Optional[AppSpecVersion] = None
17 changes: 15 additions & 2 deletions apiserver/paasng/paasng/platform/engine/deploy/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from paasng.platform.applications.constants import ApplicationType
from paasng.platform.bkapp_model.exceptions import ManifestImportError
from paasng.platform.bkapp_model.manifest import get_bkapp_resource
from paasng.platform.bkapp_model.models import ProcessServicesFlag
from paasng.platform.declarative.constants import AppSpecVersion
from paasng.platform.declarative.deployment.controller import DeployHandleResult
from paasng.platform.declarative.exceptions import DescriptionValidationError
from paasng.platform.engine.configurations.building import (
Expand Down Expand Up @@ -148,24 +150,35 @@ def compress_and_upload(
def handle_app_description(self) -> DeployHandleResult:
"""Handle the description files for deployment. It try to parse the app description
file and store the related configurations, e.g. processes.
Set the implicit_needed flag for process services at the end.
:raises HandleAppDescriptionError: When failed to handle the app description.
"""
try:
app_environment = self.deployment.app_environment
handler = get_deploy_desc_handler_by_version(
self.deployment.app_environment.module,
app_environment.module,
self.deployment.operator,
self.deployment.version_info,
self.deployment.get_source_dir(),
)
return handler.handle(self.deployment)
result = handler.handle(self.deployment)

# 非 3 版本的 app_desc.yaml/Procfile, 由于不支持用户显式配置 process services, 因此设置隐式标记, 由平台负责创建
implicit_needed = result.spec_version != AppSpecVersion.VER_3
ProcessServicesFlag.objects.update_or_create(
app_environment=app_environment, defaults={"implicit_needed": implicit_needed}
)

except InitDeployDescHandlerError as e:
raise HandleAppDescriptionError(reason=_("处理应用描述文件失败:{}".format(e)))
except (DescriptionValidationError, ManifestImportError) as e:
raise HandleAppDescriptionError(reason=_("应用描述文件解析异常: {}").format(e.message)) from e
except Exception as e:
logger.exception("Error while handling app description file, deployment: %s.", self.deployment)
raise HandleAppDescriptionError(reason=_("处理应用描述文件时出现异常, 请检查应用描述文件")) from e
else:
return result

def create_bkapp_revision(self) -> int:
"""generate bkapp model and store it into AppModelResource for querying the deployed bkapp model"""
Expand Down
Loading

0 comments on commit 3a231ef

Please sign in to comment.