diff --git a/addons/generic/generic.go b/addons/generic/generic.go index b9e61e4e..14a46464 100644 --- a/addons/generic/generic.go +++ b/addons/generic/generic.go @@ -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 } diff --git a/addons/intel/e810.go b/addons/intel/e810.go index 73f2268c..23260b0f 100644 --- a/addons/intel/e810.go +++ b/addons/intel/e810.go @@ -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 @@ -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 @@ -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 } diff --git a/pkg/daemon/daemon/daemon.go b/pkg/daemon/daemon/daemon.go index e9eb734f..ecb4cb68 100644 --- a/pkg/daemon/daemon/daemon.go +++ b/pkg/daemon/daemon/daemon.go @@ -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 { @@ -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 } @@ -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 @@ -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 } diff --git a/pkg/daemon/daemon/plugin.go b/pkg/daemon/daemon/plugin.go index c7cc3194..5bcca210 100644 --- a/pkg/daemon/daemon/plugin.go +++ b/pkg/daemon/daemon/plugin.go @@ -9,6 +9,7 @@ import ( type PluginManager struct { plugins map[string]*plugin.Plugin + data map[string]*interface{} } func HelloWorld() { @@ -18,17 +19,20 @@ 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 { @@ -36,17 +40,23 @@ func registerPlugin(name string) *plugin.Plugin { } } 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) } } diff --git a/pkg/daemon/plugin/plugin.go b/pkg/daemon/plugin/plugin.go index 50ab3149..e4cdf969 100644 --- a/pkg/daemon/plugin/plugin.go +++ b/pkg/daemon/plugin/plugin.go @@ -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 }