12
12
default_kong_upstream_template = """
13
13
{
14
14
"name": "$name",
15
- "tags": ["discovery-syncer-auto"]
15
+ "tags": ["discovery-syncer-python- auto"]
16
16
}
17
17
"""
18
18
19
19
default_kong_target_template = """
20
20
{
21
21
"target": "$ip:$port",
22
22
"weight": $weight,
23
- "tags": ["discovery-syncer-auto"]
23
+ "tags": ["discovery-syncer-python- auto"]
24
24
}
25
25
"""
26
26
@@ -33,7 +33,7 @@ def __init__(self, config):
33
33
def get_service_all_instances (self , target : dict , upstream_name : str = None ) -> List [Instance ]:
34
34
# https://docs.konghq.com/gateway/api/admin-oss/latest/
35
35
upstream_name = self .get_upstream_name (target , upstream_name )
36
- uri = f"{ upstream_name } { self ._config .config .get ('targets_uri' , '/targets' )} "
36
+ uri = f"upstreams/ { upstream_name } { self ._config .config .get ('targets_uri' , '/targets' )} "
37
37
resp = self .kong_execute ("GET" , uri , {})
38
38
if resp .status_code == 404 :
39
39
return []
@@ -51,37 +51,42 @@ def sync_instances(self, target: dict, upstream_name: str, diff_ins: list, insta
51
51
return
52
52
if upstream_name not in self .service_name_map :
53
53
tpl = Template (target .get ("config" ).get ("template" , default_kong_upstream_template ))
54
- resp = self .kong_execute ("POST " , "" , {}, tpl .substitute (name = upstream_name ))
54
+ self .kong_execute ("PUT " , "upstreams " , {}, tpl .substitute (name = upstream_name ))
55
55
self .service_name_map [upstream_name ] = True
56
56
57
57
tpl = Template (default_kong_target_template )
58
58
for instance in diff_ins :
59
- target_uri = f"{ upstream_name } /targets/"
59
+ target_uri = f"upstreams/ { upstream_name } /targets/"
60
60
method = "POST"
61
61
data = None
62
62
if instance .enabled :
63
63
data = tpl .substitute (ip = instance .ip , port = instance .port , weight = instance .weight )
64
64
else :
65
65
target_uri = f"{ target_uri } /{ instance .ip } :{ instance .port } "
66
66
method = "DELETE"
67
- self .kong_execute (method , target_uri , {}, data )
67
+ resp = self .kong_execute (method , target_uri , {}, data )
68
+ if instance .enabled and resp .status_code == 409 :
69
+ target_uri = f"{ target_uri } /{ instance .ip } :{ instance .port } "
70
+ method = "PUT"
71
+ self .kong_execute (method , target_uri , {}, data )
68
72
69
73
def fetch_admin_api_to_file (self , file_name : str ) -> Tuple [str , str ]:
70
74
# https://docs.konghq.com/gateway/3.6.x/production/deployment-topologies/db-less-and-declarative-config/
75
+ # https://docs.konghq.com/deck/1.38.x/guides/backup-restore/
71
76
raise Exception ("Unrealized" )
72
77
73
78
def migrate_to (self , target_gateway : 'Gateway' ):
79
+ # https://docs.konghq.com/deck/1.38.x/guides/backup-restore/
74
80
raise Exception ("Unrealized" )
75
81
76
82
def restore_gateway (self , body : str ) -> str :
83
+ # https://docs.konghq.com/deck/1.38.x/guides/backup-restore/
77
84
raise Exception ("Unrealized" )
78
85
79
86
def kong_execute (self , method , uri , params , data = None ):
80
87
resp = httpx .request (method , f"{ self ._config .admin_url } { self ._config .prefix } { uri } " ,
81
88
params = params , data = data , timeout = 10 ,
82
- headers = {"X-API-KEY" : self ._config .config .get ("X-API-KEY" ),
83
- "Content-Type" : "application/json" ,
84
- "Accept" : "application/json" })
89
+ headers = {"Content-Type" : "application/json" , "Accept" : "application/json" })
85
90
86
91
logger .info (
87
92
f"请求 kong 接口, method: { method } , url: { self ._config .admin_url } { self ._config .prefix } { uri } , 请求参数: { params } , 请求数据: { data } , 响应结果: { resp .text } " )
0 commit comments