Skip to content

Commit

Permalink
feat: added secondary route with key on host
Browse files Browse the repository at this point in the history
* feat: added plugin to accept dmtr key on hostname

* feat: added secondary route with key on hostname
  • Loading branch information
paulobressan authored Jan 11, 2024
1 parent d95e42e commit bf4aa87
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 282 deletions.
22 changes: 10 additions & 12 deletions bootstrap/crds/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ resource "kubernetes_manifest" "customresourcedefinition_kupoports_demeter_run"
"type" = "string"
},
{
"jsonPath" = ".spec.authentication"
"name" = "Authentication"
"jsonPath" = ".status.endpoint_key_url"
"name" = "Endpoint Key URL"
"type" = "string"
},
{
Expand All @@ -58,13 +58,6 @@ resource "kubernetes_manifest" "customresourcedefinition_kupoports_demeter_run"
"properties" = {
"spec" = {
"properties" = {
"authentication" = {
"enum" = [
"none",
"apiKey",
]
"type" = "string"
}
"network" = {
"enum" = [
"mainnet",
Expand All @@ -85,7 +78,6 @@ resource "kubernetes_manifest" "customresourcedefinition_kupoports_demeter_run"
}
}
"required" = [
"authentication",
"network",
"operatorVersion",
"pruneUtxo",
Expand All @@ -97,14 +89,20 @@ resource "kubernetes_manifest" "customresourcedefinition_kupoports_demeter_run"
"nullable" = true
"properties" = {
"authToken" = {
"nullable" = true
"type" = "string"
}
"endpointKeyUrl" = {
"type" = "string"
}
"endpointUrl" = {
"nullable" = true
"type" = "string"
}
}
"required" = [
"authToken",
"endpointKeyUrl",
"endpointUrl",
]
"type" = "object"
}
}
Expand Down
50 changes: 32 additions & 18 deletions operator/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use futures::StreamExt;
use kube::{
api::ListParams,
runtime::{controller::Action, watcher::Config as WatcherConfig, Controller},
Api, Client, CustomResource,
Api, Client, CustomResource, CustomResourceExt, ResourceExt,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -11,8 +11,8 @@ use tracing::{error, info, instrument};

use crate::{
auth::handle_auth,
gateway::{handle_http_route, handle_reference_grant},
Error, Metrics, Network, Result, State,
gateway::{handle_http_route, handle_http_route_key, handle_reference_grant},
patch_resource_status, Error, Metrics, Network, Result, State,
};

pub static KUPO_PORT_FINALIZER: &str = "kupoports.demeter.run";
Expand All @@ -27,13 +27,6 @@ impl Context {
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Authentication {
None,
ApiKey,
}

#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(
kind = "KupoPort",
Expand All @@ -48,7 +41,7 @@ pub enum Authentication {
{"name": "Pruned", "jsonPath": ".spec.pruneUtxo", "type": "boolean"},
{"name": "Throughput Tier", "jsonPath":".spec.throughputTier", "type": "string"},
{"name": "Endpoint URL", "jsonPath": ".status.endpointUrl", "type": "string"},
{"name": "Authentication", "jsonPath": ".spec.authentication", "type": "string"},
{"name": "Endpoint Key URL", "jsonPath": ".status.endpoint_key_url", "type": "string"},
{"name": "Auth Token", "jsonPath": ".status.authToken", "type": "string"}
"#)]
#[serde(rename_all = "camelCase")]
Expand All @@ -58,21 +51,42 @@ pub struct KupoPortSpec {
pub prune_utxo: bool,
// throughput should be 0, 1, 2
pub throughput_tier: String,
pub authentication: Authentication,
}

#[derive(Deserialize, Serialize, Clone, Default, Debug, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct KupoPortStatus {
pub endpoint_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auth_token: Option<String>,
pub endpoint_url: String,
pub endpoint_key_url: String,
pub auth_token: String,
}

async fn reconcile(crd: Arc<KupoPort>, ctx: Arc<Context>) -> Result<Action> {
handle_reference_grant(ctx.client.clone(), &crd).await?;
handle_http_route(ctx.client.clone(), &crd).await?;
handle_auth(ctx.client.clone(), &crd).await?;
handle_reference_grant(&ctx.client, &crd).await?;

let key = handle_auth(&ctx.client, &crd).await?;
let hostname = handle_http_route(&ctx.client, &crd).await?;
let hostname_key = handle_http_route_key(&ctx.client, &crd, &key).await?;

let status = KupoPortStatus {
endpoint_url: format!("https://{hostname}"),
endpoint_key_url: format!("https://{hostname_key}"),
auth_token: key,
};

let namespace = crd.namespace().unwrap();
let kupo_port = KupoPort::api_resource();

patch_resource_status(
ctx.client.clone(),
&namespace,
kupo_port,
&crd.name_any(),
serde_json::to_value(status)?,
)
.await?;

info!(resource = crd.name_any(), "Reconcile completed");

Ok(Action::await_change())
}
Expand Down
Loading

0 comments on commit bf4aa87

Please sign in to comment.