Queries, k8s.

This commit is contained in:
James Pace 2024-09-30 00:11:59 +00:00
parent 3250a5adc6
commit 0f20533eaa
21 changed files with 324 additions and 23 deletions

10
config/env.sh Executable file
View File

@ -0,0 +1,10 @@
SCRIPTPATH=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
export ZENOH_CONFIG=$SCRIPTPATH/peer.yaml
# I know I'm hardcoding, but this is just for testing.
export PATH=$PATH:/home/jimmy/Develop/zenoh/zenoh/target/release
run_router() {
zenohd --config $SCRIPTPATH/router.yaml
}

21
k8s/Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM registry.fedoraproject.org/fedora:latest AS builder
# You must mount the source directory when building. I.E.:
# podman build --no-cache -v $PWD:/src -f k8s/Dockerfile -t zenoh-k3s-registry:5000/zenoh-exp .
# TODO: Cache the deps piece by making a custom base image. Mounting a volume does not
# play nice with caching.
# Dependencies
RUN sudo dnf update -y && \
sudo dnf install -y git curl gcc-c++ openssl-devel
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.75.0
RUN cd /src && \
/bin/bash -c "source $HOME/.cargo/env && cargo build --release" && \
mkdir /app && \
find ./target/release -maxdepth 1 -type f -perm /a+x -not -name "*.so" -not -name "*.rlib" -exec cp {} /app \;
FROM registry.fedoraproject.org/fedora:latest
COPY --from=builder /app/* /usr/local/bin/.
RUN useradd -m -s /bin/bash -u 1000 zenoh
USER zenoh
ENTRYPOINT ["/bin/bash", "-c"]

21
k8s/Dockerfile.router Normal file
View File

@ -0,0 +1,21 @@
FROM registry.fedoraproject.org/fedora:latest AS builder
# Dependencies
RUN sudo dnf update -y && \
sudo dnf install -y git curl gcc-c++
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.75.0
# Clone and build
RUN git clone https://github.com/eclipse-zenoh/zenoh.git /zenoh-src && \
cd /zenoh-src && \
/bin/bash -c "source $HOME/.cargo/env && cargo build --release" && \
mkdir /zenoh && \
mv ./target/release/*.so /zenoh/. && \
mv ./target/release/zenohd /zenoh/.
FROM registry.fedoraproject.org/fedora:latest
COPY --from=builder /zenoh/zenohd /usr/local/bin/.
COPY --from=builder /zenoh/*.so /usr/local/lib/.
RUN useradd -m -s /bin/bash -u 1000 zenoh
USER zenoh
ENTRYPOINT ["zenohd"]

23
k8s/Dockerfile.wireshark Normal file
View File

@ -0,0 +1,23 @@
FROM docker.io/library/ubuntu:22.04
# Dependencies
RUN echo "wireshark-common wireshark-common/install-setuid boolean true" | debconf-set-selections && \
apt update && \
apt install -y software-properties-common && \
add-apt-repository -y ppa:wireshark-dev/stable && \
apt install -y wireshark-dev tshark && \
apt install -y --allow-change-held-packages wireshark
RUN apt install -y git curl g++ libssl-dev
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.75.0
RUN useradd -m -s /bin/bash -u 1000 -G wireshark zenoh
# Clone and build
RUN git clone https://github.com/ZettaScaleLabs/zenoh-dissector.git /zenoh && \
cd /zenoh && \
git checkout dev/1.0.0 && \
/bin/bash -c "source $HOME/.cargo/env && cargo build --release"
# Install
USER zenoh
RUN mkdir -p ~/.local/lib/wireshark/plugins/4.4/epan && \
cp /zenoh/target/release/libzenoh_dissector.so ~/.local/lib/wireshark/plugins/4.4/epan/libzenoh_dissector.so

14
k8s/k3d-cluster.yaml Normal file
View File

@ -0,0 +1,14 @@
apiVersion: k3d.io/v1alpha4
kind: Simple
metadata:
name: zenoh
servers: 1
agents: 0
registries:
create:
name: zenoh-k3s-registry
host: "0.0.0.0"
hostPort: "5000"
ports:
- port: 7447:7447
- port: 80:8080

View File

@ -0,0 +1,24 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: zenoh-conf
data:
peer.yaml: |
mode: peer
listen:
endpoints:
- "tcp/0.0.0.0:1223"
connect:
endpoints:
- "tcp/zenohd-zenoh-service.default.svc.cluster.local:7447"
scouting:
multicast:
enabled: false
gossip:
enabled: true
routing:
router:
peers_failover_brokering: true
peer:
mode: linkstate

View File

@ -0,0 +1,34 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zenoh-publisher-deployment
labels:
app: zenoh-publisher
spec:
replicas: 1
selector:
matchLabels:
app: zenoh-publisher
template:
metadata:
labels:
app: zenoh-publisher
spec:
containers:
- name: publisher
image: zenoh-k3s-registry:5000/zenoh-exp:latest
args: ["pub"]
env:
- name: ZENOH_CONFIG
value: "/opt/zenoh/peer.yaml"
volumeMounts:
- mountPath: /opt/zenoh
name: zenoh-conf
ports:
- containerPort: 1223
name: zenoh-port
volumes:
- name: zenoh-conf
configMap:
name: zenoh-conf

View File

@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- configmap.yaml
- deployment.yaml
- service.yaml

View File

@ -0,0 +1,12 @@
---
apiVersion: v1
kind: Service
metadata:
name: zenoh-publisher-service
spec:
selector:
app: zenoh-publisher
ports:
- protocol: TCP
port: 1223
name: zenoh-port

33
k8s/router/configmap.yaml Normal file
View File

@ -0,0 +1,33 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: zenohd-conf
data:
router.yaml: |
mode: router
listen:
endpoints:
- "tcp/0.0.0.0:7447"
scouting:
multicast:
enabled: false
gossip:
enabled: true
multihop: true
routing:
router:
peers_failover_brokering: true
peer:
mode: linkstate
plugins_loading:
enabled: true
plugins:
rest:
__required__: true
http_port: 8000
storage_manager:
storages:
primary_memory:
key_expr: "**"
volume: memory

View File

@ -0,0 +1,35 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zenohd-deployment
labels:
app: zenohd
spec:
replicas: 1
selector:
matchLabels:
app: zenohd
template:
metadata:
labels:
app: zenohd
spec:
containers:
- name: zenohd
image: zenoh-k3s-registry:5000/zenoh-router:latest
args: ["-c", "/opt/zenohd/router.yaml"]
volumeMounts:
- mountPath: /opt/zenohd
name: zenohd-conf
ports:
- containerPort: 8000
name: rest-port
- containerPort: 8001
name: websocket-port
- containerPort: 7447
name: zenoh-port
volumes:
- name: zenohd-conf
configMap:
name: zenohd-conf

View File

@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- configmap.yaml
- deployment.yaml
- service.yaml

28
k8s/router/service.yaml Normal file
View File

@ -0,0 +1,28 @@
---
apiVersion: v1
kind: Service
metadata:
name: zenohd-zenoh-service
spec:
selector:
app: zenohd
type: LoadBalancer
ports:
- protocol: TCP
port: 7447
name: zenoh-port
---
apiVersion: v1
kind: Service
metadata:
name: zenohd-web-service
spec:
selector:
app: zenohd
ports:
- protocol: TCP
port: 8000
name: rest-port
- protocol: TCP
port: 8001
name: websocket-port

4
k8s/run-wireshark.md Normal file
View File

@ -0,0 +1,4 @@
`sudo podman run --rm -it --net=host --privileged -v /tmp:/tmp localhost/zenoh-wireshark:latest bash`
`tshark -i loopback -w output.pcap`
`tshark -r output.pcap -V | more`

View File

@ -1 +0,0 @@
ZENOH_CONFIG=config/peer.yaml ./target/debug/pub

View File

@ -1 +0,0 @@
ZENOH_CONFIG=config/peer.yaml ./target/debug/sub_callback

View File

@ -1 +0,0 @@
ZENOH_CONFIG=config/peer.yaml ./target/debug/sub

15
src/bin/get.rs Normal file
View File

@ -0,0 +1,15 @@
#[tokio::main]
async fn main() {
let config = zenoh::Config::from_env().unwrap();
let session = zenoh::open(config).await.unwrap();
let replies = session.get("name").await.unwrap();
while let Ok(reply) = replies.recv_async().await {
let result = reply.result().unwrap();
println!(
"Got: {:?}",
result.payload().deserialize::<String>().unwrap()
);
}
}

View File

@ -2,28 +2,9 @@ use anyhow::anyhow;
use anyhow::Result;
use std::time::Duration;
fn build_message1() -> Result<Vec<u8>> {
let mut message = zenoh_exp::Message::default();
message.sender = "pub".to_string();
message.message = "Hi sub!".to_string();
let mut buff: Vec<u8> = Vec::new();
let _ = ciborium::into_writer(&message, &mut buff)?;
return Ok(buff);
}
fn build_message2() -> Result<Vec<u8>> {
let mut message = zenoh_exp::Message::default();
message.sender = "pub".to_string();
message.message = "yo sub!".to_string();
let mut buff: Vec<u8> = Vec::new();
let _ = ciborium::into_writer(&message, &mut buff)?;
return Ok(buff);
}
#[tokio::main]
async fn main() -> Result<()> {
zenoh::init_log_from_env_or("error");
let config = zenoh::Config::from_env().map_err(|e| anyhow!(e))?;
let session = zenoh::open(config).await.map_err(|e| anyhow!(e))?;
let test_publisher = session
@ -53,3 +34,23 @@ async fn main() -> Result<()> {
tokio::time::sleep(Duration::from_secs_f32(0.5)).await;
}
}
fn build_message1() -> Result<Vec<u8>> {
let mut message = zenoh_exp::Message::default();
message.sender = "pub".to_string();
message.message = "Hi sub!".to_string();
let mut buff: Vec<u8> = Vec::new();
let _ = ciborium::into_writer(&message, &mut buff)?;
return Ok(buff);
}
fn build_message2() -> Result<Vec<u8>> {
let mut message = zenoh_exp::Message::default();
message.sender = "pub".to_string();
message.message = "yo sub!".to_string();
let mut buff: Vec<u8> = Vec::new();
let _ = ciborium::into_writer(&message, &mut buff)?;
return Ok(buff);
}

16
src/bin/query-server.rs Normal file
View File

@ -0,0 +1,16 @@
#[tokio::main]
async fn main() {
let config = zenoh::Config::from_env().unwrap();
let session = zenoh::open(config).await.unwrap();
let queryable = session
.declare_queryable("name")
.with(zenoh::handlers::RingChannel::new(1))
.await
.unwrap();
while let Ok(query) = queryable.recv_async().await {
let reply = "hi!";
query.reply(query.key_expr().as_str(), reply).await.unwrap();
}
}

View File

@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
#[tokio::main]
async fn main() -> Result<()> {
zenoh::init_log_from_env_or("error");
let config = zenoh::Config::from_env().map_err(|e| anyhow!(e))?;
let session = zenoh::open(config).await.map_err(|e| anyhow!(e))?;