@@ -87,7 +87,7 @@ def get_json_data(self):
8787 return None
8888
8989 def update_haproxy_data (self , namespace , current_haproxy , front_ip , front_port , backend_ip , backend_port ,
90- address_type , module_name ):
90+ address_type , module_name , real_ip_mode ):
9191 # 将当前配置分割成行
9292 lines = current_haproxy .splitlines ()
9393 updated_lines = []
@@ -123,6 +123,7 @@ def update_haproxy_data(self, namespace, current_haproxy, front_ip, front_port,
123123
124124 # 添加新配置到文件末尾
125125 logger .info (f'在文件末尾添加新的配置' )
126+ send_proxy = " send-proxy" if real_ip_mode == "proxy_protocol" else ""
126127 if address_type == "management" :
127128 if front_ip is None :
128129 front_ip = '{{.ApisvrFrontVIP}}'
@@ -138,7 +139,7 @@ def update_haproxy_data(self, namespace, current_haproxy, front_ip, front_port,
138139 f" default-server inter 2s downinter 5s rise 2 fall 2 slowstart 60s maxconn 2000 maxqueue"
139140 f" 200 weight 100" ,
140141 f" balance roundrobin" ,
141- f" server app0 { backend_ip } :{ backend_port } " ,
142+ f" server app0 { backend_ip } :{ backend_port } { send_proxy } " ,
142143 f" mode tcp" ,
143144 f"{ section_end } " ,
144145 ])
@@ -157,7 +158,7 @@ def update_haproxy_data(self, namespace, current_haproxy, front_ip, front_port,
157158 f" default-server inter 2s downinter 5s rise 2 fall 2 slowstart 60s maxconn 2000 maxqueue"
158159 f" 200 weight 100" ,
159160 f" balance roundrobin" ,
160- f" server app0 { backend_ip } :{ backend_port } " ,
161+ f" server app0 { backend_ip } :{ backend_port } { send_proxy } " ,
161162 f" mode tcp" ,
162163 f"{ section_end } " ,
163164 ])
@@ -166,7 +167,8 @@ def update_haproxy_data(self, namespace, current_haproxy, front_ip, front_port,
166167 new_haproxy_content = '\n ' .join (updated_lines )
167168 return new_haproxy_content
168169
169- def update (self , namespace , front_ip , front_port , backend_ip , backend_port , address_type , module_name ):
170+ def update (self , namespace , front_ip , front_port , backend_ip , backend_port , address_type , module_name ,
171+ real_ip_mode ):
170172 if not self .dump ():
171173 logger .error ("dump cluster info failed." )
172174 return False
@@ -179,7 +181,7 @@ def update(self, namespace, front_ip, front_port, backend_ip, backend_port, addr
179181
180182 # 更新 haproxy 配置数据
181183 new_haproxy_content = self .update_haproxy_data (namespace , current_haproxy , front_ip , front_port , backend_ip ,
182- backend_port , address_type , module_name )
184+ backend_port , address_type , module_name , real_ip_mode )
183185
184186 # 更新配置数据
185187 config_data ['data' ]['haproxy' ] = new_haproxy_content
@@ -273,6 +275,8 @@ def parse_args():
273275 parser_obj .add_argument ('-a' , '--address-type' , dest = "address_type" , default = "management" , type = str ,
274276 help = 'use management id or business ip' )
275277 parser_obj .add_argument ('-m' , '--module' , required = False , default = "datamate" , type = str , help = 'module name' )
278+ parser_obj .add_argument ('--real-ip-mode' , required = False , default = "off" , type = str ,
279+ help = 'enable forwarding real ip' )
276280
277281 return parser .parse_args ()
278282
@@ -282,7 +286,7 @@ def parse_args():
282286 operator = ClusterInfoOperator ()
283287 if args .command == 'update' :
284288 operator .update (args .namespace , args .frontend_ip , args .frontend_port , args .backend_ip , args .backend_port ,
285- address_type = args .address_type , module_name = args .module )
289+ address_type = args .address_type , module_name = args .module , real_ip_mode = args . real_ip_mode )
286290 operator .clear ()
287291 else :
288292 print ("Illegal command!" )
0 commit comments