Arnavion / k8s-openapi

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

Documentation snippet about kubernetes detection is not working as expected #132

Closed flavio closed 1 year ago

flavio commented 1 year ago

I've just upgraded a project to use rust edition 2021 and suddenly cargo check started to fail because None of the v1_* features are enabled on the k8s-openapi crate.

I've edited the Cargo.toml to add the snippet mentioned inside of the readme:

[features]
__check = ["k8s-openapi/v1_24"]   # This feature is used internally for `cargo check` and `cargo doc`

However this seems to be ignored:

error: failed to run custom build command for `k8s-openapi v0.16.0`

Caused by:
  process didn't exit successfully: `/home/flavio/hacking/kubernetes/kubewarden/policy-evaluator/target/debug/build/k8s-openapi-9c7c24d8b9d76944/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=K8S_OPENAPI_ENABLED_VERSION

  --- stderr
  thread 'main' panicked at '
  None of the v1_* features are enabled on the k8s-openapi crate.

  The k8s-openapi crate requires a feature to be enabled to indicate which version of Kubernetes it should support.

  If you're using k8s-openapi in a binary crate, enable the feature corresponding to the minimum version of API server that you want to support. In case your binary crate does not directly depend on k8s-openapi, add a dependency on k8s-openapi and enable the corresponding feature in it.

  If you're using k8s-openapi in a library crate, add a dev-dependency on k8s-openapi and enable one of the features there. This way the feature will be enabled when buildings tests and examples of your library, but not when building the library itself. In case your library crate does not directly depend on k8s-openapi, add a dev-dependency on k8s-openapi and enable the corresponding feature in it.

  Alternatively, when running commands that do not build dev dependencies such as `cargo check` and `cargo doc`, you can set the `K8S_OPENAPI_ENABLED_VERSION` environment variable, such as `K8S_OPENAPI_ENABLED_VERSION=1.50`.
  Library crates *must not* enable any features in their direct dependency on k8s-openapi, only in their dev-dependency. The choice of which Kubernetes version to support should be left to the final binary crate, so only binary crates should enable a specific feature. If library crates also enabled features, it could cause multiple features to be enabled simultaneously, which k8s-openapi does not support.

  If your library crate only supports a single specific version or a specific range of versions of Kubernetes, please use the k8s_* version-specific macros to emit different code based on which feature gets enabled in the end.
  See the library docs for more details.
          ', /home/flavio/.cargo/registry/src/github.com-1ecc6299db9ec823/k8s-openapi-0.16.0/build.rs:17:42
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Am I doing something wrong?

Environment

If you want you can see the failure happening here: https://github.com/kubewarden/policy-evaluator/pull/216

Arnavion commented 1 year ago

One way to resolve this is to add a feature to your library that enables one of the k8s-openapi v1_* features and is used only when running such commands.

The point of that line in the docs is that you define the __check feature as you did and then run cargo check --features __check. Just cargo check will continue to fail.

I'll change it to not use passive voice so that it's clear that it's not like it's a cargo behavior to use that feature automatically.

flavio commented 1 year ago

I'll change it to not use passive voice so that it's clear that it's not like it's a cargo behavior to use that feature automatically.

Thanks! I was hoping that cargo check would run by default with this feature by default :sweat_smile:

clux commented 1 year ago

@flavio You're likely seeing this because of the resolver change in rust 2021.

What we do to make this more ergonomic for libraries (that are ultimately not responsible for picking kubernetes versions) without breaking cargo check / cargo publish, is to add a dev-dep entry to k8s-openapi for local use:

[dev-dependencies.k8s-openapi]
version = "0.16.0"
default-features = false
features = ["v1_25"]
flavio commented 1 year ago

@clux this is not taken into account by commands like cargo check. This works only when doing cargo test.

The documentation of the project is already clear about this limitation.