Skip to content

Commit

Permalink
Use k8s_openapi generated resources for inner types
Browse files Browse the repository at this point in the history
This is based on the conversion in Issue #4
  • Loading branch information
Anthony Nowell committed Feb 4, 2018
1 parent 93b4c0b commit 6d7d51e
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 173 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ url_serde = "0.2.0"
openssl = "0.9.15"
walkdir = "1.0.7"
reqwest = "0.8.0"
k8s-openapi = { git = "https://github.com/Arnavion/k8s-openapi-codegen", branch = "master" }
4 changes: 3 additions & 1 deletion src/clients/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use url::Url;
use std::borrow::Borrow;
use walkdir::WalkDir;
use errors::*;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;


#[derive(Clone)]
pub struct KubeLowLevel {
Expand All @@ -26,7 +28,7 @@ pub struct KubeLowLevel {
struct MinimalResource {
api_version: String,
kind: Kind,
metadata: Metadata,
metadata: ObjectMeta,
}

impl KubeLowLevel {
Expand Down
1 change: 1 addition & 0 deletions src/clients/resource_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub trait WriteClient {
///
/// ```no_run
/// # use kubeclient::prelude::*;
/// # use kubeclient::resources::ConfigMap;
/// let kube = Kubernetes::load_conf("admin.conf")?;
/// let mut cfg_map = ConfigMap::new("stage-config")?;
/// cfg_map.insert("environment", "production");
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! }
//!
//! for node in kube.nodes().list()? {
//! println!("Found node: {}", node.metadata.name);
//! println!("Found node: {}", node.metadata.name.unwrap());
//! }
//! }
//! ```
Expand All @@ -33,6 +33,7 @@
extern crate base64;
extern crate chrono;
extern crate openssl;
extern crate k8s_openapi;
extern crate reqwest;
extern crate serde;
extern crate serde_json;
Expand Down
8 changes: 6 additions & 2 deletions src/resources/config_map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use std::collections::BTreeMap;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;

pub(crate) static CONFIG_MAP_INFO: KindInfo = KindInfo {
plural: "configmaps",
Expand All @@ -9,14 +10,17 @@ pub(crate) static CONFIG_MAP_INFO: KindInfo = KindInfo {

#[derive(Serialize, Deserialize, Debug)]
pub struct ConfigMap {
/// Data contains the configuration data. Each key must consist of alphanumeric characters, '-', '_' or '.'.
data: BTreeMap<String, String>,
metadata: Metadata,

/// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
metadata: ObjectMeta,
}

impl ConfigMap {
pub fn new(name: &str) -> ConfigMap {
let data = BTreeMap::new();
let metadata = Metadata{ name: Some(name.to_owned()), ..Default::default() };
let metadata = ObjectMeta{ name: Some(name.to_owned()), ..Default::default() };
ConfigMap { data, metadata }
}

Expand Down
27 changes: 9 additions & 18 deletions src/resources/daemon_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use serde_json::Value;
use k8s_openapi::api::apps::v1beta2::{DaemonSetSpec, DaemonSetStatus};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;

pub(crate) static DAEMON_SET_INFO: KindInfo = KindInfo {
plural: "daemonsets",
Expand All @@ -9,25 +10,15 @@ pub(crate) static DAEMON_SET_INFO: KindInfo = KindInfo {

#[derive(Serialize, Deserialize, Debug)]
pub struct DaemonSet {
/// The desired behavior of this daemon set. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
pub spec: DaemonSetSpec,
pub metadata: Metadata,
pub status: Option<DaemonSetStatus>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct DaemonSetSpec {
pub selector: Option<Value>,
pub template: Option<Value>,
}
/// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
pub metadata: ObjectMeta,

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct DaemonSetStatus {
pub current_number_scheduled : u32,
pub desired_number_scheduled : u32,
pub number_misscheduled : u32,
pub number_ready : u32,
/// The current status of this daemon set. This data may be out of date by some window of time. Populated by the system. Read-only. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<DaemonSetStatus>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand All @@ -38,7 +29,7 @@ pub struct DaemonSetList {
impl DaemonSet {
pub fn new(name: &str) -> DaemonSet {
let spec = DaemonSetSpec::default();
let metadata = Metadata{ name: Some(name.to_owned()), ..Default::default() };
let metadata = ObjectMeta{ name: Some(name.to_owned()), ..Default::default() };
DaemonSet { spec, metadata, status: None }
}
}
Expand Down
63 changes: 24 additions & 39 deletions src/resources/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,78 +1,63 @@
use super::*;
use k8s_openapi::api::apps::v1::{DeploymentSpec, DeploymentStatus};
use k8s_openapi::api::apps::v1beta1::{ScaleSpec, ScaleStatus};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;

pub(crate) static DEPLOYMENT_INFO: KindInfo = KindInfo {
plural: "deployments",
default_namespace: Some("default"),
api: V1_BETA_API,
};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Deployment {
/// Specification of the desired behavior of the Deployment.
pub spec: DeploymentSpec,
pub metadata: Metadata,
pub status: Option<DeploymentStatus>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct DeploymentSpec {
pub min_ready_seconds: Option<u32>,
pub paused: Option<bool>,
pub progress_deadline_seconds: Option<u32>,
pub replicas: Option<u32>,
pub revision_history_limit: Option<u32>,
// pub rollback_to: Option<RollbackConfig>,
// pub selector: Option<LabelSelector>,
// pub strategy: Option<DeploymentStrategy>,
// pub template: Option<PodTemplateSpec>,
}
/// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
pub metadata: ObjectMeta,

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct DeploymentStatus {
// pub conditions: Option<Vec<DeploymentCondition>>,
pub observed_generation: u32,
pub replicas: u32,
pub unavailable_replicas: Option<u32>,
pub updated_replicas: u32,
/// Most recently observed status of the Deployment.
pub status: Option<DeploymentStatus>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct Scale {
/// defines the behavior of the scale. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status.
pub spec: ScaleSpec,
pub metadata: Metadata,
// pub status: Option<ScaleStatus>,

/// Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.
pub metadata: ObjectMeta,

/// current status of the scale. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status. Read-only.
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<ScaleStatus>,
}

impl Scale {
pub(crate) fn replicas(namespace: &str, name: &str, count: u32) -> Scale {
Scale {
spec: ScaleSpec { replicas: count },
metadata: Metadata {
spec: ScaleSpec { replicas: Some(count as i32) },
metadata: ObjectMeta {
name: Some(name.to_owned()),
namespace: Some(namespace.to_owned()),
..Default::default() }
..Default::default()
},
..Default::default()
}
}
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ScaleSpec {
pub replicas: u32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct DeploymentList {
items: Vec<Deployment>,
}

impl Deployment {
pub fn new(name: &str) -> Deployment {
let spec = DeploymentSpec::default();
let metadata = Metadata{ name: Some(name.to_owned()), ..Default::default() };
Deployment { spec, metadata, status: None }
let metadata = ObjectMeta{ name: Some(name.to_owned()), ..Default::default() };
Deployment { metadata, ..Default::default() }
}
}

Expand Down
15 changes: 2 additions & 13 deletions src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use self::network_policy::*;
pub use self::pod::*;
pub use self::service::*;

use chrono::{DateTime, Utc};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::fmt;
Expand Down Expand Up @@ -79,22 +79,11 @@ pub trait ListableResource: Resource {
pub struct Status {
pub kind: String,
pub api_version: String,
pub metadata: Metadata,
pub metadata: ObjectMeta,
pub status: String,
pub message: String,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
pub name: Option<String>,
pub namespace: Option<String>,
pub uid: Option<String>,
pub creation_timestamp: Option<DateTime<Utc>>,
pub annotations: Option<BTreeMap<String, String>>,
pub labels: Option<BTreeMap<String, String>>,
}

#[derive(Clone, Debug, Default)]
pub struct ListQuery {
field_selector: Option<String>,
Expand Down
20 changes: 8 additions & 12 deletions src/resources/network_policy.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use super::*;
use k8s_openapi::api::networking::v1::NetworkPolicySpec;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;

pub(crate) static NETWORK_POLICY_INFO: KindInfo = KindInfo {
plural: "networkpolicies",
default_namespace: Some("default"),
api: V1_BETA_API,
};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct NetworkPolicy {
/// Specification of the desired behavior for this NetworkPolicy.
pub spec: NetworkPolicySpec,
pub metadata: Metadata,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct NetworkPolicySpec {
// pub ingress: Option<Vec<NetworkPolicyIngressRule>>,
// pub podSelector : Option<LabelSelector>,
/// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
pub metadata: ObjectMeta,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand All @@ -25,12 +23,10 @@ pub struct NetworkPolicyList {
}



impl NetworkPolicy {
pub fn new(name: &str) -> NetworkPolicy {
let spec = NetworkPolicySpec::default();
let metadata = Metadata{ name: Some(name.to_owned()), ..Default::default() };
NetworkPolicy { spec, metadata }
let metadata = ObjectMeta{ name: Some(name.to_owned()), ..Default::default() };
NetworkPolicy { metadata, ..Default::default() }
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/resources/node.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use super::*;
use k8s_openapi::api::core::v1::{NodeSpec, NodeStatus};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;

pub(crate) static NODE_INFO: KindInfo = KindInfo {
plural: "nodes",
default_namespace: None,
api: V1_API,
};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct Node {
/// Spec defines the behavior of a node. https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
pub spec: NodeSpec,
pub metadata: Metadata,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct NodeSpec {
#[serde(rename = "podCIDR")]
pub pod_cidr: Option<String>,
#[serde(rename = "providerID")]
pub provider_id: Option<String>,
/// Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
pub metadata: ObjectMeta,

/// Most recently observed status of the node. Populated by the system. Read-only.
/// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<NodeStatus>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand All @@ -27,9 +29,8 @@ pub struct NodeList {

impl Node {
pub fn new(name: &str) -> Node {
let spec = NodeSpec::default();
let metadata = Metadata{ name: Some(name.to_owned()), ..Default::default() };
Node { spec, metadata }
let metadata = ObjectMeta{ name: Some(name.to_owned()), ..Default::default() };
Node { metadata, ..Default::default() }
}
}

Expand Down
Loading

0 comments on commit 6d7d51e

Please sign in to comment.