storefactory.go 861 B

12345678910111213141516171819202122232425262728293031
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/opencost/opencost/core/pkg/env"
  6. )
  7. // GetDefaultStorage initializes the default shared storage which is required for kubecost
  8. func GetDefaultStorage() Storage {
  9. store, err := InitializeStorage(env.GetDefaultStorageConfigFilePath())
  10. if err != nil {
  11. panic(fmt.Sprintf("failed to initialize default storage: %s", err.Error()))
  12. }
  13. return store
  14. }
  15. // InitializeStorage creates a storage from the config file at the given path
  16. func InitializeStorage(configPath string) (Storage, error) {
  17. storageConfig, err := os.ReadFile(configPath)
  18. if err != nil {
  19. return nil, fmt.Errorf("failed to read file '%s': %w", configPath, err)
  20. }
  21. store, err := NewBucketStorage(storageConfig)
  22. if err != nil {
  23. return nil, fmt.Errorf("failed to create storage from config '%s': %w", configPath, err)
  24. }
  25. return store, nil
  26. }