Skip to content

Commit

Permalink
Add ublx config to plugin
Browse files Browse the repository at this point in the history
Changes to plugin structure also required to allow sharing data between
OnPTPConfigChange and PopulateHwConfig, which is required to write
the status to the nodeptpdevice
  • Loading branch information
josephdrichard committed Jun 6, 2024
1 parent bfda192 commit 2c21933
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 33 deletions.
20 changes: 13 additions & 7 deletions addons/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import (
"github.com/k8snetworkplumbingwg/ptp-operator/pkg/daemon/plugin"
)

func onPTPConfigChangeGeneric(*ptpv1.PtpProfile) error {
func onPTPConfigChangeGeneric(*interface{}, *ptpv1.PtpProfile) error {
glog.Infof("calling onPTPConfigChangeGeneric")
return nil
}

func PopulateHwConfigGeneric(hwconfigs *[]ptpv1.HwConfig) error {
func PopulateHwConfigGeneric(*interface{}, *[]ptpv1.HwConfig) error {
return nil
}

func Reference(name string) *plugin.Plugin {
func AfterRunPTPCommandGeneric(*interface{}, *ptpv1.PtpProfile, string) error {
return nil
}

func Reference(name string) (*plugin.Plugin, *interface{}) {
if name != "reference" {
glog.Errorf("Plugin must be initialized as 'reference'")
return nil
return nil, nil
}
return &plugin.Plugin{Name: "reference",
OnPTPConfigChange: onPTPConfigChangeGeneric,
PopulateHwConfig: PopulateHwConfigGeneric,
_plugin := plugin.Plugin{Name: "reference",
OnPTPConfigChange: onPTPConfigChangeGeneric,
PopulateHwConfig: PopulateHwConfigGeneric,
AfterRunPTPCommand: AfterRunPTPCommandGeneric,
}
return &_plugin, nil
}
88 changes: 80 additions & 8 deletions addons/intel/e810.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ package intel

import (
"encoding/json"
"fmt"
"github.com/golang/glog"
ptpv1 "github.com/k8snetworkplumbingwg/ptp-operator/api/v1"
"github.com/k8snetworkplumbingwg/ptp-operator/pkg/daemon/plugin"
"os/exec"
"strings"
)

type E810Opts struct {
EnableDefaultConfig bool `json:"enableDefaultConfig"`
EnableDefaultConfig bool `json:"enableDefaultConfig"`
UblxCmds []E810UblxCmds `json:"ublxCmds"`
}

type E810UblxCmds struct {
ReportOutput bool `json:"reportOutput"`
Args []string `json:"args"`
}

type E810PluginData struct {
hwplugins *[]string
}

// Sourced from https://github.com/RHsyseng/oot-ice/blob/main/ptp-config.sh
Expand All @@ -31,7 +43,7 @@ done
echo "Disabled all SMA and U.FL Connections"
`

func OnPTPConfigChangeE810(nodeProfile *ptpv1.PtpProfile) error {
func OnPTPConfigChangeE810(data *interface{}, nodeProfile *ptpv1.PtpProfile) error {
glog.Info("calling onPTPConfigChange for e810 plugin")
var e810Opts E810Opts
var err error
Expand All @@ -56,21 +68,81 @@ func OnPTPConfigChangeE810(nodeProfile *ptpv1.PtpProfile) error {
return nil
}

func PopulateHwConfigE810(hwconfigs *[]ptpv1.HwConfig) error {
func AfterRunPTPCommandE810(data *interface{}, nodeProfile *ptpv1.PtpProfile, command string) error {
glog.Info("calling AfterRunPTPCommandE810 for e810 plugin")
var e810Opts E810Opts
var err error
var optsByteArray []byte
var stdout []byte

e810Opts.EnableDefaultConfig = false

for name, opts := range (*nodeProfile).Plugins {
if name == "e810" {
optsByteArray, _ = json.Marshal(opts)
err = json.Unmarshal(optsByteArray, &e810Opts)
if err != nil {
glog.Error("e810 failed to unmarshal opts: " + err.Error())
}
if command == "gpspipe" {
glog.Infof("AfterRunPTPCommandE810 doing ublx config for command: %s", command)
for _, ublxOpt := range e810Opts.UblxCmds {
ublxArgs := ublxOpt.Args
glog.Infof("Running /usr/bin/ubxtool with args %s", strings.Join(ublxArgs, ", "))
stdout, err = exec.Command("/usr/local/bin/ubxtool", ublxArgs...).CombinedOutput()
//stdout, err = exec.Command("/usr/local/bin/ubxtool", "-p", "STATUS").CombinedOutput()
_data := *data
if data != nil && ublxOpt.ReportOutput {
glog.Infof("Saving status to hwconfig: %s", string(stdout))
var pluginData *E810PluginData = _data.(*E810PluginData)
_pluginData := *pluginData
statusString := fmt.Sprintf("ublx data: %s", string(stdout))
*_pluginData.hwplugins = append(*_pluginData.hwplugins, statusString)
} else {
glog.Infof("Not saving status to hwconfig: %s", string(stdout))
}
}
} else {
glog.Infof("AfterRunPTPCommandE810 doing nothing for command: %s", command)
}
}
}
return nil
}

func PopulateHwConfigE810(data *interface{}, hwconfigs *[]ptpv1.HwConfig) error {
//hwConfig := ptpv1.HwConfig{}
//hwConfig.DeviceID = "e810"
//*hwconfigs = append(*hwconfigs, hwConfig)
if data != nil {
_data := *data
var pluginData *E810PluginData = _data.(*E810PluginData)
_pluginData := *pluginData
if _pluginData.hwplugins != nil {
for _, _hwconfig := range *_pluginData.hwplugins {
hwConfig := ptpv1.HwConfig{}
hwConfig.DeviceID = "e810"
hwConfig.Status = _hwconfig
*hwconfigs = append(*hwconfigs, hwConfig)
}
}
}
return nil
}

func E810(name string) *plugin.Plugin {
func E810(name string) (*plugin.Plugin, *interface{}) {
if name != "e810" {
glog.Errorf("Plugin must be initialized as 'e810'")
return nil
return nil, nil
}
glog.Infof("registering e810 plugin")
return &plugin.Plugin{Name: "e810",
OnPTPConfigChange: OnPTPConfigChangeE810,
PopulateHwConfig: PopulateHwConfigE810,
hwplugins := []string{}
pluginData := E810PluginData{hwplugins: &hwplugins}
_plugin := plugin.Plugin{Name: "e810",
OnPTPConfigChange: OnPTPConfigChangeE810,
AfterRunPTPCommand: AfterRunPTPCommandE810,
PopulateHwConfig: PopulateHwConfigE810,
}
var iface interface{} = &pluginData
return &_plugin, &iface
}
9 changes: 6 additions & 3 deletions pkg/daemon/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type ptpProcess struct {
logFilterRegex string
cmd *exec.Cmd
depProcess []process // this could gpsd and other process which needs to be stopped if the parent process is stopped
nodeProfile *ptpv1.PtpProfile
}

func (p *ptpProcess) Stopped() bool {
Expand Down Expand Up @@ -205,15 +206,18 @@ func (dn *Daemon) applyNodePTPProfiles() error {
if d != nil {
time.Sleep(3 * time.Second)
go d.cmdRun(false)
time.Sleep(2 * time.Second)
time.Sleep(3 * time.Second)
dn.pluginManager.AfterRunPTPCommand(p.nodeProfile, d.Name())
d.monitorEvent(clockType, p.configName, p.exitCh, dn.processManager.eventChannel)
}
}
}
time.Sleep(1 * time.Second)
go p.cmdRun()
dn.pluginManager.AfterRunPTPCommand(p.nodeProfile, p.name)
}
}
dn.pluginManager.PopulateHwConfig(dn.hwconfigs)
return nil
}

Expand Down Expand Up @@ -362,6 +366,7 @@ func (dn *Daemon) applyNodePtpProfile(runID int, nodeProfile *ptpv1.PtpProfile)
logFilterRegex: getLogFilterRegex(nodeProfile),
cmd: cmd,
depProcess: []process{},
nodeProfile: nodeProfile,
}
//TODO HARDWARE PLUGIN for e810
if pProcess == ts2phcProcessName { //& if the x plugin is enabled
Expand Down Expand Up @@ -399,8 +404,6 @@ func (dn *Daemon) applyNodePtpProfile(runID int, nodeProfile *ptpv1.PtpProfile)
dn.processManager.process = append(dn.processManager.process, &dprocess)
}

dn.pluginManager.PopulateHwConfig(dn.hwconfigs)

return nil
}

Expand Down
26 changes: 18 additions & 8 deletions pkg/daemon/daemon/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type PluginManager struct {
plugins map[string]*plugin.Plugin
data map[string]*interface{}
}

func HelloWorld() {
Expand All @@ -18,35 +19,44 @@ func HelloWorld() {

func registerPlugins(plugins []string) PluginManager {
glog.Infof("Begin plugin registration...")
manager := PluginManager{plugins: make(map[string]*plugin.Plugin)}
manager := PluginManager{plugins: make(map[string]*plugin.Plugin),
data: make(map[string]*interface{}),
}
for _, name := range plugins {
currentPlugin := registerPlugin(name)
currentPlugin, currentData := registerPlugin(name)
if currentPlugin != nil {
manager.plugins[name] = currentPlugin
manager.data[name] = currentData
}
}
return manager
}

func registerPlugin(name string) *plugin.Plugin {
func registerPlugin(name string) (*plugin.Plugin, *interface{}) {
glog.Infof("Trying to register plugin: " + name)
for mName, mConstructor := range mapping.PluginMapping {
if mName == name {
return mConstructor(name)
}
}
glog.Errorf("Plugin not found: " + name)
return nil
return nil, nil
}

func (pm *PluginManager) OnPTPConfigChange(nodeProfile *ptpv1.PtpProfile) {
for _, pluginObject := range pm.plugins {
pluginObject.OnPTPConfigChange(nodeProfile)
for pluginName, pluginObject := range pm.plugins {
pluginObject.OnPTPConfigChange(pm.data[pluginName], nodeProfile)
}
}

func (pm *PluginManager) AfterRunPTPCommand(nodeProfile *ptpv1.PtpProfile, command string) {
for pluginName, pluginObject := range pm.plugins {
pluginObject.AfterRunPTPCommand(pm.data[pluginName], nodeProfile, command)
}
}

func (pm *PluginManager) PopulateHwConfig(hwconfigs *[]ptpv1.HwConfig) {
for _, pluginObject := range pm.plugins {
pluginObject.PopulateHwConfig(hwconfigs)
for pluginName, pluginObject := range pm.plugins {
pluginObject.PopulateHwConfig(pm.data[pluginName], hwconfigs)
}
}
16 changes: 9 additions & 7 deletions pkg/daemon/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
ptpv1 "github.com/k8snetworkplumbingwg/ptp-operator/api/v1"
)

type New func(string) *Plugin
type OnPTPConfigChange func(*ptpv1.PtpProfile) error
type PopulateHwConfig func(*[]ptpv1.HwConfig) error
type New func(string) (*Plugin, *interface{})
type OnPTPConfigChange func(*interface{}, *ptpv1.PtpProfile) error
type PopulateHwConfig func(*interface{}, *[]ptpv1.HwConfig) error
type AfterRunPTPCommand func(*interface{}, *ptpv1.PtpProfile, string) error

type Plugin struct {
Name string
Options interface{}
OnPTPConfigChange OnPTPConfigChange
PopulateHwConfig PopulateHwConfig
Name string
Options interface{}
OnPTPConfigChange OnPTPConfigChange
AfterRunPTPCommand AfterRunPTPCommand
PopulateHwConfig PopulateHwConfig
}

0 comments on commit 2c21933

Please sign in to comment.