2
0

nodes_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package nodestats
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net/http"
  6. "testing"
  7. "github.com/opencost/opencost/core/pkg/clustercache"
  8. "github.com/opencost/opencost/core/pkg/kubeconfig"
  9. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  10. "k8s.io/client-go/kubernetes"
  11. )
  12. func TestNodeSummaryLive(t *testing.T) {
  13. // this requires a live kubernetes cluster, and is used to test live functionality
  14. // we can comment the skip if we integrate a k8s sim or a mock server in the future
  15. t.Skip("Skipping live test for node summary client")
  16. client, err := kubeconfig.LoadKubeClient("")
  17. if err != nil {
  18. t.Fatalf("failed to load kube client: %v", err)
  19. }
  20. clusterConfig, err := kubeconfig.LoadKubeconfig("")
  21. if err != nil {
  22. t.Fatalf("failed to load kubeconfig: %v", err)
  23. }
  24. cache := NewTestClusterCache(client)
  25. transport := &http.Transport{
  26. TLSClientConfig: &tls.Config{
  27. InsecureSkipVerify: true,
  28. MinVersion: tls.VersionTLS12,
  29. },
  30. }
  31. config := NewNodeClientConfig("cluster-one", 10, transport, "", "", NodeClientProxyConfig{
  32. ForceKubeProxy: true,
  33. LocalProxy: "http://localhost:8080",
  34. })
  35. statsClient := NewNodeStatsSummaryClient(cache, config, clusterConfig)
  36. summary, err := statsClient.GetNodeData()
  37. if err != nil {
  38. t.Fatalf("failed to get node data: %v", err)
  39. }
  40. for _, s := range summary {
  41. if s == nil {
  42. t.Error("received nil summary data")
  43. continue
  44. }
  45. t.Logf("Node Summary: %+v", s)
  46. }
  47. }
  48. type NodesOnlyClusterCache struct {
  49. clusterUID string
  50. k8sClient kubernetes.Interface
  51. }
  52. func NewTestClusterCache(k8sClient kubernetes.Interface) *NodesOnlyClusterCache {
  53. return &NodesOnlyClusterCache{
  54. k8sClient: k8sClient,
  55. }
  56. }
  57. // Run starts the watcher processes
  58. func (tcc *NodesOnlyClusterCache) Run() {}
  59. // Stops the watcher processes
  60. func (tcc *NodesOnlyClusterCache) Stop() {}
  61. // GetAllNamespaces returns all the cached namespaces
  62. func (tcc *NodesOnlyClusterCache) GetAllNamespaces() []*clustercache.Namespace { return nil }
  63. // GetAllNodes returns all the cached nodes
  64. func (tcc *NodesOnlyClusterCache) GetAllNodes() []*clustercache.Node {
  65. nodes, err := tcc.k8sClient.CoreV1().Nodes().List(context.Background(), v1.ListOptions{})
  66. if err != nil {
  67. return nil
  68. }
  69. var nodeList []*clustercache.Node
  70. for _, n := range nodes.Items {
  71. nodeList = append(nodeList, clustercache.TransformNode(&n))
  72. }
  73. return nodeList
  74. }
  75. // GetAllPods returns all the cached pods
  76. func (tcc *NodesOnlyClusterCache) GetAllPods() []*clustercache.Pod { return nil }
  77. // GetAllServices returns all the cached services
  78. func (tcc *NodesOnlyClusterCache) GetAllServices() []*clustercache.Service { return nil }
  79. // GetAllDaemonSets returns all the cached DaemonSets
  80. func (tcc *NodesOnlyClusterCache) GetAllDaemonSets() []*clustercache.DaemonSet { return nil }
  81. // GetAllDeployments returns all the cached deployments
  82. func (tcc *NodesOnlyClusterCache) GetAllDeployments() []*clustercache.Deployment { return nil }
  83. // GetAllStatfulSets returns all the cached StatefulSets
  84. func (tcc *NodesOnlyClusterCache) GetAllStatefulSets() []*clustercache.StatefulSet { return nil }
  85. // GetAllReplicaSets returns all the cached ReplicaSets
  86. func (tcc *NodesOnlyClusterCache) GetAllReplicaSets() []*clustercache.ReplicaSet { return nil }
  87. // GetAllPersistentVolumes returns all the cached persistent volumes
  88. func (tcc *NodesOnlyClusterCache) GetAllPersistentVolumes() []*clustercache.PersistentVolume {
  89. return nil
  90. }
  91. // GetAllPersistentVolumeClaims returns all the cached persistent volume claims
  92. func (tcc *NodesOnlyClusterCache) GetAllPersistentVolumeClaims() []*clustercache.PersistentVolumeClaim {
  93. return nil
  94. }
  95. // GetAllStorageClasses returns all the cached storage classes
  96. func (tcc *NodesOnlyClusterCache) GetAllStorageClasses() []*clustercache.StorageClass { return nil }
  97. // GetAllJobs returns all the cached jobs
  98. func (tcc *NodesOnlyClusterCache) GetAllJobs() []*clustercache.Job { return nil }
  99. // GetAllPodDisruptionBudgets returns all cached pod disruption budgets
  100. func (tcc *NodesOnlyClusterCache) GetAllPodDisruptionBudgets() []*clustercache.PodDisruptionBudget {
  101. return nil
  102. }
  103. // GetAllReplicationControllers returns all cached replication controllers
  104. func (tcc *NodesOnlyClusterCache) GetAllReplicationControllers() []*clustercache.ReplicationController {
  105. return nil
  106. }
  107. func (tcc *NodesOnlyClusterCache) GetAllResourceQuotas() []*clustercache.ResourceQuota {
  108. return nil
  109. }