-
Notifications
You must be signed in to change notification settings - Fork 118
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
add fsm example traffic light #623
Open
huerni
wants to merge
1
commit into
Edgenesis:main
Choose a base branch
from
huerni:fsm/trafficlight
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
FROM golang:1.20.2-alpine | ||
WORKDIR /app | ||
ENV GO111MODULE=on | ||
ENV GOPRIVATE=github.com/Edgenesis | ||
ENV GOPROXY=https://goproxy.cn,direct | ||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
COPY pkg/deviceshifu pkg/deviceshifu | ||
COPY pkg/logger pkg/logger | ||
RUN go mod download | ||
COPY pkg/fsm/examples/traffic-light-device/*.go ./ | ||
RUN go build -o /trafficlight | ||
EXPOSE 11111 | ||
CMD [ "/trafficlight" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
### 1. 运行 *Shifu* 并连接一个虚拟traffic light | ||
在 `shifu` 根目录下,运行下面命令来运行 *shifu* : | ||
|
||
```shell | ||
kubectl apply -f pkg/k8s/crd/install/shifu_install.yml | ||
``` | ||
在 `shifu` 根目录下,运行下面命令来把虚拟 traffic light 打包成 Docker 镜像: | ||
|
||
```shell | ||
docker build -t trafficlight-device:v0.0.1 . -f pkg/fsm/examples/traffic-light-device/Dockerfile | ||
``` | ||
在 `shifu` 根目录下,运行下面命令来把虚拟 traffic light 镜像加载到 Kind 中,并部署到 Kubernetes 集群中: | ||
|
||
```shell | ||
kind load docker-image trafficlight-device:v0.0.1 | ||
kubectl apply -f pkg/fsm/examples/traffic-light-device/configuration | ||
``` | ||
|
||
### 2. 与 *deviceShifu* 交互 | ||
我们可以通过 nginx 应用来和 *deviceShifu* 交互,命令为: | ||
|
||
```shell | ||
kubectl run nginx --image=nginx | ||
kubectl exec -it nginx -- bash | ||
``` | ||
在 nginx 命令行中通过如下命令与虚拟 traffic light 进行交互: | ||
|
||
```shell | ||
curl "trafficlight.devices.svc.cluster.local:11111/get_color";echo | ||
curl "trafficlight.devices.svc.cluster.local:11111/stop";echo | ||
curl "trafficlight.devices.svc.cluster.local:11111/proceed";echo | ||
curl "trafficlight.devices.svc.cluster.local:11111/caution";echo | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
### 1. Running Shifu and connecting a virtual traffic light | ||
In the shifu root directory, run the following command to run Shifu: | ||
```shell | ||
kubectl apply -f pkg/k8s/crd/install/shifu_install.yml | ||
``` | ||
|
||
In the shifu root directory, run the following command to package the virtual traffic light into a Docker image: | ||
```shell | ||
docker build -t trafficlight-device:v0.0.1 . -f pkg/fsm/examples/traffic-light-device/Dockerfile | ||
``` | ||
|
||
In the shifu root directory, run the following commands to load the virtual traffic light image into Kind and deploy it to the Kubernetes cluster: | ||
```shell | ||
kind load docker-image trafficlight-device:v0.0.1 | ||
kubectl apply -f pkg/fsm/examples/traffic-light-device/configuration | ||
``` | ||
|
||
### 2. Interacting with deviceShifu | ||
We can interact with deviceShifu through the nginx application using the following commands: | ||
```shell | ||
kubectl run nginx --image=nginx | ||
kubectl exec -it nginx -- bash | ||
``` | ||
|
||
In the nginx command line, use the following commands to interact with the virtual traffic light: | ||
```shell | ||
curl "trafficlight.devices.svc.cluster.local:11111/get_color";echo | ||
curl "trafficlight.devices.svc.cluster.local:11111/stop";echo | ||
curl "trafficlight.devices.svc.cluster.local:11111/proceed";echo | ||
curl "trafficlight.devices.svc.cluster.local:11111/caution";echo | ||
``` | ||
These commands allow you to communicate with the virtual traffic light deployed in the Kubernetes cluster by making HTTP requests to the specified endpoints. |
29 changes: 29 additions & 0 deletions
29
pkg/fsm/examples/traffic-light-device/configuration/deviceshifu-trafficlight-configmap.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: trafficlight-configmap-0.0.1 | ||
namespace: deviceshifu | ||
data: | ||
# device name and image address | ||
driverProperties: | | ||
driverSku: Traffic Light | ||
driverImage: trafficlight-device:v0.0.1 | ||
# available instructions | ||
instructions: | | ||
instructionSettings: | ||
defaultTimeoutSeconds: 8 | ||
instructions: | ||
stop: | ||
caution: | ||
proceed: | ||
get_color: | ||
get_statue: | ||
# telemetry retrieval methods | ||
# in this examples, a device_health telemetry is collected by calling hello instruction every 1 second | ||
telemetries: | | ||
telemetries: | ||
device_health: | ||
properties: | ||
instruction: get_statue | ||
initialDelayMs: 1000 | ||
intervalMs: 1000 |
37 changes: 37 additions & 0 deletions
37
pkg/fsm/examples/traffic-light-device/configuration/deviceshifu-trafficlight-deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
labels: | ||
app: deviceshifu-trafficlight-deployment | ||
name: deviceshifu-trafficlight-deployment | ||
namespace: deviceshifu | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: deviceshifu-trafficlight-deployment | ||
template: | ||
metadata: | ||
labels: | ||
app: deviceshifu-trafficlight-deployment | ||
spec: | ||
containers: | ||
- image: edgehub/deviceshifu-http-http:nightly | ||
name: deviceshifu-http | ||
ports: | ||
- containerPort: 8080 | ||
volumeMounts: | ||
- name: deviceshifu-config | ||
mountPath: "/etc/edgedevice/config" | ||
readOnly: true | ||
env: | ||
- name: EDGEDEVICE_NAME | ||
value: "edgedevice-trafficlight" | ||
- name: EDGEDEVICE_NAMESPACE | ||
value: "devices" | ||
volumes: | ||
- name: deviceshifu-config | ||
configMap: | ||
name: trafficlight-configmap-0.0.1 | ||
serviceAccountName: edgedevice-sa | ||
|
15 changes: 15 additions & 0 deletions
15
pkg/fsm/examples/traffic-light-device/configuration/deviceshifu-trafficlight-service.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
app: deviceshifu-trafficlight-deployment | ||
name: deviceshifu-trafficlight-service | ||
namespace: deviceshifu | ||
spec: | ||
ports: | ||
- port: 80 | ||
protocol: TCP | ||
targetPort: 8080 | ||
selector: | ||
app: deviceshifu-trafficlight-deployment | ||
type: LoadBalancer |
27 changes: 27 additions & 0 deletions
27
pkg/fsm/examples/traffic-light-device/configuration/trafficlight-deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
labels: | ||
app: trafficlight | ||
name: trafficlight | ||
namespace: devices | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: trafficlight | ||
template: | ||
metadata: | ||
labels: | ||
app: trafficlight | ||
spec: | ||
containers: | ||
- image: trafficlight-device:v0.0.1 | ||
name: trafficlight | ||
ports: | ||
- containerPort: 11111 | ||
env: | ||
- name: MOCKDEVICE_NAME | ||
value: trafficlight | ||
- name: MOCKDEVICE_PORT | ||
value: "11111" |
10 changes: 10 additions & 0 deletions
10
pkg/fsm/examples/traffic-light-device/configuration/trafficlight-edgedevice.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: shifu.edgenesis.io/v1alpha1 | ||
kind: EdgeDevice | ||
metadata: | ||
name: edgedevice-trafficlight | ||
namespace: devices | ||
spec: | ||
sku: "Traffic Light" | ||
connection: Ethernet | ||
address: trafficlight.devices.svc.cluster.local:11111 | ||
protocol: HTTP |
16 changes: 16 additions & 0 deletions
16
pkg/fsm/examples/traffic-light-device/configuration/trafficlight-service.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
app: trafficlight | ||
name: trafficlight | ||
namespace: devices | ||
spec: | ||
ports: | ||
- port: 11111 | ||
protocol: TCP | ||
targetPort: 11111 | ||
selector: | ||
app: trafficlight | ||
type: LoadBalancer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/edgenesis/shifu/pkg/deviceshifu/mockdevice/mockdevice" | ||
"github.com/edgenesis/shifu/pkg/logger" | ||
"github.com/looplab/fsm" | ||
"math/rand" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
RED string = "RED" | ||
YELLOW string = "YELLOW" | ||
GREEN string = "GREEN" | ||
) | ||
|
||
const ( | ||
STOP string = "STOP" | ||
CAUTION string = "CAUTION" | ||
PROCEED string = "PROCEED" | ||
) | ||
|
||
type TrafficLight struct { | ||
FSM *fsm.FSM | ||
} | ||
|
||
func NewTrafficLight(color string) *TrafficLight { | ||
tl := &TrafficLight{} | ||
|
||
tl.FSM = fsm.NewFSM( | ||
color, | ||
fsm.Events{ | ||
{Name: STOP, Src: []string{YELLOW}, Dst: RED}, | ||
{Name: CAUTION, Src: []string{GREEN}, Dst: YELLOW}, | ||
{Name: PROCEED, Src: []string{RED}, Dst: GREEN}, | ||
}, | ||
fsm.Callbacks{}, | ||
) | ||
|
||
return tl | ||
} | ||
|
||
var trafficLight *TrafficLight | ||
|
||
func main() { | ||
availableFuncs := []string{ | ||
"stop", | ||
"caution", | ||
"proceed", | ||
"get_color", | ||
"get_status", | ||
} | ||
trafficLight = NewTrafficLight(RED) | ||
mockdevice.StartMockDevice(availableFuncs, instructionHandler) | ||
} | ||
|
||
func instructionHandler(functionName string) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
logger.Infof("Handling: %v", functionName) | ||
switch functionName { | ||
case "stop": | ||
err := trafficLight.FSM.Event(context.Background(), STOP) | ||
|
||
if err != nil { | ||
logger.Warnf("Disable transition from %v to %v, must be %v", trafficLight.FSM.Current(), RED, YELLOW) | ||
w.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprintf(w, "Disable transition from %v to %v, must be %v", trafficLight.FSM.Current(), RED, YELLOW) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(w, "Transition from %v to %v", YELLOW, trafficLight.FSM.Current()) | ||
case "caution": | ||
err := trafficLight.FSM.Event(context.Background(), CAUTION) | ||
if err != nil { | ||
logger.Warnf("Disable transition from %v to %v, must be %v", trafficLight.FSM.Current(), YELLOW, GREEN) | ||
w.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprintf(w, "Disable transition from %v to %v, must be %v", trafficLight.FSM.Current(), YELLOW, GREEN) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(w, "Transition from %v to %v", GREEN, trafficLight.FSM.Current()) | ||
case "proceed": | ||
err := trafficLight.FSM.Event(context.Background(), PROCEED) | ||
if err != nil { | ||
logger.Warnf("Disable transition from %v to %v, must be %v", trafficLight.FSM.Current(), GREEN, RED) | ||
w.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprintf(w, "Disable transition from %v to %v, must be %v", trafficLight.FSM.Current(), GREEN, RED) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(w, "Transition from %v to %v", RED, trafficLight.FSM.Current()) | ||
case "get_color": | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(w, "traffic light current state: %v", trafficLight.FSM.Current()) | ||
case "get_status": | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(w, mockdevice.StatusSetList[(rand.Intn(len(mockdevice.StatusSetList)))]) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@huerni, to be more specific, assume the trafficlight is the edgedevice, the question would turn to - how can the FSM control this edge device?
Here you wrote a mock device that have the feature we've described while our final goal is try to make the FSM can be applied to any edge device.
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.
I apologize for the confusion, I'm not very familiar with edge devices and FSM either. To clarify, are you asking if it's possible to write an FSM controller that can be used for any edge device? Or are you suggesting that each edge device should be able to quickly build its own FSM?
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.
We are going to have a proper design for the FSM feature in the following weeks. You can refer to the design for implementation details.