Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions new-counter-demo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.PHONY: clean build-webUI build-mapper deploy-webUI deploy-mapper undeploy-webUI undeploy-mapper
clean: undeploy-webUI undeploy-mapper
build-webUI:
docker build -f ./webUI/Dockerfile -t counter-webui:v1.0 ./webUI
# kind load docker-image counter-webui:v1.0 --nodes=kind-control-plane
build-mapper:
docker build -f ./virtualprotocol/Dockerfile_nostream -t counter-mapper:v1.0 ./virtualprotocol
# kind load docker-image counter-mapper:v1.0 --nodes=kind-worker

deploy-webUI:
kubectl apply -f ./webUI/resource/deploy.yaml

deploy-mapper:
kubectl apply -f ./virtualprotocol/resource/configmap.yaml
kubectl apply -f ./virtualprotocol/resource/deployment.yaml
kubectl wait --for=condition=available --timeout=120s deployment/counter-mapper
kubectl apply -f ./crds/counter-model.yaml
kubectl apply -f ./crds/counter-instance.yaml

undeploy-webUI:
kubectl delete -f ./webUI/resource
undeploy-mapper:
kubectl delete -f ./virtualprotocol/resource/configmap.yaml
kubectl delete -f ./virtualprotocol/resource/deployment.yaml
kubectl delete -f ./crds/counter-model.yaml
kubectl delete -f ./crds/counter-instance.yaml
60 changes: 60 additions & 0 deletions new-counter-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# New Counter Demo

## Description
Counter is a virtual device that uses VirtualProtocol, so users can run the demo without an additional physical device.

## Feature
- Status Control: Supports Start/Pause/Shutdown operations
- Customizable Start Value: Enter a number to set the initial counter value
- Real-time Update: Counter status and value automatically refresh every second
- Visual Feedback: Different statuses (Running/Paused/Stopped) are displayed in different colors

## Structure
![structure](./img/structure.png)

## Prerequisites
- Docker
- KubeEdge v1.21+

## Quick Start
### Clone the repository and enter the demo directory
```
git clone https://github.com/kubeedge/examples.git
cd examples/new-counter-demo
```

### Mapper ✅(At the edge)
1. Build the mapper image
```
make build-mapper
```
2. Deploy the mapper
```
make deploy-mapper
```


### WebUI ✅(At the cloud)
1. Build the webUI image
```
make build-webUI
```
2. Deploy the webUI
```
make deploy-webUI
```
3. Open the Web Browser and go to the URL
```
http://127.0.0.1:8080
```

### Clean the resouces
```
make clean
```
## Test
![test](./img/counter-demo.gif)




45 changes: 45 additions & 0 deletions new-counter-demo/crds/counter-instance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: devices.kubeedge.io/v1beta1
kind: Device
metadata:
name: counter-instance
namespace: default
spec:
deviceModelRef:
name: counter-model
nodeName: "kind-worker"
protocol:
protocolName: virtualProtocol
configData:
# ip: "172.18.0.3"

properties:
- name: status
visitors:
configData:
name: status
collectCycle: 10000
reportCycle: 10000
reportToCloud: true
- name: count
visitors:
configData:
name: count
collectCycle: 1000
reportCycle: 10000
reportToCloud: true
# pushMethod:
# mqtt:
# topic: "current count"
# qos: 0
# address: "tcp://127.0.0.1:1884" # replace the address with your mqtt broker address
# retained: false
methods:
- name: UpdateStatus
description: update status of counter. 0=ON,1=OFF 2=PAUSED
propertyNames:
- status
- name: SetCount
description: set count of counter
propertyNames:
- count

16 changes: 16 additions & 0 deletions new-counter-demo/crds/counter-model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: devices.kubeedge.io/v1beta1
kind: DeviceModel
metadata:
name: counter-model
namespace: default
spec:
protocol: "virtualProtocol"
properties:
- name: status
description: counter status:0=ON,1=OFF 2=PAUSED
type: STRING
accessMode: ReadWrite
- name: count
description: counter count
type: STRING
accessMode: ReadWrite
Binary file added new-counter-demo/img/counter-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added new-counter-demo/img/structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions new-counter-demo/virtualprotocol/Dockerfile_nostream
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM golang:1.22.9-alpine3.19 AS builder

WORKDIR /build

ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./cmd/main.go

FROM ubuntu:22.04

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The final image uses ubuntu:22.04, which is quite large for distributing a single, statically-linked binary. Using a smaller base image like gcr.io/distroless/static-debian11 or even alpine would significantly reduce the image size and potential attack surface, improving security and efficiency.

FROM gcr.io/distroless/static-debian11


RUN mkdir -p kubeedge

COPY --from=builder /build/main kubeedge/

COPY ./config.yaml kubeedge/

WORKDIR kubeedge
35 changes: 35 additions & 0 deletions new-counter-demo/virtualprotocol/Dockerfile_stream
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM golang:1.22.9-bullseye AS builder

WORKDIR /build

ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct

COPY . .

RUN apt-get update && \
apt-get install -y bzip2 curl upx-ucl gcc-aarch64-linux-gnu libc6-dev-arm64-cross gcc-arm-linux-gnueabi libc6-dev-armel-cross libva-dev libva-drm2 libx11-dev libvdpau-dev libxext-dev libsdl1.2-dev libxcb1-dev libxau-dev libxdmcp-dev yasm

RUN curl -sLO https://ffmpeg.org/releases/ffmpeg-4.1.6.tar.bz2 && \
tar -jx --strip-components=1 -f ffmpeg-4.1.6.tar.bz2 && \
./configure && make && \
make install

RUN GOOS=linux go build -o main cmd/main.go

FROM ubuntu:18.04

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The base image ubuntu:18.04 has reached its End of Life (EOL) and no longer receives security updates. Using it poses a security risk. Please upgrade to a supported LTS version like ubuntu:22.04. Additionally, the ffmpeg compilation and dependency installation steps are duplicated in both the builder and final stages, which is inefficient. Consider optimizing this to reduce build time and image size.

FROM ubuntu:22.04


RUN mkdir -p kubeedge

RUN apt-get update && \
apt-get install -y bzip2 curl upx-ucl gcc-aarch64-linux-gnu libc6-dev-arm64-cross gcc-arm-linux-gnueabi libc6-dev-armel-cross libva-dev libva-drm2 libx11-dev libvdpau-dev libxext-dev libsdl1.2-dev libxcb1-dev libxau-dev libxdmcp-dev yasm

RUN curl -sLO https://ffmpeg.org/releases/ffmpeg-4.1.6.tar.bz2 && \
tar -jx --strip-components=1 -f ffmpeg-4.1.6.tar.bz2 && \
./configure && make && \
make install
Comment on lines +10 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This Dockerfile is inefficient and can be optimized significantly:

  1. Redundant installations: The apt-get install and ffmpeg compilation steps are repeated in both the builder and the final stage. Build-time dependencies should only be in the builder stage, and the final image should only copy the necessary artifacts.
  2. Outdated base image: The final stage uses ubuntu:18.04, which is quite old. It's better to use a more recent LTS version like ubuntu:22.04 for security and maintenance benefits, consistent with Dockerfile_nostream.
  3. Missing cache cleanup: The RUN apt-get update && apt-get install commands should be followed by && rm -rf /var/lib/apt/lists/* to reduce the image size.
  4. Outdated FFMPEG: ffmpeg-4.1.6 is from 2019. Consider using a newer version or installing it from a package manager if possible to simplify the Dockerfile.


COPY --from=builder /build/main kubeedge/
COPY ./config.yaml kubeedge/

WORKDIR kubeedge
46 changes: 46 additions & 0 deletions new-counter-demo/virtualprotocol/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
SHELL := /bin/bash

curr_dir := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
rest_args := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
$(eval $(rest_args):;@:)

help:
#
# Usage:
# make generate : generate a mapper based on a template.
# make mapper {mapper-name} <action> <parameter>: execute mapper building process.
#
# Actions:
# - mod, m : download code dependencies.
# - lint, l : verify code via go fmt and `golangci-lint`.
# - build, b : compile code.
# - package, p : package docker image.
# - clean, c : clean output binary.
#
# Parameters:
# ARM : true or undefined
# ARM64 : true or undefined
#
# Example:
# - make mapper modbus ARM64=true : execute `build` "modbus" mapper for ARM64.
# - make mapper modbus test : execute `test` "modbus" mapper.
@echo

make_rules := $(shell ls $(curr_dir)/hack/make-rules | sed 's/.sh//g')
$(make_rules):
@$(curr_dir)/hack/make-rules/[email protected] $(rest_args)

.DEFAULT_GOAL := help
.PHONY: $(make_rules) build test package

build-app:
CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./cmd/main.go
docker cp ./main kind-worker:./srv

redeploy-crds:
kubectl delete -f ../crds/counter-instance.yaml
kubectl delete -f ../crds/counter-model.yaml
kubectl apply -f ../crds/counter-model.yaml
kubectl apply -f ../crds/counter-instance.yaml
deploy-crds:
kubectl apply -f ../crds/
62 changes: 62 additions & 0 deletions new-counter-demo/virtualprotocol/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"errors"

"k8s.io/klog/v2"

"github.com/kubeedge/mapper-framework/pkg/common"
"github.com/kubeedge/mapper-framework/pkg/config"
"github.com/kubeedge/mapper-framework/pkg/grpcclient"
"github.com/kubeedge/mapper-framework/pkg/grpcserver"
"github.com/kubeedge/mapper-framework/pkg/httpserver"
"github.com/kubeedge/virtualprotocol/device"

)

func main() {

var err error
var c *config.Config

klog.InitFlags(nil)
defer klog.Flush()

if c, err = config.Parse(); err != nil {
klog.Fatal(err)
}
klog.Infof("config: %+v", c)

klog.Infoln("Mapper will register to edgecore")
deviceList, deviceModelList, err := grpcclient.RegisterMapper(true)
if err != nil {
klog.Fatal(err)
}
klog.Infoln("Mapper register finished")

panel := device.NewDevPanel()
err = panel.DevInit(deviceList, deviceModelList)
if err != nil && !errors.Is(err, device.ErrEmptyData) {
klog.Fatal(err)
}
klog.Infoln("devInit finished")
go panel.DevStart()

// start http server
httpServer := httpserver.NewRestServer(panel, c.Common.HTTPPort)
go httpServer.StartServer()

// start grpc server
grpcServer := grpcserver.NewServer(
grpcserver.Config{
SockPath: c.GrpcServer.SocketPath,
Protocol: common.ProtocolCustomized,
},
panel,
)
defer grpcServer.Stop()
if err = grpcServer.Start(); err != nil {
klog.Fatal(err)
}

}
9 changes: 9 additions & 0 deletions new-counter-demo/virtualprotocol/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
grpc_server:
socket_path: /etc/kubeedge/virtualprotocol.sock
common:
name: VirtualProtocol-mapper
version: v1.13.0
api_version: v1.0.0
protocol: virtualProtocol
address: 127.0.0.1
edgecore_sock: /etc/kubeedge/dmi.sock
Loading