|
| 1 | +package conditional |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "k8s.io/client-go/kubernetes" |
| 8 | + v1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 9 | + "k8s.io/klog/v2" |
| 10 | + |
| 11 | + "github.com/openshift/insights-operator/pkg/gatherers" |
| 12 | + "github.com/openshift/insights-operator/pkg/gatherers/common" |
| 13 | + "github.com/openshift/insights-operator/pkg/record" |
| 14 | +) |
| 15 | + |
| 16 | +// BuildGatherLogsOfUnhealthyPods collects either current or previous logs for pods firing one of the configured alerts. |
| 17 | +// |
| 18 | +// * Location in archive: conditional/namespaces/<namespace>/pods/<pod>/containers/<container>/<logs|logs-previous>/last-<tail length>-lines.log |
| 19 | +// * Since versions: |
| 20 | +// * 4.10+ |
| 21 | +func (g *Gatherer) BuildGatherLogsOfUnhealthyPods(paramsInterface interface{}) (gatherers.GatheringClosure, error) { |
| 22 | + params, ok := paramsInterface.(GatherLogsOfUnhealthyPodsParams) |
| 23 | + if !ok { |
| 24 | + return gatherers.GatheringClosure{}, fmt.Errorf( |
| 25 | + "unexpected type in paramsInterface, expected %T, got %T", |
| 26 | + GatherLogsOfUnhealthyPodsParams{}, paramsInterface, |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + return gatherers.GatheringClosure{ |
| 31 | + Run: func(ctx context.Context) ([]record.Record, []error) { |
| 32 | + kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig) |
| 33 | + if err != nil { |
| 34 | + return nil, []error{err} |
| 35 | + } |
| 36 | + return g.gatherLogsOfUnhealthyPods(ctx, kubeClient.CoreV1(), params) |
| 37 | + }, |
| 38 | + CanFail: canConditionalGathererFail, |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (g *Gatherer) gatherLogsOfUnhealthyPods( |
| 43 | + ctx context.Context, coreClient v1.CoreV1Interface, params GatherLogsOfUnhealthyPodsParams, |
| 44 | +) ([]record.Record, []error) { |
| 45 | + errs := []error{} |
| 46 | + records := []record.Record{} |
| 47 | + |
| 48 | + alertInstances, ok := g.firingAlerts[params.AlertName] |
| 49 | + if !ok { |
| 50 | + return nil, []error{fmt.Errorf("conditional gatherer triggered, but specified alert %q is not firing", params.AlertName)} |
| 51 | + } |
| 52 | + for _, alertLabels := range alertInstances { |
| 53 | + alertNamespace, ok := alertLabels["namespace"] |
| 54 | + if !ok { |
| 55 | + newErr := fmt.Errorf("alert is missing 'namespace' label") |
| 56 | + klog.Warningln(newErr.Error()) |
| 57 | + errs = append(errs, newErr) |
| 58 | + continue |
| 59 | + } |
| 60 | + alertPod, ok := alertLabels["pod"] |
| 61 | + if !ok { |
| 62 | + newErr := fmt.Errorf("alert is missing 'pod' label") |
| 63 | + klog.Warningln(newErr.Error()) |
| 64 | + errs = append(errs, newErr) |
| 65 | + continue |
| 66 | + } |
| 67 | + // The container label may not be present for all alerts (e.g., KubePodNotReady). |
| 68 | + containerFilter := "" |
| 69 | + if alertContainer, ok := alertLabels["container"]; ok && alertContainer != "" { |
| 70 | + containerFilter = fmt.Sprintf("^%s$", alertContainer) |
| 71 | + } |
| 72 | + |
| 73 | + logRecords, err := common.CollectLogsFromContainers(ctx, coreClient, |
| 74 | + common.LogContainersFilter{ |
| 75 | + Namespace: alertNamespace, |
| 76 | + FieldSelector: fmt.Sprintf("metadata.name=%s", alertPod), |
| 77 | + ContainerNameRegexFilter: containerFilter, |
| 78 | + }, |
| 79 | + common.LogMessagesFilter{ |
| 80 | + TailLines: params.TailLines, |
| 81 | + Previous: params.Previous, |
| 82 | + }, |
| 83 | + func(namespace string, podName string, containerName string) string { |
| 84 | + logDirName := "logs" |
| 85 | + if params.Previous { |
| 86 | + logDirName = "logs-previous" |
| 87 | + } |
| 88 | + return fmt.Sprintf( |
| 89 | + "%s/namespaces/%s/pods/%s/containers/%s/%s/last-%d-lines.log", |
| 90 | + g.GetName(), |
| 91 | + namespace, |
| 92 | + podName, |
| 93 | + containerName, |
| 94 | + logDirName, |
| 95 | + params.TailLines, |
| 96 | + ) |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + // This can happen when the pod is destroyed but the alert still exists. |
| 100 | + newErr := fmt.Errorf("unable to get container logs: %v", err) |
| 101 | + klog.Warningln(newErr.Error()) |
| 102 | + errs = append(errs, newErr) |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + records = append(records, logRecords...) |
| 107 | + } |
| 108 | + |
| 109 | + return records, errs |
| 110 | +} |
0 commit comments