clusterinfo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  32. // Clone creates a copy of ClusterInfo and returns it
  33. func (ci *ClusterInfo) Clone() *ClusterInfo {
  34. if ci == nil {
  35. return nil
  36. }
  37. return &ClusterInfo{
  38. ID: ci.ID,
  39. Name: ci.Name,
  40. Profile: ci.Profile,
  41. Provider: ci.Provider,
  42. Account: ci.Account,
  43. Project: ci.Project,
  44. Region: ci.Region,
  45. Provisioner: ci.Provisioner,
  46. }
  47. }
  48. type ClusterMap interface {
  49. // GetClusterIDs returns a slice containing all of the cluster identifiers.
  50. GetClusterIDs() []string
  51. // AsMap returns the cluster map as a standard go map
  52. AsMap() map[string]*ClusterInfo
  53. // InfoFor returns the ClusterInfo entry for the provided clusterID or nil if it
  54. // doesn't exist
  55. InfoFor(clusterID string) *ClusterInfo
  56. // NameFor returns the name of the cluster provided the clusterID.
  57. NameFor(clusterID string) string
  58. // NameIDFor returns an identifier in the format "<clusterName>/<clusterID>" if the cluster has an
  59. // assigned name. Otherwise, just the clusterID is returned.
  60. NameIDFor(clusterID string) string
  61. }
  62. // ClusterInfoProvider is a contract which is capable of performing cluster info lookups.
  63. type ClusterInfoProvider interface {
  64. // GetClusterInfo returns a string map containing the local/remote connected cluster info
  65. GetClusterInfo() map[string]string
  66. }
  67. // ClusterInfoDecorator is a ClusterInfoProvider that decorates another ClusterInfoProvider with additional info.
  68. type ClusterInfoDecorator struct {
  69. provider ClusterInfoProvider
  70. additionalInfo map[string]string
  71. }
  72. // NewClusterInfoDecorator creates a new ClusterInfoDecorator which will append additional info to the cluster info
  73. // returned by the provider.
  74. func NewClusterInfoDecorator(provider ClusterInfoProvider, additionalInfo map[string]string) ClusterInfoProvider {
  75. return &ClusterInfoDecorator{
  76. provider: provider,
  77. additionalInfo: additionalInfo,
  78. }
  79. }
  80. // GetClusterInfo returns a string map containing the local/remote connected cluster info
  81. func (cid *ClusterInfoDecorator) GetClusterInfo() map[string]string {
  82. info := cid.provider.GetClusterInfo()
  83. maps.Copy(info, cid.additionalInfo)
  84. return info
  85. }