Skip to content

Commit

Permalink
imeplement plc4x (#275)
Browse files Browse the repository at this point in the history
* imeplement plc4x

* dump yaml.v2 => yaml.v3

* modify crd

* Update azure-pipelines.yml for Azure Pipelines

* Update codecov.yml

* modify test

* revert some file

* add dockerfile and pipeline

* fix pipeline

* add deployment YAML file

* fix parse param but dockerfile need to update

* fix dockerfile

* fix pipeline

* dump go.mod

* test in pipeline

* Update azure-pipelines.yml

* modify io/ioutil => io

* add other protocol on plc4x

* rename deployment file name

* tiny change

* tiny modify the file

* fix pipeline

* Revert "tiny modify the file"

This reverts commit bc8a02e.

* Revert "fix pipeline"

This reverts commit 42bebf2.

* tidy up

* tidy fix
  • Loading branch information
rhoninl authored Oct 10, 2022
1 parent bdc9c4f commit 65e124d
Show file tree
Hide file tree
Showing 25 changed files with 724 additions and 303 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
- uses: actions/setup-go@v2
with:
go-version: '1.19'
- name: Install pcap
run: sudo apt-get install libpcap-dev
- name: Run coverage
run: make test
- name: Codecov
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Install pcap
run: sudo apt-get install libpcap-dev
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ buildx-push-image-deviceshifu-http-opcua:
--build-arg PROJECT_ROOT="${PROJECT_ROOT}" ${PROJECT_ROOT} \
-t edgehub/deviceshifu-http-opcua:${IMAGE_VERSION} --push

buildx-push-image-deviceshifu-http-plc4x:
docker buildx build --platform=linux/amd64,linux/arm64,linux/arm -f ${PROJECT_ROOT}/dockerfiles/Dockerfile.deviceshifuPLC4X \
--build-arg PROJECT_ROOT="${PROJECT_ROOT}" ${PROJECT_ROOT} \
-t edgehub/deviceshifu-http-plc4x:${IMAGE_VERSION} --push

.PHONY: buildx-push-image-deviceshifu
buildx-push-image-deviceshifu: \
buildx-push-image-deviceshifu-http-http \
Expand Down Expand Up @@ -60,11 +65,17 @@ buildx-build-image-deviceshifu-http-opcua:
--build-arg PROJECT_ROOT="${PROJECT_ROOT}" ${PROJECT_ROOT} \
-t edgehub/deviceshifu-http-opcua:${IMAGE_VERSION} --load

buildx-build-image-deviceshifu-http-plc4x:
docker buildx build --platform=linux/$(shell go env GOARCH) -f ${PROJECT_ROOT}/dockerfiles/Dockerfile.deviceshifuPLC4X\
--build-arg PROJECT_ROOT="${PROJECT_ROOT}" ${PROJECT_ROOT} \
-t edgehub/deviceshifu-http-plc4x:${IMAGE_VERSION} --load

buildx-build-image-deviceshifu: \
buildx-build-image-deviceshifu-http-http \
buildx-build-image-deviceshifu-http-mqtt \
buildx-build-image-deviceshifu-http-socket \
buildx-build-image-deviceshifu-http-opcua
buildx-build-image-deviceshifu-http-opcua \
buildx-build-image-deviceshifu-http-plc4x

.PHONY: download-demo-files
download-demo-files:
Expand Down
22 changes: 21 additions & 1 deletion azure-pipelines/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ stages:
command: 'get'
arguments: '-d'
workingDirectory: '$(System.DefaultWorkingDirectory)/pkg/deviceshifu/deviceshifuhttp'
- script: |
sudo apt-get install libpcap-dev
displayName: "Install pcap"
- script: |
make test
displayName: "Go test"
Expand Down Expand Up @@ -420,7 +423,24 @@ stages:
kubectl delete -f examples/deviceshifu/demo_device/edgedevice-mqtt
kubectl delete po mosquitto -n devices
displayName: "Shifu demo device MQTT E2E test"
- job: build_test_plc4x
steps:
- script: |
tag=`cat version.txt` && echo "##vso[task.setvariable variable=tag]$tag"
displayName: Set the tag name as an environment variable
- task: DockerInstaller@0
displayName: "Docker Installer"
inputs:
dockerVersion: 20.10.9
releaseType: stable
- script: |
docker buildx build --platform=linux/amd64 \
-f $(System.DefaultWorkingDirectory)/dockerfiles/Dockerfile.deviceshifuPLC4X \
--build-arg PROJECT_ROOT="$(System.DefaultWorkingDirectory)" $(System.DefaultWorkingDirectory) \
-t edgehub/deviceshifu-http-plc4x:$(tag) --load
displayName: build edgehub/deviceshifu-http-plc4x
- stage: docker_build_muiltiarch_and_push
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
variables:
Expand Down
35 changes: 35 additions & 0 deletions cmd/deviceshifu/cmdplc4x/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"os"

"github.com/edgenesis/shifu/pkg/deviceshifu/deviceshifubase"
"github.com/edgenesis/shifu/pkg/deviceshifu/deviceshifuplc4x"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
)

func main() {
deviceName := os.Getenv("EDGEDEVICE_NAME")
namespace := os.Getenv("EDGEDEVICE_NAMESPACE")

deviceShifuMetadata := &deviceshifubase.DeviceShifuMetaData{
Name: deviceName,
ConfigFilePath: deviceshifubase.DeviceConfigmapFolderPath,
KubeConfigPath: deviceshifubase.KubernetesConfigDefault,
Namespace: namespace,
}

ds, err := deviceshifuplc4x.New(deviceShifuMetadata)
if err != nil {
panic(err.Error())
}

err = ds.Start(wait.NeverStop)
if err != nil {
klog.Errorf("deviceshifu start default, error: %v", err)
return
}
select {}
}
40 changes: 40 additions & 0 deletions dockerfiles/Dockerfile.deviceshifuPLC4X
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.19.1 as builder

WORKDIR /shifu

ENV GO111MODULE=on

COPY go.mod go.mod
COPY go.sum go.sum
COPY pkg/k8s pkg/k8s
COPY cmd/deviceshifu/cmdplc4x cmd/deviceshifu/cmdplc4x
COPY pkg/deviceshifu pkg/deviceshifu

RUN apt-get update
RUN apt-get install libpcap-dev -y

RUN go mod download
RUN mkdir -p /output/lib
RUN cp $(whereis liblz4.so.1 | awk '{ print $2 }') /output/lib
RUN cp $(whereis liblzma.so.5 | awk '{ print $2 }') /output/lib
RUN cp $(whereis libzstd.so.1 | awk '{ print $2 }') /output/lib
RUN cp $(whereis libdbus-1.so.3 | awk '{ print $2 }') /output/lib
RUN cp $(whereis libpcap.so.0.8 | awk '{ print $2 }') /output/lib
RUN cp $(whereis libsystemd.so.0 | awk '{ print $2 }') /output/lib
RUN cp $(whereis libgcrypt.so.20 | awk '{ print $2 }') /output/lib
RUN cp $(whereis libgpg-error.so.0 | awk '{ print $2 }') /output/lib

# Build the Go app
ARG TARGETOS
ARG TARGETARCH

RUN CGO_ENABLED=1 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -a -v -o /output/deviceshifu cmd/deviceshifu/cmdplc4x/main.go

FROM gcr.io/distroless/base
WORKDIR /
COPY --from=builder /output/deviceshifu deviceshifu
COPY --from=builder /output/lib /lib

USER 65532:65532
ENTRYPOINT ["/deviceshifu"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"io/ioutil"
"io"
"log"
"net/http"
"strconv"
Expand All @@ -13,7 +13,7 @@ func main() {
req, _ := http.NewRequest("GET", targetURL, nil)
for {
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
body, _ := io.ReadAll(res.Body)
temperature, _ := strconv.Atoi(string(body))
if temperature > 20 {
log.Println("High temperature:", temperature)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: plc4x-configmap
namespace: deviceshifu
data:
driverProperties: |
driverSku: testPlc4x
driverImage:
instructions: |
instructions:
telemetries: |
telemetrySettings:
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: deviceshifu-plc4x-deployment
name: deviceshifu-plc4x-deployment
namespace: deviceshifu
spec:
replicas: 1
selector:
matchLabels:
app: deviceshifu-plc4x-deployment
template:
metadata:
labels:
app: deviceshifu-plc4x-deployment
spec:
containers:
- image: edgehub/deviceshifu-http-plc4x:nightly
name: deviceshifu-http
ports:
- containerPort: 8080
volumeMounts:
- name: deviceshifu-config
mountPath: "/etc/edgedevice/config"
readOnly: true
env:
- name: EDGEDEVICE_NAME
value: "edgedevice-plc4x"
- name: EDGEDEVICE_NAMESPACE
value: "devices"
volumes:
- name: deviceshifu-config
configMap:
name: plc4x-configmap
serviceAccountName: edgedevice-sa
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
labels:
app: deviceshifu-plc4x-deployment
name: deviceshifu-plc4x
namespace: deviceshifu
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: deviceshifu-plc4x-deployment
type: LoadBalancer

13 changes: 13 additions & 0 deletions examples/plc4xDeviceShifu/deployment/plc4x_edgedevice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: shifu.edgenesis.io/v1alpha1
kind: EdgeDevice
metadata:
name: edgedevice-plc4x
namespace: devices
spec:
sku: "testPlc4x"
connection: Ethernet
address: 192.168.0.1 #change this accordingly
protocol: PLC4X
protocolSettings:
PLC4XSetting:
protocol: s7
17 changes: 13 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module github.com/edgenesis/shifu
go 1.19

require (
github.com/apache/plc4x/plc4go v0.0.0-20220929155823-14e7d8450c87
github.com/eclipse/paho.mqtt.golang v1.4.1
github.com/gopcua/opcua v0.3.7
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.25.2
k8s.io/client-go v0.25.2
knative.dev/pkg v0.0.0-20210915055909-d8349b0909c4
knative.dev/pkg v0.0.0-20220929013917-2b8e4ad44f04
)

require (
Expand All @@ -22,13 +23,14 @@ require (
require github.com/pmezard/go-difflib v1.0.0 // indirect

require (
cloud.google.com/go v0.97.0 // indirect
cloud.google.com/go v0.98.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/IBM/netaddr v1.5.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -49,12 +51,18 @@ require (
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/icza/bitio v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/libp2p/go-reuseport v0.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -65,6 +73,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rs/zerolog v1.28.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0
go.uber.org/atomic v1.9.0 // indirect
Expand All @@ -73,8 +82,8 @@ require (
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
Expand Down
Loading

0 comments on commit 65e124d

Please sign in to comment.