diff --git a/pkg/clients/exec_command.go b/pkg/clients/exec_command.go index 3cb023f2..8532cc13 100644 --- a/pkg/clients/exec_command.go +++ b/pkg/clients/exec_command.go @@ -10,15 +10,20 @@ import ( log "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/remotecommand" "k8s.io/kubectl/pkg/scheme" ) +type ExecContext interface { + ExecCommand([]string) (string, string, error) + ExecCommandStdIn([]string, bytes.Buffer) (string, string, error) +} + var NewSPDYExecutor = remotecommand.NewSPDYExecutor -// ContainerContext encapsulates the context in which a command is run; the namespace, pod, and container. -type ContainerContext struct { +// ContainerExecContext encapsulates the context in which a command is run; the namespace, pod, and container. +type ContainerExecContext struct { clientset *Clientset namespace string podName string @@ -26,7 +31,7 @@ type ContainerContext struct { podNamePrefix string } -func (c *ContainerContext) Refresh() error { +func (c *ContainerExecContext) refresh() error { newPodname, err := c.clientset.FindPodNameFromPrefix(c.namespace, c.podNamePrefix) if err != nil { return err @@ -38,35 +43,35 @@ func (c *ContainerContext) Refresh() error { func NewContainerContext( clientset *Clientset, namespace, podNamePrefix, containerName string, -) (ContainerContext, error) { +) (*ContainerExecContext, error) { podName, err := clientset.FindPodNameFromPrefix(namespace, podNamePrefix) if err != nil { - return ContainerContext{}, err + return &ContainerExecContext{}, err } - ctx := ContainerContext{ + ctx := ContainerExecContext{ namespace: namespace, podName: podName, containerName: containerName, podNamePrefix: podNamePrefix, clientset: clientset, } - return ctx, nil + return &ctx, nil } -func (c *ContainerContext) GetNamespace() string { +func (c *ContainerExecContext) GetNamespace() string { return c.namespace } -func (c *ContainerContext) GetPodName() string { +func (c *ContainerExecContext) GetPodName() string { return c.podName } -func (c *ContainerContext) GetContainerName() string { +func (c *ContainerExecContext) GetContainerName() string { return c.containerName } //nolint:lll,funlen // allow slightly long function definition and function length -func (c *ContainerContext) execCommand(command []string, buffInPtr *bytes.Buffer) (stdout, stderr string, err error) { +func (c *ContainerExecContext) execCommand(command []string, buffInPtr *bytes.Buffer) (stdout, stderr string, err error) { commandStr := command var buffOut bytes.Buffer var buffErr bytes.Buffer @@ -118,9 +123,9 @@ func (c *ContainerContext) execCommand(command []string, buffInPtr *bytes.Buffer err = exec.StreamWithContext(context.TODO(), streamOptions) stdout, stderr = buffOut.String(), buffErr.String() if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { log.Debugf("Pod %s was not found, likely restarted so refreshing context", c.GetPodName()) - refreshErr := c.Refresh() + refreshErr := c.refresh() if refreshErr != nil { log.Debug("Failed to refresh container context", refreshErr) } @@ -142,11 +147,11 @@ func (c *ContainerContext) execCommand(command []string, buffInPtr *bytes.Buffer // ExecCommand runs command in a container and returns output buffers // //nolint:lll,funlen // allow slightly long function definition and allow a slightly long function -func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stderr string, err error) { +func (c *ContainerExecContext) ExecCommand(command []string) (stdout, stderr string, err error) { return c.execCommand(command, nil) } //nolint:lll // allow slightly long function definition -func (c *ContainerContext) ExecCommandContainerStdIn(command []string, buffIn bytes.Buffer) (stdout, stderr string, err error) { +func (c *ContainerExecContext) ExecCommandStdIn(command []string, buffIn bytes.Buffer) (stdout, stderr string, err error) { return c.execCommand(command, &buffIn) } diff --git a/pkg/clients/exec_command_test.go b/pkg/clients/exec_command_test.go index 1c409e55..4eb012ce 100644 --- a/pkg/clients/exec_command_test.go +++ b/pkg/clients/exec_command_test.go @@ -65,7 +65,6 @@ var _ = Describe("NewContainerContext", func() { Expect(ctx.GetPodName()).To(Equal("TestPod-8292")) }) }) - }) var _ = Describe("ExecCommandContainer", func() { @@ -84,7 +83,7 @@ var _ = Describe("ExecCommandContainer", func() { clients.NewSPDYExecutor = testutils.NewFakeNewSPDYExecutor(responder, nil) ctx, _ := clients.NewContainerContext(clientset, "TestNamespace", "Test", "TestContainer") cmd := []string{"my", "test", "command"} - stdout, stderr, err := ctx.ExecCommandContainer(cmd) + stdout, stderr, err := ctx.ExecCommand(cmd) Expect(err).NotTo(HaveOccurred()) Expect(stdout).To(Equal(expectedStdOut)) Expect(stderr).To(Equal(expectedStdErr)) @@ -103,7 +102,7 @@ var _ = Describe("ExecCommandContainer", func() { clients.NewSPDYExecutor = testutils.NewFakeNewSPDYExecutor(responder, expectedErr) ctx, _ := clients.NewContainerContext(clientset, "TestNamespace", "Test", "TestContainer") cmd := []string{"my", "test", "command"} - stdout, stderr, err := ctx.ExecCommandContainer(cmd) + stdout, stderr, err := ctx.ExecCommand(cmd) Expect(err).To(HaveOccurred()) Expect(expectedErr.Error()).To(ContainSubstring(expectedErr.Error())) Expect(stdout).To(Equal(expectedStdOut)) @@ -122,7 +121,7 @@ var _ = Describe("ExecCommandContainer", func() { clients.NewSPDYExecutor = testutils.NewFakeNewSPDYExecutor(responder, nil) ctx, _ := clients.NewContainerContext(clientset, "TestNamespace", "Test", "TestContainer") cmd := []string{"my", "test", "command"} - stdout, stderr, err := ctx.ExecCommandContainer(cmd) + stdout, stderr, err := ctx.ExecCommand(cmd) Expect(err).To(HaveOccurred()) Expect(expectedErr.Error()).To(ContainSubstring(expectedErr.Error())) Expect(stdout).To(Equal(expectedStdOut)) diff --git a/pkg/collectors/contexts/contexts.go b/pkg/collectors/contexts/contexts.go index c6adc61d..3cd4873f 100644 --- a/pkg/collectors/contexts/contexts.go +++ b/pkg/collectors/contexts/contexts.go @@ -15,7 +15,7 @@ const ( GPSContainer = "gpsd" ) -func GetPTPDaemonContext(clientset *clients.Clientset) (clients.ContainerContext, error) { +func GetPTPDaemonContext(clientset *clients.Clientset) (clients.ExecContext, error) { ctx, err := clients.NewContainerContext(clientset, PTPNamespace, PTPPodNamePrefix, PTPContainer) if err != nil { return ctx, fmt.Errorf("could not create container context %w", err) diff --git a/pkg/collectors/dev_info_collector.go b/pkg/collectors/dev_info_collector.go index 17f07ef8..f3210d63 100644 --- a/pkg/collectors/dev_info_collector.go +++ b/pkg/collectors/dev_info_collector.go @@ -20,7 +20,7 @@ import ( type DevInfoCollector struct { *baseCollector - ctx clients.ContainerContext + ctx clients.ExecContext devInfo *devices.PTPDeviceInfo quit chan os.Signal erroredPolls chan PollResult diff --git a/pkg/collectors/devices/device_info.go b/pkg/collectors/devices/device_info.go index 80d504a2..9ffbf96c 100644 --- a/pkg/collectors/devices/device_info.go +++ b/pkg/collectors/devices/device_info.go @@ -148,7 +148,7 @@ func BuildPTPDeviceInfo(interfaceName string) error { //nolint:dupl // Further d } // GetPTPDeviceInfo returns the PTPDeviceInfo for an interface -func GetPTPDeviceInfo(interfaceName string, ctx clients.ContainerContext) (PTPDeviceInfo, error) { +func GetPTPDeviceInfo(interfaceName string, ctx clients.ExecContext) (PTPDeviceInfo, error) { devInfo := PTPDeviceInfo{} // Find the dev for the GNSS for this interface fetcherInst, fetchedInstanceOk := devFetcher[interfaceName] diff --git a/pkg/collectors/devices/dpll.go b/pkg/collectors/devices/dpll.go index 7e122485..f855efcd 100644 --- a/pkg/collectors/devices/dpll.go +++ b/pkg/collectors/devices/dpll.go @@ -91,7 +91,7 @@ func BuildDPLLInfoFetcher(interfaceName string) error { //nolint:dupl // Further } // GetDevDPLLInfo returns the device DPLL info for an interface. -func GetDevDPLLInfo(ctx clients.ContainerContext, interfaceName string) (DevDPLLInfo, error) { +func GetDevDPLLInfo(ctx clients.ExecContext, interfaceName string) (DevDPLLInfo, error) { dpllInfo := DevDPLLInfo{} fetcherInst, fetchedInstanceOk := dpllFetcher[interfaceName] if !fetchedInstanceOk { @@ -112,7 +112,7 @@ func GetDevDPLLInfo(ctx clients.ContainerContext, interfaceName string) (DevDPLL return dpllInfo, nil } -func IsDPLLFileSystemPresent(ctx clients.ContainerContext, interfaceName string) (bool, error) { +func IsDPLLFileSystemPresent(ctx clients.ExecContext, interfaceName string) (bool, error) { fetcherInst, err := fetcher.FetcherFactory( []*clients.Cmd{}, []fetcher.AddCommandArgs{ diff --git a/pkg/collectors/devices/gps_ubx.go b/pkg/collectors/devices/gps_ubx.go index fba78a52..6e47d247 100644 --- a/pkg/collectors/devices/gps_ubx.go +++ b/pkg/collectors/devices/gps_ubx.go @@ -272,7 +272,7 @@ func processUBX(result map[string]string) (map[string]any, error) { //nolint:fun } // GetGPSNav returns GPSNav of the host -func GetGPSNav(ctx clients.ContainerContext) (GPSDetails, error) { +func GetGPSNav(ctx clients.ExecContext) (GPSDetails, error) { gpsNav := GPSDetails{} err := gpsFetcher.Fetch(ctx, &gpsNav) if err != nil { diff --git a/pkg/collectors/devices/gps_ubx_ver.go b/pkg/collectors/devices/gps_ubx_ver.go index ef7fab49..0dfda78c 100644 --- a/pkg/collectors/devices/gps_ubx_ver.go +++ b/pkg/collectors/devices/gps_ubx_ver.go @@ -178,7 +178,7 @@ func processGPSVer(result map[string]string) (map[string]any, error) { } // GetGPSVersions returns GPSVersions of the host -func GetGPSVersions(ctx clients.ContainerContext) (GPSVersions, error) { +func GetGPSVersions(ctx clients.ExecContext) (GPSVersions, error) { gpsVer := GPSVersions{} err := gpsVerFetcher.Fetch(ctx, &gpsVer) if err != nil { diff --git a/pkg/collectors/devices/pmc.go b/pkg/collectors/devices/pmc.go index 5af688a8..f83e9336 100644 --- a/pkg/collectors/devices/pmc.go +++ b/pkg/collectors/devices/pmc.go @@ -137,7 +137,7 @@ func processPMC(result map[string]string) (map[string]any, error) { //nolint:fun } // GetPMC returns PMCInfo -func GetPMC(ctx clients.ContainerContext) (PMCInfo, error) { +func GetPMC(ctx clients.ExecContext) (PMCInfo, error) { gmSetting := PMCInfo{} err := pmcFetcher.Fetch(ctx, &gmSetting) if err != nil { diff --git a/pkg/collectors/dpll_collector.go b/pkg/collectors/dpll_collector.go index 23b200fa..0dfa44a4 100644 --- a/pkg/collectors/dpll_collector.go +++ b/pkg/collectors/dpll_collector.go @@ -14,7 +14,7 @@ import ( type DPLLCollector struct { *baseCollector - ctx clients.ContainerContext + ctx clients.ExecContext interfaceName string } diff --git a/pkg/collectors/gps_ubx_collector.go b/pkg/collectors/gps_ubx_collector.go index c42e17a7..8086dbc1 100644 --- a/pkg/collectors/gps_ubx_collector.go +++ b/pkg/collectors/gps_ubx_collector.go @@ -18,7 +18,7 @@ var ( type GPSCollector struct { *baseCollector - ctx clients.ContainerContext + ctx clients.ExecContext interfaceName string } diff --git a/pkg/collectors/pmc_collector.go b/pkg/collectors/pmc_collector.go index 6af3609a..e9e33c10 100644 --- a/pkg/collectors/pmc_collector.go +++ b/pkg/collectors/pmc_collector.go @@ -18,7 +18,7 @@ const ( type PMCCollector struct { *baseCollector - ctx clients.ContainerContext + ctx clients.ExecContext } func (pmc *PMCCollector) poll() error { diff --git a/pkg/fetcher/fetcher.go b/pkg/fetcher/fetcher.go index 9e953f77..9f47a4a1 100644 --- a/pkg/fetcher/fetcher.go +++ b/pkg/fetcher/fetcher.go @@ -12,9 +12,11 @@ import ( "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/clients" ) +type PostProcessFuncType func(map[string]string) (map[string]any, error) + type Fetcher struct { cmdGrp *clients.CmdGroup - postProcessor func(map[string]string) (map[string]any, error) + postProcessor PostProcessFuncType } func NewFetcher() *Fetcher { @@ -27,7 +29,7 @@ func TrimSpace(s string) (string, error) { return strings.TrimSpace(s), nil } -func (inst *Fetcher) SetPostProcessor(ppFunc func(map[string]string) (map[string]any, error)) { +func (inst *Fetcher) SetPostProcessor(ppFunc PostProcessFuncType) { inst.postProcessor = ppFunc } @@ -52,7 +54,7 @@ func (inst *Fetcher) AddCommand(cmdInst *clients.Cmd) { // Fetch executes the commands on the container passed as the ctx and // use the results to populate pack -func (inst *Fetcher) Fetch(ctx clients.ContainerContext, pack any) error { +func (inst *Fetcher) Fetch(ctx clients.ExecContext, pack any) error { runResult, err := runCommands(ctx, inst.cmdGrp) if err != nil { return err @@ -79,13 +81,13 @@ func (inst *Fetcher) Fetch(ctx clients.ContainerContext, pack any) error { // runCommands executes the commands on the container passed as the ctx // and extracts the results from the stdout -func runCommands(ctx clients.ContainerContext, cmdGrp clients.Cmder) (result map[string]string, err error) { //nolint:lll // allow slightly long function definition +func runCommands(ctx clients.ExecContext, cmdGrp clients.Cmder) (result map[string]string, err error) { //nolint:lll // allow slightly long function definition cmd := cmdGrp.GetCommand() command := []string{"/usr/bin/sh"} var buffIn bytes.Buffer buffIn.WriteString(cmd) - stdout, _, err := ctx.ExecCommandContainerStdIn(command, buffIn) + stdout, _, err := ctx.ExecCommandStdIn(command, buffIn) if err != nil { log.Debugf( "command in container failed unexpectedly:\n\tcontext: %v\n\tcommand: %v\n\terror: %v",