github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/storage.go (about) 1 package storage 2 3 import ( 4 "log" 5 "time" 6 7 "github.com/gocraft/dbr" 8 "github.com/sirupsen/logrus" 9 10 eventsapi "github.com/kyma-project/kyma-environment-broker/common/events" 11 "github.com/kyma-project/kyma-environment-broker/internal/events" 12 "github.com/kyma-project/kyma-environment-broker/internal/storage/driver/memory" 13 postgres "github.com/kyma-project/kyma-environment-broker/internal/storage/driver/postsql" 14 eventstorage "github.com/kyma-project/kyma-environment-broker/internal/storage/driver/postsql/events" 15 "github.com/kyma-project/kyma-environment-broker/internal/storage/postsql" 16 ) 17 18 type BrokerStorage interface { 19 Instances() Instances 20 Operations() Operations 21 Provisioning() Provisioning 22 Deprovisioning() Deprovisioning 23 Orchestrations() Orchestrations 24 RuntimeStates() RuntimeStates 25 Events() Events 26 } 27 28 const ( 29 connectionRetries = 10 30 ) 31 32 func NewFromConfig(cfg Config, evcfg events.Config, cipher postgres.Cipher, log logrus.FieldLogger) (BrokerStorage, *dbr.Connection, error) { 33 log.Infof("Setting DB connection pool params: connectionMaxLifetime=%s "+ 34 "maxIdleConnections=%d maxOpenConnections=%d", cfg.ConnMaxLifetime, cfg.MaxIdleConns, cfg.MaxOpenConns) 35 36 connection, err := postsql.InitializeDatabase(cfg.ConnectionURL(), connectionRetries, log) 37 if err != nil { 38 return nil, nil, err 39 } 40 41 connection.SetConnMaxLifetime(cfg.ConnMaxLifetime) 42 connection.SetMaxIdleConns(cfg.MaxIdleConns) 43 connection.SetMaxOpenConns(cfg.MaxOpenConns) 44 45 fact := postsql.NewFactory(connection) 46 47 operation := postgres.NewOperation(fact, cipher) 48 return storage{ 49 instance: postgres.NewInstance(fact, operation, cipher), 50 operation: operation, 51 orchestrations: postgres.NewOrchestrations(fact), 52 runtimeStates: postgres.NewRuntimeStates(fact, cipher), 53 events: events.New(evcfg, eventstorage.New(fact, log)), 54 }, connection, nil 55 } 56 57 func NewMemoryStorage() BrokerStorage { 58 op := memory.NewOperation() 59 return storage{ 60 operation: op, 61 instance: memory.NewInstance(op), 62 orchestrations: memory.NewOrchestrations(), 63 runtimeStates: memory.NewRuntimeStates(), 64 events: events.New(events.Config{}, NewInMemoryEvents()), 65 } 66 } 67 68 type inMemoryEvents struct { 69 events []eventsapi.EventDTO 70 } 71 72 func NewInMemoryEvents() *inMemoryEvents { 73 return &inMemoryEvents{ 74 events: make([]eventsapi.EventDTO, 0), 75 } 76 } 77 78 func (_ inMemoryEvents) RunGarbageCollection(pollingPeriod, retention time.Duration) { 79 return 80 } 81 82 func (e *inMemoryEvents) InsertEvent(eventLevel eventsapi.EventLevel, message, instanceID, operationID string) { 83 e.events = append(e.events, eventsapi.EventDTO{Level: eventLevel, InstanceID: &instanceID, OperationID: &operationID, Message: message}) 84 log.Printf("EVENT [%v/%v] %v: %v\n", instanceID, operationID, eventLevel, message) 85 } 86 87 func (e *inMemoryEvents) ListEvents(filter eventsapi.EventFilter) ([]eventsapi.EventDTO, error) { 88 var events []eventsapi.EventDTO 89 for _, ev := range e.events { 90 if !requiredContains(ev.InstanceID, filter.InstanceIDs) { 91 continue 92 } 93 if !requiredContains(ev.OperationID, filter.OperationIDs) { 94 continue 95 } 96 events = append(events, ev) 97 } 98 return events, nil 99 } 100 101 func requiredContains[T comparable](el *T, sl []T) bool { 102 if len(sl) == 0 { 103 return true 104 } 105 if el == nil { 106 return false 107 } 108 for _, x := range sl { 109 if *el == x { 110 return true 111 } 112 } 113 return false 114 } 115 116 type storage struct { 117 instance Instances 118 operation Operations 119 orchestrations Orchestrations 120 runtimeStates RuntimeStates 121 events Events 122 } 123 124 func (s storage) Instances() Instances { 125 return s.instance 126 } 127 128 func (s storage) Operations() Operations { 129 return s.operation 130 } 131 132 func (s storage) Provisioning() Provisioning { 133 return s.operation 134 } 135 136 func (s storage) Deprovisioning() Deprovisioning { 137 return s.operation 138 } 139 140 func (s storage) Orchestrations() Orchestrations { 141 return s.orchestrations 142 } 143 144 func (s storage) RuntimeStates() RuntimeStates { 145 return s.runtimeStates 146 } 147 148 func (s storage) Events() Events { 149 return s.events 150 }