Skip to content

Commit

Permalink
Make ExecContext and remove container from the name of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Nov 20, 2023
1 parent 7054d1c commit c6843c9
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 36 deletions.
37 changes: 21 additions & 16 deletions pkg/clients/exec_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ 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
containerName string
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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
7 changes: 3 additions & 4 deletions pkg/clients/exec_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ var _ = Describe("NewContainerContext", func() {
Expect(ctx.GetPodName()).To(Equal("TestPod-8292"))
})
})

})

var _ = Describe("ExecCommandContainer", func() {
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/dev_info_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/devices/device_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions pkg/collectors/devices/dpll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/devices/gps_ubx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/devices/gps_ubx_ver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/devices/pmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/dpll_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/gps_ubx_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/collectors/pmc_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

type PMCCollector struct {
*baseCollector
ctx clients.ContainerContext
ctx clients.ExecContext
}

func (pmc *PMCCollector) poll() error {
Expand Down
12 changes: 7 additions & 5 deletions pkg/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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",
Expand Down

0 comments on commit c6843c9

Please sign in to comment.