-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnginx_ingress_config.rs
143 lines (123 loc) · 3.18 KB
/
nginx_ingress_config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::{fs::OpenOptions, io::Write, net::Ipv4Addr, process::Command};
const CONFIG_HEADER: &str = r#"
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
"#;
const CONFIG_BASE: &str = r#"
http {
SERVERS
server {
listen 80 default_server;
server_name _;
# Everything is a 404
location / {
return 404;
}
}
}
"#;
const SERVER_BASE: &str = r#"
server {
server_name SERVER_NAME;
listen 80;
LOCATIONS
}
"#;
const LOCATION_BASE: &str = r#"
location PATH {
proxy_pass http://IP:PORT/;
}
"#;
pub struct NginxIngressConfig {
servers: Vec<IngressHost>,
}
pub struct IngressHost {
host: String,
paths: Vec<IngressPath>,
}
pub struct IngressPath {
path: String,
ip: Ipv4Addr,
port: u16,
}
impl NginxIngressConfig {
pub fn new() -> Self {
Self {
servers: vec![],
}
}
pub fn add_host(&mut self, host: IngressHost) {
self.servers.push(host);
}
pub fn flush(&self) {
if self.servers.is_empty() {
return;
}
let config = self.to_string();
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open("/etc/nginx/nginx.conf")
.unwrap();
file.write_all(config.as_bytes()).unwrap();
Command::new("nginx")
.args(["-s", "reload"])
.output()
.expect(
"Failed to reload nginx configuration, please ensure nginx is installed and running",
);
}
}
impl IngressHost {
pub fn new(host: &String) -> Self {
Self {
host: host.to_owned(),
paths: vec![],
}
}
pub fn add_path(&mut self, path: &String, svc_ip: &Ipv4Addr, svc_port: &u16) {
self.paths.push(IngressPath {
path: path.to_owned(),
ip: svc_ip.to_owned(),
port: svc_port.to_owned(),
});
}
}
impl ToString for NginxIngressConfig {
fn to_string(&self) -> String {
let servers = self.servers.iter().fold(String::new(), |acc, server| {
acc + server.to_string().as_str()
});
let out = CONFIG_BASE.to_string().replace("SERVERS", servers.as_str());
CONFIG_HEADER.to_string()
+ nginx_config::parse_main(out.as_str())
.unwrap()
.to_string()
.as_str() // format
}
}
impl ToString for IngressHost {
fn to_string(&self) -> String {
let out = SERVER_BASE.to_string();
let out = out.replace("SERVER_NAME", &self.host);
let locations = self
.paths
.iter()
.fold(String::new(), |acc, path| acc + path.to_string().as_str());
out.replace("LOCATIONS", &locations)
}
}
impl ToString for IngressPath {
fn to_string(&self) -> String {
let out = LOCATION_BASE.to_string();
let out = out.replace("PATH", self.path.as_str());
let out = out.replace("IP", self.ip.to_string().as_str());
out.replace("PORT", self.port.to_string().as_str())
}
}