- 
                Notifications
    You must be signed in to change notification settings 
- Fork 170
Fix and optimize the counter demo #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | 
| 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 | ||
|  | ||
|  | ||
| ## 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 | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | 
| 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 | ||
|  | 
| 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 | 
| 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 | ||
|  | ||
| RUN mkdir -p kubeedge | ||
|  | ||
| COPY --from=builder /build/main kubeedge/ | ||
|  | ||
| COPY ./config.yaml kubeedge/ | ||
|  | ||
| WORKDIR kubeedge | ||
| 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 | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The base image   | ||
|  | ||
| 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
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Dockerfile is inefficient and can be optimized significantly: 
 | ||
|  | ||
| COPY --from=builder /build/main kubeedge/ | ||
| COPY ./config.yaml kubeedge/ | ||
|  | ||
| WORKDIR kubeedge | ||
| 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/ | 
| 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) | ||
| } | ||
|  | ||
| } | 
| 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 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The final image uses
ubuntu:22.04, which is quite large for distributing a single, statically-linked binary. Using a smaller base image likegcr.io/distroless/static-debian11or evenalpinewould significantly reduce the image size and potential attack surface, improving security and efficiency.