From 538c11b6475e2b5c915958c4473d7ab9a5123eb4 Mon Sep 17 00:00:00 2001 From: Eirik A Date: Wed, 3 Jan 2024 07:31:33 +0000 Subject: [PATCH 1/3] Bump MSRV to 1.70 (#1384) Now needed for latest `home` crate. This is still 5 releases from latest stable so well within our "2+" policy. Signed-off-by: clux --- .devcontainer/Dockerfile | 2 +- README.md | 2 +- kube-client/Cargo.toml | 2 +- kube-core/Cargo.toml | 2 +- kube-derive/Cargo.toml | 2 +- kube-runtime/Cargo.toml | 2 +- kube/Cargo.toml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 2b11066e2..5d810c13b 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,4 +1,4 @@ -FROM docker.io/rust:1.65.0-bullseye +FROM docker.io/rust:1.70.0-bullseye ENV DEBIAN_FRONTEND=noninteractive RUN apt update && apt upgrade -y diff --git a/README.md b/README.md index 820d9c661..013be8d34 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # kube-rs [![Crates.io](https://img.shields.io/crates/v/kube.svg)](https://crates.io/crates/kube) -[![Rust 1.65](https://img.shields.io/badge/MSRV-1.65-dea584.svg)](https://github.com/rust-lang/rust/releases/tag/1.65.0) +[![Rust 1.70](https://img.shields.io/badge/MSRV-1.70-dea584.svg)](https://github.com/rust-lang/rust/releases/tag/1.70.0) [![Tested against Kubernetes v1_23 and above](https://img.shields.io/badge/MK8SV-v1_23-326ce5.svg)](https://kube.rs/kubernetes-version) [![Best Practices](https://bestpractices.coreinfrastructure.org/projects/5413/badge)](https://bestpractices.coreinfrastructure.org/projects/5413) [![Discord chat](https://img.shields.io/discord/500028886025895936.svg?logo=discord&style=plastic)](https://discord.gg/tokio) diff --git a/kube-client/Cargo.toml b/kube-client/Cargo.toml index baefe177d..a682b902a 100644 --- a/kube-client/Cargo.toml +++ b/kube-client/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/kube-rs/kube" readme = "../README.md" keywords = ["kubernetes", "client",] categories = ["web-programming::http-client", "configuration", "network-programming", "api-bindings"] -rust-version = "1.65.0" +rust-version = "1.70.0" edition = "2021" [features] diff --git a/kube-core/Cargo.toml b/kube-core/Cargo.toml index e9514507f..af89352b4 100644 --- a/kube-core/Cargo.toml +++ b/kube-core/Cargo.toml @@ -7,7 +7,7 @@ authors = [ "kazk ", ] edition = "2021" -rust-version = "1.65.0" +rust-version = "1.70.0" license = "Apache-2.0" keywords = ["kubernetes", "apimachinery"] categories = ["api-bindings", "encoding", "parser-implementations"] diff --git a/kube-derive/Cargo.toml b/kube-derive/Cargo.toml index e05a6e744..4ba1ba02f 100644 --- a/kube-derive/Cargo.toml +++ b/kube-derive/Cargo.toml @@ -7,7 +7,7 @@ authors = [ "kazk ", ] edition = "2021" -rust-version = "1.65.0" +rust-version = "1.70.0" license = "Apache-2.0" repository = "https://github.com/kube-rs/kube" readme = "../README.md" diff --git a/kube-runtime/Cargo.toml b/kube-runtime/Cargo.toml index da2e8780c..d06ae6ece 100644 --- a/kube-runtime/Cargo.toml +++ b/kube-runtime/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/kube-rs/kube" readme = "../README.md" keywords = ["kubernetes", "runtime", "reflector", "watcher", "controller"] categories = ["web-programming::http-client", "caching", "network-programming"] -rust-version = "1.65.0" +rust-version = "1.70.0" edition = "2021" [features] diff --git a/kube/Cargo.toml b/kube/Cargo.toml index 726e1b694..85276e360 100644 --- a/kube/Cargo.toml +++ b/kube/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/kube-rs/kube" readme = "../README.md" keywords = ["kubernetes", "client", "runtime", "cncf"] categories = ["network-programming", "caching", "api-bindings", "configuration", "encoding"] -rust-version = "1.65.0" +rust-version = "1.70.0" edition = "2021" [features] From f2090b8de8973b294b137bd596485f1c5767ee74 Mon Sep 17 00:00:00 2001 From: Danil Grigorev Date: Wed, 3 Jan 2024 08:37:38 +0100 Subject: [PATCH 2/3] Add type meta data for list types (#1380) * Add type meta data for list types Signed-off-by: Danil Grigorev * Add helper methods: list and resource for TypeMeta Signed-off-by: Danil Grigorev --------- Signed-off-by: Danil Grigorev Co-authored-by: Eirik A --- kube-core/src/metadata.rs | 36 +++++++++++++++++++++ kube-core/src/object.rs | 66 +++++++++++++++++++++++++++++++++------ kube/src/mock_tests.rs | 4 +++ 3 files changed, 97 insertions(+), 9 deletions(-) diff --git a/kube-core/src/metadata.rs b/kube-core/src/metadata.rs index d087d51b6..67edf6e16 100644 --- a/kube-core/src/metadata.rs +++ b/kube-core/src/metadata.rs @@ -17,6 +17,42 @@ pub struct TypeMeta { pub kind: String, } +impl TypeMeta { + /// Construct a new `TypeMeta` for the object list from the given resource. + /// + /// ``` + /// # use k8s_openapi::api::core::v1::Pod; + /// # use kube_core::TypeMeta; + /// + /// let type_meta = TypeMeta::list::(); + /// assert_eq!(type_meta.kind, "PodList"); + /// assert_eq!(type_meta.api_version, "v1"); + /// ``` + pub fn list>() -> Self { + TypeMeta { + api_version: K::api_version(&()).into(), + kind: K::kind(&()).to_string() + "List", + } + } + + /// Construct a new `TypeMeta` for the object from the given resource. + /// + /// ``` + /// # use k8s_openapi::api::core::v1::Pod; + /// # use kube_core::TypeMeta; + /// + /// let type_meta = TypeMeta::resource::(); + /// assert_eq!(type_meta.kind, "Pod"); + /// assert_eq!(type_meta.api_version, "v1"); + /// ``` + pub fn resource>() -> Self { + TypeMeta { + api_version: K::api_version(&()).into(), + kind: K::kind(&()).into(), + } + } +} + /// A generic representation of any object with `ObjectMeta`. /// /// It allows clients to get access to a particular `ObjectMeta` diff --git a/kube-core/src/object.rs b/kube-core/src/object.rs index c4e962ac6..92ef8838e 100644 --- a/kube-core/src/object.rs +++ b/kube-core/src/object.rs @@ -21,7 +21,10 @@ pub struct ObjectList where T: Clone, { - // NB: kind and apiVersion can be set here, but no need for it atm + /// The type fields, always present + #[serde(flatten, default)] + pub types: TypeMeta, + /// ListMeta - only really used for its `resourceVersion` /// /// See [ListMeta](k8s_openapi::apimachinery::pkg::apis::meta::v1::ListMeta) @@ -50,11 +53,13 @@ impl ObjectList { /// # Example /// /// ``` - /// use kube::api::{ListMeta, ObjectList}; + /// use kube::api::{ListMeta, ObjectList, TypeMeta}; + /// use k8s_openapi::api::core::v1::Pod; /// + /// let types: TypeMeta = TypeMeta::list::(); /// let metadata: ListMeta = Default::default(); /// let items = vec![1, 2, 3]; - /// let objectlist = ObjectList { metadata, items }; + /// # let objectlist = ObjectList { types, metadata, items }; /// /// let first = objectlist.iter().next(); /// println!("First element: {:?}", first); // prints "First element: Some(1)" @@ -68,11 +73,13 @@ impl ObjectList { /// # Example /// /// ``` - /// use kube::api::{ObjectList, ListMeta}; + /// use kube::api::{ListMeta, ObjectList, TypeMeta}; + /// use k8s_openapi::api::core::v1::Pod; /// + /// let types: TypeMeta = TypeMeta::list::(); /// let metadata: ListMeta = Default::default(); /// let items = vec![1, 2, 3]; - /// let mut objectlist = ObjectList { metadata, items }; + /// # let mut objectlist = ObjectList { types, metadata, items }; /// /// let mut first = objectlist.iter_mut().next(); /// @@ -300,7 +307,9 @@ pub struct NotUsed {} #[cfg(test)] mod test { - use super::{ApiResource, HasSpec, HasStatus, NotUsed, Object, Resource}; + use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ObjectMeta, ListMeta}; + + use super::{ApiResource, HasSpec, HasStatus, NotUsed, Object, Resource, ObjectList, TypeMeta}; use crate::resource::ResourceExt; #[test] @@ -337,9 +346,10 @@ mod test { assert_eq!(mypod.namespace().unwrap(), "dev"); assert_eq!(mypod.name_unchecked(), "blog"); assert!(mypod.status().is_none()); - assert_eq!(mypod.spec().containers[0], ContainerSimple { - image: "blog".into() - }); + assert_eq!( + mypod.spec().containers[0], + ContainerSimple { image: "blog".into() } + ); assert_eq!(PodSimple::api_version(&ar), "v1"); assert_eq!(PodSimple::version(&ar), "v1"); @@ -347,4 +357,42 @@ mod test { assert_eq!(PodSimple::kind(&ar), "Pod"); assert_eq!(PodSimple::group(&ar), ""); } + + #[test] + fn k8s_object_list() { + use k8s_openapi::api::core::v1::Pod; + // by grabbing the ApiResource info from the Resource trait + let ar = ApiResource::erase::(&()); + assert_eq!(ar.group, ""); + assert_eq!(ar.kind, "Pod"); + let podlist: ObjectList = ObjectList { + types: TypeMeta{ + api_version: ar.api_version, + kind: ar.kind + "List", + }, + metadata: ListMeta{..Default::default()}, + items: vec![Pod { + metadata: ObjectMeta { + name: Some("test".into()), + namespace: Some("dev".into()), + ..ObjectMeta::default() + }, + spec: None, + status: None, + }], + }; + + assert_eq!(&podlist.types.kind, "PodList"); + assert_eq!(&podlist.types.api_version, "v1"); + + let mypod = &podlist.items[0]; + let meta = mypod.meta(); + assert_eq!(&mypod.metadata, meta); + assert_eq!(meta.namespace.as_ref().unwrap(), "dev"); + assert_eq!(meta.name.as_ref().unwrap(), "test"); + assert_eq!(mypod.namespace().unwrap(), "dev"); + assert_eq!(mypod.name_unchecked(), "test"); + assert!(mypod.status.is_none()); + assert!(mypod.spec.is_none()); + } } diff --git a/kube/src/mock_tests.rs b/kube/src/mock_tests.rs index 8bfbb818a..c8d7c3b0e 100644 --- a/kube/src/mock_tests.rs +++ b/kube/src/mock_tests.rs @@ -96,6 +96,8 @@ impl ApiServerVerifier { assert!(!req_uri.contains("continue=")); // first list has no continue let respdata = json!({ + "kind": "HackList", + "apiVersion": "kube.rs/v1", "metadata": { "continue": "first", }, @@ -111,6 +113,8 @@ impl ApiServerVerifier { let req_uri = request.uri().to_string(); assert!(req_uri.contains("&continue=first")); let respdata = json!({ + "kind": "HackList", + "apiVersion": "kube.rs/v1", "metadata": { "continue": "", "resourceVersion": "2" From c7054b5c86d09947cc4dce210cd24d8487b16f2e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 3 Jan 2024 07:40:25 +0000 Subject: [PATCH 3/3] rustfmt (#1385) Signed-off-by: GitHub Co-authored-by: clux --- kube-core/src/object.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/kube-core/src/object.rs b/kube-core/src/object.rs index 92ef8838e..2ebd3e634 100644 --- a/kube-core/src/object.rs +++ b/kube-core/src/object.rs @@ -307,9 +307,9 @@ pub struct NotUsed {} #[cfg(test)] mod test { - use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ObjectMeta, ListMeta}; + use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ListMeta, ObjectMeta}; - use super::{ApiResource, HasSpec, HasStatus, NotUsed, Object, Resource, ObjectList, TypeMeta}; + use super::{ApiResource, HasSpec, HasStatus, NotUsed, Object, ObjectList, Resource, TypeMeta}; use crate::resource::ResourceExt; #[test] @@ -346,10 +346,9 @@ mod test { assert_eq!(mypod.namespace().unwrap(), "dev"); assert_eq!(mypod.name_unchecked(), "blog"); assert!(mypod.status().is_none()); - assert_eq!( - mypod.spec().containers[0], - ContainerSimple { image: "blog".into() } - ); + assert_eq!(mypod.spec().containers[0], ContainerSimple { + image: "blog".into() + }); assert_eq!(PodSimple::api_version(&ar), "v1"); assert_eq!(PodSimple::version(&ar), "v1"); @@ -366,11 +365,11 @@ mod test { assert_eq!(ar.group, ""); assert_eq!(ar.kind, "Pod"); let podlist: ObjectList = ObjectList { - types: TypeMeta{ + types: TypeMeta { api_version: ar.api_version, kind: ar.kind + "List", }, - metadata: ListMeta{..Default::default()}, + metadata: ListMeta { ..Default::default() }, items: vec![Pod { metadata: ObjectMeta { name: Some("test".into()),