Skip to content

Commit 37cdab8

Browse files
authored
Merge pull request #167 from jpinsonneau/316_mocks
NETOBSERV-316 loki json mocks
2 parents 7b2a893 + f9c949c commit 37cdab8

File tree

20 files changed

+118
-17
lines changed

20 files changed

+118
-17
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN npm install [email protected] -g
66
RUN mkdir web && chown $USER: web
77
COPY Makefile Makefile
88
COPY web web
9+
COPY mocks mocks
910

1011
RUN NPM_INSTALL=ci make build-frontend
1112

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ IMG_USER ?= netobserv
22
TAG ?= dev
33
BUILD_VERSION := $(shell git describe --long HEAD)
44
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
5+
# You can add GO Build flags like -gcflags=all="-N -l" here to remove optimizations for debugging
6+
BUILD_FLAGS ?= -ldflags "-X 'main.buildVersion=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}'"
57
BUILD_SHA := $(shell git rev-parse --short HEAD)
68

79
BASE_IMAGE ?= quay.io/${IMG_USER}/network-observability-console-plugin
@@ -82,7 +84,7 @@ test: test-backend test-frontend
8284
.PHONY: build-backend
8385
build-backend: fmt-backend
8486
@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
87+
go build ${BUILD_FLAGS} -mod vendor -o plugin-backend cmd/plugin-backend.go
8688

8789
.PHONY: build-frontend
8890
build-frontend: install-frontend fmt-frontend
@@ -121,6 +123,10 @@ push:
121123
serve:
122124
./plugin-backend
123125

126+
.PHONY: serve-mock
127+
serve-mock:
128+
./plugin-backend --loki-mock --loglevel trace
129+
124130
.PHONY: start
125131
start: build-backend
126132
@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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/sirupsen/logrus"
1313

14+
"github.com/netobserv/network-observability-console-plugin/pkg/handler/lokiclientmock"
1415
"github.com/netobserv/network-observability-console-plugin/pkg/httpclient"
1516
"github.com/netobserv/network-observability-console-plugin/pkg/loki"
1617
"github.com/netobserv/network-observability-console-plugin/pkg/metrics"
@@ -30,6 +31,12 @@ func newLokiClient(cfg *loki.Config) httpclient.Caller {
3031
lokiOrgIDHeader: {cfg.TenantID},
3132
}
3233
}
34+
35+
if cfg.UseMocks {
36+
hlog.Debug("Mocking Loki Client")
37+
return new(lokiclientmock.LokiClientMock)
38+
}
39+
3340
// TODO: loki with auth
3441
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS)
3542
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package lokiclientmock
2+
3+
import (
4+
"io/ioutil"
5+
"strings"
6+
)
7+
8+
type LokiClientMock struct {
9+
}
10+
11+
func (o *LokiClientMock) 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)