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

k8s-openapi 0.10 fails to yaml serialize crd #349

Closed
clux opened this issue Dec 19, 2020 · 4 comments
Closed

k8s-openapi 0.10 fails to yaml serialize crd #349

clux opened this issue Dec 19, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@clux
Copy link
Member

clux commented Dec 19, 2020

This is master only, because the local version bump has not been released yet.

Reproducing

Derive a CRD, and simply primt the serde_yaml string. Can do this most easily by editing the crd_derive example to print with yaml:

diff --git examples/crd_derive.rs examples/crd_derive.rs
index 5ee24307..33a9c3ca 100644
--- examples/crd_derive.rs
+++ examples/crd_derive.rs
@@ -39,7 +39,7 @@ fn main() {
     });
     foo.status = Some(FooStatus { is_bad: true });
     println!("Spec: {:?}", foo.spec);
-    let crd = serde_json::to_string_pretty(&Foo::crd()).unwrap();
+    let crd = serde_yaml::to_string(&Foo::crd()).unwrap();
     println!("Foo CRD: \n{}", crd);
 }
 

causes the following backtrace:

thread 'main' panicked at 'attempted to leave type `linked_hash_map::Node<yaml::Yaml, yaml::Yaml>` uninitialized, which is invalid', /home/clux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:658:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/std/src/panicking.rs:483
   1: core::panicking::panic_fmt
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/core/src/panicking.rs:85
   2: core::panicking::panic
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/core/src/panicking.rs:50
   3: core::mem::uninitialized
             at /home/clux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:658
   4: linked_hash_map::LinkedHashMap<K,V,S>::ensure_guard_node
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/linked-hash-map-0.5.2/src/lib.rs:174
   5: linked_hash_map::LinkedHashMap<K,V,S>::insert
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/linked-hash-map-0.5.2/src/lib.rs:304
   6: <serde_yaml::ser::SerializeStruct as serde::ser::SerializeStruct>::serialize_field
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:359
   7: <k8s_openapi::v1_18::apiextensions_apiserver::pkg::apis::apiextensions::v1::custom_resource_definition::CustomResourceDefinition as serde::ser::Serialize>::serialize
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/k8s-openapi-0.10.0/src/v1_18/apiextensions_apiserver/pkg/apis/apiextensions/v1/custom_resource_definition.rs:838
   8: serde::ser::impls::<impl serde::ser::Serialize for &T>::serialize
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.115/src/ser/impls.rs:390
   9: serde_yaml::ser::to_yaml
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:448
  10: serde_yaml::ser::to_writer
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:394
  11: serde_yaml::ser::to_vec
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:411
  12: serde_yaml::ser::to_string
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:423
  13: crd_derive::main
             at ./examples/crd_derive.rs:42
  14: core::ops::function::FnOnce::call_once
             at /home/clux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227
@clux
Copy link
Member Author

clux commented Dec 19, 2020

This is a problem for the new schema feature in #264 where people would definitely use this fn to apply, so ought to solve this before a new release after its merge.

Will try to make a smaller example and raise against k8s-openapi.

@clux clux added the bug Something isn't working label Dec 19, 2020
@clux clux self-assigned this Dec 19, 2020
@clux
Copy link
Member Author

clux commented Dec 19, 2020

Minimal kube-derive example with k8s-openapi 0.10 with 1_19 feature:

#[derive(kube::CustomResource, serde::Serialize, serde::Deserialize, Debug, Clone)]
#[kube(group = "clux.dev", version = "v1", kind = "Foo")]
pub struct MyFoo {
    name: String,
}
fn main() {
    let crd = serde_yaml::to_string(&Foo::crd()).unwrap();
    println!("Foo CRD: \n{}", crd);
}

trace:

thread 'main' panicked at 'attempted to leave type `linked_hash_map::Node<yaml::Yaml, yaml::Yaml>` uninitialized, which is invalid', /home/clux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:658:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/std/src/panicking.rs:483
   1: core::panicking::panic_fmt
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/core/src/panicking.rs:85
   2: core::panicking::panic
             at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/core/src/panicking.rs:50
   3: core::mem::uninitialized
             at /home/clux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:658
   4: linked_hash_map::LinkedHashMap<K,V,S>::ensure_guard_node
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/linked-hash-map-0.5.2/src/lib.rs:174
   5: linked_hash_map::LinkedHashMap<K,V,S>::insert
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/linked-hash-map-0.5.2/src/lib.rs:304
   6: <serde_yaml::ser::SerializeStruct as serde::ser::SerializeStruct>::serialize_field
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:359
   7: <k8s_openapi::v1_19::apiextensions_apiserver::pkg::apis::apiextensions::v1::custom_resource_definition::CustomResourceDefinition as serde::ser::Serialize>::serialize
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/k8s-openapi-0.10.0/src/v1_19/apiextensions_apiserver/pkg/apis/apiextensions/v1/custom_resource_definition.rs:838
   8: serde::ser::impls::<impl serde::ser::Serialize for &T>::serialize
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.115/src/ser/impls.rs:390
   9: serde_yaml::ser::to_yaml
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:448
  10: serde_yaml::ser::to_writer
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:394
  11: serde_yaml::ser::to_vec
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:411
  12: serde_yaml::ser::to_string
             at /home/clux/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_yaml-0.8.13/src/ser.rs:423
  13: crd_derive::main
             at ./examples/crd_derive.rs:7
  14: core::ops::function::FnOnce::call_once
             at /home/clux/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227

@clux
Copy link
Member Author

clux commented Dec 19, 2020

crashing lines in k8s-openapi:

impl serde::Serialize for CustomResourceDefinition {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct(
            <Self as crate::Resource>::KIND,
            4 + self.status.as_ref().map_or(0, |_| 1),
        )?;
        serde::ser::SerializeStruct::serialize_field(
            &mut state,
            "apiVersion",
            <Self as crate::Resource>::API_VERSION,
        )?;
        serde::ser::SerializeStruct::serialize_field(
            &mut state,
            "kind",
            <Self as crate::Resource>::KIND,
        )?;
        serde::ser::SerializeStruct::serialize_field(&mut state, "metadata", &self.metadata)?;
        serde::ser::SerializeStruct::serialize_field(&mut state, "spec", &self.spec)?;
        if let Some(value) = &self.status {
            serde::ser::SerializeStruct::serialize_field(&mut state, "status", value)?;
        }
        serde::ser::SerializeStruct::end(state)
    }
}

don't fully understand.

HOWEVER, have reproduced this without kube-derive, so this is almost certainly a bug in k8s-openapi. Will raise an issue there.

Here is a minimal example withouth kube-derive:

use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{CustomResourceDefinition, CustomResourceDefinitionSpec, CustomResourceDefinitionNames};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;

fn main() {
    let crd = CustomResourceDefinition {
        metadata: ObjectMeta {
            name: Some("hello".to_string()),
            ..ObjectMeta::default()
        },
        spec: CustomResourceDefinitionSpec {
            group: "clux.dev".to_string(),
            scope: "Cluster".to_string(),
            names: CustomResourceDefinitionNames {
                plural: "foos".to_string(),
                kind: "Foo".to_string(),
                ..CustomResourceDefinitionNames::default()
            },
            ..CustomResourceDefinitionSpec::default()
        },
        ..CustomResourceDefinition::default()
    };
    let crd = serde_yaml::to_string(&crd).unwrap();
    println!("Foo CRD: \n{}", crd);
}

same crash

@clux
Copy link
Member Author

clux commented Dec 19, 2020

Actually our problem using an old serde_yaml.
cargo upgrade serde_yaml --all fixes it. Will commit a fix in a bit.

@clux clux closed this as completed in c2d9d85 Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant