Skip to content

feat: Add CRD previewer #1030

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,33 @@ jobs:
- run: cargo test --no-default-features --workspace
- run: cargo test --all-features --workspace

check_crd_previews:
name: Check if CRD previews are up to date
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
- name: Regenerate CRD previews
run: cargo check -p stackable-crd-previewer
- name: Check if committed CRD previews were up to date
run: git diff --exit-code generated-crd-previews
- name: Git Diff showed uncommitted changes
if: ${{ failure() }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
core.setFailed('Committed CRD previews were not up to date, please run "cargo check -p stackable-crd-previewer" and re-commit!')

tests_passed:
name: All tests passed
needs:
- run_udeps
- run_tests
- check_crd_previews
runs-on: ubuntu-latest
steps:
- name: log
Expand Down
3 changes: 2 additions & 1 deletion .scripts/verify_crate_versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ for CRATE in $(find ./crates/ -mindepth 2 -name Cargo.toml -print0 | xargs -0 -n
echo "Ensure the version in ./crates/$CRATE/Cargo.toml matches the version in ./crates/$ASSOCIATED_CRATE/Cargo.toml" >&2
exit 23
)

elif [ "$CRATE" = "stackable-crd-previewer" ]; then
: # Skip stackable-crd-previewer crate for now, it's not a "real"/user-facing crate
else
# Get the latest documented version from the CHANGELOG.md
CHANGELOG_VERSION=$(grep -oE '\[[0-9]+\.[0-9]+\.[0-9]+\]' "./crates/$CRATE/CHANGELOG.md" | head -1 | tr -d '[]')
Expand Down
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions crates/stackable-crd-previewer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- Initial commit ([#1030]).

[#1030]: https://github.com/stackabletech/operator-rs/pull/1030
25 changes: 25 additions & 0 deletions crates/stackable-crd-previewer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "stackable-crd-previewer"
description = "Previews parts of our CRD, so that the effect of changes to operators can be determined more easily"
version = "0.1.0"
authors.workspace = true
license.workspace = true
edition.workspace = true
repository.workspace = true

[dependencies]
stackable-shared = { path = "../stackable-shared" }
stackable-operator = { path = "../stackable-operator" }

snafu.workspace = true

[build-dependencies]
stackable-shared = { path = "../stackable-shared" }
stackable-operator = { path = "../stackable-operator" }
stackable-versioned = { path = "../stackable-versioned" }

kube.workspace = true
schemars.workspace = true
serde_json.workspace = true
serde.workspace = true
snafu.workspace = true
18 changes: 18 additions & 0 deletions crates/stackable-crd-previewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# stackable-crd-previewer

The purpose of this crate is to preview the effects of code changes on our CRDs.

Use the following command to re-generate the CRDs:

```bash
cargo check -p stackable-crd-previewer
```

This should implicitly happen by `rust-analyzer` or `cargo check`, so shouldn't be needed to invoke
explicitly normally.

With an existing Kubernetes context you can run the following command to check if the CRDs are valid:

```bash
kubectl apply -f generated-crd-previews/ --dry-run=server
```
150 changes: 150 additions & 0 deletions crates/stackable-crd-previewer/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use std::fs::create_dir_all;

use snafu::{Report, ResultExt, Snafu};
use stackable_operator::{
YamlSchema,
commons::resources::{JvmHeapLimits, Resources},
config::fragment::Fragment,
crd::{
authentication::core::AuthenticationClass,
listener::{Listener, ListenerClass, PodListeners},
s3::{S3Bucket, S3Connection},
},
k8s_openapi::serde::{Deserialize, Serialize},
kube::{CustomResource, core::crd::MergeError},
role_utils::Role,
schemars::JsonSchema,
shared::yaml::SerializeOptions,
status::condition::ClusterCondition,
};
use stackable_versioned::versioned;

const OPERATOR_VERSION: &str = "0.0.0-dev";
const OUTPUT_DIR: &str = "../../generated-crd-previews";

#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Failed to merge CRD for CRD {crd}"))]
MergeCRD { source: MergeError, crd: String },

#[snafu(display("Failed to create output directory {dir}"))]
CreateOutputDir { source: std::io::Error, dir: String },

#[snafu(display("Failed to write CRD to output file"))]
WriteCRD {
source: stackable_shared::yaml::Error,
},
}

pub fn main() -> Report<Error> {
Report::capture(write_crds)
}

macro_rules! write_crd {
($crd_name:ident, $stored_crd_version:ident) => {
$crd_name::merged_crd($crd_name::$stored_crd_version)
.with_context(|_| MergeCRDSnafu {
crd: stringify!($crd_name),
})?
.write_yaml_schema(
format!("{OUTPUT_DIR}/{}.yaml", stringify!($crd_name)),
OPERATOR_VERSION,
SerializeOptions::default(),
)
.context(WriteCRDSnafu)?;
};
}

pub fn write_crds() -> Result<(), Error> {
create_dir_all(OUTPUT_DIR).with_context(|_| CreateOutputDirSnafu {
dir: OUTPUT_DIR.to_string(),
})?;

// AuthenticationClass::merged_crd(AuthenticationClass::V1Alpha1)
// .with_context(|_| MergeCRDSnafu {
// crd: "AuthenticationClass".to_string(),
// })?
// .write_yaml_schema(
// format!("{OUTPUT_DIR}/{}.yaml", "AuthenticationClass"),
// OPERATOR_VERSION,
// SerializeOptions::default(),
// )
// .context(WriteCRDSnafu)?;

write_crd!(AuthenticationClass, V1Alpha1);
write_crd!(Listener, V1Alpha1);
write_crd!(ListenerClass, V1Alpha1);
write_crd!(PodListeners, V1Alpha1);
write_crd!(S3Bucket, V1Alpha1);
write_crd!(S3Connection, V1Alpha1);

// Also write a CRD with all sorts of common structs
write_crd!(DummyCluster, V1Alpha1);

Ok(())
}

#[versioned(version(name = "v1alpha1"))]
pub mod versioned {

#[derive(Clone, CustomResource, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[versioned(k8s(
group = "zookeeper.stackable.tech",
kind = "DummyCluster",
status = "v1alpha1::DummyClusterStatus",
namespaced,
crates(
kube_core = "stackable_operator::kube::core",
k8s_openapi = "stackable_operator::k8s_openapi",
schemars = "stackable_operator::schemars"
)
))]
#[serde(rename_all = "camelCase")]
pub struct DummyClusterSpec {
nodes: Option<Role<ProductConfigFragment>>,

// Not versioned yet
stackable_affinity: stackable_operator::commons::affinity::StackableAffinity,
stackable_node_selector: stackable_operator::commons::affinity::StackableNodeSelector,
user_information_cache: stackable_operator::commons::cache::UserInformationCache,
cluster_operation: stackable_operator::commons::cluster_operation::ClusterOperation,
domain_name: stackable_operator::commons::networking::DomainName,
host_name: stackable_operator::commons::networking::HostName,
kerberos_realm_name: stackable_operator::commons::networking::KerberosRealmName,
opa_config: stackable_operator::commons::opa::OpaConfig,
pdb_config: stackable_operator::commons::pdb::PdbConfig,
product_image: stackable_operator::commons::product_image_selection::ProductImage,
secret_class_volume: stackable_operator::commons::secret_class::SecretClassVolume,
secret_reference: stackable_operator::commons::secret::SecretReference,
tls_client_details: stackable_operator::commons::tls_verification::TlsClientDetails,

// Already versioned
client_authentication_details:
stackable_operator::crd::authentication::core::v1alpha1::ClientAuthenticationDetails,
}

#[derive(Debug, Default, PartialEq, Fragment, JsonSchema)]
#[fragment_attrs(
derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema),
serde(rename_all = "camelCase")
)]
pub struct ProductConfig {
#[fragment_attrs(serde(default))]
resources: Resources<ProductStorageConfig, JvmHeapLimits>,
}

#[derive(Debug, Default, PartialEq, Fragment, JsonSchema)]
#[fragment_attrs(
derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema),
serde(rename_all = "camelCase")
)]
pub struct ProductStorageConfig {
data_storage: stackable_operator::commons::resources::PvcConfig,
}

#[derive(Clone, Default, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DummyClusterStatus {
pub conditions: Vec<ClusterCondition>,
}
}
4 changes: 4 additions & 0 deletions crates/stackable-crd-previewer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// We need *something* for this to count as workspace member.
//
// This needs to be a separate workspace member, as can not add a build.rs to stackable-operator,
// that depends on stackable-operator structs.
Loading