clustermanager.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Call a handler to receive each key and value stored
  33. Each(handler func(string, []byte)) error
  34. // Closes the backing storage
  35. Close() error
  36. }
  37. type ClusterManager struct {
  38. storage ClusterStorage
  39. // cache map[string]*ClusterDefinition
  40. }
  41. // Creates a new ClusterManager instance using the provided storage
  42. func NewClusterManager(storage ClusterStorage) *ClusterManager {
  43. return &ClusterManager{
  44. storage: storage,
  45. }
  46. }
  47. // Creates a new ClusterManager instance using the provided storage and populates a
  48. // yaml configured list of clusters
  49. func NewConfiguredClusterManager(storage ClusterStorage, config string) *ClusterManager {
  50. clusterManager := NewClusterManager(storage)
  51. exists, err := util.FileExists(config)
  52. if !exists {
  53. if err != nil {
  54. klog.V(1).Infof("[Error] Failed to load config file: %s. Error: %s", config, err.Error())
  55. }
  56. return clusterManager
  57. }
  58. data, err := ioutil.ReadFile(config)
  59. if err != nil {
  60. return clusterManager
  61. }
  62. var entries []ClusterConfigEntry
  63. err = yaml.Unmarshal(data, &entries)
  64. if err != nil {
  65. return clusterManager
  66. }
  67. for _, entry := range entries {
  68. clusterManager.Add(ClusterDefinition{
  69. ID: entry.Name,
  70. Name: entry.Name,
  71. Address: entry.Address,
  72. })
  73. }
  74. return clusterManager
  75. }
  76. // Adds, but will not update an existing entry.
  77. func (cm *ClusterManager) Add(cluster ClusterDefinition) (*ClusterDefinition, error) {
  78. // First time add
  79. if cluster.ID == "" {
  80. cluster.ID = uuid.New().String()
  81. }
  82. data, err := json.Marshal(cluster)
  83. if err != nil {
  84. return nil, err
  85. }
  86. err = cm.storage.AddIfNotExists(cluster.ID, data)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &cluster, nil
  91. }
  92. func (cm *ClusterManager) AddOrUpdate(cluster ClusterDefinition) (*ClusterDefinition, error) {
  93. // First time add
  94. if cluster.ID == "" {
  95. cluster.ID = uuid.New().String()
  96. }
  97. data, err := json.Marshal(cluster)
  98. if err != nil {
  99. return nil, err
  100. }
  101. err = cm.storage.AddOrUpdate(cluster.ID, data)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return &cluster, nil
  106. }
  107. func (cm *ClusterManager) Remove(id string) error {
  108. return cm.storage.Remove(id)
  109. }
  110. func (cm *ClusterManager) GetAll() []*ClusterDefinition {
  111. clusters := []*ClusterDefinition{}
  112. err := cm.storage.Each(func(key string, cluster []byte) {
  113. var cd ClusterDefinition
  114. err := json.Unmarshal(cluster, &cd)
  115. if err != nil {
  116. klog.V(1).Infof("[Error] Failed to unmarshal json cluster definition for key: %s", key)
  117. return
  118. }
  119. clusters = append(clusters, &cd)
  120. })
  121. if err != nil {
  122. klog.Infof("[Error] Failed to load list of clusters: %s", err.Error())
  123. }
  124. return clusters
  125. }
  126. func (cm *ClusterManager) Close() error {
  127. return cm.storage.Close()
  128. }