kube-rs / kube

Rust Kubernetes client and controller runtime
https://kube.rs
Apache License 2.0
3.03k stars 314 forks source link

trait Metadata not implemented for custom spec #1642

Closed drewstone closed 2 hours ago

drewstone commented 11 hours ago

Hello,

I'm new to kubernetes and should preface by saying I'm using Claude to help create a system for deploying containers through Kube API.

I'm deriving a CustomResource but Rust complains that I'm not implementing the Metadata trait. Everything I read online says this derive should generate and implement these traits automatically, but I have had no success debugging manually.

I'm defining my service spec as follows:

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, CustomResource, JsonSchema)]
#[kube(
    group = "tangle.tools",
    version = "v1",
    kind = "EnvioIndexer",
    status = "ServiceStatus",
    derive = "Default",
    namespaced
)]
pub struct EnvioIndexerSpec {
    pub spec: EnvioIndexerConfig,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ServiceStatus>,
}

and implementing a trait

pub trait ServiceSpec:
    Clone
    + Send
    + Sync
    + Resource<Scope = NamespaceResourceScope>
    + DeserializeOwned
    + JsonSchema
    + Debug
    + Serialize
    + 'static
{
    fn get_name(&self) -> String;
    fn to_deployment_config(&self, namespace: &str) -> DeploymentConfig;
    fn status(&self) -> Option<&ServiceStatus>;
    fn status_mut(&mut self) -> Option<&mut ServiceStatus>;
}
impl ServiceSpec for EnvioIndexerSpec {
    fn get_name(&self) -> String {
        self.spec.name.clone()
    }

    fn to_deployment_config(&self, namespace: &str) -> DeploymentConfig {
        create_envio_deployment_config(&self.spec, namespace)
    }

    fn status(&self) -> Option<&ServiceStatus> {
        self.status.as_ref()
    }

    fn status_mut(&mut self) -> Option<&mut ServiceStatus> {
        self.status.as_mut()
    }
}

Error

error[E0277]: the trait bound `EnvioIndexerSpec: k8s_openapi::Metadata` is not satisfied
  --> src/kubernetes/envio.rs:30:22
   |
30 | impl ServiceSpec for EnvioIndexerSpec {
   |                      ^^^^^^^^^^^^^^^^ the trait `k8s_openapi::Metadata` is not implemented for `EnvioIndexerSpec`, which is required by `EnvioIndexerSpec: kube::Resource`
   |
clux commented 8 hours ago

Hey looks mostly sensible. You need to implement your traits on the generated top level type EnvioIndexer rather than EnvioIndexerSpec, then you should probably get it working.

Elaboration; only the top level spec gets the traits generated. The Spec struct is the "user struct", but it's not the one we use for the Kubernetes API (because it does not have explicit metadata and type information).

Moving this to discussions.