nodes_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. k8sClient kubernetes.Interface
  50. }
  51. func NewTestClusterCache(k8sClient kubernetes.Interface) *NodesOnlyClusterCache {
  52. return &NodesOnlyClusterCache{
  53. k8sClient: k8sClient,
  54. }
  55. }
  56. // Run starts the watcher processes
  57. func (tcc *NodesOnlyClusterCache) Run() {}
  58. // Stops the watcher processes
  59. func (tcc *NodesOnlyClusterCache) Stop() {}
  60. // GetAllNamespaces returns all the cached namespaces
  61. func (tcc *NodesOnlyClusterCache) GetAllNamespaces() []*clustercache.Namespace { return nil }
  62. // GetAllNodes returns all the cached nodes
  63. func (tcc *NodesOnlyClusterCache) GetAllNodes() []*clustercache.Node {
  64. nodes, err := tcc.k8sClient.CoreV1().Nodes().List(context.Background(), v1.ListOptions{})
  65. if err != nil {
  66. return nil
  67. }
  68. var nodeList []*clustercache.Node
  69. for _, n := range nodes.Items {
  70. nodeList = append(nodeList, clustercache.TransformNode(&n))
  71. }
  72. return nodeList
  73. }
  74. // GetAllPods returns all the cached pods
  75. func (tcc *NodesOnlyClusterCache) GetAllPods() []*clustercache.Pod { return nil }
  76. // GetAllServices returns all the cached services
  77. func (tcc *NodesOnlyClusterCache) GetAllServices() []*clustercache.Service { return nil }
  78. // GetAllDaemonSets returns all the cached DaemonSets
  79. func (tcc *NodesOnlyClusterCache) GetAllDaemonSets() []*clustercache.DaemonSet { return nil }
  80. // GetAllDeployments returns all the cached deployments
  81. func (tcc *NodesOnlyClusterCache) GetAllDeployments() []*clustercache.Deployment { return nil }
  82. // GetAllStatfulSets returns all the cached StatefulSets
  83. func (tcc *NodesOnlyClusterCache) GetAllStatefulSets() []*clustercache.StatefulSet { return nil }
  84. // GetAllReplicaSets returns all the cached ReplicaSets
  85. func (tcc *NodesOnlyClusterCache) GetAllReplicaSets() []*clustercache.ReplicaSet { return nil }
  86. // GetAllPersistentVolumes returns all the cached persistent volumes
  87. func (tcc *NodesOnlyClusterCache) GetAllPersistentVolumes() []*clustercache.PersistentVolume {
  88. return nil
  89. }
  90. // GetAllPersistentVolumeClaims returns all the cached persistent volume claims
  91. func (tcc *NodesOnlyClusterCache) GetAllPersistentVolumeClaims() []*clustercache.PersistentVolumeClaim {
  92. return nil
  93. }
  94. // GetAllStorageClasses returns all the cached storage classes
  95. func (tcc *NodesOnlyClusterCache) GetAllStorageClasses() []*clustercache.StorageClass { return nil }
  96. // GetAllJobs returns all the cached jobs
  97. func (tcc *NodesOnlyClusterCache) GetAllJobs() []*clustercache.Job { return nil }
  98. // GetAllPodDisruptionBudgets returns all cached pod disruption budgets
  99. func (tcc *NodesOnlyClusterCache) GetAllPodDisruptionBudgets() []*clustercache.PodDisruptionBudget {
  100. return nil
  101. }
  102. // GetAllReplicationControllers returns all cached replication controllers
  103. func (tcc *NodesOnlyClusterCache) GetAllReplicationControllers() []*clustercache.ReplicationController {
  104. return nil
  105. }