diff --git a/cmd/ks-apiserver/app/server.go b/cmd/ks-apiserver/app/server.go index f36b83dbc9fb811eadc4ba4b329689dbdafd4425..4c0ad3bb54e3b0cb1f4a5b8b0deea20efbce86d1 100644 --- a/cmd/ks-apiserver/app/server.go +++ b/cmd/ks-apiserver/app/server.go @@ -106,6 +106,11 @@ func initializeServicemeshConfig(s *options.ServerRunOptions) { tracing.JaegerQueryUrl = s.ServiceMeshOptions.JaegerQueryHost } + // Set the kiali query endpoint address + if s.ServiceMeshOptions != nil && len(s.ServiceMeshOptions.KialiQueryHost) != 0 { + tracing.KialiQueryUrl = s.ServiceMeshOptions.KialiQueryHost + } + // Exclude system namespaces config.API.Namespaces.Exclude = []string{"istio-system", "kube-.*"} config.InCluster = true diff --git a/go.mod b/go.mod index 27929dbe44094d845256179302632db4c2c25a37..5b2d77bbd5a23ac264d3876f614b67f64a9275b9 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,6 @@ require ( github.com/kubernetes-csi/external-snapshotter/v2 v2.1.0 github.com/kubesphere/sonargo v0.0.2 github.com/lib/pq v1.2.0 // indirect - github.com/mitchellh/mapstructure v1.2.2 github.com/onsi/ginkgo v1.12.0 github.com/onsi/gomega v1.9.0 github.com/open-policy-agent/opa v0.18.0 diff --git a/pkg/kapis/servicemesh/metrics/v1alpha2/handler.go b/pkg/kapis/servicemesh/metrics/v1alpha2/handler.go index e6e67e49a68aa50bc17bf34f168829f4b8b72291..a4b298b4007efb836083738f43b87d785c7fb2e2 100644 --- a/pkg/kapis/servicemesh/metrics/v1alpha2/handler.go +++ b/pkg/kapis/servicemesh/metrics/v1alpha2/handler.go @@ -29,9 +29,31 @@ import ( // default jaeger query api endpoint address var JaegerQueryUrl = "http://jaeger-query.istio-system.svc:16686" +/* +Use Kiali API directly if config existed in configmap. +Such as: +kubectl -n kubesphere-system get cm kubesphere-config -oyaml +... +kialiQueryHost: http://kiali.istio-system:20001 +... + +Otherwise, use the API provided by kiali code. + +Announce: The API provided by kiali code will deprecated in the future. +*/ + +var KialiQueryUrl string + // Get app metrics func getAppMetrics(request *restful.Request, response *restful.Response) { - handlers.AppMetrics(request, response) + if len(KialiQueryUrl) == 0 { + handlers.AppMetrics(request, response) + } else { + namespace := request.PathParameter("namespace") + app := request.PathParameter("app") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/apps/%s/metrics?%s", KialiQueryUrl, namespace, app, request.Request.URL.RawQuery) + getData(response, url) + } } // Get workload metrics @@ -43,17 +65,35 @@ func getWorkloadMetrics(request *restful.Request, response *restful.Response) { request.Request.URL.RawQuery = fmt.Sprintf("%s&namespaces=%s&workload=%s", request.Request.URL.RawQuery, namespace, workload) } - handlers.WorkloadMetrics(request, response) + if len(KialiQueryUrl) == 0 { + handlers.WorkloadMetrics(request, response) + } else { + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/workloads/%s/metrics?%s", KialiQueryUrl, namespace, workload, request.Request.URL.RawQuery) + getData(response, url) + } } // Get service metrics func getServiceMetrics(request *restful.Request, response *restful.Response) { - handlers.ServiceMetrics(request, response) + if len(KialiQueryUrl) == 0 { + handlers.ServiceMetrics(request, response) + } else { + namespace := request.PathParameter("namespace") + service := request.PathParameter("service") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/services/%s/metrics?%s", KialiQueryUrl, namespace, service, request.Request.URL.RawQuery) + getData(response, url) + } } // Get namespace metrics func getNamespaceMetrics(request *restful.Request, response *restful.Response) { - handlers.NamespaceMetrics(request, response) + if len(KialiQueryUrl) == 0 { + handlers.NamespaceMetrics(request, response) + } else { + namespace := request.PathParameter("namespace") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/metrics?%s", KialiQueryUrl, namespace, request.Request.URL.RawQuery) + getData(response, url) + } } // Get service graph for namespace @@ -64,9 +104,15 @@ func getNamespaceGraph(request *restful.Request, response *restful.Response) { request.Request.URL.RawQuery = fmt.Sprintf("%s&namespaces=%s", request.Request.URL.RawQuery, namespace) } - handlers.GetNamespaceGraph(request, response) + if len(KialiQueryUrl) == 0 { + handlers.GetNamespaceGraph(request, response) + } else { + url := fmt.Sprintf("%s/kiali/api/namespaces/graph?%s", KialiQueryUrl, request.Request.URL.RawQuery) + getData(response, url) + } } +// Deprecated // Get service graph for namespaces func getNamespacesGraph(request *restful.Request, response *restful.Response) { handlers.GraphNamespaces(request, response) @@ -74,37 +120,65 @@ func getNamespacesGraph(request *restful.Request, response *restful.Response) { // Get namespace health func getNamespaceHealth(request *restful.Request, response *restful.Response) { - handlers.NamespaceHealth(request, response) + if len(KialiQueryUrl) == 0 { + handlers.NamespaceHealth(request, response) + } else { + namespace := request.PathParameter("namespace") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/health?%s", KialiQueryUrl, namespace, request.Request.URL.RawQuery) + getData(response, url) + } } // Get workload health func getWorkloadHealth(request *restful.Request, response *restful.Response) { - handlers.WorkloadHealth(request, response) + if len(KialiQueryUrl) == 0 { + handlers.WorkloadHealth(request, response) + } else { + namespace := request.PathParameter("namespace") + workload := request.PathParameter("workload") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/workloads/%s/health?%s", KialiQueryUrl, namespace, workload, request.Request.URL.RawQuery) + getData(response, url) + } } // Get app health func getAppHealth(request *restful.Request, response *restful.Response) { - handlers.AppHealth(request, response) + if len(KialiQueryUrl) == 0 { + handlers.AppHealth(request, response) + } else { + namespace := request.PathParameter("namespace") + app := request.PathParameter("app") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/apps/%s/health?%s", KialiQueryUrl, namespace, app, request.Request.URL.RawQuery) + getData(response, url) + } } // Get service health func getServiceHealth(request *restful.Request, response *restful.Response) { - handlers.ServiceHealth(request, response) + if len(KialiQueryUrl) == 0 { + handlers.ServiceHealth(request, response) + } else { + namespace := request.PathParameter("namespace") + service := request.PathParameter("service") + url := fmt.Sprintf("%s/kiali/api/namespaces/%s/services/%s/health?%s", KialiQueryUrl, namespace, service, request.Request.URL.RawQuery) + getData(response, url) + } } func getServiceTracing(request *restful.Request, response *restful.Response) { namespace := request.PathParameter("namespace") service := request.PathParameter("service") - serviceName := fmt.Sprintf("%s.%s", service, namespace) - url := fmt.Sprintf("%s/api/traces?%s&service=%s", JaegerQueryUrl, request.Request.URL.RawQuery, serviceName) + getData(response, url) +} +func getData(response *restful.Response, url string) { resp, err := http.Get(url) - klog.V(4).Infof("Proxy trace request to %s", url) + klog.V(4).Infof("Proxy request to %s", url) if err != nil { - klog.Errorf("query jaeger failed with err %v", err) + klog.Errorf("query url %s failed with err %v", url, err) api.HandleInternalError(response, nil, err) return } diff --git a/pkg/simple/client/servicemesh/options.go b/pkg/simple/client/servicemesh/options.go index 34c540cf264fdcb7d4a9d0ef2a9773d10fce7c56..96d26b1d967df9fd0c2c0eac17ea836dbafc3ed1 100644 --- a/pkg/simple/client/servicemesh/options.go +++ b/pkg/simple/client/servicemesh/options.go @@ -26,6 +26,9 @@ type Options struct { // jaeger query service url JaegerQueryHost string `json:"jaegerQueryHost,omitempty" yaml:"jaegerQueryHost"` + // kiali query service url + KialiQueryHost string `json:"kialiQueryHost,omitempty" yaml:"kialiQueryHost"` + // prometheus service url for servicemesh metrics ServicemeshPrometheusHost string `json:"servicemeshPrometheusHost,omitempty" yaml:"servicemeshPrometheusHost"` }