-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontroller.rs
221 lines (198 loc) · 7.21 KB
/
controller.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::{
fs::{self, File},
io::{ErrorKind, Write},
process::{Command, Stdio},
};
use anyhow::{anyhow, Context, Error, Result};
use fs_extra::dir::{copy, CopyOptions};
use reqwest::Client;
use resources::{
models::Response,
objects::{
function::Function, hpa::HorizontalPodAutoscaler, replica_set::ReplicaSet, KubeObject,
Object,
},
};
use tokio::{
select,
sync::{mpsc, mpsc::Receiver},
task::JoinHandle,
};
use crate::{
utils::{create_informer, log_command, Event, ResyncNotification},
CONFIG, DOCKER_REGISTRY, TEMPLATES_DIR, TMP_DIR,
};
pub struct FunctionController {
client: Client,
func_rx: Receiver<Event<Function>>,
func_resync_rx: Receiver<ResyncNotification>,
func_informer: Option<JoinHandle<Result<(), Error>>>,
}
impl FunctionController {
pub fn new() -> Self {
let (func_tx, func_rx) = mpsc::channel::<Event<Function>>(16);
let (func_resync_tx, func_resync_rx) = mpsc::channel::<ResyncNotification>(16);
let func_informer =
create_informer::<Function>("functions".to_string(), func_tx, func_resync_tx);
let func_informer = tokio::spawn(async move { func_informer.run().await });
Self {
client: Client::new(),
func_rx,
func_resync_rx,
func_informer: Some(func_informer),
}
}
pub async fn run(&mut self) -> Result<()> {
tracing::info!("Function Controller started");
loop {
select! {
Some(event) = self.func_rx.recv() => {
let result = match event {
Event::Add(func) => self.handle_function_add(func).await,
_ => {Ok(())}
};
if let Err(e) = result {
tracing::error!("Error while processing Function update: {:#}", e);
}
},
_ = self.func_resync_rx.recv() => {}
else => break
}
}
let func_informer = std::mem::replace(&mut self.func_informer, None);
func_informer.unwrap().await??;
tracing::info!("Function Controller exited");
Ok(())
}
async fn handle_function_add(&mut self, mut func: Function) -> Result<()> {
tracing::info!("New function: {}", func.metadata.name);
let image_name = self.build_function_image(&mut func).await?;
func.status.as_mut().unwrap().image = Some(image_name);
self.post_status(&func).await?;
self.create_replica_set(&func).await?;
self.create_hpa(&func).await?;
Ok(())
}
async fn build_function_image(&self, func: &mut Function) -> Result<String> {
self.prepare_file(func).await?;
let image_name = self.build_image(func).await?;
Ok(image_name)
}
async fn prepare_file(&self, func: &Function) -> Result<()> {
let func_dir_path = std::path::Path::new(TMP_DIR).join(func.name());
self.fetch_code(func).await?;
// unzip
Command::new("unzip")
.current_dir(&func_dir_path)
.args(["-o", func.status.as_ref().unwrap().filename.as_str()])
.output()?;
// copy templates
let mut copy_option = CopyOptions::new();
copy_option.content_only = true;
copy_option.overwrite = true;
copy(TEMPLATES_DIR, func_dir_path, ©_option)?;
Ok(())
}
async fn fetch_code(&self, func: &Function) -> Result<()> {
let client = reqwest::Client::new();
let filename = func.status.as_ref().unwrap().filename.to_owned();
let response = client
.get(format!("{}/api/v1/tmp/{}", CONFIG.api_server_url, filename))
.send()
.await?;
if let Err(err) = response.error_for_status_ref() {
return Err(anyhow!(
"Error get file {}. status: {}",
filename,
err.status().unwrap_or_default()
));
}
let func_dir_path = std::path::Path::new(TMP_DIR).join(func.name());
fs::create_dir_all(&func_dir_path)?;
let code_path = func_dir_path.join(filename);
let mut file = File::create(code_path)?;
let content = response.bytes().await?;
file.write_all(content.as_ref())?;
Ok(())
}
async fn build_image(&self, func: &Function) -> Result<String> {
let tag = format!("{}/{}:latest", DOCKER_REGISTRY, func.name());
tracing::info!("building image: {}", tag);
let func_dir_path = std::path::Path::new(TMP_DIR).join(func.name());
let output = Command::new("docker")
.current_dir(&func_dir_path)
.args(["build", "-t", tag.as_str(), "."])
.stdout(Stdio::piped())
.spawn()?
.stdout
.ok_or_else(|| {
std::io::Error::new(ErrorKind::Other, "Could not capture standard output.")
})?;
log_command(output);
let output = Command::new("docker")
.current_dir(&func_dir_path)
.args(["push", tag.as_str()])
.stdout(Stdio::piped())
.spawn()?
.stdout
.ok_or_else(|| {
std::io::Error::new(ErrorKind::Other, "Could not capture standard output.")
})?;
log_command(output);
Ok(tag)
}
async fn post_status(&self, func: &Function) -> Result<()> {
let name = func.metadata.name.to_owned();
let response = self
.client
.put(format!("{}{}", CONFIG.api_server_url, func.uri()))
.json(&KubeObject::Function(func.to_owned()))
.send()
.await?
.json::<Response<()>>()
.await
.with_context(|| "Error posting status")?;
tracing::info!(
"Posted status for Function {}: {}",
name,
response.msg.unwrap_or_else(|| "".to_string())
);
Ok(())
}
async fn create_replica_set(&self, func: &Function) -> Result<()> {
let rs = ReplicaSet::from_function(func);
let response = self
.client
.post(format!("{}{}", CONFIG.api_server_url, rs.prefix()))
.json(&KubeObject::ReplicaSet(rs))
.send()
.await?
.json::<Response<()>>()
.await
.with_context(|| "Error creating replicaset")?;
tracing::info!(
"Created replicaset for Function {}: {}",
func.metadata.name,
response.msg.unwrap_or_else(|| "".to_string())
);
Ok(())
}
async fn create_hpa(&self, func: &Function) -> Result<()> {
let hpa = HorizontalPodAutoscaler::from_function(func);
let response = self
.client
.post(format!("{}{}", CONFIG.api_server_url, hpa.prefix()))
.json(&KubeObject::HorizontalPodAutoscaler(hpa))
.send()
.await?
.json::<Response<()>>()
.await
.with_context(|| "Error creating HPA")?;
tracing::info!(
"Created HPA for Function {}: {}",
func.metadata.name,
response.msg.unwrap_or_else(|| "".to_string())
);
Ok(())
}
}