diff --git a/pkg/simple/client/events/elasticsearch/clients.go b/pkg/simple/client/events/elasticsearch/clients.go index 5f2308651c82bec286674f7cf663c0f390d3d6ff..ba56e23e2a1afa74568522717d90bff7760bc258 100644 --- a/pkg/simple/client/events/elasticsearch/clients.go +++ b/pkg/simple/client/events/elasticsearch/clients.go @@ -98,7 +98,7 @@ func (c *ClientV6) parse(resp *es6api.Response, err error) (*Response, error) { return nil, fmt.Errorf(resp.String()) } var r struct { - Hits *struct { + Hits struct { Total int64 `json:"total"` Hits jsoniter.RawMessage `json:"hits"` } `json:"hits"` @@ -128,7 +128,7 @@ func (c *ClientV7) parse(resp *es7api.Response, err error) (*Response, error) { return nil, fmt.Errorf(resp.String()) } var r struct { - Hits *struct { + Hits struct { Total struct { Value int64 `json:"value"` } `json:"total"` diff --git a/pkg/simple/client/events/elasticsearch/elasticsearch.go b/pkg/simple/client/events/elasticsearch/elasticsearch.go index 281afe1543cac31c5741c898117e19205c362568..5b41ba750a93a8b2378f356df9d3430068f3bb00 100644 --- a/pkg/simple/client/events/elasticsearch/elasticsearch.go +++ b/pkg/simple/client/events/elasticsearch/elasticsearch.go @@ -49,9 +49,12 @@ func (es *elasticsearch) SearchEvents(filter *events.Filter, from, size int64, Index: resolveIndexNames(es.opts.indexPrefix, filter.StartTime, filter.EndTime), Body: bytes.NewBuffer(body), }) - if err != nil || resp == nil { + if err != nil { return nil, err } + if resp == nil || len(resp.Hits.Hits) == 0 { + return &events.Events{}, nil + } var innerHits []struct { *corev1.Event `json:"_source"` @@ -95,11 +98,17 @@ func (es *elasticsearch) CountOverTime(filter *events.Filter, interval string) ( Index: resolveIndexNames(es.opts.indexPrefix, filter.StartTime, filter.EndTime), Body: bytes.NewBuffer(body), }) - if err != nil || resp == nil { + if err != nil { return nil, err } + if resp == nil || resp.Aggregations == nil { + return &events.Histogram{}, nil + } - raw := resp.Aggregations[aggName] + raw, ok := resp.Aggregations[aggName] + if !ok || len(raw) == 0 { + return &events.Histogram{}, nil + } var agg struct { Buckets []struct { KeyAsString string `json:"key_as_string"` @@ -142,11 +151,17 @@ func (es *elasticsearch) StatisticsOnResources(filter *events.Filter) (*events.S Index: resolveIndexNames(es.opts.indexPrefix, filter.StartTime, filter.EndTime), Body: bytes.NewBuffer(body), }) - if err != nil || resp == nil { + if err != nil { return nil, err } + if resp == nil || resp.Aggregations == nil { + return &events.Statistics{}, nil + } - raw := resp.Aggregations[aggName] + raw, ok := resp.Aggregations[aggName] + if !ok || len(raw) == 0 { + return &events.Statistics{}, nil + } var agg struct { Value int64 `json:"value"` }