From 0f20533eaa9aebb08d22154924e270a3be17d1a0 Mon Sep 17 00:00:00 2001 From: James Pace Date: Mon, 30 Sep 2024 00:11:59 +0000 Subject: [PATCH] Queries, k8s. --- config/env.sh | 10 ++++++++ k8s/Dockerfile | 21 ++++++++++++++++ k8s/Dockerfile.router | 21 ++++++++++++++++ k8s/Dockerfile.wireshark | 23 ++++++++++++++++++ k8s/k3d-cluster.yaml | 14 +++++++++++ k8s/publisher/configmap.yaml | 24 +++++++++++++++++++ k8s/publisher/deployment.yaml | 34 ++++++++++++++++++++++++++ k8s/publisher/kustomization.yaml | 6 +++++ k8s/publisher/service.yaml | 12 ++++++++++ k8s/router/configmap.yaml | 33 +++++++++++++++++++++++++ k8s/router/deployment.yaml | 35 +++++++++++++++++++++++++++ k8s/router/kustomization.yaml | 6 +++++ k8s/router/service.yaml | 28 ++++++++++++++++++++++ k8s/run-wireshark.md | 4 ++++ scripts/run-pub.sh | 1 - scripts/run-sub-callback.sh | 1 - scripts/run-sub.sh | 1 - src/bin/get.rs | 15 ++++++++++++ src/bin/pub.rs | 41 ++++++++++++++++---------------- src/bin/query-server.rs | 16 +++++++++++++ src/bin/sub.rs | 1 + 21 files changed, 324 insertions(+), 23 deletions(-) create mode 100755 config/env.sh create mode 100644 k8s/Dockerfile create mode 100644 k8s/Dockerfile.router create mode 100644 k8s/Dockerfile.wireshark create mode 100644 k8s/k3d-cluster.yaml create mode 100644 k8s/publisher/configmap.yaml create mode 100644 k8s/publisher/deployment.yaml create mode 100644 k8s/publisher/kustomization.yaml create mode 100644 k8s/publisher/service.yaml create mode 100644 k8s/router/configmap.yaml create mode 100644 k8s/router/deployment.yaml create mode 100644 k8s/router/kustomization.yaml create mode 100644 k8s/router/service.yaml create mode 100644 k8s/run-wireshark.md delete mode 100755 scripts/run-pub.sh delete mode 100755 scripts/run-sub-callback.sh delete mode 100755 scripts/run-sub.sh create mode 100644 src/bin/get.rs create mode 100644 src/bin/query-server.rs diff --git a/config/env.sh b/config/env.sh new file mode 100755 index 0000000..fbca024 --- /dev/null +++ b/config/env.sh @@ -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 +} + diff --git a/k8s/Dockerfile b/k8s/Dockerfile new file mode 100644 index 0000000..2afd2cb --- /dev/null +++ b/k8s/Dockerfile @@ -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"] diff --git a/k8s/Dockerfile.router b/k8s/Dockerfile.router new file mode 100644 index 0000000..1a8e891 --- /dev/null +++ b/k8s/Dockerfile.router @@ -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"] \ No newline at end of file diff --git a/k8s/Dockerfile.wireshark b/k8s/Dockerfile.wireshark new file mode 100644 index 0000000..4c0ffbc --- /dev/null +++ b/k8s/Dockerfile.wireshark @@ -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 \ No newline at end of file diff --git a/k8s/k3d-cluster.yaml b/k8s/k3d-cluster.yaml new file mode 100644 index 0000000..a2f869a --- /dev/null +++ b/k8s/k3d-cluster.yaml @@ -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 \ No newline at end of file diff --git a/k8s/publisher/configmap.yaml b/k8s/publisher/configmap.yaml new file mode 100644 index 0000000..301efad --- /dev/null +++ b/k8s/publisher/configmap.yaml @@ -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 \ No newline at end of file diff --git a/k8s/publisher/deployment.yaml b/k8s/publisher/deployment.yaml new file mode 100644 index 0000000..65f255a --- /dev/null +++ b/k8s/publisher/deployment.yaml @@ -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 \ No newline at end of file diff --git a/k8s/publisher/kustomization.yaml b/k8s/publisher/kustomization.yaml new file mode 100644 index 0000000..45badd0 --- /dev/null +++ b/k8s/publisher/kustomization.yaml @@ -0,0 +1,6 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- configmap.yaml +- deployment.yaml +- service.yaml \ No newline at end of file diff --git a/k8s/publisher/service.yaml b/k8s/publisher/service.yaml new file mode 100644 index 0000000..1e4d91a --- /dev/null +++ b/k8s/publisher/service.yaml @@ -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 \ No newline at end of file diff --git a/k8s/router/configmap.yaml b/k8s/router/configmap.yaml new file mode 100644 index 0000000..98cef84 --- /dev/null +++ b/k8s/router/configmap.yaml @@ -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 \ No newline at end of file diff --git a/k8s/router/deployment.yaml b/k8s/router/deployment.yaml new file mode 100644 index 0000000..f2df13b --- /dev/null +++ b/k8s/router/deployment.yaml @@ -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 \ No newline at end of file diff --git a/k8s/router/kustomization.yaml b/k8s/router/kustomization.yaml new file mode 100644 index 0000000..45badd0 --- /dev/null +++ b/k8s/router/kustomization.yaml @@ -0,0 +1,6 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- configmap.yaml +- deployment.yaml +- service.yaml \ No newline at end of file diff --git a/k8s/router/service.yaml b/k8s/router/service.yaml new file mode 100644 index 0000000..abe2b0c --- /dev/null +++ b/k8s/router/service.yaml @@ -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 \ No newline at end of file diff --git a/k8s/run-wireshark.md b/k8s/run-wireshark.md new file mode 100644 index 0000000..1c5fc98 --- /dev/null +++ b/k8s/run-wireshark.md @@ -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` diff --git a/scripts/run-pub.sh b/scripts/run-pub.sh deleted file mode 100755 index 4abbfd5..0000000 --- a/scripts/run-pub.sh +++ /dev/null @@ -1 +0,0 @@ -ZENOH_CONFIG=config/peer.yaml ./target/debug/pub diff --git a/scripts/run-sub-callback.sh b/scripts/run-sub-callback.sh deleted file mode 100755 index 18891e1..0000000 --- a/scripts/run-sub-callback.sh +++ /dev/null @@ -1 +0,0 @@ -ZENOH_CONFIG=config/peer.yaml ./target/debug/sub_callback diff --git a/scripts/run-sub.sh b/scripts/run-sub.sh deleted file mode 100755 index 5c0ff41..0000000 --- a/scripts/run-sub.sh +++ /dev/null @@ -1 +0,0 @@ -ZENOH_CONFIG=config/peer.yaml ./target/debug/sub diff --git a/src/bin/get.rs b/src/bin/get.rs new file mode 100644 index 0000000..8721a64 --- /dev/null +++ b/src/bin/get.rs @@ -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::().unwrap() + ); + } +} diff --git a/src/bin/pub.rs b/src/bin/pub.rs index b8c05b9..f13eff1 100644 --- a/src/bin/pub.rs +++ b/src/bin/pub.rs @@ -2,28 +2,9 @@ use anyhow::anyhow; use anyhow::Result; use std::time::Duration; -fn build_message1() -> Result> { - let mut message = zenoh_exp::Message::default(); - message.sender = "pub".to_string(); - message.message = "Hi sub!".to_string(); - let mut buff: Vec = Vec::new(); - let _ = ciborium::into_writer(&message, &mut buff)?; - - return Ok(buff); -} - -fn build_message2() -> Result> { - let mut message = zenoh_exp::Message::default(); - message.sender = "pub".to_string(); - message.message = "yo sub!".to_string(); - let mut buff: Vec = 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> { + let mut message = zenoh_exp::Message::default(); + message.sender = "pub".to_string(); + message.message = "Hi sub!".to_string(); + let mut buff: Vec = Vec::new(); + let _ = ciborium::into_writer(&message, &mut buff)?; + + return Ok(buff); +} + +fn build_message2() -> Result> { + let mut message = zenoh_exp::Message::default(); + message.sender = "pub".to_string(); + message.message = "yo sub!".to_string(); + let mut buff: Vec = Vec::new(); + let _ = ciborium::into_writer(&message, &mut buff)?; + + return Ok(buff); +} diff --git a/src/bin/query-server.rs b/src/bin/query-server.rs new file mode 100644 index 0000000..7c717f8 --- /dev/null +++ b/src/bin/query-server.rs @@ -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(); + } +} diff --git a/src/bin/sub.rs b/src/bin/sub.rs index 499f3b8..4934810 100644 --- a/src/bin/sub.rs +++ b/src/bin/sub.rs @@ -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))?;