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
//! A port of request parameter *Optionals from apimachinery/types.go
use crate::{Error, Result};
use serde::Serialize;
/// Common query parameters used in watch/list/delete calls on collections
#[derive(Clone, Debug)]
pub struct ListParams {
/// A selector to restrict the list of returned objects by their labels.
///
/// Defaults to everything if `None`.
pub label_selector: Option<String>,
/// A selector to restrict the list of returned objects by their fields.
///
/// Defaults to everything if `None`.
pub field_selector: Option<String>,
/// Timeout for the list/watch call.
///
/// This limits the duration of the call, regardless of any activity or inactivity.
/// If unset for a watch call, we will use 290s.
/// We limit this to 295s due to [inherent watch limitations](https://github.com/kubernetes/kubernetes/issues/6513).
pub timeout: Option<u32>,
/// Enables watch events with type "BOOKMARK".
///
/// Servers that do not implement bookmarks ignore this flag and
/// bookmarks are sent at the server's discretion. Clients should not
/// assume bookmarks are returned at any specific interval, nor may they
/// assume the server will send any BOOKMARK event during a session.
/// If this is not a watch, this field is ignored.
/// If the feature gate WatchBookmarks is not enabled in apiserver,
/// this field is ignored.
pub bookmarks: bool,
/// Limit the number of results.
///
/// If there are more results, the server will respond with a continue token which can be used to fetch another page
/// of results. See the [Kubernetes API docs](https://kubernetes.io/docs/reference/using-api/api-concepts/#retrieving-large-results-sets-in-chunks)
/// for pagination details.
pub limit: Option<u32>,
/// Fetch a second page of results.
///
/// After listing results with a limit, a continue token can be used to fetch another page of results.
pub continue_token: Option<String>,
}
impl Default for ListParams {
/// Default `ListParams` without any constricting selectors
fn default() -> Self {
Self {
// bookmarks stable since 1.17, and backwards compatible
bookmarks: true,
label_selector: None,
field_selector: None,
timeout: None,
limit: None,
continue_token: None,
}
}
}
impl ListParams {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(to) = &self.timeout {
// https://github.com/kubernetes/kubernetes/issues/6513
if *to >= 295 {
return Err(Error::RequestValidation(
"ListParams::timeout must be < 295s".into(),
));
}
}
Ok(())
}
}
/// Builder interface to ListParams
///
/// Usage:
/// ```
/// use kube::api::ListParams;
/// let lp = ListParams::default()
/// .timeout(60)
/// .labels("kubernetes.io/lifecycle=spot");
/// ```
impl ListParams {
/// Configure the timeout for list/watch calls
///
/// This limits the duration of the call, regardless of any activity or inactivity.
/// Defaults to 290s
pub fn timeout(mut self, timeout_secs: u32) -> Self {
self.timeout = Some(timeout_secs);
self
}
/// Configure the selector to restrict the list of returned objects by their fields.
///
/// Defaults to everything.
/// Supports `=`, `==`, `!=`, and can be comma separated: `key1=value1,key2=value2`.
/// The server only supports a limited number of field queries per type.
pub fn fields(mut self, field_selector: &str) -> Self {
self.field_selector = Some(field_selector.to_string());
self
}
/// Configure the selector to restrict the list of returned objects by their labels.
///
/// Defaults to everything.
/// Supports `=`, `==`, `!=`, and can be comma separated: `key1=value1,key2=value2`.
pub fn labels(mut self, label_selector: &str) -> Self {
self.label_selector = Some(label_selector.to_string());
self
}
/// Disables watch bookmarks to simplify watch handling
///
/// This is not recommended to use with production watchers as it can cause desyncs.
/// See [#219](https://github.com/kube-rs/kube-rs/issues/219) for details.
pub fn disable_bookmarks(mut self) -> Self {
self.bookmarks = false;
self
}
/// Sets a result limit.
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
/// Sets a continue token.
pub fn continue_token(mut self, token: &str) -> Self {
self.continue_token = Some(token.to_string());
self
}
}
/// Common query parameters for put/post calls
#[derive(Default, Clone, Debug)]
pub struct PostParams {
/// Whether to run this as a dry run
pub dry_run: bool,
/// fieldManager is a name of the actor that is making changes
pub field_manager: Option<String>,
}
impl PostParams {
pub(crate) fn validate(&self) -> Result<()> {
if let Some(field_manager) = &self.field_manager {
// Implement the easy part of validation, in future this may be extended to provide validation as in go code
// For now it's fine, because k8s API server will return an error
if field_manager.len() > 128 {
return Err(Error::RequestValidation(
"Failed to validate PostParams::field_manager!".into(),
));
}
}
Ok(())
}
}
/// Describes changes that should be applied to a resource
///
/// Takes arbitrary serializable data for all strategies except `Json`.
///
/// We recommend using ([server-side](https://kubernetes.io/blog/2020/04/01/kubernetes-1.18-feature-server-side-apply-beta-2)) `Apply` patches on new kubernetes releases.
///
/// See [kubernetes patch docs](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment) for the older patch types.
///
/// Note that patches have different effects on different fields depending on their merge strategies.
/// These strategies are configurable when deriving your [`CustomResource`](https://docs.rs/kube-derive/*/kube_derive/derive.CustomResource.html#customizing-schemas).
///
/// # Creating a patch via serde_json
/// ```
/// use kube::api::Patch;
/// let patch = serde_json::json!({
/// "apiVersion": "v1",
/// "kind": "Pod",
/// "metadata": {
/// "name": "blog"
/// },
/// "spec": {
/// "activeDeadlineSeconds": 5
/// }
/// });
/// let patch = Patch::Apply(&patch);
/// ```
/// # Creating a patch from a type
/// ```
/// use kube::api::Patch;
/// use k8s_openapi::api::rbac::v1::Role;
/// use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
/// let r = Role {
/// metadata: ObjectMeta { name: Some("user".into()), ..ObjectMeta::default() },
/// rules: Some(vec![])
/// };
/// let patch = Patch::Apply(&r);
/// ```
#[non_exhaustive]
#[derive(Debug)]
pub enum Patch<T: Serialize> {
/// [Server side apply](https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply)
///
/// Requires kubernetes >= 1.16
Apply(T),
/// [JSON patch](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment)
///
/// Using this variant will require you to explicitly provide a type for `T` at the moment.
///
/// # Example
///
/// ```
/// use kube::api::Patch;
/// let json_patch = json_patch::Patch(vec![]);
/// let patch = Patch::Json::<()>(json_patch);
/// ```
#[cfg(feature = "jsonpatch")]
#[cfg_attr(docsrs, doc(cfg(feature = "jsonpatch")))]
Json(json_patch::Patch),
/// [JSON Merge patch](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment)
Merge(T),
/// [Strategic JSON Merge patch](https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-strategic-merge-patch-to-update-a-deployment)
Strategic(T),
}
impl<T: Serialize> Patch<T> {
pub(crate) fn is_apply(&self) -> bool {
matches!(self, Patch::Apply(_))
}
pub(crate) fn content_type(&self) -> &'static str {
match &self {
Self::Apply(_) => "application/apply-patch+yaml",
#[cfg(feature = "jsonpatch")]
#[cfg_attr(docsrs, doc(cfg(feature = "jsonpatch")))]
Self::Json(_) => "application/json-patch+json",
Self::Merge(_) => "application/merge-patch+json",
Self::Strategic(_) => "application/strategic-merge-patch+json",
}
}
}
impl<T: Serialize> Patch<T> {
pub(crate) fn serialize(&self) -> Result<Vec<u8>> {
match self {
Self::Apply(p) => serde_json::to_vec(p),
#[cfg(feature = "jsonpatch")]
#[cfg_attr(docsrs, doc(cfg(feature = "jsonpatch")))]
Self::Json(p) => serde_json::to_vec(p),
Self::Strategic(p) => serde_json::to_vec(p),
Self::Merge(p) => serde_json::to_vec(p),
}
.map_err(Into::into)
}
}
/// Common query parameters for patch calls
#[derive(Default, Clone, Debug)]
pub struct PatchParams {
/// Whether to run this as a dry run
pub dry_run: bool,
/// force Apply requests. Applicable only to [`Patch::Apply`].
pub force: bool,
/// fieldManager is a name of the actor that is making changes. Required for [`Patch::Apply`]
/// optional for everything else.
pub field_manager: Option<String>,
}
impl PatchParams {
pub(crate) fn validate<P: Serialize>(&self, patch: &Patch<P>) -> Result<()> {
if let Some(field_manager) = &self.field_manager {
// Implement the easy part of validation, in future this may be extended to provide validation as in go code
// For now it's fine, because k8s API server will return an error
if field_manager.len() > 128 {
return Err(Error::RequestValidation(
"Failed to validate PatchParams::field_manager!".into(),
));
}
}
if self.force && !patch.is_apply() {
return Err(Error::RequestValidation(
"PatchParams::force only works with Patch::Apply".into(),
));
}
Ok(())
}
pub(crate) fn populate_qp(&self, qp: &mut form_urlencoded::Serializer<String>) {
if self.dry_run {
qp.append_pair("dryRun", "All");
}
if self.force {
qp.append_pair("force", "true");
}
if let Some(ref fm) = self.field_manager {
qp.append_pair("fieldManager", fm);
}
}
/// Construct `PatchParams` for server-side apply
pub fn apply(manager: &str) -> Self {
Self {
field_manager: Some(manager.into()),
..Self::default()
}
}
/// Force the result through on conflicts
///
/// NB: Force is a concept restricted to the server-side [`Patch::Apply`].
pub fn force(mut self) -> Self {
self.force = true;
self
}
/// Perform a dryRun only
pub fn dry_run(mut self) -> Self {
self.dry_run = true;
self
}
}
/// Common query parameters for delete calls
#[derive(Default, Clone, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DeleteParams {
/// When present, indicates that modifications should not be persisted.
#[serde(
serialize_with = "dry_run_all_ser",
skip_serializing_if = "std::ops::Not::not"
)]
pub dry_run: bool,
/// The duration in seconds before the object should be deleted.
///
/// Value must be non-negative integer. The value zero indicates delete immediately.
/// If this value is `None`, the default grace period for the specified type will be used.
/// Defaults to a per object value if not specified. Zero means delete immediately.
#[serde(skip_serializing_if = "Option::is_none")]
pub grace_period_seconds: Option<u32>,
/// Whether or how garbage collection is performed.
///
/// The default policy is decided by the existing finalizer set in
/// `metadata.finalizers`, and the resource-specific default policy.
#[serde(skip_serializing_if = "Option::is_none")]
pub propagation_policy: Option<PropagationPolicy>,
/// Condtions that must be fulfilled before a deletion is carried out
///
/// If not possible, a `409 Conflict` status will be returned.
#[serde(skip_serializing_if = "Option::is_none")]
pub preconditions: Option<Preconditions>,
}
// dryRun serialization differ when used as body parameters and query strings:
// query strings are either true/false
// body params allow only: missing field, or ["All"]
// The latter is a very awkward API causing users to do to
// dp.dry_run = vec!["All".into()];
// just to turn on dry_run..
// so we hide this detail for now.
fn dry_run_all_ser<S>(t: &bool, s: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
use serde::ser::SerializeTuple;
match t {
true => {
let mut map = s.serialize_tuple(1)?;
map.serialize_element("All")?;
map.end()
}
false => s.serialize_none(),
}
}
#[cfg(test)]
mod test {
use super::DeleteParams;
#[test]
fn delete_param_serialize() {
let mut dp = DeleteParams::default();
let emptyser = serde_json::to_string(&dp).unwrap();
//println!("emptyser is: {}", emptyser);
assert_eq!(emptyser, "{}");
dp.dry_run = true;
let ser = serde_json::to_string(&dp).unwrap();
//println!("ser is: {}", ser);
assert_eq!(ser, "{\"dryRun\":[\"All\"]}");
}
}
/// Preconditions must be fulfilled before an operation (update, delete, etc.) is carried out.
#[derive(Default, Clone, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Preconditions {
/// Specifies the target ResourceVersion
#[serde(skip_serializing_if = "Option::is_none")]
pub resource_version: Option<String>,
/// Specifies the target UID
#[serde(skip_serializing_if = "Option::is_none")]
pub uid: Option<String>,
}
/// Propagation policy when deleting single objects
#[derive(Clone, Debug, Serialize)]
pub enum PropagationPolicy {
/// Orphan dependents
Orphan,
/// Allow the garbage collector to delete the dependents in the background
Background,
/// A cascading policy that deletes all dependents in the foreground
Foreground,
}