clusterinfo.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package costmodel
  2. import (
  3. "fmt"
  4. cloudProvider "github.com/kubecost/cost-model/pkg/cloud"
  5. "github.com/kubecost/cost-model/pkg/costmodel/clusters"
  6. "github.com/kubecost/cost-model/pkg/env"
  7. "github.com/kubecost/cost-model/pkg/thanos"
  8. "k8s.io/client-go/kubernetes"
  9. "k8s.io/klog"
  10. )
  11. var (
  12. logCollectionEnabled bool = env.IsLogCollectionEnabled()
  13. productAnalyticsEnabled bool = env.IsProductAnalyticsEnabled()
  14. errorReportingEnabled bool = env.IsErrorReportingEnabled()
  15. valuesReportingEnabled bool = env.IsValuesReportingEnabled()
  16. clusterProfile string = env.GetClusterProfile()
  17. )
  18. // writeReportingFlags writes the reporting flags to the cluster info map
  19. func writeReportingFlags(clusterInfo map[string]string) {
  20. clusterInfo["logCollection"] = fmt.Sprintf("%t", logCollectionEnabled)
  21. clusterInfo["productAnalytics"] = fmt.Sprintf("%t", productAnalyticsEnabled)
  22. clusterInfo["errorReporting"] = fmt.Sprintf("%t", errorReportingEnabled)
  23. clusterInfo["valuesReporting"] = fmt.Sprintf("%t", valuesReportingEnabled)
  24. }
  25. // writeClusterProfile writes the data associated with the cluster profile
  26. func writeClusterProfile(clusterInfo map[string]string) {
  27. clusterInfo["clusterProfile"] = clusterProfile
  28. }
  29. func writeThanosFlags(clusterInfo map[string]string) {
  30. // Include Thanos Offset Duration if Applicable
  31. clusterInfo["thanosEnabled"] = fmt.Sprintf("%t", thanos.IsEnabled())
  32. if thanos.IsEnabled() {
  33. clusterInfo["thanosOffset"] = thanos.Offset()
  34. }
  35. }
  36. // default local cluster info provider implementation which provides an instanced object for
  37. // getting the local cluster info
  38. type defaultLocalClusterInfoProvider struct {
  39. k8s kubernetes.Interface
  40. provider cloudProvider.Provider
  41. }
  42. // GetClusterInfo returns a string map containing the local cluster info
  43. func (dlcip *defaultLocalClusterInfoProvider) GetClusterInfo() map[string]string {
  44. return GetClusterInfo(dlcip.k8s, dlcip.provider)
  45. }
  46. // NewLocalClusterInfoProvider creates a new clusters.LocalClusterInfoProvider implementation for providing local
  47. // cluster information
  48. func NewLocalClusterInfoProvider(k8s kubernetes.Interface, cloud cloudProvider.Provider) clusters.LocalClusterInfoProvider {
  49. return &defaultLocalClusterInfoProvider{
  50. k8s: k8s,
  51. provider: cloud,
  52. }
  53. }
  54. // GetClusterInfo provides specific information about the cluster cloud provider as well as
  55. // generic configuration values.
  56. func GetClusterInfo(kubeClient kubernetes.Interface, cloud cloudProvider.Provider) map[string]string {
  57. data, err := cloud.ClusterInfo()
  58. // Ensure we create the info object if it doesn't exist
  59. if data == nil {
  60. data = make(map[string]string)
  61. }
  62. kc, ok := kubeClient.(*kubernetes.Clientset)
  63. if ok && data != nil {
  64. v, err := kc.ServerVersion()
  65. if err != nil {
  66. klog.Infof("Could not get k8s version info: %s", err.Error())
  67. } else if v != nil {
  68. data["version"] = v.Major + "." + v.Minor
  69. }
  70. } else {
  71. klog.Infof("Could not get k8s version info: %s", err.Error())
  72. }
  73. writeClusterProfile(data)
  74. writeReportingFlags(data)
  75. writeThanosFlags(data)
  76. return data
  77. }