Skip to content

Commit cfc139f

Browse files
authored
Merge pull request #37 from wasmCloud/fix_nats_ports
fix: add options for setting NATS leaf and client ports separately
2 parents 45ada5a + 6df71d1 commit cfc139f

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/types/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasmcloud-operator-types"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2021"
55

66
[dependencies]

crates/types/src/v1alpha1/wasmcloud_host_config.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use k8s_openapi::api::core::v1::{PodSpec, ResourceRequirements};
22
use kube::CustomResource;
33
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
44
use serde::{Deserialize, Serialize};
5-
use std::collections::{BTreeSet, HashMap};
5+
use std::collections::{BTreeMap, BTreeSet};
66

77
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
88
#[cfg_attr(test, derive(Default))]
@@ -26,7 +26,7 @@ pub struct WasmCloudHostConfigSpec {
2626
/// The lattice to use for these hosts.
2727
pub lattice: String,
2828
/// An optional set of labels to apply to these hosts.
29-
pub host_labels: Option<HashMap<String, String>>,
29+
pub host_labels: Option<BTreeMap<String, String>>,
3030
/// The version of the wasmCloud host to deploy.
3131
pub version: String,
3232
/// The image to use for the wasmCloud host.
@@ -51,6 +51,12 @@ pub struct WasmCloudHostConfigSpec {
5151
/// The address of the NATS server to connect to. Defaults to "nats://nats.default.svc.cluster.local".
5252
#[serde(default = "default_nats_address")]
5353
pub nats_address: String,
54+
/// The port of the NATS server to connect to. Defaults to 4222.
55+
#[serde(default = "default_nats_port")]
56+
pub nats_client_port: u16,
57+
/// The port of the NATS server to connect to for leaf node connections. Defaults to 7422.
58+
#[serde(default = "default_nats_leafnode_port")]
59+
pub nats_leafnode_port: u16,
5460
/// The Jetstream domain to use for the NATS sidecar. Defaults to "default".
5561
#[serde(default = "default_jetstream_domain")]
5662
pub jetstream_domain: String,
@@ -128,6 +134,14 @@ fn default_log_level() -> String {
128134
"INFO".to_string()
129135
}
130136

137+
fn default_nats_port() -> u16 {
138+
4222
139+
}
140+
141+
fn default_nats_leafnode_port() -> u16 {
142+
7422
143+
}
144+
131145
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
132146
pub struct WasmCloudHostConfigResources {
133147
pub nats: Option<ResourceRequirements>,

sample.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ spec:
1212
# Additional labels to apply to the host other than the defaults set in the controller
1313
hostLabels:
1414
test: value
15+
cluster: kind
1516
# Which wasmCloud version to use
1617
version: "1.0.2"
1718
# The name of a secret in the same namespace that provides the required secrets.
1819
secretName: cluster-secrets
1920
logLevel: INFO
20-
natsAddress: nats://nats-cluster.default.svc.cluster.local:7422
21+
natsAddress: nats://nats-cluster.default.svc.cluster.local
2122
################################################
2223
# Additional options that can be set for hosts:
2324
################################################

src/controller.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ async fn reconcile_crd(config: &WasmCloudHostConfig, ctx: Arc<Context>) -> Resul
165165
let nc = s.nats_creds.map(SecretString::new);
166166
let apps = crate::resources::application::list_apps(
167167
&cfg.spec.nats_address,
168+
&cfg.spec.nats_client_port,
168169
nc.as_ref(),
169170
cfg.spec.lattice.clone(),
170171
)
@@ -202,6 +203,7 @@ async fn reconcile_crd(config: &WasmCloudHostConfig, ctx: Arc<Context>) -> Resul
202203
// Start the watcher so that services are automatically created in the cluster.
203204
let nats_client = get_client(
204205
&cfg.spec.nats_address,
206+
&cfg.spec.nats_client_port,
205207
ctx.nats_creds.clone(),
206208
NameNamespace::new(name.clone(), ns.clone()),
207209
)
@@ -835,7 +837,7 @@ jetstream {
835837
leafnodes {
836838
remotes: [
837839
{
838-
url: "{{cluster_url}}"
840+
url: "{{cluster_url}}:{{leafnode_port}}"
839841
{{#if use_credentials}}
840842
credentials: "/nats/nats.creds"
841843
{{/if}}
@@ -844,7 +846,7 @@ leafnodes {
844846
}
845847
"#;
846848
let tpl = Handlebars::new();
847-
let rendered = tpl.render_template(template, &json!({"jetstream_domain": config.spec.leaf_node_domain, "cluster_url": config.spec.nats_address, "use_credentials": use_nats_creds}))?;
849+
let rendered = tpl.render_template(template, &json!({"jetstream_domain": config.spec.leaf_node_domain, "cluster_url": config.spec.nats_address, "leafnode_port": config.spec.nats_leafnode_port,"use_credentials": use_nats_creds}))?;
848850
let mut contents = BTreeMap::new();
849851
contents.insert("nats.conf".to_string(), rendered);
850852
let cm = ConfigMap {

src/resources/application.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,14 @@ pub async fn list_all_applications(
386386
let secret = map.get(&nst);
387387
// Prevent listing applications within a given lattice more than once
388388
if !lattices.contains(&lattice_id) {
389-
let result = match list_apps(&cfg.spec.nats_address, secret, lattice_id.clone()).await {
389+
let result = match list_apps(
390+
&cfg.spec.nats_address,
391+
&cfg.spec.nats_client_port,
392+
secret,
393+
lattice_id.clone(),
394+
)
395+
.await
396+
{
390397
Ok(apps) => apps,
391398
Err(e) => return internal_error(anyhow!("unable to list applications: {}", e)),
392399
};
@@ -440,7 +447,14 @@ pub async fn list_applications(
440447
let secret = map.get(&nst);
441448
// This is to check that we don't list a lattice more than once
442449
if !lattices.contains(&lattice_id) {
443-
let result = match list_apps(&cfg.spec.nats_address, secret, lattice_id.clone()).await {
450+
let result = match list_apps(
451+
&cfg.spec.nats_address,
452+
&cfg.spec.nats_client_port,
453+
secret,
454+
lattice_id.clone(),
455+
)
456+
.await
457+
{
444458
Ok(apps) => apps,
445459
Err(e) => return internal_error(anyhow!("unable to list applications: {}", e)),
446460
};
@@ -466,16 +480,18 @@ pub async fn list_applications(
466480

467481
pub async fn list_apps(
468482
cluster_url: &str,
483+
port: &u16,
469484
creds: Option<&SecretString>,
470485
lattice_id: String,
471486
) -> Result<Vec<ModelSummary>, Error> {
487+
let addr = format!("{}:{}", cluster_url, port);
472488
let client = match creds {
473489
Some(creds) => {
474490
ConnectOptions::with_credentials(creds.expose_secret())?
475-
.connect(cluster_url)
491+
.connect(addr)
476492
.await?
477493
}
478-
None => ConnectOptions::new().connect(cluster_url).await?,
494+
None => ConnectOptions::new().connect(addr).await?,
479495
};
480496
let models = wash_lib::app::get_models(&client, Some(lattice_id)).await?;
481497

@@ -484,19 +500,21 @@ pub async fn list_apps(
484500

485501
pub async fn get_client(
486502
cluster_url: &str,
503+
port: &u16,
487504
nats_creds: Arc<RwLock<HashMap<NameNamespace, SecretString>>>,
488505
namespace: NameNamespace,
489506
) -> Result<async_nats::Client, async_nats::ConnectError> {
507+
let addr = format!("{}:{}", cluster_url, port);
490508
let creds = nats_creds.read().await;
491509
match creds.get(&namespace) {
492510
Some(creds) => {
493511
let creds = creds.expose_secret();
494512
ConnectOptions::with_credentials(creds)
495513
.expect("unable to create nats client")
496-
.connect(cluster_url)
514+
.connect(addr)
497515
.await
498516
}
499-
None => ConnectOptions::new().connect(cluster_url).await,
517+
None => ConnectOptions::new().connect(addr).await,
500518
}
501519
}
502520

@@ -809,11 +827,12 @@ async fn get_lattice_connection(
809827
let lattice_id = cfg.spec.lattice;
810828
let lattice_name = cfg.metadata.name?;
811829
let nst: NameNamespace = NameNamespace::new(lattice_name, namespace);
812-
Some((cluster_url, nst, lattice_id))
830+
let port = cfg.spec.nats_client_port;
831+
Some((cluster_url, nst, lattice_id, port))
813832
});
814833

815-
for (cluster_url, ns, lattice_id) in connection_data {
816-
match get_client(&cluster_url, state.nats_creds.clone(), ns).await {
834+
for (cluster_url, ns, lattice_id, port) in connection_data {
835+
match get_client(&cluster_url, &port, state.nats_creds.clone(), ns).await {
817836
Ok(c) => return Ok((c, lattice_id)),
818837
Err(e) => {
819838
error!(err = %e, %lattice_id, "error connecting to nats");

0 commit comments

Comments
 (0)