clusterinfo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package clusters
  2. import "maps"
  3. // The following constants are used as keys into the cluster info map data structure
  4. const (
  5. ClusterInfoIdKey = "id"
  6. ClusterInfoNameKey = "name"
  7. ClusterInfoProviderKey = "provider"
  8. ClusterInfoProjectKey = "project"
  9. ClusterInfoAccountKey = "account"
  10. ClusterInfoRegionKey = "region"
  11. ClusterInfoProvisionerKey = "provisioner"
  12. ClusterInfoProfileKey = "clusterProfile"
  13. ClusterInfoLogCollectionKey = "logCollection"
  14. ClusterInfoProductAnalyticsKey = "productAnalytics"
  15. ClusterInfoErrorReportingKey = "errorReporting"
  16. ClusterInfoValuesReportingKey = "valuesReporting"
  17. ClusterInfoThanosEnabledKey = "thanosEnabled"
  18. ClusterInfoThanosOffsetKey = "thanosOffset"
  19. ClusterInfoVersionKey = "version"
  20. )
  21. // ClusterInfo holds attributes of Cluster from metrics pulled from Prometheus
  22. type ClusterInfo struct {
  23. ID string `json:"id"`
  24. Name string `json:"name"`
  25. Profile string `json:"profile"`
  26. Provider string `json:"provider"`
  27. Account string `json:"account"`
  28. Project string `json:"project"`
  29. Region string `json:"region"`
  30. Provisioner string `json:"provisioner"`
  31. Version string `json:"version"`
  32. }
  33. // Clone creates a copy of ClusterInfo and returns it
  34. func (ci *ClusterInfo) Clone() *ClusterInfo {
  35. if ci == nil {
  36. return nil
  37. }
  38. return &ClusterInfo{
  39. ID: ci.ID,
  40. Name: ci.Name,
  41. Profile: ci.Profile,
  42. Provider: ci.Provider,
  43. Account: ci.Account,
  44. Project: ci.Project,
  45. Region: ci.Region,
  46. Provisioner: ci.Provisioner,
  47. Version: ci.Version,
  48. }
  49. }
  50. type ClusterMap interface {
  51. // GetClusterIDs returns a slice containing all of the cluster identifiers.
  52. GetClusterIDs() []string
  53. // AsMap returns the cluster map as a standard go map
  54. AsMap() map[string]*ClusterInfo
  55. // InfoFor returns the ClusterInfo entry for the provided clusterID or nil if it
  56. // doesn't exist
  57. InfoFor(clusterID string) *ClusterInfo
  58. // NameFor returns the name of the cluster provided the clusterID.
  59. NameFor(clusterID string) string
  60. // NameIDFor returns an identifier in the format "<clusterName>/<clusterID>" if the cluster has an
  61. // assigned name. Otherwise, just the clusterID is returned.
  62. NameIDFor(clusterID string) string
  63. }
  64. // ClusterInfoProvider is a contract which is capable of performing cluster info lookups.
  65. type ClusterInfoProvider interface {
  66. // GetClusterInfo returns a string map containing the local/remote connected cluster info
  67. GetClusterInfo() map[string]string
  68. }
  69. // ClusterInfoDecorator is a ClusterInfoProvider that decorates another ClusterInfoProvider with additional info.
  70. type ClusterInfoDecorator struct {
  71. provider ClusterInfoProvider
  72. additionalInfo map[string]string
  73. }
  74. // NewClusterInfoDecorator creates a new ClusterInfoDecorator which will append additional info to the cluster info
  75. // returned by the provider.
  76. func NewClusterInfoDecorator(provider ClusterInfoProvider, additionalInfo map[string]string) ClusterInfoProvider {
  77. return &ClusterInfoDecorator{
  78. provider: provider,
  79. additionalInfo: additionalInfo,
  80. }
  81. }
  82. // GetClusterInfo returns a string map containing the local/remote connected cluster info
  83. func (cid *ClusterInfoDecorator) GetClusterInfo() map[string]string {
  84. info := cid.provider.GetClusterInfo()
  85. maps.Copy(info, cid.additionalInfo)
  86. return info
  87. }