Skip to content

Commit 9e9902d

Browse files
doraemonlovewangjiaju.716
andauthored
fix(vecr): fix/verc (#140)
* Add veadk-version in requirements.txt * Add ve_cr * Add ve_cr create_instance, create_namespace, create_repo * Add ve_cr create_instance, create_namespace, create_repo --------- Co-authored-by: wangjiaju.716 <[email protected]>
1 parent a83441f commit 9e9902d

File tree

5 files changed

+224
-2
lines changed

5 files changed

+224
-2
lines changed

veadk/cli/cli_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from typing import Any
1717

1818
import click
19-
2019
from veadk.version import VERSION
2120

21+
2222
warnings.filterwarnings(
2323
"ignore", category=UserWarning, module="pydantic._internal._fields"
2424
)

veadk/consts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@
4343

4444
DEFAULT_TLS_OTEL_EXPORTER_ENDPOINT = "https://tls-cn-beijing.volces.com:4318/v1/traces"
4545
DEFAULT_TLS_OTEL_EXPORTER_REGION = "cn-beijing"
46+
47+
DEFAULT_CR_INSTANCE_NAME = "veadk-user-instance"
48+
DEFAULT_CR_NAMESPACE_NAME = "veadk-user-namespace"
49+
DEFAULT_CR_REPO_NAME = "veadk-user-repo"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

veadk/integrations/ve_cr/ve_cr.py

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from veadk.utils.volcengine_sign import ve_request
16+
from veadk.utils.logger import get_logger
17+
from veadk.consts import (
18+
DEFAULT_CR_INSTANCE_NAME,
19+
DEFAULT_CR_NAMESPACE_NAME,
20+
DEFAULT_CR_REPO_NAME,
21+
)
22+
import time
23+
24+
logger = get_logger(__name__)
25+
26+
27+
class VeCR:
28+
def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing"):
29+
self.ak = access_key
30+
self.sk = secret_key
31+
self.region = region
32+
assert region in ["cn-beijing", "cn-guangzhou", "cn-shanghai"]
33+
self.version = "2022-05-12"
34+
35+
def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME) -> str:
36+
"""
37+
create cr instance
38+
39+
Args:
40+
instance_name: cr instance name
41+
42+
Returns:
43+
cr instance name
44+
"""
45+
status = self._check_instance(instance_name)
46+
if status != "NONEXIST":
47+
logger.debug(f"cr instance {instance_name} already running")
48+
return instance_name
49+
response = ve_request(
50+
request_body={
51+
"Name": instance_name,
52+
"ResourceTags": [
53+
{"Key": "provider", "Value": "veadk"},
54+
],
55+
},
56+
action="CreateRegistry",
57+
ak=self.ak,
58+
sk=self.sk,
59+
service="cr",
60+
version=self.version,
61+
region=self.region,
62+
host=f"cr.{self.region}.volcengineapi.com",
63+
)
64+
logger.debug(f"create cr instance {instance_name}: {response}")
65+
66+
while True:
67+
status = self._check_instance(instance_name)
68+
if status == "Running":
69+
break
70+
elif status == "Failed":
71+
raise ValueError(f"cr instance {instance_name} create failed")
72+
else:
73+
logger.debug(f"cr instance status: {status}")
74+
time.sleep(5)
75+
76+
return instance_name
77+
78+
def _check_instance(self, instance_name: str) -> str:
79+
"""
80+
check cr instance status
81+
82+
Args:
83+
instance_name: cr instance name
84+
85+
Returns:
86+
cr instance status
87+
"""
88+
response = ve_request(
89+
request_body={
90+
"Filter": {
91+
"Names": [instance_name],
92+
}
93+
},
94+
action="ListRegistries",
95+
ak=self.ak,
96+
sk=self.sk,
97+
service="cr",
98+
version=self.version,
99+
region=self.region,
100+
host=f"cr.{self.region}.volcengineapi.com",
101+
)
102+
logger.debug(f"check cr instance {instance_name}: {response}")
103+
104+
try:
105+
if response["Result"]["TotalCount"] == 0:
106+
return "NONEXIST"
107+
return response["Result"]["Items"][0]["Status"]["Phase"]
108+
except Exception as _:
109+
raise ValueError(f"Error check cr instance {instance_name}: {response}")
110+
111+
def _create_namespace(
112+
self,
113+
instance_name: str = DEFAULT_CR_INSTANCE_NAME,
114+
namespace_name: str = DEFAULT_CR_NAMESPACE_NAME,
115+
) -> str:
116+
"""
117+
create cr namespace
118+
119+
Args:
120+
instance_name: cr instance name
121+
namespace_name: cr namespace name
122+
123+
Returns:
124+
cr namespace name
125+
"""
126+
response = ve_request(
127+
request_body={
128+
"Name": namespace_name,
129+
"Registry": instance_name,
130+
},
131+
action="CreateNamespace",
132+
ak=self.ak,
133+
sk=self.sk,
134+
service="cr",
135+
version=self.version,
136+
region=self.region,
137+
host=f"cr.{self.region}.volcengineapi.com",
138+
)
139+
logger.debug(f"create cr namespace {namespace_name}: {response}")
140+
141+
if "Error" in response["ResponseMetadata"]:
142+
error_code = response["ResponseMetadata"]["Error"]["Code"]
143+
error_message = response["ResponseMetadata"]["Error"]["Message"]
144+
if error_code == "AlreadyExists.Namespace":
145+
logger.debug(f"cr namespace {namespace_name} already exists")
146+
return namespace_name
147+
else:
148+
logger.error(
149+
f"Error create cr namespace {namespace_name}: {error_code} {error_message}"
150+
)
151+
raise ValueError(
152+
f"Error create cr namespace {namespace_name}: {error_code} {error_message}"
153+
)
154+
155+
return namespace_name
156+
157+
def _create_repo(
158+
self,
159+
instance_name: str = DEFAULT_CR_INSTANCE_NAME,
160+
namespace_name: str = DEFAULT_CR_NAMESPACE_NAME,
161+
repo_name: str = DEFAULT_CR_REPO_NAME,
162+
) -> str:
163+
"""
164+
create cr repo
165+
166+
Args:
167+
instance_name: cr instance name
168+
namespace_name: cr namespace name
169+
repo_name: cr repo name
170+
171+
Returns:
172+
cr repo name
173+
"""
174+
response = ve_request(
175+
request_body={
176+
"Name": repo_name,
177+
"Registry": instance_name,
178+
"Namespace": namespace_name,
179+
"Description": "veadk cr repo",
180+
},
181+
action="CreateRepository",
182+
ak=self.ak,
183+
sk=self.sk,
184+
service="cr",
185+
version=self.version,
186+
region=self.region,
187+
host=f"cr.{self.region}.volcengineapi.com",
188+
)
189+
logger.debug(f"create cr repo {repo_name}: {response}")
190+
191+
if "Error" in response["ResponseMetadata"]:
192+
error_code = response["ResponseMetadata"]["Error"]["Code"]
193+
error_message = response["ResponseMetadata"]["Error"]["Message"]
194+
if error_code == "AlreadyExists.Repository":
195+
logger.debug(f"cr repo {repo_name} already exists")
196+
return repo_name
197+
else:
198+
logger.error(
199+
f"Error create cr repo {repo_name}: {error_code} {error_message}"
200+
)
201+
raise ValueError(
202+
f"Error create cr repo {repo_name}: {error_code} {error_message}"
203+
)
204+
205+
return repo_name
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
veadk-python=={{ cookiecutter.veadk_version }}
22
fastapi
3-
uvicorn[standard]
3+
uvicorn[standard]

0 commit comments

Comments
 (0)