Skip to content

Commit

Permalink
use snafu errors (stackabletech/operator-rs#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickLarsenNZ committed Jan 11, 2024
1 parent 38817d2 commit d882f2d
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions rust/operator-binary/src/discovery.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
use std::collections::BTreeMap;

use product_config::writer::to_hadoop_xml;
use snafu::{ResultExt, Snafu};
use stackable_hbase_crd::{HbaseCluster, HbaseRole, HBASE_SITE_XML};
use stackable_operator::{
builder::{ConfigMapBuilder, ObjectMetaBuilder},
builder::{ConfigMapBuilder, ObjectMetaBuilder, ObjectMetaBuilderError},
commons::product_image_selection::ResolvedProductImage,
error::OperatorResult,
k8s_openapi::api::core::v1::ConfigMap,
kube::runtime::reflector::ObjectRef,
};

use crate::{
hbase_controller::build_recommended_labels, zookeeper::ZookeeperConnectionInformation,
};

type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("object {hbase} is missing metadata to build owner reference"))]
ObjectMissingMetadataForOwnerRef {
source: stackable_operator::error::Error,
hbase: ObjectRef<HbaseCluster>,
},

#[snafu(display("failed to build ConfigMap"))]
BuildConfigMap {
source: stackable_operator::error::Error,
},

#[snafu(display("failed to build object meta data"))]
ObjectMeta { source: ObjectMetaBuilderError },
}

/// Creates a discovery config map containing the `hbase-site.xml` for clients.
pub fn build_discovery_configmap(
hbase: &HbaseCluster,
zookeeper_connection_information: &ZookeeperConnectionInformation,
resolved_product_image: &ResolvedProductImage,
) -> OperatorResult<ConfigMap> {
) -> Result<ConfigMap> {
let hbase_site = zookeeper_connection_information.as_hbase_settings();

ConfigMapBuilder::new()
.metadata(
ObjectMetaBuilder::new()
.name_and_namespace(hbase)
.ownerreference_from_resource(hbase, None, Some(true))?
.ownerreference_from_resource(hbase, None, Some(true))
.with_context(|_| ObjectMissingMetadataForOwnerRefSnafu {
hbase: ObjectRef::from_obj(hbase),
})?
.with_recommended_labels(build_recommended_labels(
hbase,
&resolved_product_image.app_version_label,
&HbaseRole::RegionServer.to_string(),
"discovery",
))
.context(ObjectMetaSnafu)?
.build(),
)
.add_data(
Expand All @@ -45,4 +69,5 @@ pub fn build_discovery_configmap(
),
)
.build()
.context(BuildConfigMapSnafu)
}

0 comments on commit d882f2d

Please sign in to comment.