Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Bk Bcs Saas
Commits
7de07fde
Unverified
Commit
7de07fde
authored
3 years ago
by
schnee
Committed by
GitHub
3 years ago
Browse files
Options
Download
Plain Diff
Merge pull request #1320 from dellkeji/add_cache_validate
feat: invalidate cluster context cache
parents
150ea3e9
6abbcedf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bcs-app/backend/components/bcs/k8s_client.py
+17
-5
bcs-app/backend/components/bcs/k8s_client.py
with
17 additions
and
5 deletions
+17
-5
bcs-app/backend/components/bcs/k8s_client.py
+
17
-
5
View file @
7de07fde
...
...
@@ -12,6 +12,7 @@ Unless required by applicable law or agreed to in writing, software distributed
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.
"""
import
logging
from
typing
import
Dict
from
cachetools
import
LRUCache
,
cached
...
...
@@ -23,9 +24,14 @@ from kubernetes import client
from
backend.components.bcs
import
BCSClientBase
,
resources
from
backend.components.utils
import
http_get
logger
=
logging
.
getLogger
(
__name__
)
# 获取cluster context使用的缓存策略
cluster_context_cache_policy
=
LRUCache
(
maxsize
=
128
)
@
cached
(
cache
=
LRUCache
(
maxsize
=
128
)
,
cache
=
cluster_context_cache_policy
,
key
=
lambda
url_prefix
,
access_token
,
project_id
,
cluster_id
:
hashkey
(
url_prefix
,
project_id
,
cluster_id
),
)
def
make_cluster_context
(
url_prefix
:
str
,
access_token
:
str
,
project_id
:
str
,
cluster_id
:
str
)
->
Dict
:
...
...
@@ -55,12 +61,18 @@ class K8SAPIClient(BCSClientBase):
@
cached_property
def
api_client
(
self
):
context
=
make_cluster_context
(
self
.
rest_host
,
self
.
access_token
,
self
.
project_id
,
self
.
cluster_id
)
configure
=
client
.
Configuration
()
configure
.
verify_ssl
=
False
configure
.
host
=
f
"
{
self
.
_bcs_server_host
}{
context
[
'server_address_path'
]
}
"
.
rstrip
(
"/"
)
configure
.
api_key
=
{
"authorization"
:
f
"Bearer
{
context
[
'user_token'
]
}
"
}
# 获取集群context,如果调用接口或者其它异常导致失败,需要主动使缓存失效
try
:
context
=
make_cluster_context
(
self
.
rest_host
,
self
.
access_token
,
self
.
project_id
,
self
.
cluster_id
)
configure
.
host
=
f
"
{
self
.
_bcs_server_host
}{
context
[
'server_address_path'
]
}
"
.
rstrip
(
"/"
)
configure
.
api_key
=
{
"authorization"
:
f
"Bearer
{
context
[
'user_token'
]
}
"
}
except
Exception
as
e
:
logger
.
exception
(
"make cluster context error, %s"
,
e
)
# 当出现异常时,需要清空缓存
cluster_context_cache_policy
.
clear
()
raise
api_client
=
client
.
ApiClient
(
configure
)
return
api_client
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help