Skip to content

Commit

Permalink
[WIP] Abstract away the API versions and kinds of resource types.
Browse files Browse the repository at this point in the history
This change adds `TypeMeta` and `ValueMeta` traits that are implemented for
all resource types. The traits define the API version and kind of each resource
automatically, so the corresponding fields have been removed from the
resource types.

The traits are derived from https://github.com/anguslees/kubernetes-rs
based on the discussion in anguslees/kubernetes-rs#9
  • Loading branch information
Arnavion committed Oct 10, 2018
1 parent 789fa0b commit b25b6fe
Show file tree
Hide file tree
Showing 173 changed files with 4,585 additions and 3,642 deletions.
2 changes: 1 addition & 1 deletion k8s-openapi-tests/src/api_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ fn list() {
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("couldn't get API versions");

assert_eq!(api_versions.kind, Some("APIGroupList".to_string()));
assert_eq!(::k8s_openapi::ValueMeta::kind(&api_versions), "APIGroupList");
}
2 changes: 1 addition & 1 deletion k8s-openapi-tests/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn list() {
other => Err(format!("{:?} {}", other, status_code).into()),
})).expect("couldn't list deployments");

assert_eq!(deployment_list.kind, Some("DeploymentList".to_string()));
assert_eq!(::k8s_openapi::ValueMeta::kind(&deployment_list), "DeploymentList");

k8s_if_le_1_10! {
let dns_deployment_name = "kube-dns";
Expand Down
2 changes: 1 addition & 1 deletion k8s-openapi-tests/src/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn list() {
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("couldn't list pods");

assert_eq!(pod_list.kind, Some("PodList".to_string()));
assert_eq!(::k8s_openapi::ValueMeta::kind(&pod_list), "PodList");

let addon_manager_pod =
pod_list
Expand Down
22 changes: 22 additions & 0 deletions k8s-openapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,28 @@ impl serde::Serialize for ByteString {
}
}

pub trait TypeMeta {
fn api_version() -> &'static str;

fn kind() -> &'static str;
}

pub trait ValueMeta {
fn api_version(&self) -> &'static str;

fn kind(&self) -> &'static str;
}

impl<T> ValueMeta for T where T: TypeMeta {
fn api_version(&self) -> &'static str {
<Self as TypeMeta>::api_version()
}

fn kind(&self) -> &'static str {
<Self as TypeMeta>::kind()
}
}

/// The type of errors returned by the Kubernetes API functions that prepare the HTTP request.
#[derive(Debug)]
pub enum RequestError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
/// InitializerConfiguration describes the configuration of initializers.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct InitializerConfiguration {
/// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
pub api_version: Option<String>,

/// Initializers is a list of resources and their default initializers Order-sensitive. When merging multiple InitializerConfigurations, we sort the initializers from different InitializerConfigurations by the name of the InitializerConfigurations; the order of the initializers from the same InitializerConfiguration is preserved.
pub initializers: Option<Vec<::v1_12::api::admissionregistration::v1alpha1::Initializer>>,

/// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
pub kind: Option<String>,

/// Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.
pub metadata: Option<::v1_12::apimachinery::pkg::apis::meta::v1::ObjectMeta>,
}
Expand Down Expand Up @@ -921,13 +915,23 @@ impl ::Response for WatchAdmissionregistrationV1alpha1InitializerConfigurationLi

// End admissionregistration.k8s.io/v1alpha1/InitializerConfiguration

impl ::TypeMeta for InitializerConfiguration {
fn api_version() -> &'static str {
"admissionregistration.k8s.io/v1alpha1"
}

fn kind() -> &'static str {
"InitializerConfiguration"
}
}

impl<'de> ::serde::Deserialize<'de> for InitializerConfiguration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'de> {
#[allow(non_camel_case_types)]
enum Field {
Key_api_version,
Key_initializers,
Key_kind,
Key_initializers,
Key_metadata,
Other,
}
Expand All @@ -946,8 +950,8 @@ impl<'de> ::serde::Deserialize<'de> for InitializerConfiguration {
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: ::serde::de::Error {
Ok(match v {
"apiVersion" => Field::Key_api_version,
"initializers" => Field::Key_initializers,
"kind" => Field::Key_kind,
"initializers" => Field::Key_initializers,
"metadata" => Field::Key_metadata,
_ => Field::Other,
})
Expand All @@ -968,25 +972,31 @@ impl<'de> ::serde::Deserialize<'de> for InitializerConfiguration {
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: ::serde::de::MapAccess<'de> {
let mut value_api_version: Option<String> = None;
let mut value_initializers: Option<Vec<::v1_12::api::admissionregistration::v1alpha1::Initializer>> = None;
let mut value_kind: Option<String> = None;
let mut value_metadata: Option<::v1_12::apimachinery::pkg::apis::meta::v1::ObjectMeta> = None;

while let Some(key) = ::serde::de::MapAccess::next_key::<Field>(&mut map)? {
match key {
Field::Key_api_version => value_api_version = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Key_api_version => {
let value_api_version: String = ::serde::de::MapAccess::next_value(&mut map)?;
if value_api_version != <Self::Value as ::TypeMeta>::api_version() {
return Err(::serde::de::Error::invalid_value(::serde::de::Unexpected::Str(&value_api_version), &<Self::Value as ::TypeMeta>::api_version()));
}
},
Field::Key_kind => {
let value_kind: String = ::serde::de::MapAccess::next_value(&mut map)?;
if value_kind != <Self::Value as ::TypeMeta>::kind() {
return Err(::serde::de::Error::invalid_value(::serde::de::Unexpected::Str(&value_kind), &<Self::Value as ::TypeMeta>::kind()));
}
},
Field::Key_initializers => value_initializers = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Key_kind => value_kind = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Key_metadata => value_metadata = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Other => { let _: ::serde::de::IgnoredAny = ::serde::de::MapAccess::next_value(&mut map)?; },
}
}

Ok(InitializerConfiguration {
api_version: value_api_version,
initializers: value_initializers,
kind: value_kind,
metadata: value_metadata,
})
}
Expand All @@ -996,8 +1006,8 @@ impl<'de> ::serde::Deserialize<'de> for InitializerConfiguration {
"InitializerConfiguration",
&[
"apiVersion",
"initializers",
"kind",
"initializers",
"metadata",
],
Visitor,
Expand All @@ -1010,20 +1020,15 @@ impl ::serde::Serialize for InitializerConfiguration {
let mut state = serializer.serialize_struct(
"InitializerConfiguration",
0 +
self.api_version.as_ref().map_or(0, |_| 1) +
2 +
self.initializers.as_ref().map_or(0, |_| 1) +
self.kind.as_ref().map_or(0, |_| 1) +
self.metadata.as_ref().map_or(0, |_| 1),
)?;
if let Some(value) = &self.api_version {
::serde::ser::SerializeStruct::serialize_field(&mut state, "apiVersion", value)?;
}
::serde::ser::SerializeStruct::serialize_field(&mut state, "apiVersion", <Self as ::TypeMeta>::api_version())?;
::serde::ser::SerializeStruct::serialize_field(&mut state, "kind", <Self as ::TypeMeta>::kind())?;
if let Some(value) = &self.initializers {
::serde::ser::SerializeStruct::serialize_field(&mut state, "initializers", value)?;
}
if let Some(value) = &self.kind {
::serde::ser::SerializeStruct::serialize_field(&mut state, "kind", value)?;
}
if let Some(value) = &self.metadata {
::serde::ser::SerializeStruct::serialize_field(&mut state, "metadata", value)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
/// InitializerConfigurationList is a list of InitializerConfiguration.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct InitializerConfigurationList {
/// APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
pub api_version: Option<String>,

/// List of InitializerConfiguration.
pub items: Vec<::v1_12::api::admissionregistration::v1alpha1::InitializerConfiguration>,

/// Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
pub kind: Option<String>,

/// Standard list metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
pub metadata: Option<::v1_12::apimachinery::pkg::apis::meta::v1::ListMeta>,
}

impl ::TypeMeta for InitializerConfigurationList {
fn api_version() -> &'static str {
"admissionregistration.k8s.io/v1alpha1"
}

fn kind() -> &'static str {
"InitializerConfigurationList"
}
}

impl<'de> ::serde::Deserialize<'de> for InitializerConfigurationList {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'de> {
#[allow(non_camel_case_types)]
enum Field {
Key_api_version,
Key_items,
Key_kind,
Key_items,
Key_metadata,
Other,
}
Expand All @@ -41,8 +45,8 @@ impl<'de> ::serde::Deserialize<'de> for InitializerConfigurationList {
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: ::serde::de::Error {
Ok(match v {
"apiVersion" => Field::Key_api_version,
"items" => Field::Key_items,
"kind" => Field::Key_kind,
"items" => Field::Key_items,
"metadata" => Field::Key_metadata,
_ => Field::Other,
})
Expand All @@ -63,25 +67,31 @@ impl<'de> ::serde::Deserialize<'de> for InitializerConfigurationList {
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: ::serde::de::MapAccess<'de> {
let mut value_api_version: Option<String> = None;
let mut value_items: Option<Vec<::v1_12::api::admissionregistration::v1alpha1::InitializerConfiguration>> = None;
let mut value_kind: Option<String> = None;
let mut value_metadata: Option<::v1_12::apimachinery::pkg::apis::meta::v1::ListMeta> = None;

while let Some(key) = ::serde::de::MapAccess::next_key::<Field>(&mut map)? {
match key {
Field::Key_api_version => value_api_version = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Key_api_version => {
let value_api_version: String = ::serde::de::MapAccess::next_value(&mut map)?;
if value_api_version != <Self::Value as ::TypeMeta>::api_version() {
return Err(::serde::de::Error::invalid_value(::serde::de::Unexpected::Str(&value_api_version), &<Self::Value as ::TypeMeta>::api_version()));
}
},
Field::Key_kind => {
let value_kind: String = ::serde::de::MapAccess::next_value(&mut map)?;
if value_kind != <Self::Value as ::TypeMeta>::kind() {
return Err(::serde::de::Error::invalid_value(::serde::de::Unexpected::Str(&value_kind), &<Self::Value as ::TypeMeta>::kind()));
}
},
Field::Key_items => value_items = Some(::serde::de::MapAccess::next_value(&mut map)?),
Field::Key_kind => value_kind = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Key_metadata => value_metadata = ::serde::de::MapAccess::next_value(&mut map)?,
Field::Other => { let _: ::serde::de::IgnoredAny = ::serde::de::MapAccess::next_value(&mut map)?; },
}
}

Ok(InitializerConfigurationList {
api_version: value_api_version,
items: value_items.ok_or_else(|| ::serde::de::Error::missing_field("items"))?,
kind: value_kind,
metadata: value_metadata,
})
}
Expand All @@ -91,8 +101,8 @@ impl<'de> ::serde::Deserialize<'de> for InitializerConfigurationList {
"InitializerConfigurationList",
&[
"apiVersion",
"items",
"kind",
"items",
"metadata",
],
Visitor,
Expand All @@ -105,18 +115,13 @@ impl ::serde::Serialize for InitializerConfigurationList {
let mut state = serializer.serialize_struct(
"InitializerConfigurationList",
0 +
self.api_version.as_ref().map_or(0, |_| 1) +
2 +
1 +
self.kind.as_ref().map_or(0, |_| 1) +
self.metadata.as_ref().map_or(0, |_| 1),
)?;
if let Some(value) = &self.api_version {
::serde::ser::SerializeStruct::serialize_field(&mut state, "apiVersion", value)?;
}
::serde::ser::SerializeStruct::serialize_field(&mut state, "apiVersion", <Self as ::TypeMeta>::api_version())?;
::serde::ser::SerializeStruct::serialize_field(&mut state, "kind", <Self as ::TypeMeta>::kind())?;
::serde::ser::SerializeStruct::serialize_field(&mut state, "items", &self.items)?;
if let Some(value) = &self.kind {
::serde::ser::SerializeStruct::serialize_field(&mut state, "kind", value)?;
}
if let Some(value) = &self.metadata {
::serde::ser::SerializeStruct::serialize_field(&mut state, "metadata", value)?;
}
Expand Down
Loading

0 comments on commit b25b6fe

Please sign in to comment.