-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
203 lines (171 loc) · 6.72 KB
/
Copy pathlib.rs
File metadata and controls
203 lines (171 loc) · 6.72 KB
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
#![deny(rustdoc::broken_intra_doc_links, rustdoc::bare_urls, rust_2018_idioms)]
const FINALIZER: &str = "sinker.influxdata.io/target";
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Kube Error: {0}")]
KubeError(#[from] kube::Error),
#[error("Parsing apiVersion and Kind: {0}")]
ParseGroupVersionError(#[from] kube::core::gvk::ParseGroupVersionError),
#[error("Error parsing kubeconfig from secret")]
KubeconfigError(#[from] kube::config::KubeconfigError),
#[error("error parsing kubeconfig from secret")]
KubeconfigUtf8Error(#[source] std::str::Utf8Error),
#[error("referenced key '{0}' does not exist in secret '{0}' in namespace '{0}'")]
MissingKeyError(String, String, String),
#[error("SerializationError: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("JsonPathError: {0}")]
JsonPathError(#[from] serde_json_path::ParseError),
#[error("Name is required")]
NameRequired,
#[error("UID is required")]
UIDRequired,
#[error("Namespace is required")]
NamespaceRequired,
#[error("Failed to acquire ResourceVersion")]
ResourceVersionRequired,
#[error(transparent)]
AddToPathError(#[from] mapping::AddToPathError),
#[error("JSONPath '{0}' produced no values")]
JsonPathNoValues(String),
#[error("JSONPath '{0}' didn't produce exactly one value")]
JsonPathExactlyOneValue(String),
#[error("Expected k8s resource at subtree: {0}")]
MalformedInnerResource(String),
#[error("Mapping block must contain from_field_path, to_field_path or both, cannot be empty")]
MappingEmpty,
#[error("Resource {0} of kind {1} not found: {2}")]
ResourceNotFoundError(String, String, kube::Error),
#[error("Referenced Kubeconfig Secret cannot be accessed due to namespace restrictions")]
UnauthorizedKubeconfigAccess(),
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Expose all controller components used by main
pub mod controller;
mod filters;
mod mapping;
pub mod metrics;
mod remote_watcher;
mod remote_watcher_manager;
mod resource_extensions;
pub mod resources;
mod util;
#[cfg(test)]
mod test_support {
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use http::{Request, Response};
use kube::client::Body;
use kube::Client;
use serde_json::{json, Value};
use crate::resources::{ClusterResourceRef, ResourceSync, ResourceSyncSpec, GVKN};
/// A finite, in-memory API script. Unexpected requests fail and are recorded, and
/// `finish` checks that every scripted response was consumed.
pub struct MockApi {
pub client: Client,
responses: Arc<Mutex<VecDeque<Response<Body>>>>,
requests: Arc<Mutex<Vec<Request<Value>>>>,
}
impl MockApi {
pub fn new(responses: Vec<Response<Body>>) -> Self {
let responses = Arc::new(Mutex::new(VecDeque::from(responses)));
let requests = Arc::new(Mutex::new(Vec::new()));
let service = tower::service_fn({
let responses = Arc::clone(&responses);
let requests = Arc::clone(&requests);
move |request: Request<Body>| {
let responses = Arc::clone(&responses);
let requests = Arc::clone(&requests);
async move {
let (parts, body) = request.into_parts();
let bytes = body.collect_bytes().await?;
let body = if bytes.is_empty() {
Value::Null
} else {
serde_json::from_slice(&bytes).map_err(kube::Error::SerdeError)?
};
requests
.lock()
.expect("request lock poisoned")
.push(Request::from_parts(parts, body));
responses
.lock()
.expect("response lock poisoned")
.pop_front()
.ok_or_else(|| kube::Error::Service("unexpected API request".into()))
}
}
});
Self {
client: Client::new(service, "client-default"),
responses,
requests,
}
}
pub fn finish(self, expected: &[(&str, &str)]) -> Vec<Request<Value>> {
assert!(self
.responses
.lock()
.expect("response lock poisoned")
.is_empty());
let requests =
std::mem::take(&mut *self.requests.lock().expect("request lock poisoned"));
let actual: Vec<_> = requests
.iter()
.map(|request| (request.method().as_str(), request.uri().path()))
.collect();
assert_eq!(actual, expected);
requests
}
}
pub fn response(code: u16, body: Value) -> Response<Body> {
Response::builder()
.status(code)
.header("content-type", "application/json")
.body(Body::from(
serde_json::to_vec(&body).expect("serialize response"),
))
.expect("build response")
}
pub fn api_error(code: u16) -> Response<Body> {
response(
code,
json!({
"apiVersion": "v1", "kind": "Status", "status": "Failure",
"code": code, "reason": "TestFailure", "message": "scripted API failure"
}),
)
}
pub fn discovery_response(kind: &str, plural: &str, namespaced: bool) -> Response<Body> {
response(
200,
json!({
"apiVersion": "v1", "kind": "APIResourceList", "groupVersion": "v1",
"resources": [{"name": plural, "kind": kind, "namespaced": namespaced,
"verbs": ["get", "patch", "delete", "watch"]}]
}),
)
}
pub fn resource_sync() -> ResourceSync {
let reference = |name: &str| ClusterResourceRef {
resource_ref: GVKN {
api_version: "v1".to_string(),
kind: "ConfigMap".to_string(),
name: name.to_string(),
},
cluster: None,
};
let mut sync = ResourceSync::new(
"copy-config",
ResourceSyncSpec {
source: reference("source-config"),
target: reference("target-config"),
mappings: vec![],
},
);
sync.metadata.namespace = Some("team-a".to_string());
sync.metadata.uid = Some("sync-uid".to_string());
sync.metadata.generation = Some(7);
sync
}
}