Skip to content

Commit cb700f8

Browse files
authored
Including code from beyla-k8s-meta repository (#1278)
1 parent c2f7c8f commit cb700f8

File tree

352 files changed

+68731
-2548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

352 files changed

+68731
-2548
lines changed

Makefile

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# Main binary configuration
22
CMD ?= beyla
33
MAIN_GO_FILE ?= cmd/$(CMD)/main.go
4+
5+
CACHE_CMD ?= k8s-cache
6+
CACHE_MAIN_GO_FILE ?= cmd/$(CACHE_CMD)/main.go
7+
48
GOOS ?= linux
59
GOARCH ?= amd64
610

11+
# todo: upload to a grafana artifact
12+
PROTOC_IMAGE = docker.io/mariomac/protoc-go:latest
13+
714
# RELEASE_VERSION will contain the tag name, or the branch name if current commit is not a tag
815
RELEASE_VERSION := $(shell git describe --all | cut -d/ -f2)
916
RELEASE_REVISION := $(shell git rev-parse --short HEAD )
@@ -33,7 +40,7 @@ CFLAGS := -O2 -g -Wall -Werror $(CFLAGS)
3340
CLANG_TIDY ?= clang-tidy
3441

3542
# regular expressions for excluded file patterns
36-
EXCLUDE_COVERAGE_FILES="(_bpfel.go)|(/pingserver/)|(/grafana/beyla/test/)|(integration/components)|(/grafana/beyla/docs/)|(/grafana/beyla/configs/)|(/grafana/beyla/examples/)"
43+
EXCLUDE_COVERAGE_FILES="(_bpfel.go)|(/pingserver/)|(/grafana/beyla/test/)|(integration/components)|(/grafana/beyla/docs/)|(/grafana/beyla/configs/)|(/grafana/beyla/examples/)|(.pb.go)"
3744

3845
.DEFAULT_GOAL := all
3946

@@ -90,6 +97,16 @@ KIND = $(TOOLS_DIR)/kind
9097
DASHBOARD_LINTER = $(TOOLS_DIR)/dashboard-linter
9198
GINKGO = $(TOOLS_DIR)/ginkgo
9299

100+
# Required for k8s-cache unit tests
101+
ENVTEST = $(TOOLS_DIR)/setup-envtest
102+
ENVTEST_K8S_VERSION = 1.30.0
103+
104+
# Setting SHELL to bash allows bash commands to be executed by recipes.
105+
# This is a requirement for 'setup-envtest.sh' in the test target.
106+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
107+
SHELL = /usr/bin/env bash -o pipefail
108+
.SHELLFLAGS = -ec
109+
93110
GOIMPORTS_REVISER_ARGS = -company-prefixes github.com/grafana -project-name github.com/grafana/beyla/
94111

95112
define check_format
@@ -117,6 +134,7 @@ prereqs: install-hooks
117134
$(call go-install-tool,$(GO_LICENSES),github.com/google/go-licenses,v1.6.0)
118135
$(call go-install-tool,$(KIND),sigs.k8s.io/kind,v0.20.0)
119136
$(call go-install-tool,$(DASHBOARD_LINTER),github.com/grafana/dashboard-linter,latest)
137+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,latest)
120138

121139
.PHONY: fmt
122140
fmt: prereqs
@@ -178,29 +196,36 @@ build: verify compile
178196
.PHONY: all
179197
all: generate build
180198

181-
.PHONY: compile
199+
.PHONY: compile compile-cache
182200
compile:
183-
@echo "### Compiling project"
201+
@echo "### Compiling Beyla"
184202
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -mod vendor -ldflags="-X '$(BUILDINFO_PKG).Version=$(RELEASE_VERSION)' -X '$(BUILDINFO_PKG).Revision=$(RELEASE_REVISION)'" -a -o bin/$(CMD) $(MAIN_GO_FILE)
203+
compile-cache:
204+
@echo "### Compiling Beyla K8s cache"
205+
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -mod vendor -ldflags="-X '$(BUILDINFO_PKG).Version=$(RELEASE_VERSION)' -X '$(BUILDINFO_PKG).Revision=$(RELEASE_REVISION)'" -a -o bin/$(CACHE_CMD) $(CACHE_MAIN_GO_FILE)
206+
185207

186208
.PHONY: dev
187209
dev: prereqs generate compile-for-coverage
188210

189211
# Generated binary can provide coverage stats according to https://go.dev/blog/integration-test-coverage
190-
.PHONY: compile-for-coverage
212+
.PHONY: compile-for-coverage compile-cache-for-coverage
191213
compile-for-coverage:
192214
@echo "### Compiling project to generate coverage profiles"
193215
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -mod vendor -cover -a -o bin/$(CMD) $(MAIN_GO_FILE)
216+
compile-cache-for-coverage:
217+
@echo "### Compiling K8s cache service to generate coverage profiles"
218+
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -mod vendor -cover -a -o bin/$(CACHE_CMD) $(CACHE_MAIN_GO_FILE)
194219

195220
.PHONY: test
196221
test:
197222
@echo "### Testing code"
198-
go test -race -mod vendor -a ./... -coverpkg=./... -coverprofile $(TEST_OUTPUT)/cover.all.txt
223+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -race -mod vendor -a ./... -coverpkg=./... -coverprofile $(TEST_OUTPUT)/cover.all.txt
199224

200225
.PHONY: test-privileged
201226
test-privileged:
202227
@echo "### Testing code with privileged tests enabled"
203-
PRIVILEGED_TESTS=true go test -race -mod vendor -a ./... -coverpkg=./... -coverprofile $(TEST_OUTPUT)/cover.all.txt
228+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" PRIVILEGED_TESTS=true go test -race -mod vendor -a ./... -coverpkg=./... -coverprofile $(TEST_OUTPUT)/cover.all.txt
204229

205230
.PHONY: cov-exclude-generated
206231
cov-exclude-generated:
@@ -350,3 +375,7 @@ check-ebpf-integrity: docker-generate
350375
.PHONY: clang-tidy
351376
clang-tidy:
352377
cd bpf && $(CLANG_TIDY) *.c *.h
378+
379+
.PHONY: protoc-gen
380+
protoc-gen:
381+
docker run --rm -v $(PWD):/work -w /work $(PROTOC_IMAGE) protoc --go_out=pkg/kubecache --go-grpc_out=pkg/kubecache proto/informer.proto

cmd/k8s-cache/main.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
"os/signal"
8+
"strconv"
9+
"syscall"
10+
"time"
11+
12+
"github.com/grafana/beyla/pkg/kubecache/meta"
13+
"github.com/grafana/beyla/pkg/kubecache/service"
14+
)
15+
16+
const defaultPort = 50055
17+
18+
// main code of te Kubernetes K8s informer's metadata cache service, when it runs as a separate service and not
19+
// as a library inside Beyla
20+
21+
func main() {
22+
// TODO: use buildinfo to print version
23+
// TODO: let configure logger
24+
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug})))
25+
26+
ic := service.InformersCache{
27+
Port: defaultPort,
28+
}
29+
portStr := os.Getenv("BEYLA_K8S_CACHE_PORT")
30+
if portStr != "" {
31+
var err error
32+
if ic.Port, err = strconv.Atoi(portStr); err != nil {
33+
slog.Error("invalid BEYLA_K8S_CACHE_PORT, using default port", "error", err)
34+
ic.Port = defaultPort
35+
}
36+
}
37+
38+
// Adding shutdown hook for graceful stop.
39+
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
40+
41+
if err := ic.Run(ctx,
42+
// TODO: make it configurable
43+
meta.WithResyncPeriod(30*time.Minute)); err != nil {
44+
slog.Error("starting informers' cache service", "error", err)
45+
os.Exit(-1)
46+
}
47+
slog.Info("service stopped. Exiting now")
48+
49+
if gc := os.Getenv("GOCOVERDIR"); gc != "" {
50+
slog.Info("Waiting 1s to collect coverage data...")
51+
time.Sleep(time.Second)
52+
}
53+
}

examples/k8s-meta-cache/app.yml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: beyla
5+
---
6+
apiVersion: rbac.authorization.k8s.io/v1
7+
kind: ClusterRole
8+
metadata:
9+
name: beyla
10+
rules:
11+
- apiGroups: [ "apps" ]
12+
resources: [ "replicasets" ]
13+
verbs: [ "list", "watch" ]
14+
- apiGroups: [ "" ]
15+
resources: [ "pods", "services", "nodes" ]
16+
verbs: [ "list", "watch" ]
17+
---
18+
apiVersion: rbac.authorization.k8s.io/v1
19+
kind: ClusterRoleBinding
20+
metadata:
21+
name: beyla
22+
subjects:
23+
- kind: ServiceAccount
24+
name: beyla
25+
namespace: default
26+
roleRef:
27+
apiGroup: rbac.authorization.k8s.io
28+
kind: ClusterRole
29+
name: beyla
30+
---
31+
kind: Deployment
32+
apiVersion: apps/v1
33+
metadata:
34+
name: beyla-cache
35+
spec:
36+
selector:
37+
matchLabels:
38+
instrumentation: beyla-cache
39+
template:
40+
metadata:
41+
labels:
42+
instrumentation: beyla-cache
43+
spec:
44+
serviceAccountName: beyla
45+
containers:
46+
- name: beyla-cache
47+
image: grafana/beyla-k8s-cache:latest
48+
ports:
49+
- containerPort: 8999
50+
protocol: TCP
51+
name: protobuf
52+
---
53+
kind: Service
54+
apiVersion: v1
55+
metadata:
56+
name: beyla-cache
57+
spec:
58+
selector:
59+
instrumentation: "beyla-cache"
60+
ports:
61+
- port: 8999
62+
protocol: TCP
63+
targetPort: protobuf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log/slog"
7+
"os"
8+
9+
"google.golang.org/grpc"
10+
"google.golang.org/grpc/credentials/insecure"
11+
12+
"github.com/grafana/beyla/pkg/kubecache/informer"
13+
)
14+
15+
const (
16+
address = "localhost:50055"
17+
)
18+
19+
// simple example program that shows how to connect a gRPC+protobuf client to the Kube API cache
20+
func main() {
21+
// Set up a connection to the server.
22+
conn, err := grpc.NewClient(address,
23+
grpc.WithTransportCredentials(insecure.NewCredentials()))
24+
25+
if err != nil {
26+
slog.Error("could not connect", "address", address, "error", err)
27+
os.Exit(-1)
28+
}
29+
client := informer.NewEventStreamServiceClient(conn)
30+
31+
// Subscribe to the event stream.
32+
stream, err := client.Subscribe(context.TODO(), &informer.SubscribeMessage{})
33+
if err != nil {
34+
slog.Error("could not subscribe", "error", err)
35+
_ = conn.Close()
36+
os.Exit(-1)
37+
}
38+
defer conn.Close()
39+
40+
// Receive and print messages.
41+
for {
42+
event, err := stream.Recv()
43+
if err != nil {
44+
slog.Error("receiving message", "error", err)
45+
break
46+
}
47+
fmt.Printf("Received event: %v\n", event)
48+
}
49+
}

go.mod

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/goccy/go-json v0.10.2
1414
github.com/google/uuid v1.6.0
1515
github.com/gorilla/mux v1.8.1
16-
github.com/grafana/beyla-k8s-cache v0.0.0-20241022104537-4c9302930749
1716
github.com/grafana/go-offsets-tracker v0.1.7
1817
github.com/hashicorp/golang-lru/v2 v2.0.7
1918
github.com/mariomac/guara v0.0.0-20230621100729-42bd7716e524
@@ -61,10 +60,12 @@ require (
6160
golang.org/x/net v0.28.0
6261
golang.org/x/sys v0.24.0
6362
google.golang.org/grpc v1.67.1
64-
google.golang.org/protobuf v1.34.2
63+
google.golang.org/protobuf v1.35.1
6564
gopkg.in/yaml.v3 v3.0.1
66-
k8s.io/apimachinery v0.31.1
67-
k8s.io/client-go v0.31.1
65+
k8s.io/api v0.31.2
66+
k8s.io/apimachinery v0.31.2
67+
k8s.io/client-go v0.31.2
68+
sigs.k8s.io/controller-runtime v0.19.1
6869
sigs.k8s.io/e2e-framework v0.3.0
6970
)
7071

@@ -89,7 +90,7 @@ require (
8990
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
9091
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
9192
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
92-
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
93+
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
9394
github.com/felixge/httpsnoop v1.0.4 // indirect
9495
github.com/fsnotify/fsnotify v1.7.0 // indirect
9596
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
@@ -105,6 +106,7 @@ require (
105106
github.com/go-playground/validator/v10 v10.14.0 // indirect
106107
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
107108
github.com/gogo/protobuf v1.3.2 // indirect
109+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
108110
github.com/golang/protobuf v1.5.4 // indirect
109111
github.com/golang/snappy v0.0.4 // indirect
110112
github.com/google/gnostic-models v0.6.8 // indirect
@@ -164,20 +166,20 @@ require (
164166
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
165167
go.uber.org/multierr v1.11.0 // indirect
166168
golang.org/x/crypto v0.26.0 // indirect
167-
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect
169+
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
168170
golang.org/x/oauth2 v0.22.0 // indirect
169171
golang.org/x/term v0.23.0 // indirect
170172
golang.org/x/text v0.17.0 // indirect
171173
golang.org/x/time v0.5.0 // indirect
174+
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
172175
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
173176
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
174177
gopkg.in/inf.v0 v0.9.1 // indirect
175178
gopkg.in/yaml.v2 v2.4.0 // indirect
176-
k8s.io/api v0.31.1 // indirect
179+
k8s.io/apiextensions-apiserver v0.31.0 // indirect
177180
k8s.io/klog/v2 v2.130.1 // indirect
178181
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b // indirect
179182
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
180-
sigs.k8s.io/controller-runtime v0.15.1 // indirect
181183
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
182184
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
183185
sigs.k8s.io/yaml v1.4.0 // indirect

0 commit comments

Comments
 (0)