message: simplify wait for crd creation into fn
diff: #[macro_use] extern crate log;
  use futures::StreamExt;
  -use futures_timer::Delay;
  use kube_derive::CustomResource;
  use serde_derive::{Deserialize, Serialize};
  use serde_yaml;
  -use std::time::Duration;
  use apiexts::CustomResourceDefinition;
  use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1beta1 as apiexts;
  @@ -67,34 +65,8 @@ async fn main() -> anyhow::Result<()> {
  }
  Err(e) => return Err(e.into()),
  }
  - // Wait for the apply to take place (takes a sec or two during first install)
  - let lp = ListParams::default()
  - .fields(&format!("metadata.name={}", "foos.clux.dev")) // our crd only
  - .timeout(10); // should not take long
  - let mut stream = crds.watch(&lp, "0").await?.boxed();
  -
  - while let Some(status) = stream.next().await {
  - match status {
  - WatchEvent::Added(s) => info!("Added {}", Meta::name(&s)),
  - WatchEvent::Modified(s) => {
  - info!("Modify event for {}", Meta::name(&s));
  - if let Some(s) = s.status {
  - if let Some(conds) = s.conditions {
  - if let Some(pcond) = conds.iter().find(|c| c.type_ == "NamesAccepted") {
  - if pcond.status == "True" {
  - info!("crd was accepted: {:?}", pcond);
  - break;
  - }
  - }
  - }
  - }
  - }
  - WatchEvent::Deleted(s) => info!("Deleted {}", Meta::name(&s)),
  - WatchEvent::Error(s) => error!("{}", s),
  - }
  - }
  + wait_for_crd_ready(&crds).await?; // wait for k8s to deal with it
  - Delay::new(Duration::from_secs(2)).await;
  // Start applying foos
  let foos: Api<Foo> = Api::namespaced(client.clone(), &namespace);
  @@ -129,3 +101,31 @@ async fn main() -> anyhow::Result<()> {
  Ok(())
  }
  +
  +async fn wait_for_crd_ready(crds: &Api<CustomResourceDefinition>) -> anyhow::Result<()> {
  + if crds.get("foos.clux.dev").await.is_ok() {
  + return Ok(());
  + }
  + // Wait for the apply to take place (takes a sec or two during first install)
  + let lp = ListParams::default()
  + .fields(&format!("metadata.name={}", "foos.clux.dev")) // our crd only
  + .timeout(5); // should not take long
  + let mut stream = crds.watch(&lp, "0").await?.boxed();
  +
  + while let Some(status) = stream.next().await {
  + if let WatchEvent::Modified(s) = status {
  + info!("Modify event for {}", Meta::name(&s));
  + if let Some(s) = s.status {
  + if let Some(conds) = s.conditions {
  + if let Some(pcond) = conds.iter().find(|c| c.type_ == "NamesAccepted") {
  + if pcond.status == "True" {
  + info!("crd was accepted: {:?}", pcond);
  + return Ok(());
  + }
  + }
  + }
  + }
  + }
  + }
  + Err(anyhow::anyhow!("Timed out waiting for crd to become accepted"))
  +}
  