Skip to content
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 config load #698

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions pkg/deviceshifu/deviceshifubase/configmap_snippet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,24 @@ data:
intervalMs: 1000
pushSettings:
pushToServer: false
fsm: |
states:
red:
actions:
go:
next_state: green
forbid:
- caution
green:
actions:
caution:
next_state: yellow
forbid:
- stop
yellow:
actions:
stop:
next_state: red
forbid:
- go
startingState: red
Copy link
Contributor

Choose a reason for hiding this comment

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

Add an empty line

1 change: 1 addition & 0 deletions pkg/deviceshifu/deviceshifubase/deviceshifubase.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
PythonHandlersModuleName = "customized_handlers"
PythonScriptDir = "pythoncustomizedhandlers"
ControlMsgsConfigStr = "controlMsgs"
FSMStr = "fsm"
)

var (
Expand Down
23 changes: 23 additions & 0 deletions pkg/deviceshifu/deviceshifubase/deviceshifubase_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type DeviceShifuConfig struct {
Telemetries *DeviceShifuTelemetries
CustomInstructionsPython map[string]string `yaml:"customInstructionsPython"`
ControlMsgs map[string]string `yaml:"controlMsgs,omitempty"`
FSM *DeviceShifuFSM `yaml:"fsm,omitempty"`
}

// DeviceShifuDriverProperties properties of deviceshifuDriver
Expand Down Expand Up @@ -98,6 +99,20 @@ type EdgeDeviceConfig struct {
KubeconfigPath string
}

type DeviceShifuFSM struct {
States map[string]DeviceShifuState `yaml:"states"`
StartingState string `yaml:"startingState"`
}

type DeviceShifuState struct {
Actions map[string]DeviceShifuAction `yaml:"actions"`
Forbid []string `yaml:"forbid"`
}

type DeviceShifuAction struct {
NextState string `yaml:"next_state"`
}

// default value
const (
DeviceInstructionInitialDelay int64 = 3000
Expand Down Expand Up @@ -164,6 +179,14 @@ func NewDeviceShifuConfig(path string) (*DeviceShifuConfig, error) {
}
}

if fsm, ok := cfg[FSMStr]; ok {
err = yaml.Unmarshal([]byte(fsm), &dsc.FSM)
if err != nil {
logger.Errorf("Error parsing %v from ConfigMap, error: %v", FSMStr, err)
return nil, err
}
}

err = dsc.load()
return dsc, err
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/deviceshifu/deviceshifubase/deviceshifubase_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ConfigMapData struct {
DriverProperties string `yaml:"driverProperties"`
Instructions string `yaml:"instructions"`
Telemetries string `yaml:"telemetries"`
FSM string `yaml:"fsm"`
} `yaml:"data"`
}

Expand Down Expand Up @@ -76,6 +77,30 @@ func TestNewDeviceShifuConfig(t *testing.T) {
},
}

var mockDeviceFSM = &DeviceShifuFSM{
States: map[string]DeviceShifuState{
"red": {
Actions: map[string]DeviceShifuAction{
"go": {NextState: "green"},
},
Forbid: []string{"caution"},
},
"green": {
Actions: map[string]DeviceShifuAction{
"caution": {NextState: "yellow"},
},
Forbid: []string{"stop"},
},
"yellow": {
Actions: map[string]DeviceShifuAction{
"stop": {NextState: "red"},
},
Forbid: []string{"go"},
},
},
StartingState: "red",
}

mockdsc, err := NewDeviceShifuConfig(MockDeviceConfigFolder)
if err != nil {
t.Errorf(err.Error())
Expand All @@ -97,6 +122,10 @@ func TestNewDeviceShifuConfig(t *testing.T) {
t.Errorf("Telemetries mismatch")
}

eq = reflect.DeepEqual(mockDeviceFSM, mockdsc.FSM)
if !eq {
t.Errorf("FSM mismatch")
}
}

func GenerateConfigMapFromSnippet(fileName string, folder string) error {
Expand All @@ -116,6 +145,7 @@ func GenerateConfigMapFromSnippet(fileName string, folder string) error {
path.Join(MockDeviceConfigFolder, ConfigmapDriverPropertiesStr): cmData.Data.DriverProperties,
path.Join(MockDeviceConfigFolder, ConfigmapInstructionsStr): cmData.Data.Instructions,
path.Join(MockDeviceConfigFolder, ConfigmapTelemetriesStr): cmData.Data.Telemetries,
path.Join(MockDeviceConfigFolder, FSMStr): cmData.Data.FSM,
}

err = os.MkdirAll(MockDeviceConfigFolder, os.ModePerm)
Expand Down
1 change: 1 addition & 0 deletions pkg/k8s/api/v1alpha1/edgedevice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type EdgeDeviceSpec struct {
ProtocolSettings *ProtocolSettings `json:"protocolSettings,omitempty"`
CustomMetadata *map[string]string `json:"customMetadata,omitempty"`

State *string `json:"state,omitempty"`
// TODO: add other fields like disconnectTimemoutInSeconds
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/k8s/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ spec:
sku:
description: Sku specifies the EdgeDevice's SKU.
type: string
state:
type: string
type: object
status:
description: EdgeDeviceStatus defines the observed state of EdgeDevice
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/crd/install/config_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ spec:
sku:
description: Sku specifies the EdgeDevice's SKU.
type: string
state:
type: string
type: object
status:
description: EdgeDeviceStatus defines the observed state of EdgeDevice
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/crd/install/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ spec:
sku:
description: Sku specifies the EdgeDevice's SKU.
type: string
state:
type: string
type: object
status:
description: EdgeDeviceStatus defines the observed state of EdgeDevice
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/crd/install/shifu_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ spec:
sku:
description: Sku specifies the EdgeDevice's SKU.
type: string
state:
type: string
type: object
status:
description: EdgeDeviceStatus defines the observed state of EdgeDevice
Expand Down