Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package processmanager
import (
"context"
"errors"
"time"
"ascend-common/common-utils/hwlog"
"nodeD/pkg/common"
"nodeD/pkg/control/dpccontrol"
"nodeD/pkg/control/dtfscontrol"
"nodeD/pkg/control/faultcontrol"
"nodeD/pkg/kubeclient"
"nodeD/pkg/monitoring/config"
"nodeD/pkg/monitoring/dpcmonitor"
"nodeD/pkg/monitoring/dtfsmonitor"
"nodeD/pkg/monitoring/ipmimonitor"
"nodeD/pkg/reporter/cmreporter"
"nodeD/pkg/reporter/publicfault"
)
var (
processPluginMap map[string]Plugin = nil
)
const (
processNum = 4
retryTime = 3
)
type Plugin struct {
monitor common.PluginMonitor
reporters []common.PluginReporter
controls []common.PluginControl
}
func InitPlugin(ctx context.Context) error {
if kubeclient.GetK8sClient() == nil {
return errors.New("k8s client is nil")
}
ipmiEventMonitor := ipmimonitor.NewIpmiEventMonitor()
configmapEventMonitor := config.NewFaultConfigurator(kubeclient.GetK8sClient())
dpcEventMonitor := dpcmonitor.NewDpcEventMonitor(ctx)
dtfsEventMonitor := dtfsmonitor.NewDtfsEventMonitor(ctx)
nodeController := faultcontrol.NewNodeController()
dpcController := dpccontrol.NewDpcController()
dtfsController := dtfscontrol.NewDtfsController()
configMapReporter := cmreporter.NewConfigMapReporter(kubeclient.GetK8sClient())
pfReporter := publicfault.NewGrpcReporter()
processPluginMap = make(map[string]Plugin, processNum)
processPluginMap[common.IpmiProcess] = Plugin{
monitor: ipmiEventMonitor,
controls: []common.PluginControl{nodeController},
reporters: []common.PluginReporter{configMapReporter},
}
processPluginMap[common.ConfigProcess] = Plugin{
monitor: configmapEventMonitor,
controls: []common.PluginControl{nodeController},
}
processPluginMap[common.DpcProcess] = Plugin{
monitor: dpcEventMonitor,
controls: []common.PluginControl{dpcController},
reporters: []common.PluginReporter{pfReporter},
}
processPluginMap[common.DtfsProcess] = Plugin{
monitor: dtfsEventMonitor,
controls: []common.PluginControl{dtfsController},
reporters: []common.PluginReporter{pfReporter},
}
if err := startAllMonitor(); err != nil {
return err
}
return nil
}
func GetMonitorPlugins(processType string) common.PluginMonitor {
if pluginMonitor, ok := processPluginMap[processType]; !ok {
return nil
} else {
return pluginMonitor.monitor
}
}
func GetControlPlugins(processType string) []common.PluginControl {
if pluginControl, ok := processPluginMap[processType]; !ok {
return []common.PluginControl{}
} else {
return pluginControl.controls
}
}
func GetReporterPlugins(processType string) []common.PluginReporter {
if pluginReporter, ok := processPluginMap[processType]; !ok {
return []common.PluginReporter{}
} else {
return pluginReporter.reporters
}
}
func GetAllProcessType() []string {
return []string{common.IpmiProcess, common.ConfigProcess, common.DpcProcess, common.DtfsProcess}
}
func GetAllLoopProcessType() []string {
return []string{common.IpmiProcess}
}
func startAllMonitor() error {
errNum := 0
for _, processPlugin := range processPluginMap {
for i := 0; i < retryTime; i++ {
if err := processPlugin.monitor.Init(); err == nil {
hwlog.RunLog.Infof("init monitor[%s] success", processPlugin.monitor.Name())
go processPlugin.monitor.Monitoring()
break
} else if i+1 < retryTime {
hwlog.RunLog.Errorf("init monitor[%s] failed, error: %v, retry count: %d",
processPlugin.monitor.Name(), err, i+1)
time.Sleep(time.Second)
} else {
errNum++
}
}
}
if errNum == len(processPluginMap) {
return errors.New("all monitor init failed")
}
return nil
}