diff --git a/pkg/apiserver/logging/logging.go b/pkg/apiserver/logging/logging.go index 049a1282b1a3bfa5c6b8f53fb1978d3a2a8a3365..ab51fdc77e2d51138ccd703d91f1e9ef8c6a1bf0 100644 --- a/pkg/apiserver/logging/logging.go +++ b/pkg/apiserver/logging/logging.go @@ -138,6 +138,7 @@ func logQuery(level log.LogQueryLevel, request *restful.Request) *es.QueryResult { param.NamespaceFilled, param.Namespaces = log.QueryWorkspace(request.QueryParameter("workspaces"), request.QueryParameter("workspace_query")) param.NamespaceFilled, param.Namespaces = log.MatchNamespace(request.QueryParameter("namespaces"), param.NamespaceFilled, param.Namespaces) + param.NamespaceFilled, param.NamespaceWithCreationTime = log.GetNamespaceCreationTimeMap(param.Namespaces) param.NamespaceQuery = request.QueryParameter("namespace_query") param.PodFilled, param.Pods = log.QueryWorkload(request.QueryParameter("workloads"), request.QueryParameter("workload_query"), param.Namespaces) param.PodFilled, param.Pods = log.MatchPod(request.QueryParameter("pods"), param.PodFilled, param.Pods) @@ -149,6 +150,7 @@ func logQuery(level log.LogQueryLevel, request *restful.Request) *es.QueryResult { param.NamespaceFilled, param.Namespaces = log.QueryWorkspace(request.PathParameter("workspace"), "") param.NamespaceFilled, param.Namespaces = log.MatchNamespace(request.QueryParameter("namespaces"), param.NamespaceFilled, param.Namespaces) + param.NamespaceFilled, param.NamespaceWithCreationTime = log.GetNamespaceCreationTimeMap(param.Namespaces) param.NamespaceQuery = request.QueryParameter("namespace_query") param.PodFilled, param.Pods = log.QueryWorkload(request.QueryParameter("workloads"), request.QueryParameter("workload_query"), param.Namespaces) param.PodFilled, param.Pods = log.MatchPod(request.QueryParameter("pods"), param.PodFilled, param.Pods) @@ -159,6 +161,7 @@ func logQuery(level log.LogQueryLevel, request *restful.Request) *es.QueryResult case log.QueryLevelNamespace: { param.NamespaceFilled, param.Namespaces = log.MatchNamespace(request.PathParameter("namespace"), false, nil) + param.NamespaceFilled, param.NamespaceWithCreationTime = log.GetNamespaceCreationTimeMap(param.Namespaces) param.PodFilled, param.Pods = log.QueryWorkload(request.QueryParameter("workloads"), request.QueryParameter("workload_query"), param.Namespaces) param.PodFilled, param.Pods = log.MatchPod(request.QueryParameter("pods"), param.PodFilled, param.Pods) param.PodQuery = request.QueryParameter("pod_query") @@ -168,6 +171,7 @@ func logQuery(level log.LogQueryLevel, request *restful.Request) *es.QueryResult case log.QueryLevelWorkload: { param.NamespaceFilled, param.Namespaces = log.MatchNamespace(request.PathParameter("namespace"), false, nil) + param.NamespaceFilled, param.NamespaceWithCreationTime = log.GetNamespaceCreationTimeMap(param.Namespaces) param.PodFilled, param.Pods = log.QueryWorkload(request.PathParameter("workload"), "", param.Namespaces) param.PodFilled, param.Pods = log.MatchPod(request.QueryParameter("pods"), param.PodFilled, param.Pods) param.PodQuery = request.QueryParameter("pod_query") @@ -177,6 +181,7 @@ func logQuery(level log.LogQueryLevel, request *restful.Request) *es.QueryResult case log.QueryLevelPod: { param.NamespaceFilled, param.Namespaces = log.MatchNamespace(request.PathParameter("namespace"), false, nil) + param.NamespaceFilled, param.NamespaceWithCreationTime = log.GetNamespaceCreationTimeMap(param.Namespaces) param.PodFilled, param.Pods = log.MatchPod(request.PathParameter("pod"), false, nil) param.ContainerFilled, param.Containers = log.MatchContainer(request.QueryParameter("containers")) param.ContainerQuery = request.QueryParameter("container_query") @@ -184,6 +189,7 @@ func logQuery(level log.LogQueryLevel, request *restful.Request) *es.QueryResult case log.QueryLevelContainer: { param.NamespaceFilled, param.Namespaces = log.MatchNamespace(request.PathParameter("namespace"), false, nil) + param.NamespaceFilled, param.NamespaceWithCreationTime = log.GetNamespaceCreationTimeMap(param.Namespaces) param.PodFilled, param.Pods = log.MatchPod(request.PathParameter("pod"), false, nil) param.ContainerFilled, param.Containers = log.MatchContainer(request.PathParameter("container")) } diff --git a/pkg/models/log/logcollector.go b/pkg/models/log/logcollector.go index d0a30894f5f04c4b5b76f13a5f8fdbd059b0939d..42253683c64c5dd2b702befe2e72281f0ae014d1 100644 --- a/pkg/models/log/logcollector.go +++ b/pkg/models/log/logcollector.go @@ -24,7 +24,9 @@ import ( "kubesphere.io/kubesphere/pkg/constants" "kubesphere.io/kubesphere/pkg/informers" "reflect" + "strconv" "strings" + "time" ) func intersection(s1, s2 []string) (inter []string) { @@ -181,6 +183,37 @@ func MatchNamespace(namespaceMatch string, namespaceFilled bool, namespaces []st return true, namespacesMatch } +func GetNamespaceCreationTimeMap(namespaces []string) (bool, map[string]string) { + + namespaceWithCreationTime := make(map[string]string) + + nsLister := informers.SharedInformerFactory().Core().V1().Namespaces().Lister() + + if len(namespaces) == 0 { + nsList, err := nsLister.List(labels.Everything()) + if err != nil { + glog.Error("failed to list namespace, error: ", err) + return true, namespaceWithCreationTime + } + + for _, ns := range nsList { + namespaces = append(namespaces, ns.Name) + } + } + + for _, item := range namespaces { + ns, err := nsLister.Get(item) + if err != nil { + glog.Error("failed to get namespace, error: ", err) + continue + } + + namespaceWithCreationTime[ns.Name] = strconv.FormatInt(ns.CreationTimestamp.UnixNano()/int64(time.Millisecond), 10) + } + + return true, namespaceWithCreationTime +} + func QueryWorkload(workloadMatch string, workloadQuery string, namespaces []string) (bool, []string) { if workloadMatch == "" && workloadQuery == "" { return false, nil diff --git a/pkg/simple/client/elasticsearch/esclient.go b/pkg/simple/client/elasticsearch/esclient.go index d63766f00740de89078388c1b5f8c32ce08613bc..c5075459cec99cd5c028fc435f9801a0d902a325 100644 --- a/pkg/simple/client/elasticsearch/esclient.go +++ b/pkg/simple/client/elasticsearch/esclient.go @@ -58,7 +58,7 @@ type Request struct { From int64 `json:"from"` Size int64 `json:"size"` Sorts []Sort `json:"sort,omitempty"` - MainQuery MainQuery `json:"query"` + MainQuery BoolQuery `json:"query"` Aggs interface{} `json:"aggs,omitempty"` MainHighLight MainHighLight `json:"highlight,omitempty"` } @@ -71,11 +71,11 @@ type Order struct { Order string `json:"order"` } -type MainQuery struct { - MainBoolQuery MainBoolQuery `json:"bool"` +type BoolQuery struct { + BoolMusts BoolMusts `json:"bool"` } -type MainBoolQuery struct { +type BoolMusts struct { Musts []interface{} `json:"must"` } @@ -173,17 +173,24 @@ type DateHistogram struct { func createQueryRequest(param QueryParameters) (int, []byte, error) { var request Request - var mainBoolQuery MainBoolQuery + var mainBoolQuery BoolMusts if param.NamespaceFilled { var shouldMatchPhrase ShouldMatchPhrase - if len(param.Namespaces) == 0 { + if len(param.NamespaceWithCreationTime) == 0 { matchPhrase := MatchPhrase{map[string]interface{}{"kubernetes.namespace_name.key_word": QueryWord{""}}} shouldMatchPhrase.Shoulds = append(shouldMatchPhrase.Shoulds, matchPhrase) } else { - for _, namespace := range param.Namespaces { + for namespace, creationTime := range param.NamespaceWithCreationTime { + var boolQuery BoolQuery + matchPhrase := MatchPhrase{map[string]interface{}{"kubernetes.namespace_name.keyword": QueryWord{namespace}}} - shouldMatchPhrase.Shoulds = append(shouldMatchPhrase.Shoulds, matchPhrase) + rangeQuery := RangeQuery{RangeSpec{TimeRange{creationTime, ""}}} + + boolQuery.BoolMusts.Musts = append(boolQuery.BoolMusts.Musts, matchPhrase) + boolQuery.BoolMusts.Musts = append(boolQuery.BoolMusts.Musts, rangeQuery) + + shouldMatchPhrase.Shoulds = append(shouldMatchPhrase.Shoulds, boolQuery) } } shouldMatchPhrase.MinimumShouldMatch = 1 @@ -278,7 +285,7 @@ func createQueryRequest(param QueryParameters) (int, []byte, error) { request.MainHighLight = mainHighLight } - request.MainQuery = MainQuery{mainBoolQuery} + request.MainQuery = BoolQuery{mainBoolQuery} queryRequest, err := json.Marshal(request) @@ -544,12 +551,13 @@ func parseQueryResult(operation int, param QueryParameters, body []byte, query [ } type QueryParameters struct { - NamespaceFilled bool - Namespaces []string - PodFilled bool - Pods []string - ContainerFilled bool - Containers []string + NamespaceFilled bool + Namespaces []string + NamespaceWithCreationTime map[string]string + PodFilled bool + Pods []string + ContainerFilled bool + Containers []string NamespaceQuery string PodQuery string