|
| 1 | +package conditional |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/openshift/insights-operator/pkg/gatherers" |
| 8 | + "github.com/openshift/insights-operator/pkg/record" |
| 9 | + "github.com/openshift/insights-operator/pkg/utils" |
| 10 | + "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 13 | + "k8s.io/client-go/dynamic" |
| 14 | +) |
| 15 | + |
| 16 | +// APIRequestCount defines a type used when marshaling into JSON |
| 17 | +type APIRequestCount struct { |
| 18 | + ResourceName string `json:"resource"` |
| 19 | + RemovedInRelease string `json:"removed_in_release"` |
| 20 | + TotalRequestCount int64 `json:"total_request_count"` |
| 21 | + LastDayRequestCount int64 `json:"last_day_request_count"` |
| 22 | +} |
| 23 | + |
| 24 | +// BuildGatherApiRequestCounts creates a gathering closure which collects logs api requests counts for the |
| 25 | +// resources mentioned in |
| 26 | +// Params is of type AlertIsFiringConditionParams: |
| 27 | +// - alert_name string - name of the firing alert |
| 28 | +// |
| 29 | +// |
| 30 | +// * Location in archive: conditional/api_request_counts.json |
| 31 | +// * Since versions: |
| 32 | +// * 4.10+ |
| 33 | +func (g *Gatherer) BuildGatherAPIRequestCounts(paramsInterface interface{}) (gatherers.GatheringClosure, error) { |
| 34 | + params, ok := paramsInterface.(GatherAPIRequestCountsParams) |
| 35 | + if !ok { |
| 36 | + return gatherers.GatheringClosure{}, fmt.Errorf( |
| 37 | + "unexpected type in paramsInterface, expected %T, got %T", |
| 38 | + GatherAPIRequestCountsParams{}, paramsInterface, |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + return gatherers.GatheringClosure{ |
| 43 | + Run: func(ctx context.Context) ([]record.Record, []error) { |
| 44 | + records, err := g.gatherAPIRequestCounts(ctx, params.AlertName) |
| 45 | + if err != nil { |
| 46 | + return records, err |
| 47 | + } |
| 48 | + return records, nil |
| 49 | + }, |
| 50 | + CanFail: canConditionalGathererFail, |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (g *Gatherer) gatherAPIRequestCounts(ctx context.Context, alertName string) ([]record.Record, []error) { |
| 55 | + var resources []string |
| 56 | + for _, a := range g.firingAlerts[alertName] { |
| 57 | + resourceName := fmt.Sprintf("%s.%s.%s", a.Resource, a.Version, a.Group) |
| 58 | + resources = append(resources, resourceName) |
| 59 | + } |
| 60 | + |
| 61 | + dynamicClient, err := dynamic.NewForConfig(g.gatherProtoKubeConfig) |
| 62 | + if err != nil { |
| 63 | + return nil, []error{err} |
| 64 | + } |
| 65 | + gvr := schema.GroupVersionResource{Group: "apiserver.openshift.io", Version: "v1", Resource: "apirequestcounts"} |
| 66 | + apiReqCountsList, err := dynamicClient.Resource(gvr).List(ctx, metav1.ListOptions{}) |
| 67 | + if errors.IsNotFound(err) { |
| 68 | + return nil, nil |
| 69 | + } |
| 70 | + if err != nil { |
| 71 | + return nil, []error{err} |
| 72 | + } |
| 73 | + var records []record.Record |
| 74 | + var errrs []error |
| 75 | + var apiReqCounts []APIRequestCount |
| 76 | + for i := range apiReqCountsList.Items { |
| 77 | + it := apiReqCountsList.Items[i] |
| 78 | + |
| 79 | + // filter only resources we're interested in |
| 80 | + if utils.IsInSlice(it.GetName(), resources) { |
| 81 | + totalReqCount, err := utils.NestedInt64Wrapper(it.Object, "status", "requestCount") |
| 82 | + if err != nil { |
| 83 | + errrs = append(errrs, err) |
| 84 | + } |
| 85 | + lastDayReqCount, err := utils.NestedInt64Wrapper(it.Object, "status", "currentHour", "requestCount") |
| 86 | + if err != nil { |
| 87 | + errrs = append(errrs, err) |
| 88 | + } |
| 89 | + removedInRel, err := utils.NestedStringWrapper(it.Object, "status", "removedInRelease") |
| 90 | + if err != nil { |
| 91 | + errrs = append(errrs, err) |
| 92 | + } |
| 93 | + apiReqCount := APIRequestCount{ |
| 94 | + TotalRequestCount: totalReqCount, |
| 95 | + LastDayRequestCount: lastDayReqCount, |
| 96 | + ResourceName: it.GetName(), |
| 97 | + RemovedInRelease: removedInRel, |
| 98 | + } |
| 99 | + apiReqCounts = append(apiReqCounts, apiReqCount) |
| 100 | + } |
| 101 | + } |
| 102 | + records = append(records, record.Record{ |
| 103 | + Name: fmt.Sprintf("%v/api_request_counts", g.GetName()), |
| 104 | + Item: record.JSONMarshaller{Object: apiReqCounts}, |
| 105 | + }) |
| 106 | + return records, errrs |
| 107 | +} |
0 commit comments