@@ -29,13 +29,53 @@ local setmetatable = setmetatable
29
29
local string = string
30
30
local tonumber = tonumber
31
31
local ngx_config_prefix = ngx .config .prefix ()
32
+ local ngx_socket_tcp = ngx .socket .tcp
32
33
33
34
34
35
local is_http = ngx .config .subsystem == " http"
35
36
local _M = {}
36
37
37
38
38
- -- this function create the etcd client instance used in the Admin API
39
+ local function has_mtls_support ()
40
+ local s = ngx_socket_tcp ()
41
+ return s .tlshandshake ~= nil
42
+ end
43
+
44
+
45
+ local function _new (etcd_conf )
46
+ local prefix = etcd_conf .prefix
47
+ etcd_conf .http_host = etcd_conf .host
48
+ etcd_conf .host = nil
49
+ etcd_conf .prefix = nil
50
+ etcd_conf .protocol = " v3"
51
+ etcd_conf .api_prefix = " /v3"
52
+
53
+ -- default to verify etcd cluster certificate
54
+ etcd_conf .ssl_verify = true
55
+ if etcd_conf .tls then
56
+ if etcd_conf .tls .verify == false then
57
+ etcd_conf .ssl_verify = false
58
+ end
59
+
60
+ if etcd_conf .tls .cert then
61
+ etcd_conf .ssl_cert_path = etcd_conf .tls .cert
62
+ etcd_conf .ssl_key_path = etcd_conf .tls .key
63
+ end
64
+
65
+ if etcd_conf .tls .sni then
66
+ etcd_conf .sni = etcd_conf .tls .sni
67
+ end
68
+ end
69
+
70
+ local etcd_cli , err = etcd .new (etcd_conf )
71
+ if not etcd_cli then
72
+ return nil , nil , err
73
+ end
74
+
75
+ return etcd_cli , prefix
76
+ end
77
+
78
+
39
79
local function new ()
40
80
local local_conf , err = fetch_local_conf ()
41
81
if not local_conf then
@@ -60,32 +100,20 @@ local function new()
60
100
proxy_by_conf_server = true
61
101
62
102
elseif local_conf .deployment .role == " control_plane" then
63
- -- TODO: add the proxy conf in control_plane
64
- proxy_by_conf_server = true
65
- end
66
- end
67
-
68
- local prefix = etcd_conf .prefix
69
- etcd_conf .http_host = etcd_conf .host
70
- etcd_conf .host = nil
71
- etcd_conf .prefix = nil
72
- etcd_conf .protocol = " v3"
73
- etcd_conf .api_prefix = " /v3"
74
-
75
- -- default to verify etcd cluster certificate
76
- etcd_conf .ssl_verify = true
77
- if etcd_conf .tls then
78
- if etcd_conf .tls .verify == false then
79
- etcd_conf .ssl_verify = false
80
- end
103
+ local addr = local_conf .deployment .role_control_plane .conf_server .listen
104
+ etcd_conf .host = {" https://" .. addr }
105
+ etcd_conf .tls = {
106
+ verify = false ,
107
+ }
81
108
82
- if etcd_conf .tls .cert then
83
- etcd_conf .ssl_cert_path = etcd_conf .tls .cert
84
- etcd_conf .ssl_key_path = etcd_conf .tls .key
85
- end
109
+ if has_mtls_support () and local_conf .deployment .certs .cert then
110
+ local cert = local_conf .deployment .certs .cert
111
+ local cert_key = local_conf .deployment .certs .cert_key
112
+ etcd_conf .tls .cert = cert
113
+ etcd_conf .tls .key = cert_key
114
+ end
86
115
87
- if etcd_conf .tls .sni then
88
- etcd_conf .sni = etcd_conf .tls .sni
116
+ proxy_by_conf_server = true
89
117
end
90
118
end
91
119
@@ -102,15 +130,28 @@ local function new()
102
130
})
103
131
end
104
132
105
- local etcd_cli
106
- etcd_cli , err = etcd .new (etcd_conf )
107
- if not etcd_cli then
133
+ return _new (etcd_conf )
134
+ end
135
+ _M .new = new
136
+
137
+
138
+ ---
139
+ -- Create an etcd client which will connect to etcd without being proxyed by conf server.
140
+ -- This method is used in init_worker phase when the conf server is not ready.
141
+ --
142
+ -- @function core.etcd.new_without_proxy
143
+ -- @treturn table|nil the etcd client, or nil if failed.
144
+ -- @treturn string|nil the configured prefix of etcd keys, or nil if failed.
145
+ -- @treturn nil|string the error message.
146
+ function _M .new_without_proxy ()
147
+ local local_conf , err = fetch_local_conf ()
148
+ if not local_conf then
108
149
return nil , nil , err
109
150
end
110
151
111
- return etcd_cli , prefix
152
+ local etcd_conf = clone_tab (local_conf .etcd )
153
+ return _new (etcd_conf )
112
154
end
113
- _M .new = new
114
155
115
156
116
157
-- convert ETCD v3 entry to v2 one
0 commit comments