|
| 1 | +use kube::CustomResource; |
| 2 | +use schemars::JsonSchema; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use crate::{commons::tls_verification::TlsClientDetails, versioned::versioned}; |
| 6 | + |
| 7 | +mod v1alpha1_impl; |
| 8 | + |
| 9 | +// FIXME (@Techassi): This should be versioned as well, but the macro cannot |
| 10 | +// handle new-type structs yet. |
| 11 | +/// Use this type in your operator! |
| 12 | +pub type ResolvedOpenLineageConnection = v1alpha1::OpenLineageConnectionSpec; |
| 13 | + |
| 14 | +#[versioned( |
| 15 | + version(name = "v1alpha1"), |
| 16 | + crates( |
| 17 | + kube_core = "kube::core", |
| 18 | + k8s_openapi = "k8s_openapi", |
| 19 | + schemars = "schemars", |
| 20 | + ) |
| 21 | +)] |
| 22 | +pub mod versioned { |
| 23 | + pub mod v1alpha1 { |
| 24 | + pub use v1alpha1_impl::OpenLineageError; |
| 25 | + } |
| 26 | + |
| 27 | + /// OpenLineage connection definition as a resource. |
| 28 | + /// Learn more about [OpenLineage](https://openlineage.io/). |
| 29 | + #[versioned(crd( |
| 30 | + group = "openlineage.stackable.tech", |
| 31 | + kind = "OpenLineageConnection", |
| 32 | + plural = "openlineageconnections", |
| 33 | + doc = "A reusable definition of a connection to an OpenLineage backend.", |
| 34 | + namespaced |
| 35 | + ))] |
| 36 | + #[derive(CustomResource, Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] |
| 37 | + #[serde(rename_all = "camelCase")] |
| 38 | + pub struct OpenLineageConnectionSpec { |
| 39 | + /// Host of the OpenLineage backend without any protocol or port. For example: `marquez`. |
| 40 | + pub host: String, |
| 41 | + |
| 42 | + /// Port the OpenLineage backend listens on. For example: `5000`. |
| 43 | + pub port: u16, |
| 44 | + |
| 45 | + /// Use a TLS connection. If not specified no TLS will be used. |
| 46 | + /// When TLS server verification is configured, the transport uses `https` instead of `http`. |
| 47 | + #[serde(flatten)] |
| 48 | + pub tls: TlsClientDetails, |
| 49 | + } |
| 50 | + |
| 51 | + /// An OpenLineage connection, either inlined or referenced by the name of an |
| 52 | + /// [`OpenLineageConnection`] resource in the same namespace. |
| 53 | + #[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] |
| 54 | + #[serde(rename_all = "camelCase")] |
| 55 | + // TODO: This probably should be serde(untagged), but this would be a breaking change |
| 56 | + pub enum InlineConnectionOrReference { |
| 57 | + Inline(OpenLineageConnectionSpec), |
| 58 | + Reference(String), |
| 59 | + } |
| 60 | + |
| 61 | + /// OpenLineage lineage-emission configuration for a single workload/application. |
| 62 | + /// |
| 63 | + /// Embed this in an operator's workload spec to enable OpenLineage for that workload. |
| 64 | + #[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] |
| 65 | + #[serde(rename_all = "camelCase")] |
| 66 | + pub struct OpenLineageJob { |
| 67 | + /// The OpenLineage backend connection, either inlined or referencing an |
| 68 | + /// `OpenLineageConnection` resource by name. |
| 69 | + pub connection: InlineConnectionOrReference, |
| 70 | + |
| 71 | + /// The OpenLineage namespace lineage is reported under. |
| 72 | + /// If unset, operators typically default to the workload's Kubernetes namespace. |
| 73 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 74 | + pub namespace: Option<String>, |
| 75 | + |
| 76 | + /// A stable OpenLineage job/application name. Setting this prevents fragmented run history. |
| 77 | + /// If unset, operators resolve a name from workload-specific configuration. |
| 78 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 79 | + pub app_name: Option<String>, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +impl stackable_versioned::test_utils::RoundtripTestData for v1alpha1::OpenLineageConnectionSpec { |
| 85 | + fn roundtrip_test_data() -> Vec<Self> { |
| 86 | + crate::utils::yaml_from_str_singleton_map(indoc::indoc! {" |
| 87 | + - host: marquez |
| 88 | + port: 5000 |
| 89 | + - host: marquez |
| 90 | + port: 5000 |
| 91 | + tls: |
| 92 | + verification: |
| 93 | + none: {} |
| 94 | + - host: marquez |
| 95 | + port: 5000 |
| 96 | + tls: |
| 97 | + verification: |
| 98 | + server: |
| 99 | + caCert: |
| 100 | + secretClass: openlineage-cert |
| 101 | + "}) |
| 102 | + .expect("Failed to parse OpenLineageConnectionSpec YAML") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use crate::{ |
| 109 | + commons::tls_verification::{ |
| 110 | + CaCert, Tls, TlsClientDetails, TlsServerVerification, TlsVerification, |
| 111 | + }, |
| 112 | + crd::openlineage::v1alpha1::OpenLineageConnectionSpec, |
| 113 | + }; |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn http_transport_url_without_tls() { |
| 117 | + let connection = OpenLineageConnectionSpec { |
| 118 | + host: "marquez".to_string(), |
| 119 | + port: 5000, |
| 120 | + tls: TlsClientDetails { tls: None }, |
| 121 | + }; |
| 122 | + |
| 123 | + assert_eq!(connection.transport_url(), "http://marquez:5000"); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn https_transport_url_with_server_verification() { |
| 128 | + let connection = OpenLineageConnectionSpec { |
| 129 | + host: "marquez".to_string(), |
| 130 | + port: 5000, |
| 131 | + tls: TlsClientDetails { |
| 132 | + tls: Some(Tls { |
| 133 | + verification: TlsVerification::Server(TlsServerVerification { |
| 134 | + ca_cert: CaCert::WebPki {}, |
| 135 | + }), |
| 136 | + }), |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + assert_eq!(connection.transport_url(), "https://marquez:5000"); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn http_transport_url_without_verification() { |
| 145 | + let connection = OpenLineageConnectionSpec { |
| 146 | + host: "marquez".to_string(), |
| 147 | + port: 5000, |
| 148 | + tls: TlsClientDetails { |
| 149 | + tls: Some(Tls { |
| 150 | + verification: TlsVerification::None {}, |
| 151 | + }), |
| 152 | + }, |
| 153 | + }; |
| 154 | + |
| 155 | + assert_eq!(connection.transport_url(), "http://marquez:5000"); |
| 156 | + } |
| 157 | +} |
0 commit comments