Skip to content

Commit

Permalink
chore: improve metrics controller and controller start procedure (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan authored Jan 8, 2024
1 parent 45c8bcb commit c0cb210
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 11 deletions.
16 changes: 12 additions & 4 deletions operator/src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use futures::StreamExt;
use kube::{
api::ListParams,
runtime::{controller::Action, watcher::Config as WatcherConfig, Controller},
Api, Client, CustomResource, ResourceExt,
};
Expand Down Expand Up @@ -76,18 +77,25 @@ fn error_policy(crd: Arc<OgmiosPort>, err: &Error, ctx: Arc<Context>) -> Action
}

#[instrument("controller run", skip_all)]
pub async fn run(state: Arc<State>) -> Result<(), Error> {
pub async fn run(state: Arc<State>) {
info!("listening crds running");

let client = Client::try_default().await?;
let client = Client::try_default()
.await
.expect("failed to create kube client");

let crds = Api::<OgmiosPort>::all(client.clone());
if let Err(e) = crds.list(&ListParams::default().limit(1)).await {
error!("CRD is not queryable; {e:?}. Is the CRD installed?");
std::process::exit(1);
}

let ctx = Context::new(client, state.metrics.clone());

Controller::new(crds, WatcherConfig::default().any_semantic())
.shutdown_on_signal()
.run(reconcile, error_policy, Arc::new(ctx))
.filter_map(|x| async move { std::result::Result::ok(x) })
.for_each(|_| futures::future::ready(()))
.await;

Ok(())
}
9 changes: 2 additions & 7 deletions operator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() -> io::Result<()> {

let state = Arc::new(State::default());

let controller = tokio::spawn(controller::run(state.clone()));
let controller = controller::run(state.clone());

let addr = std::env::var("ADDR").unwrap_or("0.0.0.0:8080".into());

Expand All @@ -44,12 +44,7 @@ async fn main() -> io::Result<()> {
.bind(&addr)?;
info!({ addr }, "metrics server running");

let signal = tokio::spawn(async {
tokio::signal::ctrl_c().await.expect("Fail to exit");
std::process::exit(0);
});

tokio::join!(server.run(), controller, signal).0?;
tokio::join!(server.run(), controller,).0?;

Ok(())
}
67 changes: 67 additions & 0 deletions operator/yaml/crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: ogmiosports.demeter.run
spec:
group: demeter.run
names:
categories: []
kind: OgmiosPort
plural: ogmiosports
shortNames: []
singular: ogmiosport
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.network
name: Network
type: string
- jsonPath: .spec.version
name: Version
type: number
- jsonPath: .status.endpointUrl
name: Endpoint URL
type: string
- jsonPath: .status.authToken
name: Auth Token
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: Auto-generated derived type for OgmiosPortSpec via `CustomResource`
properties:
spec:
properties:
network:
enum:
- mainnet
- preprod
- preview
- sanchonet
type: string
version:
format: uint8
minimum: 0.0
type: integer
required:
- network
- version
type: object
status:
nullable: true
properties:
authToken:
nullable: true
type: string
endpointUrl:
nullable: true
type: string
type: object
required:
- spec
title: OgmiosPort
type: object
served: true
storage: true
subresources:
status: {}
14 changes: 14 additions & 0 deletions operator/yaml/port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Namespace
metadata:
name: prj-mainnet-test
---
apiVersion: demeter.run/v1alpha1
kind: OgmiosPort
metadata:
name: mainnet-user
namespace: prj-mainnet-test
spec:
network: "mainnet"
version: 1

0 comments on commit c0cb210

Please sign in to comment.