/
kubernetes.go
157 lines (145 loc) · 5.98 KB
/
kubernetes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package details_apis
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/olivere/elastic"
"github.com/erda-project/erda/modules/core/monitor/metric/query/query"
"github.com/erda-project/erda/modules/monitor/utils"
)
// PodInfoSummary .
type PodInfoSummary struct {
ClusterName interface{} `json:"clusterName"`
NodeName interface{} `json:"nodeName"`
HostIP interface{} `json:"hostIP"`
Namespace interface{} `json:"namespace"`
PodName interface{} `json:"podName"`
RestartTotal interface{} `json:"restartTotal"`
StateCode interface{} `json:"stateCode"`
TerminatedReason interface{} `json:"terminatedReason"`
}
// PodInfoInstanse .
type PodInfoInstanse struct {
ContainerID interface{} `json:"containerId"`
HostIP interface{} `json:"hostIP"`
StartedAt interface{} `json:"startedAt"`
FinishedAt interface{} `json:"finishedAt"`
ExitCode interface{} `json:"exitCode"`
OomKilled interface{} `json:"oomKilled"`
}
// PodInfo .
type PodInfo struct {
Summary PodInfoSummary `json:"summary"`
Instances []*PodInfoInstanse `json:"instances"`
}
func (p *provider) getPodInfo(clusterName, podName string, start, end int64) (*PodInfo, error) {
boolQuery := elastic.NewBoolQuery().
Filter(elastic.NewRangeQuery(query.TimestampKey).Gte(start * 1000000).Lte(end * 1000000)).
Filter(elastic.NewTermQuery(query.ClusterNameKey, clusterName)).
Filter(elastic.NewTermQuery(query.TagKey+".pod_name", podName))
searchSource := elastic.NewSearchSource().Query(boolQuery).Size(0)
searchSource = searchSource.Aggregation("pod", elastic.NewTopHitsAggregation().Size(1).Sort(query.TimestampKey, false))
resp, err := p.metricq.QueryRaw([]string{"kubernetes_pod_container"},
[]string{clusterName}, start, end, searchSource)
if err != nil {
if esErr, ok := err.(*elastic.Error); ok {
if esErr.Status < http.StatusInternalServerError {
return &PodInfo{}, nil
}
}
return nil, fmt.Errorf("fail to query pod and containers: %s", err)
}
var info PodInfo
if resp.Aggregations == nil {
return &info, nil
}
topHis, ok := resp.Aggregations.TopHits("pod")
if ok && topHis != nil && topHis.Hits != nil && len(topHis.Hits.Hits) > 0 {
var source map[string]interface{}
err := json.Unmarshal([]byte(*topHis.Hits.Hits[0].Source), &source)
if err == nil {
info.Summary.ClusterName = utils.GetMapValue(query.TagKey+".cluster_name", source)
info.Summary.NodeName = utils.GetMapValue(query.TagKey+".node_name", source)
info.Summary.HostIP = utils.GetMapValue(query.TagKey+".host_ip", source)
info.Summary.Namespace = utils.GetMapValue(query.TagKey+".namespace", source)
info.Summary.PodName = utils.GetMapValue(query.TagKey+".pod_name", source)
info.Summary.StateCode = utils.GetMapValue(query.FieldKey+".state_code", source)
info.Summary.RestartTotal = utils.GetMapValue(query.FieldKey+".restarts_total", source)
info.Summary.TerminatedReason = utils.GetMapValue(query.FieldKey+".terminated_reason", source)
}
}
err = p.getContainers(clusterName, podName, start, end, &info)
if err != nil {
return nil, err
}
return &info, nil
}
func (p *provider) getContainers(clusterName, podName string, start, end int64, info *PodInfo) error {
boolQuery := elastic.NewBoolQuery().
Filter(elastic.NewRangeQuery(query.TimestampKey).Gte(start * 1000000).Lte(end * 1000000)).
Filter(elastic.NewTermQuery(query.ClusterNameKey, clusterName)).
Filter(elastic.NewTermQuery(query.TagKey+".pod_name", podName)).
MustNot(elastic.NewTermQuery(query.TagKey+".podsandbox", "true"))
searchSource := elastic.NewSearchSource().Query(boolQuery).Size(0)
const containerIDKey = query.TagKey + ".container_id"
searchSource.Aggregation(containerIDKey, elastic.NewTermsAggregation().Field(containerIDKey).
SubAggregation("container", elastic.NewTopHitsAggregation().Size(1).Sort(query.TimestampKey, false).Sort(query.FieldKey+".finished_at", false)))
resp, err := p.metricq.QueryRaw([]string{"docker_container_summary"},
[]string{clusterName}, start, end, searchSource)
if err != nil {
if esErr, ok := err.(*elastic.Error); ok {
if esErr.Status < http.StatusInternalServerError {
return nil
}
}
return fmt.Errorf("fail to query pod and containers: %s", err)
}
if resp.Aggregations == nil {
return nil
}
terms, ok := resp.Aggregations.Terms(containerIDKey)
if ok && terms != nil {
for _, b := range terms.Buckets {
if b.Aggregations == nil {
continue
}
topHis, ok := b.Aggregations.TopHits("container")
if ok && topHis != nil && topHis.Hits != nil && len(topHis.Hits.Hits) > 0 {
var source map[string]interface{}
err := json.Unmarshal([]byte(*topHis.Hits.Hits[0].Source), &source)
if err == nil {
inst := &PodInfoInstanse{
ContainerID: b.Key,
HostIP: utils.GetMapValue(query.TagKey+".host_ip", source),
StartedAt: formatDate(utils.GetMapValue(query.FieldKey+".started_at", source)),
FinishedAt: formatDate(utils.GetMapValue(query.FieldKey+".finished_at", source)),
ExitCode: utils.GetMapValue(query.FieldKey+".exitcode", source),
OomKilled: utils.GetMapValue(query.FieldKey+".oomkilled", source),
}
info.Instances = append(info.Instances, inst)
}
}
}
}
return nil
}
func formatDate(date interface{}) interface{} {
val, ok := utils.ConvertInt64(date)
if !ok {
return date
}
return time.Unix(val/int64(time.Second), val%int64(time.Second)).Format("2006-01-02 15:04:05")
}