clustermanager.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package clustermanager
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "github.com/google/uuid"
  6. "github.com/kubecost/cost-model/pkg/util"
  7. "k8s.io/klog"
  8. "sigs.k8s.io/yaml"
  9. )
  10. // Cluster definition from a configuration yaml
  11. type ClusterConfigEntry struct {
  12. Name string `yaml:"name"`
  13. Address string `yaml:"address"`
  14. }
  15. // ClusterDefinition
  16. type ClusterDefinition struct {
  17. ID string `json:"id,omitempty"`
  18. Name string `json:"name"`
  19. Address string `json:"address"`
  20. Details map[string]interface{} `json:"details,omitempty"`
  21. }
  22. // ClusterStorage interface defines an implementation prototype for a storage responsible
  23. // for ClusterDefinition instances
  24. type ClusterStorage interface {
  25. // Add only if the key does not exist
  26. AddIfNotExists(key string, cluster []byte) error
  27. // Adds the encoded cluster to storage if it doesn't exist. Otherwise, update the existing
  28. // value with the provided.
  29. AddOrUpdate(key string, cluster []byte) error
  30. // Removes a key from the cluster storage
  31. Remove(key string) error
  32. // Iterates through all key/values for the storage and calls the handler func. If a handler returns
  33. // an error, the iteration stops.
  34. Each(handler func(string, []byte) error) error
  35. // Closes the backing storage
  36. Close() error
  37. }
  38. type ClusterManager struct {
  39. storage ClusterStorage
  40. // cache map[string]*ClusterDefinition
  41. }
  42. // Creates a new ClusterManager instance using the provided storage
  43. func NewClusterManager(storage ClusterStorage) *ClusterManager {
  44. return &ClusterManager{
  45. storage: storage,
  46. }
  47. }
  48. // Creates a new ClusterManager instance using the provided storage and populates a
  49. // yaml configured list of clusters
  50. func NewConfiguredClusterManager(storage ClusterStorage, config string) *ClusterManager {
  51. clusterManager := NewClusterManager(storage)
  52. exists, err := util.FileExists(config)
  53. if !exists {
  54. if err != nil {
  55. klog.V(1).Infof("[Error] Failed to load config file: %s. Error: %s", config, err.Error())
  56. }
  57. return clusterManager
  58. }
  59. data, err := ioutil.ReadFile(config)
  60. if err != nil {
  61. return clusterManager
  62. }
  63. var entries []ClusterConfigEntry
  64. err = yaml.Unmarshal(data, &entries)
  65. if err != nil {
  66. return clusterManager
  67. }
  68. for _, entry := range entries {
  69. clusterManager.Add(ClusterDefinition{
  70. ID: entry.Name,
  71. Name: entry.Name,
  72. Address: entry.Address,
  73. })
  74. }
  75. return clusterManager
  76. }
  77. // Adds, but will not update an existing entry.
  78. func (cm *ClusterManager) Add(cluster ClusterDefinition) (*ClusterDefinition, error) {
  79. // First time add
  80. if cluster.ID == "" {
  81. cluster.ID = uuid.New().String()
  82. }
  83. data, err := json.Marshal(cluster)
  84. if err != nil {
  85. return nil, err
  86. }
  87. err = cm.storage.AddIfNotExists(cluster.ID, data)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &cluster, nil
  92. }
  93. func (cm *ClusterManager) AddOrUpdate(cluster ClusterDefinition) (*ClusterDefinition, error) {
  94. // First time add
  95. if cluster.ID == "" {
  96. cluster.ID = uuid.New().String()
  97. }
  98. data, err := json.Marshal(cluster)
  99. if err != nil {
  100. return nil, err
  101. }
  102. err = cm.storage.AddOrUpdate(cluster.ID, data)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return &cluster, nil
  107. }
  108. func (cm *ClusterManager) Remove(id string) error {
  109. return cm.storage.Remove(id)
  110. }
  111. func (cm *ClusterManager) GetAll() []*ClusterDefinition {
  112. clusters := []*ClusterDefinition{}
  113. err := cm.storage.Each(func(key string, cluster []byte) error {
  114. var cd ClusterDefinition
  115. err := json.Unmarshal(cluster, &cd)
  116. if err != nil {
  117. klog.V(1).Infof("[Error] Failed to unmarshal json cluster definition for key: %s", key)
  118. return nil
  119. }
  120. clusters = append(clusters, &cd)
  121. return nil
  122. })
  123. if err != nil {
  124. klog.Infof("[Error] Failed to load list of clusters: %s", err.Error())
  125. }
  126. return clusters
  127. }
  128. func (cm *ClusterManager) Close() error {
  129. return cm.storage.Close()
  130. }