Skip to content

Commit 83665c2

Browse files
committed
loki json mocks
1 parent 07ebba8 commit 83665c2

File tree

20 files changed

+116
-19
lines changed

20 files changed

+116
-19
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ test: test-backend test-frontend
8282
.PHONY: build-backend
8383
build-backend: fmt-backend
8484
@echo "### Building backend"
85-
go build -ldflags "-X 'main.buildVersion=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}'" -mod vendor -o plugin-backend cmd/plugin-backend.go
85+
go build -gcflags='-N -l' -ldflags "-X 'main.buildVersion=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}'" -mod vendor -o plugin-backend cmd/plugin-backend.go
8686

8787
.PHONY: build-frontend
8888
build-frontend: install-frontend fmt-frontend
@@ -121,6 +121,10 @@ push:
121121
serve:
122122
./plugin-backend
123123

124+
.PHONY: serve-mock
125+
serve-mock:
126+
./plugin-backend --loki-mock --loglevel trace
127+
124128
.PHONY: start
125129
start: build-backend
126130
@echo "### Starting backend on http://localhost:9002"

cmd/plugin-backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
lokiTimeout = flag.Duration("loki-timeout", 10*time.Second, "Timeout of the Loki query to retrieve logs")
3232
lokiTenantID = flag.String("loki-tenant-id", "", "Tenant organization ID for multi-tenant-loki (submitted as the X-Scope-OrgID HTTP header)")
3333
lokiSkipTLS = flag.Bool("loki-skip-tls", false, "Skip TLS checks for loki HTTPS connection")
34+
lokiMock = flag.Bool("loki-mock", false, "Fake loki results using saved mocks")
3435
logLevel = flag.String("loglevel", "info", "log level (default: info)")
3536
frontendConfig = flag.String("frontend-config", "", "path to the console plugin config file")
3637
versionFlag = flag.Bool("v", false, "print version")
@@ -73,7 +74,7 @@ func main() {
7374
CORSAllowMethods: *corsMethods,
7475
CORSAllowHeaders: *corsHeaders,
7576
CORSMaxAge: *corsMaxAge,
76-
Loki: loki.NewConfig(lURL, *lokiTimeout, *lokiTenantID, *lokiSkipTLS, strings.Split(lLabels, ",")),
77+
Loki: loki.NewConfig(lURL, *lokiTimeout, *lokiTenantID, *lokiSkipTLS, *lokiMock, strings.Split(lLabels, ",")),
7778
FrontendConfig: *frontendConfig,
7879
})
7980
}

mocks/loki/flows.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

mocks/loki/namespaces.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"status":"success","data":["","default","network-observability","openshift-apiserver","openshift-apiserver-operator","openshift-authentication","openshift-authentication-operator","openshift-cloud-credential-operator","openshift-cloud-network-config-controller","openshift-cluster-csi-drivers","openshift-cluster-node-tuning-operator","openshift-cluster-samples-operator","openshift-cluster-storage-operator","openshift-config-operator","openshift-console","openshift-console-operator","openshift-controller-manager","openshift-controller-manager-operator","openshift-dns","openshift-dns-operator","openshift-etcd-operator","openshift-image-registry","openshift-ingress","openshift-ingress-canary","openshift-ingress-operator","openshift-insights","openshift-kube-apiserver","openshift-kube-apiserver-operator","openshift-kube-controller-manager-operator","openshift-kube-scheduler-operator","openshift-kube-storage-version-migrator","openshift-kube-storage-version-migrator-operator","openshift-machine-api","openshift-machine-config-operator","openshift-marketplace","openshift-monitoring","openshift-multus","openshift-network-diagnostics","openshift-oauth-apiserver","openshift-operator-lifecycle-manager","openshift-service-ca","openshift-service-ca-operator"]}

mocks/loki/topology.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

mocks/updateMocks.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This shell script allow you to update fake loki results from a running instance.
2+
# Simply run your loki on http://localhost:3100
3+
# or port forward it using 'oc port-forward service/loki 3100:3100 -n network-observability'
4+
5+
# flows.json contains query result for table display
6+
curl -o ./loki/flows.json 'http://localhost:3100/loki/api/v1/query_range?query=\{app=%22netobserv-flowcollector%22,FlowDirection=%221%22\}&limit=100' \
7+
8+
# topology.json contains query result for topology display
9+
curl -o ./loki/topology.json 'http://localhost:3100/loki/api/v1/query_range?query=topk(100,sum%20by(SrcK8S_Name,SrcK8S_Type,SrcK8S_OwnerName,SrcK8S_OwnerType,SrcK8S_Namespace,SrcAddr,SrcK8S_HostName,DstK8S_Name,DstK8S_Type,DstK8S_OwnerName,DstK8S_OwnerType,DstK8S_Namespace,DstAddr,DstK8S_HostName)%20(sum_over_time(\{app=%22netobserv-flowcollector%22,FlowDirection=%220%22\}|json|unwrap%20Bytes|__error__=%22%22\[300s\])))&limit=100&step=60s' \
10+
11+
# namespaces.json contains label values for autocomplete
12+
curl -o ./loki/namespaces.json 'http://localhost:3100/loki/api/v1/label/SrcK8S_Namespace/values'

pkg/handler/loki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func newLokiClient(cfg *loki.Config) httpclient.Caller {
3131
}
3232
}
3333
// TODO: loki with auth
34-
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS)
34+
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS, cfg.UseMocks)
3535
}
3636

3737
/* loki query will fail if spaces or quotes are not encoded

pkg/httpclient/http_client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"time"
99

10+
"github.com/netobserv/network-observability-console-plugin/pkg/httpclient/httpclienttest"
1011
"github.com/sirupsen/logrus"
1112
)
1213

@@ -22,7 +23,7 @@ type httpClient struct {
2223

2324
var slog = logrus.WithField("module", "server")
2425

25-
func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, skipTLS bool) Caller {
26+
func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, skipTLS bool, useMocks bool) Caller {
2627
transport := &http.Transport{
2728
DialContext: (&net.Dialer{Timeout: timeout}).DialContext,
2829
IdleConnTimeout: timeout,
@@ -34,6 +35,11 @@ func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, s
3435
slog.Warn("skipping TLS checks. SSL certificate verification is now disabled !")
3536
}
3637

38+
if useMocks {
39+
slog.Debug("Mocking HTTP Client")
40+
return new(httpclienttest.HTTPClientJSONMock)
41+
}
42+
3743
return &httpClient{
3844
client: http.Client{Transport: transport, Timeout: timeout},
3945
headers: overrideHeaders,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package httpclienttest
2+
3+
import (
4+
"io/ioutil"
5+
"strings"
6+
)
7+
8+
type HTTPClientJSONMock struct {
9+
}
10+
11+
func (o *HTTPClientJSONMock) Get(url string) ([]byte, int, error) {
12+
var path string
13+
14+
isLabel := strings.Contains(url, "/label/")
15+
if isLabel {
16+
path = "mocks/loki/namespaces.json"
17+
} else {
18+
isTopology := strings.Contains(url, "query=topk")
19+
if isTopology {
20+
path = "mocks/loki/topology.json"
21+
} else {
22+
path = "mocks/loki/flows.json"
23+
}
24+
}
25+
26+
file, err := ioutil.ReadFile(path)
27+
if err != nil {
28+
return nil, 500, err
29+
}
30+
31+
return []byte(file), 200, nil
32+
}

pkg/loki/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ type Config struct {
1212
Timeout time.Duration
1313
TenantID string
1414
SkipTLS bool
15+
UseMocks bool
1516
Labels map[string]struct{}
1617
}
1718

18-
func NewConfig(url *url.URL, timeout time.Duration, tenantID string, skipTLS bool, labels []string) Config {
19+
func NewConfig(url *url.URL, timeout time.Duration, tenantID string, skipTLS bool, useMocks bool, labels []string) Config {
1920
return Config{
2021
URL: url,
2122
Timeout: timeout,
2223
TenantID: tenantID,
2324
SkipTLS: skipTLS,
25+
UseMocks: useMocks,
2426
Labels: utils.GetMapInterface(labels),
2527
}
2628
}

0 commit comments

Comments
 (0)