func runAgentDiagnostics()

in internal/agentdiag.go [24:82]


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")
	if err != nil {
		zipFile.AddError(err)
		return // unrecoverable let's return
	}
	if err := resources.Visit(func(info *resource.Info, _ error) error {
		select {
		case <-stopCh:
			return errors.New("aborting Elastic Agent diagnostic")
		default:
			// continue processing agents
		}

		resourceName := info.Name
		labels, err := meta.NewAccessor().Labels(info.Object)
		if err != nil {
			zipFile.AddError(fmt.Errorf("while accessing labels for %s/%s: %w", ns, resourceName, err))
			return nil
		}

		v, found := labels["agent.k8s.elastic.co/version"]
		if !found || v == "" {
			logger.Printf("Skipping %s/%s as it has no version", ns, resourceName)
			return nil
		}
		ver, err := version.ParseSemantic(v)
		if err != nil {
			zipFile.AddError(err)
			return nil
		}

		if ver.LessThan(version.MustParseSemantic("7.16.0")) {
			logger.Printf("Skipping %s/%s as it is below min version of 7.16.0", ns, resourceName)
			return nil
		}

		if !filters.Matches(labels) {
			return nil
		}

		nsn := types.NamespacedName{Namespace: ns, Name: resourceName}

		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, 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 {
				zipFile.AddError(fmt.Errorf("while cleaning up agent container %s: %w", nsn, err))
			}
		}
		return nil
	}); err != nil {
		zipFile.AddError(err)
	}
}