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

Implement MongoDB store #4

Merged
merged 4 commits into from
Sep 16, 2024
Merged
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
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Configuration struct {
Type string `mapstructure:"type"`
Redis RedisConfiguration `mapstructure:"redis"`
Hazelcast HazelcastConfiguration `mapstructure:"hazelcast"`
Mongo MongoConfiguration `mapstructure:"mongo"`
} `mapstructure:"store"`
Fallback struct {
Type string `mapstructure:"type"`
Expand Down
3 changes: 3 additions & 0 deletions internal/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func setDefaults() {
viper.SetDefault("store.hazelcast.password", "")
viper.SetDefault("store.hazelcast.writeBehind", true)

viper.SetDefault("store.mongo.uri", "mongodb://localhost:27017")
viper.SetDefault("store.mongo.database", "horizon")

viper.SetDefault("resources", []ResourceConfiguration{})

viper.SetDefault("fallback.type", "mongo")
Expand Down
4 changes: 4 additions & 0 deletions internal/fallback/errors.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Deutsche Telekom IT GmbH
//
// SPDX-License-Identifier: Apache-2.0

package fallback

import "errors"
Expand Down
4 changes: 4 additions & 0 deletions internal/fallback/fallback.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Deutsche Telekom IT GmbH
//
// SPDX-License-Identifier: Apache-2.0

package fallback

import (
Expand Down
4 changes: 4 additions & 0 deletions internal/fallback/mongo.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Deutsche Telekom IT GmbH
//
// SPDX-License-Identifier: Apache-2.0

package fallback

import (
Expand Down
93 changes: 93 additions & 0 deletions internal/store/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 Deutsche Telekom IT GmbH
//
// SPDX-License-Identifier: Apache-2.0

package store

import (
"context"
"github.com/rs/zerolog/log"
"github.com/telekom/quasar/internal/config"
"github.com/telekom/quasar/internal/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type MongoStore struct {
client *mongo.Client
}

func (m *MongoStore) Initialize() {
var err error
m.client, err = mongo.Connect(context.Background(), options.Client().ApplyURI(config.Current.Store.Mongo.Uri))
if err != nil {
log.Fatal().Err(err).Msg("Could not create mongo-store")
}

if err := m.client.Ping(context.Background(), nil); err != nil {
log.Fatal().Err(err).Msg("Could not reach mongodb")
}

}

func (m *MongoStore) InitializeResource(resourceConfig *config.ResourceConfiguration) {
for _, index := range resourceConfig.MongoIndexes {
var model = index.ToIndexModel()
var collection = m.client.Database(config.Current.Fallback.Mongo.Database).Collection(resourceConfig.GetCacheName())
_, err := collection.Indexes().CreateOne(context.Background(), model)
if err != nil {
var resource = resourceConfig.GetGroupVersionResource()
log.Warn().Fields(utils.CreateFieldForResource(&resource)).Err(err).Msg("Could not create index in MongoDB")
}
}
}

func (m *MongoStore) OnAdd(obj *unstructured.Unstructured) {
var filter = bson.M{"_id": string(obj.GetUID())}
var collection = m.getCollection(obj)

_, err := collection.ReplaceOne(context.Background(), filter, obj.Object, options.Replace().SetUpsert(true))
if err != nil {
log.Warn().Fields(map[string]any{
"_id": obj.GetUID(),
}).Err(err).Msg("Could not add object to MongoDB")
}

log.Debug().Fields(utils.CreateFieldsForOp("add", obj)).Msg("Object added to MongoDB")
}

func (m *MongoStore) OnUpdate(oldObj *unstructured.Unstructured, newObj *unstructured.Unstructured) {
var filter = bson.M{"_id": string(oldObj.GetUID())}
var collection = m.getCollection(oldObj)

_, err := collection.ReplaceOne(context.Background(), filter, newObj.Object, options.Replace().SetUpsert(true))
if err != nil {
log.Warn().Fields(map[string]any{
"_id": newObj.GetUID(),
}).Err(err).Msg("Could not add object to MongoDB")
}

log.Debug().Fields(utils.CreateFieldsForOp("add", newObj)).Msg("Object updated in MongoDB")
}

func (m *MongoStore) OnDelete(obj *unstructured.Unstructured) {
var filter = bson.M{"_id": string(obj.GetUID())}

_, err := m.getCollection(obj).DeleteOne(context.Background(), filter)
if err != nil {
log.Warn().Fields(map[string]any{
"_id": obj.GetUID(),
}).Err(err).Msg("Could not delete object from MongoDB")
return
}
}

func (m *MongoStore) getCollection(obj *unstructured.Unstructured) *mongo.Collection {
return m.client.Database(config.Current.Store.Mongo.Database).Collection(utils.GetGroupVersionId(obj))
}

func (m *MongoStore) Shutdown() {
_ = m.client.Disconnect(context.TODO())
}
3 changes: 3 additions & 0 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func createStore(storeType string) (Store, error) {
case "hazelcast":
return new(HazelcastStore), nil

case "mongo":
return new(MongoStore), nil

default:
return nil, ErrUnknownStoreType

Expand Down