Skip to content

Commit

Permalink
Move all applicable to reusable connection
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Dec 22, 2023
1 parent 43794f6 commit d6d97a3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 46 deletions.
12 changes: 10 additions & 2 deletions pkg/collectors/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type baseCollector struct {
isAnnouncer bool
running bool
pollInterval time.Duration
ctx clients.ExecContext
}

func (base *baseCollector) GetPollInterval() time.Duration {
Expand All @@ -54,24 +55,31 @@ 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
}

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,
}
}
12 changes: 5 additions & 7 deletions pkg/collectors/dev_info_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +19,6 @@ import (

type DevInfoCollector struct {
*baseCollector
ctx clients.ExecContext
devInfo *devices.PTPDeviceInfo
quit chan os.Signal
erroredPolls chan PollResult
Expand Down Expand Up @@ -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)
}
Expand All @@ -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),
Expand All @@ -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)
}
4 changes: 1 addition & 3 deletions pkg/collectors/dpll_collector_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ 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"
)

type DPLLFilesystemCollector struct {
*baseCollector
ctx clients.ExecContext
interfaceName string
}

Expand Down Expand Up @@ -81,9 +79,9 @@ func NewDPLLFilesystemCollector(constructor *CollectionConstructor) (Collector,
constructor.PollInterval,
false,
constructor.Callback,
ctx,
),
interfaceName: constructor.PTPInterface,
ctx: ctx,
}
return &collector, nil
}
16 changes: 12 additions & 4 deletions pkg/collectors/dpll_collector_netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

type DPLLNetlinkCollector struct {
*baseCollector
ctx *clients.ContainerCreationExecContext
interfaceName string
clockID int64
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -100,9 +108,9 @@ func NewDPLLNetlinkCollector(constructor *CollectionConstructor) (Collector, err
constructor.PollInterval,
false,
constructor.Callback,
ctx,
),
interfaceName: constructor.PTPInterface,
ctx: ctx,
}

return &collector, nil
Expand Down
6 changes: 2 additions & 4 deletions pkg/collectors/gps_ubx_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,7 +17,6 @@ var (

type GPSCollector struct {
*baseCollector
ctx clients.ExecContext
interfaceName string
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -64,8 +62,8 @@ func NewGPSCollector(constructor *CollectionConstructor) (Collector, error) {
constructor.PollInterval,
false,
constructor.Callback,
ctx,
),
ctx: ctx,
interfaceName: constructor.PTPInterface,
}

Expand Down
1 change: 1 addition & 0 deletions pkg/collectors/log_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func NewLogsCollector(constructor *CollectionConstructor) (Collector, error) {
logPollInterval,
false,
constructor.Callback,
nil,
),
client: constructor.Clientset,
sliceQuit: make(chan os.Signal),
Expand Down
13 changes: 1 addition & 12 deletions pkg/collectors/pmc_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,7 +17,6 @@ const (

type PMCCollector struct {
*baseCollector
ctx clients.ExecContext
}

func (pmc *PMCCollector) poll() error {
Expand Down Expand Up @@ -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)
Expand All @@ -72,8 +61,8 @@ func NewPMCCollector(constructor *CollectionConstructor) (Collector, error) {
constructor.PollInterval,
false,
constructor.Callback,
ctx,
),
ctx: ctx,
}

return &collector, nil
Expand Down
23 changes: 9 additions & 14 deletions pkg/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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),
Expand Down

0 comments on commit d6d97a3

Please sign in to comment.