From 5a537881307377b9b51891d8631537b81b18d5f6 Mon Sep 17 00:00:00 2001 From: "wangjiaju.716" Date: Wed, 3 Sep 2025 11:27:47 +0800 Subject: [PATCH 1/4] Add veadk-version in requirements.txt --- veadk/cli/cli_deploy.py | 2 ++ veadk/cli/cli_init.py | 2 ++ veadk/integrations/ve_faas/template/cookiecutter.json | 3 ++- .../{{cookiecutter.local_dir_name}}/src/requirements.txt | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/veadk/cli/cli_deploy.py b/veadk/cli/cli_deploy.py index 0d8ece95..56bb53f4 100644 --- a/veadk/cli/cli_deploy.py +++ b/veadk/cli/cli_deploy.py @@ -14,6 +14,7 @@ import click +from veadk.version import VERSION TEMP_PATH = "/tmp" @@ -94,6 +95,7 @@ def deploy( "veapig_service_name": veapig_service_name, "veapig_upstream_name": veapig_upstream_name, "use_adk_web": use_adk_web, + "veadk_version": VERSION, } cookiecutter( diff --git a/veadk/cli/cli_init.py b/veadk/cli/cli_init.py index 961baff5..e320badb 100644 --- a/veadk/cli/cli_init.py +++ b/veadk/cli/cli_init.py @@ -16,6 +16,7 @@ from typing import Any import click +from veadk.version import VERSION warnings.filterwarnings( "ignore", category=UserWarning, module="pydantic._internal._fields" @@ -58,6 +59,7 @@ def _render_prompts() -> dict[str, Any]: "veapig_service_name": veapig_service_name, "veapig_upstream_name": veapig_upstream_name, "use_adk_web": deploy_mode == "2", + "veadk_version": VERSION, } diff --git a/veadk/integrations/ve_faas/template/cookiecutter.json b/veadk/integrations/ve_faas/template/cookiecutter.json index 8ae10f47..8df19781 100644 --- a/veadk/integrations/ve_faas/template/cookiecutter.json +++ b/veadk/integrations/ve_faas/template/cookiecutter.json @@ -7,5 +7,6 @@ "veapig_instance_name": "", "veapig_service_name": "", "veapig_upstream_name": "", - "use_adk_web": false + "use_adk_web": false, + "veadk_version": "" } \ No newline at end of file diff --git a/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/requirements.txt b/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/requirements.txt index d4b2885a..0a9f13d9 100644 --- a/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/requirements.txt +++ b/veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/requirements.txt @@ -1 +1 @@ -veadk-python \ No newline at end of file +veadk-python=={{ cookiecutter.veadk_version }} \ No newline at end of file From 4c1e816f2080e505742fa259ec9d0594b05e3446 Mon Sep 17 00:00:00 2001 From: "wangjiaju.716" Date: Fri, 5 Sep 2025 11:59:03 +0800 Subject: [PATCH 2/4] Add ve_cr --- veadk/consts.py | 4 ++ veadk/integrations/ve_cr/__init__.py | 0 veadk/integrations/ve_cr/ve_cr.py | 79 ++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 veadk/integrations/ve_cr/__init__.py create mode 100644 veadk/integrations/ve_cr/ve_cr.py diff --git a/veadk/consts.py b/veadk/consts.py index dd877690..9320d7ad 100644 --- a/veadk/consts.py +++ b/veadk/consts.py @@ -43,3 +43,7 @@ DEFAULT_TLS_OTEL_EXPORTER_ENDPOINT = "https://tls-cn-beijing.volces.com:4318/v1/traces" DEFAULT_TLS_OTEL_EXPORTER_REGION = "cn-beijing" + +DEFAULT_CR_INSTANCE_NAME = "veadk-user-instance" +DEFAULT_CR_NAMESPACE_NAME = "veadk-user-namespace" +DEFAULT_CR_REPO_NAME = "veadk-user-repo" diff --git a/veadk/integrations/ve_cr/__init__.py b/veadk/integrations/ve_cr/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/veadk/integrations/ve_cr/ve_cr.py b/veadk/integrations/ve_cr/ve_cr.py new file mode 100644 index 00000000..61790ac0 --- /dev/null +++ b/veadk/integrations/ve_cr/ve_cr.py @@ -0,0 +1,79 @@ +from veadk.utils.volcengine_sign import ve_request +from veadk.utils.logger import get_logger +from veadk.consts import ( + DEFAULT_CR_INSTANCE_NAME, + DEFAULT_CR_NAMESPACE_NAME, + DEFAULT_CR_REPO_NAME, +) +import time + +logger = get_logger(__name__) + + +class VeCR: + def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing"): + self.ak = access_key + self.sk = secret_key + self.region = region + assert region in ["cn-beijing", "cn-guangzhou", "cn-shanghai"] + self.version = "2022-05-12" + + def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): + response = ve_request( + request_body={ + "Name": instance_name, + "ResourceTags": [ + {"Key": "provider", "Value": "veadk"}, + ], + }, + action="CreateRegistry", + ak=self.ak, + sk=self.sk, + service="cr", + version=self.version, + region=self.region, + host=f"cr.{self.region}.volcengineapi.com", + ) + logger.info(f"create cr instance {instance_name}: {response}") + return response + + def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): + response = ve_request( + request_body={ + "Filter": { + "Names": [instance_name], + } + }, + action="ListRegistries", + ak=self.ak, + sk=self.sk, + service="vecr", + version=self.version, + region=self.region, + host=f"cr.{self.region}.volcengineapi.com", + ) + + try: + return response["Result"]["Items"][0]["Status"] + except Exception as e: + raise ValueError(f"cr instance {instance_name} not found: {e}") + + def _create_namespace(self, namespace_name: str = DEFAULT_CR_NAMESPACE_NAME): + pass + + def _create_repo(self, repo_name: str = DEFAULT_CR_REPO_NAME): + pass + + +if __name__ == "__main__": + cr = VeCR("", "") + cr._create_instance() + + while True: + status = cr._check_instance() + if status["Phase"] == "Running": + print("cr instance running") + break + else: + print("cr instance not running") + time.sleep(30) From d8eb59b0742d49b86c687757fb695f3ad0e2372a Mon Sep 17 00:00:00 2001 From: "wangjiaju.716" Date: Fri, 5 Sep 2025 15:16:01 +0800 Subject: [PATCH 3/4] Add ve_cr create_instance, create_namespace, create_repo --- veadk/integrations/ve_cr/ve_cr.py | 158 +++++++++++++++++++++++++----- 1 file changed, 135 insertions(+), 23 deletions(-) diff --git a/veadk/integrations/ve_cr/ve_cr.py b/veadk/integrations/ve_cr/ve_cr.py index 61790ac0..09a57098 100644 --- a/veadk/integrations/ve_cr/ve_cr.py +++ b/veadk/integrations/ve_cr/ve_cr.py @@ -18,7 +18,20 @@ def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing") assert region in ["cn-beijing", "cn-guangzhou", "cn-shanghai"] self.version = "2022-05-12" - def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): + def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME) -> str: + """ + create cr instance + + Args: + instance_name: cr instance name + + Returns: + cr instance name + """ + status = self._check_instance(instance_name) + if status != "NONEXIST": + logger.debug(f"cr instance {instance_name} already running") + return instance_name response = ve_request( request_body={ "Name": instance_name, @@ -34,10 +47,30 @@ def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): region=self.region, host=f"cr.{self.region}.volcengineapi.com", ) - logger.info(f"create cr instance {instance_name}: {response}") - return response + logger.debug(f"create cr instance {instance_name}: {response}") + + while True: + status = self._check_instance(instance_name) + if status == "Running": + break + elif status == "Failed": + raise ValueError(f"cr instance {instance_name} create failed") + else: + logger.debug(f"cr instance status: {status}") + time.sleep(5) + + return instance_name - def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): + def _check_instance(self, instance_name: str) -> str: + """ + check cr instance status + + Args: + instance_name: cr instance name + + Returns: + cr instance status + """ response = ve_request( request_body={ "Filter": { @@ -47,33 +80,112 @@ def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): action="ListRegistries", ak=self.ak, sk=self.sk, - service="vecr", + service="cr", version=self.version, region=self.region, host=f"cr.{self.region}.volcengineapi.com", ) + logger.debug(f"check cr instance {instance_name}: {response}") try: - return response["Result"]["Items"][0]["Status"] - except Exception as e: - raise ValueError(f"cr instance {instance_name} not found: {e}") + if response["Result"]["TotalCount"] == 0: + return "NONEXIST" + return response["Result"]["Items"][0]["Status"]["Phase"] + except Exception as _: + raise ValueError(f"Error check cr instance {instance_name}: {response}") + + def _create_namespace( + self, + instance_name: str = DEFAULT_CR_INSTANCE_NAME, + namespace_name: str = DEFAULT_CR_NAMESPACE_NAME, + ) -> str: + """ + create cr namespace + + Args: + instance_name: cr instance name + namespace_name: cr namespace name + + Returns: + cr namespace name + """ + response = ve_request( + request_body={ + "Name": namespace_name, + "Registry": instance_name, + }, + action="CreateNamespace", + ak=self.ak, + sk=self.sk, + service="cr", + version=self.version, + region=self.region, + host=f"cr.{self.region}.volcengineapi.com", + ) + logger.debug(f"create cr namespace {namespace_name}: {response}") - def _create_namespace(self, namespace_name: str = DEFAULT_CR_NAMESPACE_NAME): - pass + if "Error" in response["ResponseMetadata"]: + error_code = response["ResponseMetadata"]["Error"]["Code"] + error_message = response["ResponseMetadata"]["Error"]["Message"] + if error_code == "AlreadyExists.Namespace": + logger.debug(f"cr namespace {namespace_name} already exists") + return namespace_name + else: + logger.error( + f"Error create cr namespace {namespace_name}: {error_code} {error_message}" + ) + raise ValueError( + f"Error create cr namespace {namespace_name}: {error_code} {error_message}" + ) - def _create_repo(self, repo_name: str = DEFAULT_CR_REPO_NAME): - pass + return namespace_name + def _create_repo( + self, + instance_name: str = DEFAULT_CR_INSTANCE_NAME, + namespace_name: str = DEFAULT_CR_NAMESPACE_NAME, + repo_name: str = DEFAULT_CR_REPO_NAME, + ) -> str: + """ + create cr repo + + Args: + instance_name: cr instance name + namespace_name: cr namespace name + repo_name: cr repo name + + Returns: + cr repo name + """ + response = ve_request( + request_body={ + "Name": repo_name, + "Registry": instance_name, + "Namespace": namespace_name, + "Description": "veadk cr repo", + }, + action="CreateRepository", + ak=self.ak, + sk=self.sk, + service="cr", + version=self.version, + region=self.region, + host=f"cr.{self.region}.volcengineapi.com", + ) + logger.debug(f"create cr repo {repo_name}: {response}") -if __name__ == "__main__": - cr = VeCR("", "") - cr._create_instance() + if "Error" in response["ResponseMetadata"]: + error_code = response["ResponseMetadata"]["Error"]["Code"] + error_message = response["ResponseMetadata"]["Error"]["Message"] + if error_code == "AlreadyExists.Repository": + logger.debug(f"cr repo {repo_name} already exists") + return repo_name + else: + logger.error( + f"Error create cr repo {repo_name}: {error_code} {error_message}" + ) + raise ValueError( + f"Error create cr repo {repo_name}: {error_code} {error_message}" + ) - while True: - status = cr._check_instance() - if status["Phase"] == "Running": - print("cr instance running") - break - else: - print("cr instance not running") - time.sleep(30) + return repo_name From b3f55c4e74445f6c87d2a70777ba0475ce0ab700 Mon Sep 17 00:00:00 2001 From: "wangjiaju.716" Date: Fri, 5 Sep 2025 15:16:01 +0800 Subject: [PATCH 4/4] Add ve_cr create_instance, create_namespace, create_repo --- veadk/integrations/ve_cr/ve_cr.py | 172 ++++++++++++++++++++++++++---- 1 file changed, 149 insertions(+), 23 deletions(-) diff --git a/veadk/integrations/ve_cr/ve_cr.py b/veadk/integrations/ve_cr/ve_cr.py index 61790ac0..44abf582 100644 --- a/veadk/integrations/ve_cr/ve_cr.py +++ b/veadk/integrations/ve_cr/ve_cr.py @@ -1,3 +1,17 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on 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. + from veadk.utils.volcengine_sign import ve_request from veadk.utils.logger import get_logger from veadk.consts import ( @@ -18,7 +32,20 @@ def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing") assert region in ["cn-beijing", "cn-guangzhou", "cn-shanghai"] self.version = "2022-05-12" - def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): + def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME) -> str: + """ + create cr instance + + Args: + instance_name: cr instance name + + Returns: + cr instance name + """ + status = self._check_instance(instance_name) + if status != "NONEXIST": + logger.debug(f"cr instance {instance_name} already running") + return instance_name response = ve_request( request_body={ "Name": instance_name, @@ -34,10 +61,30 @@ def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): region=self.region, host=f"cr.{self.region}.volcengineapi.com", ) - logger.info(f"create cr instance {instance_name}: {response}") - return response + logger.debug(f"create cr instance {instance_name}: {response}") + + while True: + status = self._check_instance(instance_name) + if status == "Running": + break + elif status == "Failed": + raise ValueError(f"cr instance {instance_name} create failed") + else: + logger.debug(f"cr instance status: {status}") + time.sleep(5) + + return instance_name - def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): + def _check_instance(self, instance_name: str) -> str: + """ + check cr instance status + + Args: + instance_name: cr instance name + + Returns: + cr instance status + """ response = ve_request( request_body={ "Filter": { @@ -47,33 +94,112 @@ def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME): action="ListRegistries", ak=self.ak, sk=self.sk, - service="vecr", + service="cr", version=self.version, region=self.region, host=f"cr.{self.region}.volcengineapi.com", ) + logger.debug(f"check cr instance {instance_name}: {response}") try: - return response["Result"]["Items"][0]["Status"] - except Exception as e: - raise ValueError(f"cr instance {instance_name} not found: {e}") + if response["Result"]["TotalCount"] == 0: + return "NONEXIST" + return response["Result"]["Items"][0]["Status"]["Phase"] + except Exception as _: + raise ValueError(f"Error check cr instance {instance_name}: {response}") - def _create_namespace(self, namespace_name: str = DEFAULT_CR_NAMESPACE_NAME): - pass + def _create_namespace( + self, + instance_name: str = DEFAULT_CR_INSTANCE_NAME, + namespace_name: str = DEFAULT_CR_NAMESPACE_NAME, + ) -> str: + """ + create cr namespace - def _create_repo(self, repo_name: str = DEFAULT_CR_REPO_NAME): - pass + Args: + instance_name: cr instance name + namespace_name: cr namespace name + Returns: + cr namespace name + """ + response = ve_request( + request_body={ + "Name": namespace_name, + "Registry": instance_name, + }, + action="CreateNamespace", + ak=self.ak, + sk=self.sk, + service="cr", + version=self.version, + region=self.region, + host=f"cr.{self.region}.volcengineapi.com", + ) + logger.debug(f"create cr namespace {namespace_name}: {response}") + + if "Error" in response["ResponseMetadata"]: + error_code = response["ResponseMetadata"]["Error"]["Code"] + error_message = response["ResponseMetadata"]["Error"]["Message"] + if error_code == "AlreadyExists.Namespace": + logger.debug(f"cr namespace {namespace_name} already exists") + return namespace_name + else: + logger.error( + f"Error create cr namespace {namespace_name}: {error_code} {error_message}" + ) + raise ValueError( + f"Error create cr namespace {namespace_name}: {error_code} {error_message}" + ) + + return namespace_name + + def _create_repo( + self, + instance_name: str = DEFAULT_CR_INSTANCE_NAME, + namespace_name: str = DEFAULT_CR_NAMESPACE_NAME, + repo_name: str = DEFAULT_CR_REPO_NAME, + ) -> str: + """ + create cr repo + + Args: + instance_name: cr instance name + namespace_name: cr namespace name + repo_name: cr repo name + + Returns: + cr repo name + """ + response = ve_request( + request_body={ + "Name": repo_name, + "Registry": instance_name, + "Namespace": namespace_name, + "Description": "veadk cr repo", + }, + action="CreateRepository", + ak=self.ak, + sk=self.sk, + service="cr", + version=self.version, + region=self.region, + host=f"cr.{self.region}.volcengineapi.com", + ) + logger.debug(f"create cr repo {repo_name}: {response}") -if __name__ == "__main__": - cr = VeCR("", "") - cr._create_instance() + if "Error" in response["ResponseMetadata"]: + error_code = response["ResponseMetadata"]["Error"]["Code"] + error_message = response["ResponseMetadata"]["Error"]["Message"] + if error_code == "AlreadyExists.Repository": + logger.debug(f"cr repo {repo_name} already exists") + return repo_name + else: + logger.error( + f"Error create cr repo {repo_name}: {error_code} {error_message}" + ) + raise ValueError( + f"Error create cr repo {repo_name}: {error_code} {error_message}" + ) - while True: - status = cr._check_instance() - if status["Phase"] == "Running": - print("cr instance running") - break - else: - print("cr instance not running") - time.sleep(30) + return repo_name