From cb458e60b17910ca720d5c7a00bf773c9dbbdb1d Mon Sep 17 00:00:00 2001 From: yq <118748448+beingStrongeryqqq@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:11:22 +0800 Subject: [PATCH] Lwm2m without security (#1022) * lwm2m without security update * modify lwm2m without security completedly * add sleep command in azure-pipelines * push for go test * fix space in azure-pipelines * push for go test * update script * update shell script to python script * back to shell * back to shell * back to shell * update azure-pipelines * update shell --- azure-pipelines/azure-pipelines.yml | 37 ++++++++++++ .../mockdevice/lwM2M/checkoutput.sh | 60 +++++++++++++++++++ .../lwM2M/lwm2m-deviceshifu-configmap.yaml | 21 +++++++ .../lwM2M/lwm2m-deviceshifu-deployment.yaml | 38 ++++++++++++ .../lwM2M/lwm2m-deviceshifu-service.yaml | 22 +++++++ .../lwM2M/lwm2m-edgedevice.yaml | 13 ++++ .../mockdevice/lwm2m-mock.yaml | 22 +++++++ 7 files changed, 213 insertions(+) create mode 100755 examples/deviceshifu/mockdevice/lwM2M/checkoutput.sh create mode 100644 examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-configmap.yaml create mode 100644 examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-deployment.yaml create mode 100644 examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-service.yaml create mode 100644 examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-edgedevice.yaml create mode 100644 examples/lwM2MDeviceshifuWithoutSecurity/mockdevice/lwm2m-mock.yaml diff --git a/azure-pipelines/azure-pipelines.yml b/azure-pipelines/azure-pipelines.yml index de3f20af1..91801e532 100644 --- a/azure-pipelines/azure-pipelines.yml +++ b/azure-pipelines/azure-pipelines.yml @@ -211,6 +211,43 @@ stages: kubectl delete -f $(System.DefaultWorkingDirectory)/examples/deviceshifu/demo_device/edgedevice-opcua/mock-device displayName: "Shifu demo device OPCUA E2E test" + - job: kind_e2e_test_lwm2m_without_security + steps: + - task: GoTool@0 + inputs: + version: $(goVersion) + - script: | + tag=`cat version.txt` && echo "##vso[task.setvariable variable=tag]$tag" + displayName: Set the tag name as an environment variable + - script: | + make buildx-build-image-shifu-controller + displayName: build edgehub/shifu-controller + - script: | + make buildx-build-image-deviceshifu-http-lwm2m + displayName: build edgehub/deviceshifu-http-lwm2m + - script: | + set -e + kind --version + kind delete cluster && kind create cluster + kind load docker-image edgehub/shifu-controller:$(tag) + kind load docker-image edgehub/deviceshifu-http-lwm2m:$(tag) + + kubectl version + kubectl apply -f $(System.DefaultWorkingDirectory)/pkg/k8s/crd/install/shifu_install.yml + kubectl wait --for=condition=Available deploy/shifu-crd-controller-manager -n shifu-crd-system --timeout=150s + displayName: "setup Kind cluster and install Shifu" + - script: | + set -e + kubectl run nginx --image=nginx -n deviceshifu + kubectl apply -f $(System.DefaultWorkingDirectory)/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M + kubectl wait --for=condition=Available deploy/deviceshifu-lwm2m-deployment -n deviceshifu --timeout=150s + kubectl wait --for=condition=Ready pod/nginx -n deviceshifu --timeout=150s + kubectl apply -f $(System.DefaultWorkingDirectory)/examples/lwM2MDeviceshifuWithoutSecurity/mockdevice + kubectl wait --for=condition=available --timeout=150s deployment/leshan-client -n deviceshifu + bash $(System.DefaultWorkingDirectory)/examples/deviceshifu/mockdevice/lwM2M/checkoutput.sh + kubectl delete -R -f $(System.DefaultWorkingDirectory)/examples/lwM2MDeviceshifuWithoutSecurity + displayName: "Shifu demo device lwM2M E2E test" + - job: kind_e2e_test_socket steps: - task: GoTool@0 diff --git a/examples/deviceshifu/mockdevice/lwM2M/checkoutput.sh b/examples/deviceshifu/mockdevice/lwM2M/checkoutput.sh new file mode 100755 index 000000000..7273c4a09 --- /dev/null +++ b/examples/deviceshifu/mockdevice/lwM2M/checkoutput.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Default value to write to the mock device +writeData=88.8 + +# Get the pod name of deviceshifu +pod_name=$(kubectl get pods -n deviceshifu -l app=deviceshifu-lwm2m-deployment -o jsonpath='{.items[0].metadata.name}') + +if [ -z "$pod_name" ]; then + echo "No deviceshifu-lwm2m pod found. Exiting..." + exit 1 +fi + +# Function to retrieve value from the LwM2M server +get_value() { + kubectl exec -n deviceshifu nginx -- curl --connect-timeout 5 deviceshifu-lwm2m.deviceshifu.svc.cluster.local/float_value +} + +# Attempt to get the value with retries +for i in {1..5}; do + out=$(get_value) + + # Remove any whitespace and newline characters + out=$(echo "$out" | tr -d '\r\n') + + # Output the status + echo "Received value: $out" + + # Check if the server response indicates an error + if [[ $out != "Error on reading object" ]]; then + break + fi + + echo "Device is unhealthy. Attempting to reconnect... ($i/5)" + sleep 3 +done + +if [[ $out == "Error on reading object" ]]; then + echo "Device is still unhealthy after 5 attempts. Exiting..." + kubectl logs -n deviceshifu $pod_name + exit 1 +fi + +# Use deviceshifu to write data to the mock device with retry settings +kubectl exec -n deviceshifu nginx -- curl --retry 5 --retry-delay 3 --max-time 15 --connect-timeout 5 -X PUT deviceshifu-lwm2m.deviceshifu.svc.cluster.local/float_value -d $writeData + +# Retrieve the value again after writing to verify if it was successful +out=$(get_value) + +# Remove any whitespace and newline characters +out=$(echo "$out" | tr -d '\r\n') + +# Check if the modification was successful +if awk "BEGIN {exit !($out == $writeData)}"; then + echo "Modification successful" + exit 0 +else + echo "Modification failed, expected: $writeData, got: $out" + exit 1 +fi diff --git a/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-configmap.yaml b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-configmap.yaml new file mode 100644 index 000000000..bf02f6891 --- /dev/null +++ b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-configmap.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: deviceshifu-lwm2m + namespace: deviceshifu +data: + driverProperties: | + driverSku: LwM2M Device + driverImage: lwm2m-device:v0.0.1 + leshanConfig: | + serverUrl: coap://leshan-service.deviceshifu.svc.cluster.local:5683 + instructions: | + instructions: + float_value: + protocolPropertyList: + ObjectId: /3442/0/130 + EnableObserve: false + reset: + protocolPropertyList: + ObjectId: /3303/0/5605 + EnableObserve: false diff --git a/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-deployment.yaml b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-deployment.yaml new file mode 100644 index 000000000..6d087db6e --- /dev/null +++ b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-deployment.yaml @@ -0,0 +1,38 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: deviceshifu-lwm2m-deployment + name: deviceshifu-lwm2m-deployment + namespace: deviceshifu +spec: + replicas: 1 + selector: + matchLabels: + app: deviceshifu-lwm2m-deployment + template: + metadata: + labels: + app: deviceshifu-lwm2m-deployment + spec: + containers: + - image: edgehub/deviceshifu-http-lwm2m:nightly + name: deviceshifu-lwm2m + ports: + - containerPort: 8080 + volumeMounts: + - name: deviceshifu-config + mountPath: "/etc/edgedevice/config" + readOnly: true + env: + - name: EDGEDEVICE_NAME + value: "edgedevice-lwm2m" + - name: EDGEDEVICE_NAMESPACE + value: "devices" + - name: LOG_LEVEL + value: debug + volumes: + - name: deviceshifu-config + configMap: + name: deviceshifu-lwm2m + serviceAccountName: edgedevice-sa diff --git a/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-service.yaml b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-service.yaml new file mode 100644 index 000000000..4692c1ee1 --- /dev/null +++ b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-deviceshifu-service.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: deviceshifu-lwm2m-deployment + name: deviceshifu-lwm2m + namespace: deviceshifu +spec: + ports: + - name: deviceshifu + port: 80 + protocol: TCP + nodePort: 30080 + targetPort: 8080 + - name: lwm2mserver-coap + port: 5683 + protocol: UDP + nodePort: 30000 + targetPort: 5683 + selector: + app: deviceshifu-lwm2m-deployment + type: NodePort diff --git a/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-edgedevice.yaml b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-edgedevice.yaml new file mode 100644 index 000000000..b1e50dd46 --- /dev/null +++ b/examples/lwM2MDeviceshifuWithoutSecurity/lwM2M/lwm2m-edgedevice.yaml @@ -0,0 +1,13 @@ +apiVersion: shifu.edgenesis.io/v1alpha1 +kind: EdgeDevice +metadata: + name: edgedevice-lwm2m + namespace: devices +spec: + sku: "LwM2M Device" + connection: Ethernet + protocol: LwM2M + protocolSettings: + LwM2MSetting: + endpointName: leshan-client + securityMode: None diff --git a/examples/lwM2MDeviceshifuWithoutSecurity/mockdevice/lwm2m-mock.yaml b/examples/lwM2MDeviceshifuWithoutSecurity/mockdevice/lwm2m-mock.yaml new file mode 100644 index 000000000..1e7da4dd2 --- /dev/null +++ b/examples/lwM2MDeviceshifuWithoutSecurity/mockdevice/lwm2m-mock.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: leshan-client + namespace: deviceshifu + labels: + app: leshan-client +spec: + replicas: 1 + selector: + matchLabels: + app: leshan-client + template: + metadata: + labels: + app: leshan-client + spec: + containers: + - name: leshan-client + image: edgenesis/lwm2m-demo-leshan-client:nightly + command: ["java", "-jar", "leshan-client-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar", "-u", "coap://deviceshifu-lwm2m.deviceshifu.svc.cluster.local:5683", "-n", "leshan-client"] + restartPolicy: Always