Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add federation API to query pod lists from multiple clusters #89

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/docker-helm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ on:

env:
DOCKERHUB_REGISTRY: registry-1.docker.io
DOCKERHUB_REGISTRY_NAMESPACE: lunettes
DOCKERHUB_LUNETTES_REPO: lunettes/lunettes
DOCKERHUB_GRAFANA_REPO: lunettes/grafana
DOCKERHUB_REGISTRY_NAMESPACE: d3c3mber
DOCKERHUB_LUNETTES_REPO: d3c3mber/lunettes
DOCKERHUB_GRAFANA_REPO: d3c3mber/grafana
# Plugins to be installed.
GRAFANA_PLUGINS: yesoreyeram-infinity-datasource marcusolsson-json-datasource volkovlabs-form-panel marcusolsson-dynamictext-panel

Expand Down
13 changes: 11 additions & 2 deletions cmd/grafanadi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
func newRootCmd() *cobra.Command {
config := &server.ServerConfig{}
var cfgFile, kubeConfigFile string
var fedCfgFile string

cmd := &cobra.Command{
Use: "grafanadi",
Expand All @@ -42,15 +43,19 @@ func newRootCmd() *cobra.Command {
panic(err.Error())
}

fedOptions, err := common.InitFedConfig(fedCfgFile)
common.GSiteOptions = *fedOptions

err = utils.InitKube(kubeConfigFile)
if err != nil {
klog.Errorf("failed to init kube client [%s], err:%s", kubeConfigFile, err.Error())
panic(err.Error())
}

serverConfig := &server.ServerConfig{
ListenAddr: config.ListenAddr,
Storage: storage,
ListenAddr: config.ListenAddr,
ListenAuthAddr: config.ListenAuthAddr,
Storage: storage,
}
hcsServer, err := server.NewAPIServer(serverConfig)
if err != nil {
Expand All @@ -65,10 +70,14 @@ func newRootCmd() *cobra.Command {
// for server listen port
cmd.PersistentFlags().StringVarP(&config.MetricsAddr, "metrics-addr", "", ":9091", "metrics listen address (default :9091)")
cmd.PersistentFlags().StringVarP(&config.ListenAddr, "listen-addr", "", ":8080", "api server listen address (default :8080)")
cmd.PersistentFlags().StringVarP(&config.ListenAuthAddr, "listen-auth-addr", "", ":8081", "api server listen address (default :8081)")

// for storage
cmd.PersistentFlags().StringVarP(&cfgFile, "config-file", "", "/app/storage-config.yaml", "storage config file")

// for federation API config file
cmd.PersistentFlags().StringVarP(&fedCfgFile, "fed-config-file", "", "/app/fed-config.yaml", "federation config file")

// kubeconfig for k8s client
cmd.PersistentFlags().StringVarP(&kubeConfigFile, "kubeconfig", "", "/etc/kubernetes/kubeconfig/admin.kubeconfig", "Path to kubeconfig file with authorization and apiserver information.")

Expand Down
5 changes: 5 additions & 0 deletions deploy/helm/lunettes/templates/grafanadi/grafanadi-cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ data:
endpoint: "http://es-cluster-svc.{{ .Values.namespace }}:9200"
username: {{ .Values.esUser }}
password: {{ .Values.esPassword }}
fed-config-file.yaml: |
site-infos:
- dashboard-url: "http://{{ template "lunettes.fullname" . }}.{{ .Values.namespace }}:9097"
di-url: "http://grafanadi.{{ .Values.namespace }}:9099"
site-name: "mainsite"
kind: ConfigMap
metadata:
name: grafanadi-cm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spec:
- command:
- ./grafanadi
- --config-file=/app/config-file.yaml
- --fed-config-file=/app/fed-config-file.yaml
- --enable-kube-client={{ .Values.grafanadiInitKubeClient }}
- --log_dir={{ .Values.grafanadiLogsPath }}
- --logtostderr=false
Expand All @@ -33,17 +34,19 @@ spec:
- containerPort: 8080
name: http-grafanadi
protocol: TCP
- containerPort: 8081
name: http-auth
protocol: TCP
resources:
{{ toYaml .Values.grafanadiResources | indent 10 }}
volumeMounts:
- mountPath: {{ .Values.grafanadiLogsPath }}
name: logs
- mountPath: /var/grafana
name: grafana-pv
- mountPath: /app/config-file.yaml
- mountPath: /app
name: cm-vol
readOnly: true
subPath: config-file.yaml
volumes:
- name: grafana-pv
hostPath:
Expand Down
14 changes: 11 additions & 3 deletions deploy/helm/lunettes/templates/grafanadi/grafanadi-svc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ metadata:
namespace: {{ .Values.namespace }}
spec:
ports:
- port: 8080
- name: common-port
port: 8080
protocol: TCP
targetPort: http-grafanadi
{{- if eq .Values.grafanadiType "NodePort" }}
{{- if eq .Values.grafanadiType "NodePort" }}
nodePort: {{ .Values.grafanadiNodePort }}
{{- end }}
{{- end }}
- name: auth-port
port: 8081
protocol: TCP
targetPort: http-auth
{{- if eq .Values.grafanadiType "NodePort" }}
nodePort: {{ .Values.grafanadiAuthNodePort }}
{{- end }}
selector:
app: grafanadi
type: {{ .Values.grafanadiType }}
1 change: 1 addition & 0 deletions deploy/helm/lunettes/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ lunettesResources:
grafanadiInitKubeClient: false
grafanadiType: LoadBalancer
grafanadiNodePort: 30280
grafanadiAuthNodePort: 30281
grafanadiLogsPath: /logs
grafanadiLogsHostPath: /home/var/logs/grafanadi
grafanadiResources:
Expand Down
105 changes: 105 additions & 0 deletions internal/grafanadi/handler/debugpodlist_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package handler

import (
"fmt"
"net/http"
"time"

"github.com/alipay/container-observability-service/internal/grafanadi/service"
"github.com/alipay/container-observability-service/pkg/dal/storage-client/data_access"
"github.com/alipay/container-observability-service/pkg/dal/storage-client/model"
"github.com/alipay/container-observability-service/pkg/metrics"
"github.com/alipay/container-observability-service/pkg/utils"
)

type DebugPodListHandler struct {
request *http.Request
writer http.ResponseWriter
requestParams *DebugPodListParams
storage data_access.StorageInterface
}

type DebugPodListParams struct {
SearchKey string
SearchValue string
}

func (handler *DebugPodListHandler) RequestParams() interface{} {
return handler.requestParams
}

func (handler *DebugPodListHandler) ParseRequest() error {
params := DebugPodListParams{}
if handler.request.Method == http.MethodGet {
key := handler.request.URL.Query().Get("searchkey")
value := handler.request.URL.Query().Get("searchvalue")
params.SearchKey = key
params.SearchValue = value
}

handler.requestParams = &params
return nil
}

func (handler *DebugPodListHandler) ValidRequest() error {

reqParam := handler.requestParams
switch reqParam.SearchKey {
case "uid", "name", "hostname", "podip":
return nil
default:
return fmt.Errorf("uid, name, podip, hostname are all empty")
}
}

func (handler *DebugPodListHandler) QueryDebugPodListWithPodUid(key, value string) (int, interface{}, error) {
podYamls := make([]*model.PodYaml, 0)
if value == "" {
return http.StatusOK, nil, nil
}

begin := time.Now()
defer func() {
cost := utils.TimeSinceInMilliSeconds(begin)
metrics.QueryMethodDurationMilliSeconds.WithLabelValues("QueryDebugPodListWithPodUid").Observe(cost)
}()

var err error
if key == "uid" {
err = handler.storage.QueryPodYamlsWithPodUID(&podYamls, value)
} else if key == "name" {
err = handler.storage.QueryPodYamlsWithPodName(&podYamls, value)
} else if key == "hostname" {
err = handler.storage.QueryPodYamlsWithHostName(&podYamls, value)
} else if key == "podip" {
err = handler.storage.QueryPodYamlsWithPodIp(&podYamls, value)
}

if err != nil {
return http.StatusOK, nil, fmt.Errorf("QueryPodYamlsWithPodUID error, error is %s", err)
}

tables := service.ConvertPodYamls2Table(podYamls)
return http.StatusOK, tables, nil

}

func (handler *DebugPodListHandler) Process() (int, interface{}, error) {
defer utils.IgnorePanic("DebugPodListHandler.Process ")

var result interface{}
var err error
var httpStatus int

httpStatus, result, err = handler.QueryDebugPodListWithPodUid(handler.requestParams.SearchKey, handler.requestParams.SearchValue)

return httpStatus, result, err
}

func DebugPodListFactory(w http.ResponseWriter, r *http.Request, storage data_access.StorageInterface) Handler {
return &DebugPodListHandler{
request: r,
writer: w,
storage: storage,
}
}
Loading
Loading