Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions common-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*.dylib
bin/*
Dockerfile.cross
data/*
*.tmp.yaml

# Test binary, built with `go test -c`
*.test
Expand Down
4 changes: 4 additions & 0 deletions common-server/helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: db-data
mountPath: /data
- name: app-config
mountPath: /etc/common-server
readOnly: true
Expand Down Expand Up @@ -84,6 +86,8 @@ spec:
{{- toYaml . | nindent 8 }}
{{- end }}
volumes:
- name: db-data
emptyDir: {}
- name: app-config
configMap:
name: {{ .Release.Name }}-config
Expand Down
6 changes: 6 additions & 0 deletions common-server/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type SecurityConfig struct {
type ServerConfig struct {
Address string `json:"address"`
BasePath string `json:"basepath"`
// Filepath is used to configure the database to use the filesystem.
// It is used as base path for the database files.
Filepath string `json:"filepath"`

AddGroupToPath bool `yaml:"addGroupToPath" json:"addGroupToPath"`
Resources []ResourceConfig `json:"resources"`
Expand Down Expand Up @@ -166,6 +169,9 @@ func (c *ServerConfig) BuildServer(ctx context.Context, dynamicClient dynamic.In
GVR: crd.GVR,
GVK: crd.GVK,
AllowedSorts: resource.AllowedSorts,
Database: inmemory.DatabaseOpts{
Filepath: c.Filepath,
},
}

resourceId := strings.ToLower(crd.GVR.Resource)
Expand Down
4 changes: 4 additions & 0 deletions common-server/internal/informer/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func SanitizeObject(obj *unstructured.Unstructured) {
}

delete(metadata, "managedFields")
annotations, ok := metadata["annotations"].(map[string]any)
if ok {
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
}
}

func wrapEventHandler(ctx context.Context, log logr.Logger, eh EventHandler) cache.ResourceEventHandlerFuncs {
Expand Down
37 changes: 33 additions & 4 deletions common-server/pkg/store/inmemory/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ package inmemory

import (
"context"
"fmt"
"path/filepath"
"strings"
"sync"

"github.com/bytedance/sonic"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/options"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/telekom/controlplane/common-server/internal/informer"
Expand All @@ -35,6 +39,12 @@ type StoreOpts struct {
GVR schema.GroupVersionResource
GVK schema.GroupVersionKind
AllowedSorts []string

Database DatabaseOpts
}

type DatabaseOpts struct {
Filepath string
}

type InmemoryObjectStore[T store.Object] struct {
Expand All @@ -49,9 +59,28 @@ type InmemoryObjectStore[T store.Object] struct {
sortValueCache sync.Map
}

func newDbOrDie(log logr.Logger) *badger.DB {
opts := badger.DefaultOptions("").WithInMemory(true)
opts.IndexCacheSize = 100 << 20
func newDbOrDie(storeOpts StoreOpts, log logr.Logger) *badger.DB {
useFilesystem := storeOpts.Database.Filepath != ""
path := ""
if useFilesystem {
dbName := fmt.Sprintf("db-%s-%s-%s", strings.ToLower(storeOpts.GVR.Group), strings.ToLower(storeOpts.GVR.Version), strings.ToLower(storeOpts.GVR.Resource))
path = filepath.Join(storeOpts.Database.Filepath, dbName)
}

log.Info("initializing badger DB", "inMemory", !useFilesystem, "path", path)

opts := badger.DefaultOptions(path).
WithInMemory(!useFilesystem).
WithMetricsEnabled(false).
WithIndexCacheSize(0).
WithMemTableSize(32 << 20). // 32 MB
WithValueLogFileSize(64 << 20). // 64 MB
WithBlockCacheSize(64 << 20). // 64 MB
WithBlockSize(4 << 10). // 4 KB
WithValueThreshold(1 << 20). // 1 MB
WithBloomFalsePositive(0.01).
WithCompression(options.Snappy)

opts.Logger = NewLoggerShim(log)
db, err := badger.Open(opts)
if err != nil {
Expand All @@ -69,7 +98,7 @@ func NewOrDie[T store.Object](ctx context.Context, storeOpts StoreOpts) store.Ob
k8sClient: storeOpts.Client.Resource(storeOpts.GVR),
}
var err error
store.db = newDbOrDie(store.log)
store.db = newDbOrDie(storeOpts, store.log)
store.informer = informer.New(ctx, store.gvr, storeOpts.Client, store)

if err = store.informer.Start(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions common-server/pkg/store/inmemory/sorted_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("Sorted Store", func() {
sortedStore := Sortable(
&InmemoryObjectStore[*unstructured.Unstructured]{
ctx: ctx,
db: newDbOrDie(logr.Discard()),
db: newDbOrDie(StoreOpts{}, logr.Discard()),
log: logr.Discard(),
},
StoreOpts{
Expand Down Expand Up @@ -136,7 +136,7 @@ func BenchmarkStore(b *testing.B) {
s := Sortable(
&InmemoryObjectStore[*unstructured.Unstructured]{
ctx: ctx,
db: newDbOrDie(logr.Discard()),
db: newDbOrDie(StoreOpts{}, logr.Discard()),
log: logr.Discard(),
},
StoreOpts{
Expand Down
4 changes: 2 additions & 2 deletions common-server/pkg/store/secrets/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *SecretManagerResolver) ReplaceAllFromBytes(ctx context.Context, b []byt
log.V(1).Info("Replacing secrets in array", "jsonPath", paths)
b, err = s.ReplaceAllFromBytes(ctx, b, paths)
if err != nil {
return nil, errors.Wrapf(err, "failed to replace all from bytes for json path %s", jsonPath)
return nil, errors.Wrapf(err, "failed to replace all from bytes for json path %q", jsonPath)
}
continue
}
Expand All @@ -124,7 +124,7 @@ func (s *SecretManagerResolver) ReplaceAllFromBytes(ctx context.Context, b []byt
log.V(1).Info("Replacing secret", "jsonPath", jsonPath, "secretRef", secretRef)
secretValue, err := s.M.Get(ctx, secretRef)
if err != nil {
return nil, errors.Wrap(err, "failed to get secret value")
return nil, errors.Wrapf(err, "failed to get secret value for reference %q", secretRef)
}

b, err = sjson.SetBytes(b, jsonPath, secretValue)
Expand Down
1 change: 1 addition & 0 deletions rover-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.dylib
bin/*
Dockerfile.cross
data

# Test binary, built with `go test -c`
*.test
Expand Down
1 change: 1 addition & 0 deletions rover-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The server can be configured using environment variables or configuration files:
- `SECURITY_TRUSTEDISSUERS`: Comma-separated list of trusted issuers for JWT validation
- `SECURITY_LMS_BASEPATH`: Base path for the LMS (Last Mile Security) checking
- `SECURITY_DEFAULTSCOPE`: Default scope if token does not contain one
- `DATABASE_FILEPATH`: This enables the database to store data also in the filesystem. If empty, the database will be in-memory only.

# Installation

Expand Down
1 change: 1 addition & 0 deletions rover-server/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ configMapGenerator:
literals:
- SECURITY_TRUSTEDISSUERS=""
- SECURITY_LMS_BASEPATH=""
- DATABASE_FILEPATH=""

patches:
# [SECRET_MANAGER] The following patch will add the secret manager to the deployment.
Expand Down
8 changes: 6 additions & 2 deletions rover-server/config/server/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ spec:
requests:
cpu: 10m
memory: 64Mi
volumeMounts: []
volumes: []
volumeMounts:
- name: db-data
mountPath: /data
volumes:
- name: db-data
emptyDir: {}
serviceAccountName: rover-server
terminationGracePeriodSeconds: 10
---
Expand Down
2 changes: 1 addition & 1 deletion rover-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.11.1
go.uber.org/zap v1.27.0
go.yaml.in/yaml/v4 v4.0.0-rc.2
golang.org/x/text v0.29.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.34.1
Expand Down Expand Up @@ -162,7 +163,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
go.yaml.in/yaml/v4 v4.0.0-rc.2 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/mod v0.27.0 // indirect
Expand Down
5 changes: 4 additions & 1 deletion rover-server/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func setDefaults() {
// LMS
viper.SetDefault("security.lms.basePath", "")

//FileManager
// FileManager
viper.SetDefault("fileManager.skipTLS", true)

// Database
viper.SetDefault("database.filepath", "") // empty string means in-memory only
}
6 changes: 5 additions & 1 deletion rover-server/pkg/store/stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package store
import (
"context"

"github.com/spf13/viper"
adminv1 "github.com/telekom/controlplane/admin/api/v1"
apiv1 "github.com/telekom/controlplane/api/api/v1"
applicationv1 "github.com/telekom/controlplane/application/api/v1"
Expand Down Expand Up @@ -64,10 +65,13 @@ var InitOrDie = func(ctx context.Context, cfg *rest.Config) {

func NewOrDie[T store.Object](ctx context.Context, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind) store.ObjectStore[T] {
storeOpts := inmemory.StoreOpts{
Client: dynamicClient,
GVR: gvr,
GVK: gvk,
AllowedSorts: []string{},
Client: dynamicClient,
Database: inmemory.DatabaseOpts{
Filepath: viper.GetString("database.filepath"),
},
}

return inmemory.NewSortableOrDie[T](ctx, storeOpts)
Expand Down
Loading