@@ -13,51 +13,24 @@ use k8s_openapi::{
1313 api:: core:: v1:: { EnvVar , ResourceRequirements } ,
1414 apimachinery:: pkg:: {
1515 api:: resource:: Quantity ,
16- apis:: meta:: v1:: { Condition , OwnerReference , Time } ,
16+ apis:: meta:: v1:: { Condition , Time } ,
1717 } ,
1818} ;
19- use kube:: { CustomResource , Resource , ResourceExt , api:: ObjectMeta } ;
20- use rand:: Rng ;
21- use rand:: distr:: Uniform ;
19+ use kube:: { CustomResource , Resource , ResourceExt } ;
2220use schemars:: JsonSchema ;
2321use semver:: Version ;
2422use serde:: { Deserialize , Serialize } ;
2523use uuid:: Uuid ;
2624
25+ use crate :: crd:: { ManagedResource , MaterializeCertSpec , new_resource_id} ;
2726use mz_server_core:: listeners:: AuthenticatorKind ;
2827
29- use crate :: crd:: generated:: cert_manager:: certificates:: {
30- CertificateIssuerRef , CertificateSecretTemplate ,
31- } ;
32-
3328pub const LAST_KNOWN_ACTIVE_GENERATION_ANNOTATION : & str =
3429 "materialize.cloud/last-known-active-generation" ;
3530
3631pub mod v1alpha1 {
37-
3832 use super :: * ;
3933
40- // This is intentionally a subset of the fields of a Certificate.
41- // We do not want customers to configure options that may conflict with
42- // things we override or expand in our code.
43- #[ derive( Clone , Debug , Default , PartialEq , Deserialize , Serialize , JsonSchema ) ]
44- #[ serde( rename_all = "camelCase" ) ]
45- pub struct MaterializeCertSpec {
46- /// Additional DNS names the certificate will be valid for.
47- pub dns_names : Option < Vec < String > > ,
48- /// Duration the certificate will be requested for.
49- /// Value must be in units accepted by Go
50- /// [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration).
51- pub duration : Option < String > ,
52- /// Duration before expiration the certificate will be renewed.
53- /// Value must be in units accepted by Go
54- /// [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration).
55- pub renew_before : Option < String > ,
56- /// Reference to an `Issuer` or `ClusterIssuer` that will generate the certificate.
57- pub issuer_ref : Option < CertificateIssuerRef > ,
58- /// Additional annotations and labels to include in the Certificate object.
59- pub secret_template : Option < CertificateSecretTemplate > ,
60- }
6134 #[ derive( Clone , Debug , Default , PartialEq , Deserialize , Serialize , JsonSchema ) ]
6235 pub enum MaterializeRolloutStrategy {
6336 /// Create a new generation of pods, leaving the old generation around until the
@@ -368,23 +341,6 @@ pub mod v1alpha1 {
368341 } )
369342 }
370343
371- pub fn default_labels ( & self ) -> BTreeMap < String , String > {
372- BTreeMap :: from_iter ( [
373- (
374- "materialize.cloud/organization-name" . to_owned ( ) ,
375- self . name_unchecked ( ) ,
376- ) ,
377- (
378- "materialize.cloud/organization-namespace" . to_owned ( ) ,
379- self . namespace ( ) ,
380- ) ,
381- (
382- "materialize.cloud/mz-resource-id" . to_owned ( ) ,
383- self . resource_id ( ) . to_owned ( ) ,
384- ) ,
385- ] )
386- }
387-
388344 pub fn environment_id ( & self , cloud_provider : & str , region : & str ) -> String {
389345 format ! (
390346 "{}-{}-{}-0" ,
@@ -479,29 +435,11 @@ pub mod v1alpha1 {
479435 }
480436 }
481437
482- pub fn managed_resource_meta ( & self , name : String ) -> ObjectMeta {
483- ObjectMeta {
484- namespace : Some ( self . namespace ( ) ) ,
485- name : Some ( name) ,
486- labels : Some ( self . default_labels ( ) ) ,
487- owner_references : Some ( vec ! [ owner_reference( self ) ] ) ,
488- ..Default :: default ( )
489- }
490- }
491-
492438 pub fn status ( & self ) -> MaterializeStatus {
493439 self . status . clone ( ) . unwrap_or_else ( || {
494440 let mut status = MaterializeStatus :: default ( ) ;
495- // DNS-1035 names are supposed to be case insensitive,
496- // so we define our own character set, rather than use the
497- // built-in Alphanumeric distribution from rand, which
498- // includes both upper and lowercase letters.
499- const CHARSET : & [ u8 ] = b"abcdefghijklmnopqrstuvwxyz0123456789" ;
500- status. resource_id = rand:: rng ( )
501- . sample_iter ( Uniform :: new ( 0 , CHARSET . len ( ) ) . expect ( "valid range" ) )
502- . take ( 10 )
503- . map ( |i| char:: from ( CHARSET [ i] ) )
504- . collect ( ) ;
441+
442+ status. resource_id = new_resource_id ( ) ;
505443
506444 // If we're creating the initial status on an un-soft-deleted
507445 // Environment we need to ensure that the last active generation
@@ -552,6 +490,25 @@ pub mod v1alpha1 {
552490 a != b
553491 }
554492 }
493+
494+ impl ManagedResource for Materialize {
495+ fn default_labels ( & self ) -> BTreeMap < String , String > {
496+ BTreeMap :: from_iter ( [
497+ (
498+ "materialize.cloud/organization-name" . to_owned ( ) ,
499+ self . name_unchecked ( ) ,
500+ ) ,
501+ (
502+ "materialize.cloud/organization-namespace" . to_owned ( ) ,
503+ self . namespace ( ) ,
504+ ) ,
505+ (
506+ "materialize.cloud/mz-resource-id" . to_owned ( ) ,
507+ self . resource_id ( ) . to_owned ( ) ,
508+ ) ,
509+ ] )
510+ }
511+ }
555512}
556513
557514fn parse_image_ref ( image_ref : & str ) -> Option < Version > {
@@ -568,17 +525,6 @@ fn parse_image_ref(image_ref: &str) -> Option<Version> {
568525 } )
569526}
570527
571- fn owner_reference < T : Resource < DynamicType = ( ) > > ( t : & T ) -> OwnerReference {
572- OwnerReference {
573- api_version : T :: api_version ( & ( ) ) . to_string ( ) ,
574- kind : T :: kind ( & ( ) ) . to_string ( ) ,
575- name : t. name_unchecked ( ) ,
576- uid : t. uid ( ) . unwrap ( ) ,
577- block_owner_deletion : Some ( true ) ,
578- ..Default :: default ( )
579- }
580- }
581-
582528#[ cfg( test) ]
583529mod tests {
584530 use kube:: core:: ObjectMeta ;
0 commit comments