|
| 1 | +package conditional |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/client-go/kubernetes" |
| 9 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 10 | + "k8s.io/klog/v2" |
| 11 | + |
| 12 | + "github.com/openshift/insights-operator/pkg/gatherers" |
| 13 | + "github.com/openshift/insights-operator/pkg/record" |
| 14 | +) |
| 15 | + |
| 16 | +// BuildGatherPodDefinition collects pod definition from pods that are firing one of the configured alerts. |
| 17 | +// |
| 18 | +// * Location in archive: conditional/namespaces/<namespace>/pods/<pod>/<pod>.json |
| 19 | +// * Since versions: |
| 20 | +// * 4.11+ |
| 21 | +func (g *Gatherer) BuildGatherPodDefinition(paramsInterface interface{}) (gatherers.GatheringClosure, error) { // nolint: dupl |
| 22 | + params, ok := paramsInterface.(GatherPodDefinitionParams) |
| 23 | + if !ok { |
| 24 | + return gatherers.GatheringClosure{}, fmt.Errorf( |
| 25 | + "unexpected type in paramsInterface, expected %T, got %T", |
| 26 | + GatherContainersLogsParams{}, |
| 27 | + paramsInterface) |
| 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 | + coreClient := kubeClient.CoreV1() |
| 37 | + return g.gatherPodDefinition(ctx, params, coreClient) |
| 38 | + }, |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (g *Gatherer) gatherPodDefinition( |
| 43 | + ctx context.Context, |
| 44 | + params GatherPodDefinitionParams, |
| 45 | + coreClient corev1client.CoreV1Interface, |
| 46 | +) ([]record.Record, []error) { |
| 47 | + alertInstances, ok := g.firingAlerts[params.AlertName] |
| 48 | + if !ok { |
| 49 | + err := fmt.Errorf("conditional gather triggered, but specified alert %q is not firing", params.AlertName) |
| 50 | + return nil, []error{err} |
| 51 | + } |
| 52 | + |
| 53 | + const logMissingAlert = "%s at alertName: %s" |
| 54 | + |
| 55 | + var errs []error |
| 56 | + var records []record.Record |
| 57 | + |
| 58 | + for _, alertLabels := range alertInstances { |
| 59 | + podNamespace, err := getAlertPodNamespace(alertLabels) |
| 60 | + if err != nil { |
| 61 | + klog.Warningf(logMissingAlert, err.Error(), params.AlertName) |
| 62 | + errs = append(errs, err) |
| 63 | + continue |
| 64 | + } |
| 65 | + podName, err := getAlertPodName(alertLabels) |
| 66 | + if err != nil { |
| 67 | + klog.Warningf(logMissingAlert, err.Error(), params.AlertName) |
| 68 | + errs = append(errs, err) |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + pod, err := coreClient.Pods(podNamespace).Get(ctx, podName, metav1.GetOptions{}) |
| 73 | + if err != nil { |
| 74 | + klog.Warningf("pod %s not found in %s namespace: %w", podName, podNamespace, err) |
| 75 | + errs = append(errs, err) |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + records = append(records, record.Record{ |
| 80 | + Name: fmt.Sprintf( |
| 81 | + "%s/namespaces/%s/pods/%s/%s", |
| 82 | + g.GetName(), |
| 83 | + podNamespace, |
| 84 | + podName, |
| 85 | + podName), |
| 86 | + Item: record.ResourceMarshaller{Resource: pod}, |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + return records, errs |
| 91 | +} |
0 commit comments