Arnavion / k8s-openapi

Rust definitions of the resource types in the Kubernetes client API
Apache License 2.0
373 stars 42 forks source link

can't create a namespace #123

Closed that-soda-pop closed 2 years ago

that-soda-pop commented 2 years ago

I am unable to create a new namespace using the api, I assume this is due to a check request done, since the error received is: Unsuccessful: ErrorResponse { status: "Failure", message: "namespaces \"my-new-8402e3c0-c4a2-40b7-827a-ecc3371380a7\" not found", reason: "NotFound", code: 404

I was able to serialize the json into a string and apply it using kubectl, looking at -v=10 i can see that kubectl sends a request to /api/v1/namespaces/my-new-8402e3c0-c4a2-40b7-827a-ecc3371380a7 and get s 404, however a post message is sent afterwards to create the namespace regardless.

If there's a different way to create a namespace please let me know

and thank you for the hard work.

Arnavion commented 2 years ago

Show your code.

that-soda-pop commented 2 years ago

       async fn create_namespace_if_missing(&self, namespace_name: &str) -> anyhow::Result<()> {
        let client = self.client.clone();

        let s: Api<Namespace> = Api::namespaced(client, "default");

        if s.get(namespace_name).await.is_err() {
            debug!("missing namespace");
            let prj = Namespace {
                metadata: ObjectMeta {
                    name: Some(namespace_name.to_string()),
                    labels: bt_helper("name", namespace_name),
                    ..Default::default()
                },
                ..Default::default()
            };

            dbg!(serde_json::to_string(&prj).unwrap());
            debug!("Creating");
            let mut b = CreateOptional::default();
            b.field_manager = Some("kubectl-client-side-apply");

            match Namespace::create(&prj, b) {
                Ok((x, y)) => {
                    debug!("{:?}", x);
                    debug!("{:?}", String::from_utf8_lossy(x.body()));
                    dbg!(y);
                }
                Err(e) => {
                    dbg!(e);
                }
            }
            // let mut a = PostParams::default();
            // a.field_manager = Some("kubectl-client-side-apply".to_owned());
            // a.dry_run = false;
            // s.create(&a, &prj).await?;
        } else {
            debug!("namespace exists");
        }
        Ok(())
    }

fn bt_helper(k: &str, v: &str) -> Option<BTreeMap<String, String>> {
    let mut data = BTreeMap::new();
    data.insert(k.to_string(), v.to_string());
    Some(data)
}
Arnavion commented 2 years ago

A pure k8s-openapi-using solution would've used the x returned by Namespace::create with an HTTP client. Instead you're throwing it away and using s (presumably kube::Api) in the commented-out code. So you need to ask the library you could your s from.

that-soda-pop commented 2 years ago

I see, thank you for the info