diff --git a/pkg/collectors/collector.go b/pkg/collectors/collector.go index d2c5b1bd..db323415 100644 --- a/pkg/collectors/collector.go +++ b/pkg/collectors/collector.go @@ -43,6 +43,7 @@ type baseCollector struct { isAnnouncer bool running bool pollInterval time.Duration + ctx clients.ExecContext } func (base *baseCollector) GetPollInterval() time.Duration { @@ -54,12 +55,17 @@ func (base *baseCollector) IsAnnouncer() bool { } func (base *baseCollector) Start() error { - base.running = true return nil } func (base *baseCollector) CleanUp() error { - base.running = false + base.running = true + if base.ctx != nil { + c, ok := base.ctx.(*clients.ReusedConnectionContext) + if ok { + c.CloseShell() + } + } return nil } @@ -67,11 +73,13 @@ func newBaseCollector( pollInterval int, isAnnouncer bool, callback callbacks.Callback, + ctx clients.ExecContext, ) *baseCollector { return &baseCollector{ callback: callback, isAnnouncer: isAnnouncer, running: false, pollInterval: time.Duration(pollInterval) * time.Second, + ctx: ctx, } } diff --git a/pkg/collectors/dev_info_collector.go b/pkg/collectors/dev_info_collector.go index 641d6d91..a364f258 100644 --- a/pkg/collectors/dev_info_collector.go +++ b/pkg/collectors/dev_info_collector.go @@ -11,7 +11,6 @@ import ( log "github.com/sirupsen/logrus" - "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/clients" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/contexts" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/devices" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils" @@ -20,7 +19,6 @@ import ( type DevInfoCollector struct { *baseCollector - ctx clients.ExecContext devInfo *devices.PTPDeviceInfo quit chan os.Signal erroredPolls chan PollResult @@ -151,7 +149,7 @@ func verify(ptpDevInfo *devices.PTPDeviceInfo, constructor *CollectionConstructo // Returns a new DevInfoCollector from the CollectionConstuctor Factory func NewDevInfoCollector(constructor *CollectionConstructor) (Collector, error) { // Build DPPInfoFetcher ahead of time call to GetPTPDeviceInfo will build the other - ctx, err := contexts.GetPTPDaemonContext(constructor.Clientset) + ctx, err := contexts.GetPTPDaemonContextReusedConnection(constructor.Clientset) if err != nil { return &DevInfoCollector{}, fmt.Errorf("failed to create DevInfoCollector: %w", err) } @@ -175,8 +173,8 @@ func NewDevInfoCollector(constructor *CollectionConstructor) (Collector, error) constructor.DevInfoAnnouceInterval, true, constructor.Callback, + ctx, ), - ctx: ctx, interfaceName: constructor.PTPInterface, devInfo: &ptpDevInfo, quit: make(chan os.Signal), @@ -187,6 +185,6 @@ func NewDevInfoCollector(constructor *CollectionConstructor) (Collector, error) return &collector, nil } -// func init() { -// RegisterCollector(DevInfoCollectorName, NewDevInfoCollector, required) -// } +func init() { + RegisterCollector(DevInfoCollectorName, NewDevInfoCollector, required) +} diff --git a/pkg/collectors/dpll_collector_fs.go b/pkg/collectors/dpll_collector_fs.go index c36d83ec..6c834829 100644 --- a/pkg/collectors/dpll_collector_fs.go +++ b/pkg/collectors/dpll_collector_fs.go @@ -5,7 +5,6 @@ package collectors import ( "fmt" - "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/clients" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/contexts" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/devices" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils" @@ -13,7 +12,6 @@ import ( type DPLLFilesystemCollector struct { *baseCollector - ctx clients.ExecContext interfaceName string } @@ -81,9 +79,9 @@ func NewDPLLFilesystemCollector(constructor *CollectionConstructor) (Collector, constructor.PollInterval, false, constructor.Callback, + ctx, ), interfaceName: constructor.PTPInterface, - ctx: ctx, } return &collector, nil } diff --git a/pkg/collectors/dpll_collector_netlink.go b/pkg/collectors/dpll_collector_netlink.go index d53b9c92..d1cfb4e7 100644 --- a/pkg/collectors/dpll_collector_netlink.go +++ b/pkg/collectors/dpll_collector_netlink.go @@ -15,7 +15,6 @@ import ( type DPLLNetlinkCollector struct { *baseCollector - ctx *clients.ContainerCreationExecContext interfaceName string clockID int64 } @@ -28,7 +27,12 @@ const ( // Start sets up the collector so it is ready to be polled func (dpll *DPLLNetlinkCollector) Start() error { dpll.running = true - err := dpll.ctx.CreatePodAndWait() + ctx, ok := dpll.ctx.(*clients.ContainerCreationExecContext) + if !ok { + return fmt.Errorf("Incorrect Context type") + } + + err := ctx.CreatePodAndWait() if err != nil { return fmt.Errorf("dpll netlink collector failed to start pod: %w", err) } @@ -81,7 +85,11 @@ func (dpll *DPLLNetlinkCollector) Poll(resultsChan chan PollResult, wg *utils.Wa // CleanUp stops a running collector func (dpll *DPLLNetlinkCollector) CleanUp() error { dpll.running = false - err := dpll.ctx.DeletePodAndWait() + ctx, ok := dpll.ctx.(*clients.ContainerCreationExecContext) + if !ok { + return fmt.Errorf("Incorrect Context type") + } + err := ctx.DeletePodAndWait() if err != nil { return fmt.Errorf("dpll netlink collector failed to clean up: %w", err) } @@ -100,9 +108,9 @@ func NewDPLLNetlinkCollector(constructor *CollectionConstructor) (Collector, err constructor.PollInterval, false, constructor.Callback, + ctx, ), interfaceName: constructor.PTPInterface, - ctx: ctx, } return &collector, nil diff --git a/pkg/collectors/gps_ubx_collector.go b/pkg/collectors/gps_ubx_collector.go index 8086dbc1..42706aa0 100644 --- a/pkg/collectors/gps_ubx_collector.go +++ b/pkg/collectors/gps_ubx_collector.go @@ -5,7 +5,6 @@ package collectors //nolint:dupl // new collector import ( "fmt" - "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/clients" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/contexts" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/devices" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils" @@ -18,7 +17,6 @@ var ( type GPSCollector struct { *baseCollector - ctx clients.ExecContext interfaceName string } @@ -54,7 +52,7 @@ func (gps *GPSCollector) Poll(resultsChan chan PollResult, wg *utils.WaitGroupCo // Returns a new GPSCollector based on values in the CollectionConstructor func NewGPSCollector(constructor *CollectionConstructor) (Collector, error) { - ctx, err := contexts.GetPTPDaemonContext(constructor.Clientset) + ctx, err := contexts.GetPTPDaemonContextReusedConnection(constructor.Clientset) if err != nil { return &GPSCollector{}, fmt.Errorf("failed to create DPLLCollector: %w", err) } @@ -64,8 +62,8 @@ func NewGPSCollector(constructor *CollectionConstructor) (Collector, error) { constructor.PollInterval, false, constructor.Callback, + ctx, ), - ctx: ctx, interfaceName: constructor.PTPInterface, } diff --git a/pkg/collectors/log_follower.go b/pkg/collectors/log_follower.go index 16db7a9b..27cf43b2 100644 --- a/pkg/collectors/log_follower.go +++ b/pkg/collectors/log_follower.go @@ -268,6 +268,7 @@ func NewLogsCollector(constructor *CollectionConstructor) (Collector, error) { logPollInterval, false, constructor.Callback, + nil, ), client: constructor.Clientset, sliceQuit: make(chan os.Signal), diff --git a/pkg/collectors/pmc_collector.go b/pkg/collectors/pmc_collector.go index 5c7ced7c..fd459749 100644 --- a/pkg/collectors/pmc_collector.go +++ b/pkg/collectors/pmc_collector.go @@ -5,7 +5,6 @@ package collectors //nolint:dupl // new collector import ( "fmt" - "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/clients" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/contexts" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors/devices" "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils" @@ -18,7 +17,6 @@ const ( type PMCCollector struct { *baseCollector - ctx clients.ExecContext } func (pmc *PMCCollector) poll() error { @@ -51,15 +49,6 @@ func (pmc *PMCCollector) Poll(resultsChan chan PollResult, wg *utils.WaitGroupCo } } -func (pmc *PMCCollector) CleanUp() error { - pmc.baseCollector.CleanUp() - c, ok := pmc.ctx.(*clients.ReusedConnectionContext) - if ok { - c.CloseShell() - } - return nil -} - // Returns a new PMCCollector based on values in the CollectionConstructor func NewPMCCollector(constructor *CollectionConstructor) (Collector, error) { ctx, err := contexts.GetPTPDaemonContextReusedConnection(constructor.Clientset) @@ -72,8 +61,8 @@ func NewPMCCollector(constructor *CollectionConstructor) (Collector, error) { constructor.PollInterval, false, constructor.Callback, + ctx, ), - ctx: ctx, } return &collector, nil diff --git a/pkg/verify/verify.go b/pkg/verify/verify.go index 4d61d7dd..b8dd7475 100644 --- a/pkg/verify/verify.go +++ b/pkg/verify/verify.go @@ -25,11 +25,8 @@ const ( //nolint:ireturn // this needs to be an interface func getDevInfoValidations( - clientset *clients.Clientset, - interfaceName string, + ctx clients.ExecContext, interfaceName string, ) []validations.Validation { - ctx, err := contexts.GetPTPDaemonContext(clientset) - utils.IfErrorExitOrPanic(err) devInfo, err := devices.GetPTPDeviceInfo(interfaceName, ctx) utils.IfErrorExitOrPanic(err) devDetails := validations.NewDeviceDetails(&devInfo) @@ -39,10 +36,8 @@ func getDevInfoValidations( } func getGPSVersionValidations( - clientset *clients.Clientset, + ctx clients.ExecContext, ) []validations.Validation { - ctx, err := contexts.GetPTPDaemonContext(clientset) - utils.IfErrorExitOrPanic(err) gnssVersions, err := devices.GetGPSVersions(ctx) utils.IfErrorExitOrPanic(err) return []validations.Validation{ @@ -55,14 +50,12 @@ func getGPSVersionValidations( } func getGPSStatusValidation( - clientset *clients.Clientset, + ctx clients.ExecContext, ) []validations.Validation { - ctx, err := contexts.GetPTPDaemonContext(clientset) - utils.IfErrorExitOrPanic(err) - // If we need to do this for more validations then consider a generic var antCheck *validations.GNSSAntStatus var gpsDetails devices.GPSDetails + var err error for i := 0; i < antPowerRetries; i++ { gpsDetails, err = devices.GetGPSNav(ctx) if err != nil { @@ -84,9 +77,11 @@ func getValidations(interfaceName, kubeConfig string) []validations.Validation { checks := make([]validations.Validation, 0) clientset, err := clients.GetClientset(kubeConfig) utils.IfErrorExitOrPanic(err) - checks = append(checks, getDevInfoValidations(clientset, interfaceName)...) - checks = append(checks, getGPSVersionValidations(clientset)...) - checks = append(checks, getGPSStatusValidation(clientset)...) + ctx, err := contexts.GetPTPDaemonContext(clientset) + utils.IfErrorExitOrPanic(err) + checks = append(checks, getDevInfoValidations(ctx, interfaceName)...) + checks = append(checks, getGPSVersionValidations(ctx)...) + checks = append(checks, getGPSStatusValidation(ctx)...) checks = append( checks, validations.NewIsGrandMaster(clientset),