roperator 0.3.0

Easily create Kubernetes Operators with Rust
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::config::UpdateStrategy;
use crate::handler::{Handler, SyncRequest, SyncResponse};
use crate::resource::{InvalidResourceError, JsonObject, K8sResource, ObjectIdRef, ResourceJson};
use crate::runner::client::{self, Client};
use crate::runner::informer::{EventType, ResourceMessage};
use crate::runner::reconcile::compare::compare_values;
use crate::runner::reconcile::{
    does_finalizer_exist, update_status_if_different, SyncHandler, UpdateError,
};
use crate::runner::resource_map::IdSet;
use crate::runner::{duration_to_millis, ChildRuntimeConfig, RuntimeConfig};

use serde_json::{json, Value};

use std::sync::Arc;
use std::time::{Duration, Instant};

pub(crate) async fn handle_sync(handler: SyncHandler) {
    let SyncHandler {
        sender,
        request,
        handler,
        client,
        runtime_config,
        parent_index_key,
    } = handler;
    let parent_id = request.parent.get_object_id().to_owned();
    let parent_id_ref = parent_id.as_id_ref();

    let start_time = Instant::now();
    let result = private_handle_sync(start_time, request, handler, client, &*runtime_config).await;

    let update_result = match result {
        Ok(duration) => {
            log::info!("Finished sync for parent: {}", parent_id);
            Ok(duration)
        }
        Err(err) => {
            runtime_config.metrics.parent_sync_error(&parent_id_ref);
            log::error!("Error while syncing parent: {}: {:?}", parent_id, err);
            Err(())
        }
    };
    let message = ResourceMessage {
        event_type: EventType::UpdateOperationComplete {
            result: update_result,
        },
        resource_id: parent_id,
        resource_type: runtime_config.parent_type,
        index_key: Some(parent_index_key),
    };
    let _ = sender.send(message).await;
}

/// Performs the whole sync, including invoking the Handler, updating the parent status, and updating any children that need it.
/// If our operator isn't in the list of parent finalizers, then we'll just add it to the list and then move on without even invoking the handler
/// this is because adding the finalizer will cause the resourceVersion of the parent to be incremented, which will mean that our update to the
/// status would fail due to the conflicting resource version, at least until we observe that version
async fn private_handle_sync(
    start_time: Instant,
    request: SyncRequest,
    handler: Arc<dyn Handler>,
    client: Client,
    runtime_config: &RuntimeConfig,
) -> Result<Option<Duration>, UpdateError> {
    if !does_finalizer_exist(&request.parent, runtime_config) {
        // We'll only add the finalizer this time, and then immediately re-sync
        // This is because adding the finalizer will change the resourceVersion, so
        // we need to observe the new one before attempting to sync
        add_finalizer_to_parent(&request.parent, &client, runtime_config).await?;
        log::info!(
            "Observed new parent: {} and added '{}' as a finalizer",
            request.parent.get_object_id(),
            runtime_config.operator_name
        );
        Ok(Some(Duration::from_secs(0)))
    } else {
        let (request, result) = {
            tokio::task::spawn_blocking(move || {
                let result = handler.sync(&request);
                log::debug!(
                    "finished invoking handler for parent: {} in {}ms",
                    request.parent.get_object_id(),
                    duration_to_millis(start_time.elapsed())
                );
                (request, result)
            })
            .await?
        };
        let response = result.map_err(UpdateError::HandlerError)?;
        let resync = response.resync;
        update_all(request, response, client, runtime_config).await?;
        Ok(resync)
    }
}

async fn update_all(
    request: SyncRequest,
    handler_response: SyncResponse,
    client: Client,
    runtime_config: &RuntimeConfig,
) -> Result<(), UpdateError> {
    let start_time = Instant::now();
    let SyncResponse {
        status, children, ..
    } = handler_response;
    let parent_id = request.parent.get_object_id().to_owned();
    update_status_if_different(&request.parent, &client, runtime_config, status).await?;
    log::debug!(
        "Successfully updated status for parent: {} in {}ms",
        parent_id,
        duration_to_millis(start_time.elapsed())
    );
    let child_ids = update_children(&client, runtime_config, &request, children).await?;
    log::debug!(
        "Successfully updated all {} children of parent: {} in {}ms",
        child_ids.len(),
        parent_id,
        duration_to_millis(start_time.elapsed())
    );

    // now that all the child updates have completed successfully, we'll delete any children that are no longer desired
    delete_undesired_children(&client, runtime_config, &child_ids, &request).await?;
    Ok(())
}

async fn add_finalizer_to_parent(
    parent: &K8sResource,
    client: &Client,
    runtime_config: &RuntimeConfig,
) -> Result<(), client::Error> {
    let patch =
        crate::runner::client::Patch::add_finalizer(parent, runtime_config.operator_name.as_str());
    client
        .patch_resource(runtime_config.parent_type, &parent.get_object_id(), &patch)
        .await
}

async fn delete_undesired_children(
    client: &Client,
    runtime_config: &RuntimeConfig,
    desired_children: &IdSet,
    sync_request: &SyncRequest,
) -> Result<(), client::Error> {
    for existing_child in sync_request.children.iter() {
        let child_id = existing_child.get_object_id();
        if !desired_children.contains(&child_id) && !existing_child.is_deletion_timestamp_set() {
            log::info!("Need to delete child: {} of parent: {} because it was not included in the handler response",
                    child_id, sync_request.parent.get_object_id());
            let child_type = runtime_config
                .type_for(&existing_child.get_type_ref())
                .expect("No configuration found for existing child type");
            client.delete_resource(child_type, &child_id).await?;
        }
    }
    Ok(())
}

async fn update_children(
    client: &Client,
    runtime_config: &RuntimeConfig,
    req: &SyncRequest,
    response_children: Vec<Value>,
) -> Result<IdSet, UpdateError> {
    let parent_uid = req.parent.uid();
    let parent_id = req.parent.get_object_id();
    let mut child_ids = IdSet::new();
    for mut child in response_children {
        let child_id = child
            .get_id_ref()
            .ok_or_else(|| InvalidResourceError::new("missing name", child.clone()))?
            .to_owned();

        // ensure that the child has the same namespace as the parent. This is a deliberate constraint that
        // we place on users of this library, as having non-namespaced children of namespaced parents would
        // add considerable complexity.
        let valid_namespaces = match (parent_id.namespace(), child_id.namespace()) {
            (None, None) => true,
            (None, Some(_)) => true,
            (Some(p), Some(c)) => p == c,
            (Some(_), None) => false,
        };

        if !valid_namespaces {
            log::error!(
                "Child {} is not in the same namespace as parent: {}",
                child_id,
                parent_id
            );
            const MESSAGE: &str = "Child namespace does not match the namespace of the parent";
            return Err(InvalidResourceError::new(MESSAGE, child.clone()).into());
        }

        let child_config: &ChildRuntimeConfig = {
            let child_type_ref = child.get_type_ref().ok_or_else(|| {
                InvalidResourceError::new("missing either apiVersion or kind", child.clone())
            })?;

            // get the configuration for this child type, and bail if it doesn't exist
            runtime_config
                .get_child_config(&child_type_ref)
                .ok_or_else(|| {
                    UpdateError::UnknownChildType(
                        child_type_ref.api_version().to_string(),
                        child_type_ref.kind().to_string(),
                    )
                })?
        };
        let existing_child = req
            .children()
            .of_type(child_config.child_type)
            .get(&child_id);
        let update_required = is_child_update_required(
            &parent_id,
            child_config,
            existing_child,
            &child_id.as_id_ref(),
            &child,
        )?;
        add_parent_references(runtime_config, parent_id.name(), parent_uid, &mut child)?;
        if let Some(update_type) = update_required {
            let start_time = Instant::now();
            log::debug!(
                "Starting child update for parent_uid: {}, child_type: {}, child_id: {}",
                parent_uid,
                child_config.child_type,
                child_id
            );
            let result = do_child_update(update_type, child_config, client, child).await;
            let total_millis = duration_to_millis(start_time.elapsed());
            log::debug!(
                "Finshed child update for {} in {}ms with result: {:?}",
                child_id,
                total_millis,
                result
            );
            result?; // return early if it failed
        }
        child_ids.insert(child_id);
    }
    Ok(child_ids)
}

async fn do_child_update(
    update_type: UpdateType,
    child_config: &ChildRuntimeConfig,
    client: &Client,
    mut desired_child: Value,
) -> Result<(), client::Error> {
    let k8s_type = &child_config.child_type;
    match update_type {
        UpdateType::Create => client.create_resource(k8s_type, &desired_child).await,
        UpdateType::Replace(resource_version) => {
            {
                // if we're replacing the resource, then we need to specify the old resourceVersion
                let obj = desired_child
                    .pointer_mut("/metadata")
                    .and_then(Value::as_object_mut)
                    .unwrap();
                obj.insert(
                    "resourceVersion".to_owned(),
                    Value::String(resource_version),
                );
            }
            // the desired child should have already been validated
            let child_id = desired_child
                .get_id_ref()
                .expect("failed to get id from desired child resource");
            client
                .replace_resource(k8s_type, &child_id, &desired_child)
                .await
        }
        UpdateType::Delete => {
            let child_id = desired_child
                .get_id_ref()
                .expect("failed to get id from desired child resource");
            // TODO: Deleting a resource could return a 409 error if it's already being deleted. Figure out how to deal with that
            client.delete_resource(k8s_type, &child_id).await
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
enum UpdateType {
    Create,
    Replace(String),
    Delete,
}

fn is_child_update_required(
    parent_id: &ObjectIdRef<'_>,
    child_config: &ChildRuntimeConfig,
    existing_child: Option<&K8sResource>,
    child_id: &ObjectIdRef<'_>,
    child: &Value,
) -> Result<Option<UpdateType>, UpdateError> {
    let update_type = match (existing_child, child_config.update_strategy) {
        (Some(_), UpdateStrategy::OnDelete) => {
            log::debug!("UpdateStrategy for child type {} is OnDelete and child {} already exists, so will not update",
                    child_config.child_type, child_id);
            None
        }
        (Some(existing_child), update_strategy) => {
            let diffs = compare_values(existing_child.as_ref(), child);
            if diffs.non_empty() {
                log::info!(
                    "Found {} diffs in child of parent: {} with type: {} and id: {}, diffs: {}",
                    diffs.len(),
                    parent_id,
                    child_config.child_type,
                    child_id,
                    diffs
                );
                determine_update_type(existing_child, update_strategy)
            } else {
                log::debug!(
                    "No difference in child of parent: {}, with type: {} and id: {}",
                    parent_id,
                    child_config.child_type,
                    child_id
                );
                None
            }
        }
        (None, _) => {
            log::debug!(
                "No existing child of parent: {} with type: {} and id: {}",
                parent_id,
                child_config.child_type,
                child_id
            );
            Some(UpdateType::Create)
        }
    };
    Ok(update_type)
}

/// figures out what to do when the desired child is different from the existing one. The answer
/// may be to do nothing for now, if the existing child is in the process of being deleted
fn determine_update_type(
    existing_child: &K8sResource,
    update_strategy: UpdateStrategy,
) -> Option<UpdateType> {
    if existing_child.is_deletion_timestamp_set() {
        log::debug!(
            "Will skip updating child: {} : {} on this loop because it is currently being deleted",
            existing_child.get_type_ref(),
            existing_child.get_object_id()
        );
        None
    } else if update_strategy == UpdateStrategy::Recreate {
        // the existing child is not yet being deleted, but we'll need to delete it before we can re-create it.
        // When updateStrategy is recreate, we only delete it on the first go around and then we'll do a Create
        // once the delete has finished. This allows us to continue to make progress on the rest of the sync operations
        // since deletion can sometimes take quite a while due to finalizers needing to run.
        Some(UpdateType::Delete)
    } else {
        let resource_version = existing_child.resource_version();
        Some(UpdateType::Replace(resource_version.to_owned()))
    }
}

fn add_parent_references(
    runtime_config: &RuntimeConfig,
    parent_name: &str,
    parent_uid: &str,
    child: &mut Value,
) -> Result<(), InvalidResourceError> {
    let meta = require_object_mut(child, "/metadata", "child object is missing 'metadata'")?;
    if !meta.contains_key("labels") || !meta.get("labels").unwrap().is_object() {
        meta.insert("labels".to_owned(), Value::Object(JsonObject::new()));
    }
    {
        let labels = meta.get_mut("labels").unwrap().as_object_mut().unwrap(); // we just ensured this above
        labels.insert(
            runtime_config.correlation_label_name.clone(),
            parent_uid.into(),
        );
        labels.insert(
            runtime_config.controller_label_name.clone(),
            runtime_config.operator_name.as_str().into(),
        );
    }
    if !meta.contains_key("ownerReferences") || !meta.get("ownerReferences").unwrap().is_array() {
        meta.insert("ownerReferences".to_owned(), Value::Array(Vec::new()));
    }
    let owner_refs = meta
        .get_mut("ownerReferences")
        .unwrap()
        .as_array_mut()
        .unwrap();
    let new_ref = make_owner_ref(parent_uid, parent_name, runtime_config);
    if !owner_refs.contains(&new_ref) {
        owner_refs.push(new_ref);
    }
    Ok(())
}

fn make_owner_ref(parent_uid: &str, parent_name: &str, runtime_config: &RuntimeConfig) -> Value {
    json!({
        "apiVersion": runtime_config.parent_type.api_version,
        "controller": true,
        "kind": runtime_config.parent_type.kind,
        "name": parent_name,
        "uid": parent_uid,
    })
}

fn require_object_mut<'a>(
    value: &'a mut Value,
    pointer: &'static str,
    err_msg: &'static str,
) -> Result<&'a mut JsonObject, InvalidResourceError> {
    if value
        .pointer(pointer)
        .map(Value::is_object)
        .unwrap_or(false)
    {
        Ok(value
            .pointer_mut(pointer)
            .and_then(Value::as_object_mut)
            .unwrap())
    } else {
        Err(InvalidResourceError::new(err_msg, value.clone()))
    }
}