Skip to content

Commit

Permalink
chore: feat: add push cnb image (#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgetx authored Jun 7, 2024
1 parent e08eba8 commit 5632e53
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
13 changes: 9 additions & 4 deletions apiserver/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ ensure-golang-buildpack() {
"GOPROXY=${PAAS_BUILDPACK_GOLANG_GOPROXY}"
}

ensure-buleking-image() {
ensure-blueking-image() {
region="$1"
apt_buildpack_name="$2"
python_buildpack_name="$3"
Expand All @@ -205,6 +205,7 @@ ensure-buleking-image() {
image_name="blueking"
python manage.py manage_image \
--region "${region}" \
--type "legacy" \
--image "${PAAS_APP_IMAGE}" \
--name "${image_name}" \
--display_name_zh_cn "蓝鲸基础镜像" \
Expand All @@ -220,7 +221,9 @@ ensure-buleking-image() {
cnb_image_name="blueking-cloudnative"
python manage.py manage_image \
--region "${region}" \
--image "${PAAS_HEROKU_BUILDER_IMAGE}" \
--type "cnb" \
--slugbuilder "${PAAS_HEROKU_BUILDER_IMAGE}" \
--slugrunner "${PAAS_HEROKU_RUNNER_IMAGE}" \
--name "${cnb_image_name}" \
--display_name_zh_cn "蓝鲸基础镜像" \
--display_name_en "Blueking Basic Image" \
Expand All @@ -244,6 +247,7 @@ ensure-legacy-image() {
legacy_image_name="legacy-blueking"
python manage.py manage_image \
--region "${region}" \
--type "legacy" \
--image "${PAAS_APP_IMAGE}" \
--name "${legacy_image_name}" \
--display_name_zh_cn "蓝鲸基础镜像(旧)" \
Expand All @@ -258,7 +262,8 @@ ensure-legacy-image() {
}

ensure-smart-image() {
python manage.py push_smart_image --image "${PAAS_APP_IMAGE}" --dry-run "${PAAS_SKIP_PUSH_SMART_BASE_IMAGE:-False}"
python manage.py push_smart_image --image "${PAAS_APP_IMAGE}" --type legacy --dry-run "${PAAS_SKIP_PUSH_SMART_BASE_IMAGE:-False}"
python manage.py push_smart_image --image "${PAAS_HEROKU_RUNNER_IMAGE}" --type cnb --dry-run "${PAAS_SKIP_PUSH_SMART_BASE_IMAGE:-False}"
}

ensure-runtimes() {
Expand Down Expand Up @@ -288,7 +293,7 @@ ensure-runtimes() {
ensure-golang-buildpack "${bkrepo_endpoint}" "${bkrepo_project}" "${region}" "${buildpack_url}" "${vendor_url}" "${golang_buildpack_name}"

# blueking image
ensure-buleking-image "${region}" "${apt_buildpack_name}" "${python_buildpack_name}" "${nodejs_buildpack_name}" "${golang_buildpack_name}"
ensure-blueking-image "${region}" "${apt_buildpack_name}" "${python_buildpack_name}" "${nodejs_buildpack_name}" "${golang_buildpack_name}"

# legacy blueking image
ensure-legacy-image "${region}" "${apt_buildpack_name}" "${python_buildpack_name}" "${nodejs_buildpack_name}" "${golang_buildpack_name}"
Expand Down
3 changes: 3 additions & 0 deletions apiserver/paasng/conf.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@
## 是否使用 DockerRegistryToken 来验证,为 false 时使用 HTTPBasicAuthentication
# SMART_DOCKER_AUTH_BY_TOKEN: true

## 如果用到 python manage.py push_smart_image, SMART_IMAGE_TAG 和 SMART_CNB_IMAGE_TAG 必须设置有效值
# SMART_IMAGE_TAG: v0.0.1-smart
# SMART_CNB_IMAGE_TAG: v0.0.1-smart

## ------------------------------------ 插件开发中心配置 ------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("-n", "--name", required=True, dest="name", help="name")
parser.add_argument("-r", "--region", required=True, dest="regions", help="available region name", nargs="+")
parser.add_argument("--type", required=True, dest="type_", help="image type can be either cnb or legacy")
parser.add_argument("--hidden", dest="is_hidden", help="is_hidden", action="store_true")
parser.add_argument("--slugbuilder", required=False, help="slugbuilder image")
parser.add_argument("--slugrunner", required=False, help="slugrunner image")
Expand Down Expand Up @@ -60,6 +61,7 @@ def handle(
self,
name,
image,
type_,
slugbuilder,
slugrunner,
regions,
Expand All @@ -82,6 +84,7 @@ def handle(
image_name,
region,
slugbuilder,
type_,
display_name_zh_cn,
display_name_en,
description_zh_cn,
Expand All @@ -95,6 +98,7 @@ def handle(
image_name,
region,
slugrunner,
type_,
display_name_zh_cn,
display_name_en,
description_zh_cn,
Expand All @@ -110,6 +114,7 @@ def update_or_create_image(
name,
region,
image,
type_,
display_name_zh_cn,
display_name_en,
description_zh_cn,
Expand All @@ -132,6 +137,7 @@ def update_or_create_image(
"display_name_zh_cn": display_name_zh_cn,
"display_name_en": display_name_en,
"image": image,
"type": type_,
"tag": tag,
"description_zh_cn": description_zh_cn,
"description_en": description_en,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,45 @@
import pathlib
import shutil
import tempfile
from typing import Dict

from django.core.management.base import BaseCommand
from moby_distribution import APIEndpoint, DockerRegistryV2Client, ImageRef
from moby_distribution.registry.utils import parse_image

from paasng.platform.modules.constants import AppImageType
from paasng.platform.smart_app.conf import bksmart_settings
from paasng.utils.validators import str2bool

logger = logging.getLogger(__name__)


DEST_IMAGE_CONFIGS: Dict[str, Dict] = {
AppImageType.CNB.value: {
"repo": bksmart_settings.cnb_base_image.name,
"reference": bksmart_settings.cnb_base_image.tag,
},
AppImageType.LEGACY.value: {
"repo": bksmart_settings.base_image.name,
"reference": bksmart_settings.base_image.tag,
},
}


class Command(BaseCommand):
help = "管理 S-Mart 基础镜像"

def add_arguments(self, parser):
parser.add_argument("--image", required=True, help="universal image")
parser.add_argument(
"--type",
required=True,
dest="type_",
help="image type can be either cnb or legacy",
)
parser.add_argument("--dry-run", dest="dry_run", type=str2bool, help="dry run", default=False)

def handle(self, image: str, dry_run: bool, *args, **options):
def handle(self, image: str, type_: str, dry_run: bool, *args, **options):
if dry_run:
logger.warning("Skipped the step of pushing S-Mart base image to bkrepo!")
return
Expand All @@ -59,12 +79,14 @@ def handle(self, image: str, dry_run: bool, *args, **options):
raise

workplace = tempfile.mkdtemp()
to_config = DEST_IMAGE_CONFIGS[type_]

try:
ref = ImageRef.from_tarball(
workplace=pathlib.Path(workplace),
src=image_tarball_path,
to_repo=bksmart_settings.base_image.name,
to_reference=bksmart_settings.base_image.tag,
to_repo=to_config["repo"],
to_reference=to_config["reference"],
client=bksmart_settings.registry.get_client(),
)
ref.push()
Expand Down
5 changes: 3 additions & 2 deletions apiserver/paasng/paasng/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,10 +1226,11 @@ def _build_file_handler(log_path: Path, filename: str, format: str) -> Dict:
# 用于访问 Registry 的密码
SMART_DOCKER_REGISTRY_PASSWORD = settings.get("SMART_DOCKER_PASSWORD", "blueking")
# S-Mart 基础镜像信息
_SMART_TAG_SUFFIX = "smart"
SMART_IMAGE_NAME = f"{SMART_DOCKER_REGISTRY_NAMESPACE}/slug-pilot"
SMART_IMAGE_TAG = "heroku-18-v1.6.1"
SMART_IMAGE_TAG = f'{settings.get("APP_IMAGE", "").partition(":")[-1]}-{_SMART_TAG_SUFFIX}'
SMART_CNB_IMAGE_NAME = f"{SMART_DOCKER_REGISTRY_NAMESPACE}/run-heroku-bionic"
SMART_CNB_IMAGE_TAG = "heroku-18-v1.4.0"
SMART_CNB_IMAGE_TAG = f'{settings.get("HEROKU_RUNNER_IMAGE", "").partition(":")[-1]}-{_SMART_TAG_SUFFIX}'

# slugbuilder build 的超时时间, 单位秒
BUILD_PROCESS_TIMEOUT = int(settings.get("BUILD_PROCESS_TIMEOUT", 60 * 15))
Expand Down

0 comments on commit 5632e53

Please sign in to comment.