Skip to content

Commit

Permalink
Merge pull request #486 from MikailBag/break-resource-even-more
Browse files Browse the repository at this point in the history
Change Resource trait
  • Loading branch information
clux authored Apr 6, 2021
2 parents fa543b5 + b73cb9c commit a1a50cc
Show file tree
Hide file tree
Showing 29 changed files with 210 additions and 142 deletions.
10 changes: 3 additions & 7 deletions examples/configmap_reflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::core::v1::ConfigMap;
use kube::{
api::{Api, ListParams, Resource},
api::{Api, ListParams, ResourceExt},
Client,
};
use kube_runtime::{reflector, reflector::Store, utils::try_flatten_applied, watcher};
Expand All @@ -12,11 +12,7 @@ fn spawn_periodic_reader(reader: Store<ConfigMap>) {
loop {
// Periodically read our state
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
let cms: Vec<_> = reader
.state()
.iter()
.map(|obj| Resource::name(obj).clone())
.collect();
let cms: Vec<_> = reader.state().iter().map(|obj| obj.name()).collect();
info!("Current configmaps: {:?}", cms);
}
});
Expand All @@ -40,7 +36,7 @@ async fn main() -> anyhow::Result<()> {

let mut applied_events = try_flatten_applied(rf).boxed_local();
while let Some(event) = applied_events.try_next().await? {
info!("Applied {}", Resource::name(&event))
info!("Applied {}", event.name())
}
Ok(())
}
30 changes: 15 additions & 15 deletions examples/crd_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use apiexts::CustomResourceDefinition;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1beta1 as apiexts;

use kube::{
api::{Api, DeleteParams, ListParams, Patch, PatchParams, PostParams, Resource},
api::{Api, DeleteParams, ListParams, Patch, PatchParams, PostParams, ResourceExt},
Client, CustomResource,
};

Expand Down Expand Up @@ -50,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
res.map_left(|o| {
info!(
"Deleting {}: ({:?})",
Resource::name(&o),
o.name(),
o.status.unwrap().conditions.unwrap().last()
);
})
Expand All @@ -69,7 +69,7 @@ async fn main() -> anyhow::Result<()> {
let patch_params = PatchParams::default();
match crds.create(&pp, &foocrd).await {
Ok(o) => {
info!("Created {} ({:?})", Resource::name(&o), o.status.unwrap());
info!("Created {} ({:?})", o.name(), o.status.unwrap());
debug!("Created CRD: {:?}", o.spec);
}
Err(kube::Error::Api(ae)) => assert_eq!(ae.code, 409), // if you skipped delete, for instance
Expand All @@ -89,8 +89,8 @@ async fn main() -> anyhow::Result<()> {
replicas: 1,
});
let o = foos.create(&pp, &f1).await?;
assert_eq!(Resource::name(&f1), Resource::name(&o));
info!("Created {}", Resource::name(&o));
assert_eq!(ResourceExt::name(&f1), ResourceExt::name(&o));
info!("Created {}", o.name());

// Verify we can get it
info!("Get Foo baz");
Expand All @@ -105,7 +105,7 @@ async fn main() -> anyhow::Result<()> {
"metadata": {
"name": "baz",
// Updates need to provide our last observed version:
"resourceVersion": Resource::resource_ver(&f1cpy),
"resourceVersion": f1cpy.resource_version(),
},
"spec": { "name": "baz", "info": "new baz", "replicas": 1 },
}))?;
Expand All @@ -129,7 +129,7 @@ async fn main() -> anyhow::Result<()> {
f2.status = Some(FooStatus::default());

let o = foos.create(&pp, &f2).await?;
info!("Created {}", Resource::name(&o));
info!("Created {}", o.name());

// Update status on qux
info!("Replace Status on Foo instance qux");
Expand All @@ -139,12 +139,12 @@ async fn main() -> anyhow::Result<()> {
"metadata": {
"name": "qux",
// Updates need to provide our last observed version:
"resourceVersion": Resource::resource_ver(&o),
"resourceVersion": o.resource_version(),
},
"status": FooStatus { is_bad: true, replicas: 0 }
});
let o = foos.replace_status("qux", &pp, serde_json::to_vec(&fs)?).await?;
info!("Replaced status {:?} for {}", o.status, Resource::name(&o));
info!("Replaced status {:?} for {}", o.status, o.name());
assert!(o.status.unwrap().is_bad);

info!("Patch Status on Foo instance qux");
Expand All @@ -154,12 +154,12 @@ async fn main() -> anyhow::Result<()> {
let o = foos
.patch_status("qux", &patch_params, &Patch::Merge(&fs))
.await?;
info!("Patched status {:?} for {}", o.status, Resource::name(&o));
info!("Patched status {:?} for {}", o.status, o.name());
assert!(!o.status.unwrap().is_bad);

info!("Get Status on Foo instance qux");
let o = foos.get_status("qux").await?;
info!("Got status {:?} for {}", o.status, Resource::name(&o));
info!("Got status {:?} for {}", o.status, o.name());
assert!(!o.status.unwrap().is_bad);

// Check scale subresource:
Expand All @@ -173,7 +173,7 @@ async fn main() -> anyhow::Result<()> {
"spec": { "replicas": 2 }
});
let o = foos.patch_scale("qux", &patch_params, &Patch::Merge(&fs)).await?;
info!("Patched scale {:?} for {}", o.spec, Resource::name(&o));
info!("Patched scale {:?} for {}", o.spec, o.name());
assert_eq!(o.status.unwrap().replicas, 1);
assert_eq!(o.spec.unwrap().replicas.unwrap(), 2); // we only asked for more

Expand All @@ -183,7 +183,7 @@ async fn main() -> anyhow::Result<()> {
"spec": { "info": "patched qux" }
});
let o = foos.patch("qux", &patch_params, &Patch::Merge(&patch)).await?;
info!("Patched {} with new name: {}", Resource::name(&o), o.spec.name);
info!("Patched {} with new name: {}", o.name(), o.spec.name);
assert_eq!(o.spec.info, "patched qux");
assert_eq!(o.spec.name, "qux"); // didn't blat existing params

Expand All @@ -198,7 +198,7 @@ async fn main() -> anyhow::Result<()> {
// Cleanup the full collection - expect a wait
match foos.delete_collection(&dp, &lp).await? {
Left(list) => {
let deleted: Vec<_> = list.iter().map(Resource::name).collect();
let deleted: Vec<_> = list.iter().map(ResourceExt::name).collect();
info!("Deleting collection of foos: {:?}", deleted);
}
Right(status) => {
Expand All @@ -211,7 +211,7 @@ async fn main() -> anyhow::Result<()> {
Left(o) => {
info!(
"Deleting {} CRD definition: {:?}",
Resource::name(&o),
o.name(),
o.status.unwrap().conditions.unwrap().last()
);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/crd_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use apiexts::CustomResourceDefinition;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1 as apiexts;

use kube::{
api::{Api, ListParams, Patch, PatchParams, Resource, WatchEvent},
api::{Api, ListParams, Patch, PatchParams, ResourceExt, WatchEvent},
Client, CustomResource,
};

Expand Down Expand Up @@ -59,7 +59,7 @@ async fn main() -> anyhow::Result<()> {
info!("Applying 1: \n{}", serde_yaml::to_string(&foo)?);
let o = foos.patch("baz", &ssapply, &Patch::Apply(&foo)).await?;
// NB: kubernetes < 1.20 will fail to admit scale subresources - see #387
info!("Applied 1 {}: {:?}", Resource::name(&o), o.spec);
info!("Applied 1 {}: {:?}", o.name(), o.spec);

// 2. Apply from partial json!
let patch = serde_json::json!({
Expand All @@ -73,7 +73,7 @@ async fn main() -> anyhow::Result<()> {

info!("Applying 2: \n{}", serde_yaml::to_string(&patch)?);
let o2 = foos.patch("baz", &ssapply, &Patch::Apply(patch)).await?;
info!("Applied 2 {}: {:?}", Resource::name(&o2), o2.spec);
info!("Applied 2 {}: {:?}", o2.name(), o2.spec);

Ok(())
}
Expand All @@ -90,7 +90,7 @@ async fn wait_for_crd_ready(crds: &Api<CustomResourceDefinition>) -> anyhow::Res

while let Some(status) = stream.try_next().await? {
if let WatchEvent::Modified(s) = status {
info!("Modify event for {}", Resource::name(&s));
info!("Modify event for {}", s.name());
if let Some(s) = s.status {
if let Some(conds) = s.conditions {
if let Some(pcond) = conds.iter().find(|c| c.type_ == "NamesAccepted") {
Expand Down
6 changes: 3 additions & 3 deletions examples/crd_reflector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use] extern crate log;
use futures::{StreamExt, TryStreamExt};
use kube::{
api::{Api, ListParams, Resource},
api::{Api, ListParams, ResourceExt},
Client, CustomResource,
};
use kube_runtime::{reflector, utils::try_flatten_applied, watcher};
Expand Down Expand Up @@ -33,13 +33,13 @@ async fn main() -> anyhow::Result<()> {
loop {
// Periodically read our state
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
let crds = reader.state().iter().map(Resource::name).collect::<Vec<_>>();
let crds = reader.state().iter().map(ResourceExt::name).collect::<Vec<_>>();
info!("Current crds: {:?}", crds);
}
});
let mut rfa = try_flatten_applied(rf).boxed();
while let Some(event) = rfa.try_next().await? {
info!("Applied {}", Resource::name(&event));
info!("Applied {}", event.name());
}
Ok(())
}
6 changes: 3 additions & 3 deletions examples/deployment_reflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::apps::v1::Deployment;
use kube::{
api::{Api, ListParams, Resource},
api::{Api, ListParams, ResourceExt},
Client,
};
use kube_runtime::{reflector, utils::try_flatten_applied, watcher};
Expand All @@ -29,7 +29,7 @@ async fn main() -> anyhow::Result<()> {
tokio::spawn(async move {
loop {
// Periodically read our state
let deploys: Vec<_> = reader.state().iter().map(Resource::name).collect();
let deploys: Vec<_> = reader.state().iter().map(ResourceExt::name).collect();
info!("Current deploys: {:?}", deploys);
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
}
Expand All @@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> {
// We can look at the events we want and use it as a watcher
let mut rfa = try_flatten_applied(rf).boxed();
while let Some(event) = rfa.try_next().await? {
info!("Applied {}", Resource::name(&event));
info!("Applied {}", event.name());
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/dynamic_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use k8s_openapi::apimachinery::pkg::apis::meta::v1::APIResourceList;
use kube::{
api::{Api, DynamicObject, GroupVersionKind, Resource},
api::{Api, DynamicObject, GroupVersionKind, ResourceExt},
Client,
};
use log::{info, warn};
Expand Down
4 changes: 2 additions & 2 deletions examples/dynamic_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::prelude::*;
use kube::{
api::{DynamicObject, GroupVersionKind, ListParams, Resource},
api::{DynamicObject, GroupVersionKind, ListParams, ResourceExt},
Api, Client,
};
use kube_runtime::{utils::try_flatten_applied, watcher};
Expand All @@ -26,7 +26,7 @@ async fn main() -> anyhow::Result<()> {
let watcher = watcher(api, ListParams::default());
try_flatten_applied(watcher)
.try_for_each(|p| async move {
log::info!("Applied: {}", Resource::name(&p));
log::info!("Applied: {}", p.name());
Ok(())
})
.await?;
Expand Down
10 changes: 5 additions & 5 deletions examples/job_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use k8s_openapi::api::batch::v1::Job;
use serde_json::json;

use kube::{
api::{Api, DeleteParams, ListParams, PostParams, Resource, WatchEvent},
api::{Api, DeleteParams, ListParams, PostParams, ResourceExt, WatchEvent},
Client,
};

Expand Down Expand Up @@ -52,18 +52,18 @@ async fn main() -> anyhow::Result<()> {

while let Some(status) = stream.try_next().await? {
match status {
WatchEvent::Added(s) => info!("Added {}", Resource::name(&s)),
WatchEvent::Added(s) => info!("Added {}", s.name()),
WatchEvent::Modified(s) => {
let current_status = s.status.clone().expect("Status is missing");
match current_status.completion_time {
Some(_) => {
info!("Modified: {} is complete", Resource::name(&s));
info!("Modified: {} is complete", s.name());
break;
}
_ => info!("Modified: {} is running", Resource::name(&s)),
_ => info!("Modified: {} is running", s.name()),
}
}
WatchEvent::Deleted(s) => info!("Deleted {}", Resource::name(&s)),
WatchEvent::Deleted(s) => info!("Deleted {}", s.name()),
WatchEvent::Error(s) => error!("{}", s),
_ => {}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/multi_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use k8s_openapi::api::{
core::v1::{ConfigMap, Secret},
};
use kube::{
api::{Api, ListParams, Resource},
api::{Api, ListParams, ResourceExt},
Client,
};
use kube_runtime::{utils::try_flatten_applied, watcher};
Expand Down Expand Up @@ -38,9 +38,9 @@ async fn main() -> anyhow::Result<()> {
}
while let Some(o) = combo_stream.try_next().await? {
match o {
Watched::Config(cm) => info!("Got configmap: {}", Resource::name(&cm)),
Watched::Deploy(d) => info!("Got deployment: {}", Resource::name(&d)),
Watched::Secret(s) => info!("Got secret: {}", Resource::name(&s)),
Watched::Config(cm) => info!("Got configmap: {}", cm.name()),
Watched::Deploy(d) => info!("Got deployment: {}", d.name()),
Watched::Secret(s) => info!("Got secret: {}", s.name()),
}
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions examples/node_reflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::core::v1::Node;
use kube::{
api::{Api, ListParams, Resource},
api::{Api, ListParams, ResourceExt},
Client,
};
use kube_runtime::{reflector, utils::try_flatten_applied, watcher};
Expand All @@ -25,7 +25,7 @@ async fn main() -> anyhow::Result<()> {
// Periodically read our state in the background
tokio::spawn(async move {
loop {
let nodes = reader.state().iter().map(Resource::name).collect::<Vec<_>>();
let nodes = reader.state().iter().map(ResourceExt::name).collect::<Vec<_>>();
info!("Current {} nodes: {:?}", nodes.len(), nodes);
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
Expand All @@ -34,7 +34,7 @@ async fn main() -> anyhow::Result<()> {
// Drain and log applied events from the reflector
let mut rfa = try_flatten_applied(rf).boxed();
while let Some(event) = rfa.try_next().await? {
info!("Applied {}", Resource::name(&event));
info!("Applied {}", event.name());
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions examples/node_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::core::v1::{Event, Node};
use kube::{
api::{Api, ListParams, Resource},
api::{Api, ListParams, ResourceExt},
Client,
};
use kube_runtime::{utils::try_flatten_applied, watcher};
Expand All @@ -26,7 +26,7 @@ async fn main() -> anyhow::Result<()> {

// A simple node problem detector
async fn check_for_node_failures(events: &Api<Event>, o: Node) -> anyhow::Result<()> {
let name = Resource::name(&o);
let name = o.name();
// Nodes often modify a lot - only print broken nodes
if let Some(true) = o.spec.unwrap().unschedulable {
let failed = o
Expand Down
Loading

0 comments on commit a1a50cc

Please sign in to comment.