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

async/await, 2018 edition, updated deps #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

brigand
Copy link

@brigand brigand commented Apr 1, 2020

This brings the crate up to date with changes in the ecosystem.

The changes here backwards-incompatible, but allow it to be easily used with recent tokio (std::future based) and k8s-openapi-codegen versions (previously broken by a switch to [email protected]).

The file system I/O is still done with blocking APIs, as the code looks tricky to migrate. The consumer can schedule that part with spawn_blocking if needed.

Tests and the list_pod example work. I haven't tested the incluster_config example.

@brigand
Copy link
Author

brigand commented Apr 8, 2020

I've learned that more work is needed to properly support the newer version of k8s-openapi.

Properly making the request is more like the following, where the tuple argument is returned by the k8s-openapi functions. This has the benefit of encoding the request/response types in one function call, rather than manually specifying the expected response type.

use tokio::stream::StreamExt;
use k8s_openapi as kapi;

// ...snip

    pub async fn request<T>(
        &self,
        request: (
            http::Request<Vec<u8>>,
            fn(_: http::StatusCode) -> kapi::ResponseBody<T>,
        ),
    ) -> Result<T>
    where
        T: kapi::Response,
    {
        let (parts, body) = request.0.into_parts();
        let uri_str = format!("{}{}", self.host, parts.uri);
        let mut req = match parts.method {
            http::Method::GET => self.client.get(&uri_str),
            http::Method::POST => self.client.post(&uri_str),
            http::Method::DELETE => self.client.delete(&uri_str),
            http::Method::PUT => self.client.put(&uri_str),
            method => {
                bail!(other(format!("Unexpected HTTP method {:?}", method)));
            }
        };

        let res = req.body(body).send().await?;
        let mut k8s_res = request.1(res.status());

        let mut stream = res.bytes_stream();

        while let Some(item) = stream.next().await {
            if let Ok(item) = item {
                k8s_res.append_slice(item.as_ref());
            }
        }

        let result = k8s_res.parse()?;

        Ok(result)
    }

Currently the PR probably shouldn't be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant