Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove kube Dependency in kube-derive #517

Merged
merged 5 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ jobs:
- run: cargo test -p kube-runtime
- run: cargo test -p kube
- run: cargo test -p kube-derive
- run: cargo test -p kube-core --features k8s-openapi/v1_20
- run: cargo test --lib --all -j4
- run: cargo test --doc --all -j4
- run: cargo test -j4 -p examples
Expand Down Expand Up @@ -134,6 +135,7 @@ jobs:
- run: cargo test -p kube-runtime
- run: cargo test -p kube
- run: cargo test -p kube-derive
- run: cargo test -p kube-core --features k8s-openapi/v1_20
- run: cargo test --lib --all -j4
- run: cargo test --doc --all -j4
- run: cargo test -j4 -p examples
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
default-members = ["kube"]
members = [
"kube",
"kube-core",
"kube-derive",
"kube-runtime",

# internal
"tests",
"examples",
]
]
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ futures = "0.3.8"
kube = { path = "../kube", version = "^0.53.0", default-features = false, features = ["admission"] }
kube-derive = { path = "../kube-derive", version = "^0.53.0", default-features = false } # only needed to opt out of schema
kube-runtime = { path = "../kube-runtime", version = "^0.53.0", default-features = false }
kube-core = { path = "../kube-core", version = "^0.53.0", default-features = false }
k8s-openapi = { version = "0.11.0", features = ["v1_20"], default-features = false }
log = "0.4.11"
serde = { version = "1.0.118", features = ["derive"] }
Expand Down
33 changes: 33 additions & 0 deletions kube-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "kube-core"
description = "Type definitions used amongst workspace crates"
version = "0.53.0"
authors = [
"clux <[email protected]>",
"kazk <[email protected]>",
]
edition = "2018"
license = "Apache-2.0"
repository = "https://github.com/clux/kube-rs"
readme = "../README.md"

[features]
ws = []

[dependencies]
serde = { version = "1.0.118", features = ["derive"] }
serde_json = "1.0.61"
thiserror = "1.0.23"
once_cell = "1.7.2"
url = "2.2.0"
log = "0.4.11"
http = "0.2.2"

[dependencies.k8s-openapi]
version = "0.11.0"
default-features = false
features = []

[dev-dependencies]
kube = { path = "../kube", version = "^0.53.0" }

102 changes: 102 additions & 0 deletions kube-core/src/api_resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use crate::gvk::GroupVersionKind;
use crate::resource::Resource;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::APIResource;

/// Contains information about Kubernetes API resources
/// which is enough for working with it.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct ApiResource {
/// Resource group, empty for core group.
pub group: String,
/// group version
pub version: String,
/// apiVersion of the resource (v1 for core group,
/// groupName/groupVersions for other).
pub api_version: String,
/// Singular PascalCase name of the resource
pub kind: String,
/// Plural name of the resource
pub plural: String,
}

impl ApiResource {
/// Creates ApiResource from `meta::v1::APIResource` instance.
///
/// `APIResource` objects can be extracted from [`Client::list_api_group_resources`](crate::Client::list_api_group_resources).
/// If it does not specify version and/or group, they will be taken from `group_version`
/// (otherwise the second parameter is ignored).
///
/// ### Example usage:
/// ```
/// use kube::api::{ApiResource, Api, DynamicObject};
/// # async fn scope(client: kube::Client) -> Result<(), Box<dyn std::error::Error>> {
/// let apps = client.list_api_group_resources("apps/v1").await?;
/// for ar in &apps.resources {
/// let resource = ApiResource::from_apiresource(ar, &apps.group_version);
/// dbg!(&resource);
/// let api: Api<DynamicObject> = Api::namespaced_with(client.clone(), "default", &resource);
/// }
/// # Ok(())
/// # }
/// ```
pub fn from_apiresource(ar: &APIResource, group_version: &str) -> Self {
let gvsplit = group_version.splitn(2, '/').collect::<Vec<_>>();
let (default_group, default_version) = match *gvsplit.as_slice() {
[g, v] => (g, v), // standard case
[v] => ("", v), // core v1 case
_ => unreachable!(),
};
let group = ar.group.clone().unwrap_or_else(|| default_group.into());
let version = ar.version.clone().unwrap_or_else(|| default_version.into());
let kind = ar.kind.to_string();
let api_version = if group.is_empty() {
version.clone()
} else {
format!("{}/{}", group, version)
};
let plural = ar.name.clone();
ApiResource {
group,
version,
api_version,
kind,
plural,
}
}

/// Creates ApiResource by type-erasing another Resource
pub fn erase<K: Resource>(dt: &K::DynamicType) -> Self {
ApiResource {
group: K::group(dt).to_string(),
version: K::version(dt).to_string(),
api_version: K::api_version(dt).to_string(),
kind: K::kind(dt).to_string(),
plural: K::plural(dt).to_string(),
}
}

/// Creates ApiResource from group, version and kind.
/// # Warning
/// This function has to **guess** resource plural name.
/// While it makes it best to guess correctly, sometimes it can
/// be wrong, and using returned ApiResource will lead to incorrect
/// api requests.
pub fn from_gvk(gvk: &GroupVersionKind) -> Self {
ApiResource::from_gvk_with_plural(gvk, &crate::resource::to_plural(&gvk.kind.to_ascii_lowercase()))
}

/// Creates ApiResource from group, version, kind and plural name.
pub fn from_gvk_with_plural(gvk: &GroupVersionKind, plural: &str) -> Self {
let api_version = match gvk.group.as_str() {
"" => gvk.version.clone(),
_ => format!("{}/{}", gvk.group, gvk.version),
};
ApiResource {
group: gvk.group.clone(),
version: gvk.version.clone(),
api_version,
kind: gvk.kind.clone(),
plural: plural.to_string(),
}
}
}
23 changes: 23 additions & 0 deletions kube-core/src/gvk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use serde::{Deserialize, Serialize};

/// Contains enough information to identify API Resource.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct GroupVersionKind {
/// API group
pub group: String,
/// Version
pub version: String,
/// Kind
pub kind: String,
}

impl GroupVersionKind {
/// Set the api group, version, and kind for a resource
pub fn gvk(group_: &str, version_: &str, kind_: &str) -> Self {
let version = version_.to_string();
let group = group_.to_string();
let kind = kind_.to_string();

Self { group, version, kind }
}
}
27 changes: 27 additions & 0 deletions kube-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use thiserror::Error;

pub mod api_resource;
pub mod gvk;
pub mod resource;
pub mod params;
pub mod request;
pub mod subresource;

#[macro_use] extern crate log;

#[derive(Error, Debug)]
pub enum Error {
/// A request validation failed
#[error("Request validation failed with {0}")]
RequestValidation(String),

/// Common error case when requesting parsing into own structs
#[error("Error deserializing response")]
SerdeError(#[from] serde_json::Error),

/// Http based error
#[error("HttpError: {0}")]
HttpError(#[from] http::Error),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
File renamed without changes.
7 changes: 4 additions & 3 deletions kube/src/api/request.rs → kube-core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ impl Request {
/// Cheap sanity check to ensure type maps work as expected
#[cfg(test)]
mod test {
use crate::api::{PostParams, Request, Resource};

use crate::params::PostParams;
use crate::request::Request;
use crate::resource::Resource;
use k8s::{
admissionregistration::v1beta1 as adregv1beta1,
apps::v1 as appsv1,
Expand Down Expand Up @@ -341,7 +342,7 @@ mod test {

/// -----------------------------------------------------------------
/// Tests that the misc mappings are also sensible
use crate::api::{DeleteParams, ListParams, Patch, PatchParams};
use crate::params::{DeleteParams, ListParams, Patch, PatchParams};
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1beta1 as apiextsv1beta1;

#[test]
Expand Down
Loading