Skip to content

Commit

Permalink
Specify container name when running agent diagnostics (#212)
Browse files Browse the repository at this point in the history
This PR handles this issue (#211) and
makes agent diagnostics to be exec'ed always against the `agent` container
  • Loading branch information
pkoutsovasilis authored Jan 9, 2024
1 parent 931896f commit f7af962
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions internal/agentdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"k8s.io/cli-runtime/pkg/resource"
)

// based on eck-operator code the agent container name is constant "agent"
const agentContainerName = "agent"

func runAgentDiagnostics(k *Kubectl, ns string, zipFile *archive.ZipFile, verbose bool, stopCh chan struct{}, filters internal_filters.Filters) {
outputFile := time.Now().Format("eck-agent-diag-2006-01-02T15-04-05Z.zip")
resources, err := k.getResourcesMatching("pod", ns, "common.k8s.elastic.co/type=agent")
Expand Down Expand Up @@ -65,7 +68,7 @@ func runAgentDiagnostics(k *Kubectl, ns string, zipFile *archive.ZipFile, verbos
needsCleanup := diagnosticForAgentPod(nsn, k, outputFile, zipFile, verbose)

// no matter what happened: try to clean up the diagnostic archive in the agent container
if err := k.Exec(nsn, "rm", outputFile); err != nil {
if err := k.Exec(nsn, agentContainerName, "rm", outputFile); err != nil {
// but only report any errors during cleaning up if there is a likelihood that we created an archive to clean up
// in the first place
if needsCleanup {
Expand All @@ -83,12 +86,12 @@ func runAgentDiagnostics(k *Kubectl, ns string, zipFile *archive.ZipFile, verbos
// the containers file system.
func diagnosticForAgentPod(nsn types.NamespacedName, k *Kubectl, outputFile string, zipFile *archive.ZipFile, verbose bool) bool {
logger.Printf("Extracting agent diagnostics for %s", nsn)
if err := k.Exec(nsn, "elastic-agent", "diagnostics", "collect", "-f", outputFile); err != nil {
if err := k.Exec(nsn, agentContainerName, "elastic-agent", "diagnostics", "collect", "-f", outputFile); err != nil {
zipFile.AddError(fmt.Errorf("while extracting agent diagnostics: %w", err))
return false
}

reader, err := k.Copy(nsn, "agent", outputFile, zipFile.AddError)
reader, err := k.Copy(nsn, agentContainerName, outputFile, zipFile.AddError)
if err != nil {
zipFile.AddError(err)
return true
Expand Down
7 changes: 4 additions & 3 deletions internal/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c Kubectl) Copy(nsn types.NamespacedName, container string, path string, r
return reader, nil
}

func (c Kubectl) Exec(nsn types.NamespacedName, cmd ...string) error {
func (c Kubectl) Exec(nsn types.NamespacedName, containerName string, cmd ...string) error {
execErrOut := io.Discard
if c.verbose {
execErrOut = c.errOut
Expand All @@ -148,8 +148,9 @@ func (c Kubectl) Exec(nsn types.NamespacedName, cmd ...string) error {
ErrOut: execErrOut,
},

Namespace: nsn.Namespace,
PodName: nsn.Name,
Namespace: nsn.Namespace,
PodName: nsn.Name,
ContainerName: containerName,
},
Config: c.config,
PodClient: c.CoreV1(),
Expand Down

0 comments on commit f7af962

Please sign in to comment.