nodes_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. },
  29. }
  30. config := NewNodeClientConfig("cluster-one", 10, transport, "", "", NodeClientProxyConfig{
  31. ForceKubeProxy: true,
  32. LocalProxy: "http://localhost:8080",
  33. })
  34. statsClient := NewNodeStatsSummaryClient(cache, config, clusterConfig)
  35. summary, err := statsClient.GetNodeData()
  36. if err != nil {
  37. t.Fatalf("failed to get node data: %v", err)
  38. }
  39. for _, s := range summary {
  40. if s == nil {
  41. t.Error("received nil summary data")
  42. continue
  43. }
  44. t.Logf("Node Summary: %+v", s)
  45. }
  46. }
  47. type NodesOnlyClusterCache struct {
  48. k8sClient kubernetes.Interface
  49. }
  50. func NewTestClusterCache(k8sClient kubernetes.Interface) *NodesOnlyClusterCache {
  51. return &NodesOnlyClusterCache{
  52. k8sClient: k8sClient,
  53. }
  54. }
  55. // Run starts the watcher processes
  56. func (tcc *NodesOnlyClusterCache) Run() {}
  57. // Stops the watcher processes
  58. func (tcc *NodesOnlyClusterCache) Stop() {}
  59. // GetAllNamespaces returns all the cached namespaces
  60. func (tcc *NodesOnlyClusterCache) GetAllNamespaces() []*clustercache.Namespace { return nil }
  61. // GetAllNodes returns all the cached nodes
  62. func (tcc *NodesOnlyClusterCache) GetAllNodes() []*clustercache.Node {
  63. nodes, err := tcc.k8sClient.CoreV1().Nodes().List(context.Background(), v1.ListOptions{})
  64. if err != nil {
  65. return nil
  66. }
  67. var nodeList []*clustercache.Node
  68. for _, n := range nodes.Items {
  69. nodeList = append(nodeList, clustercache.TransformNode(&n))
  70. }
  71. return nodeList
  72. }
  73. // GetAllPods returns all the cached pods
  74. func (tcc *NodesOnlyClusterCache) GetAllPods() []*clustercache.Pod { return nil }
  75. // GetAllServices returns all the cached services
  76. func (tcc *NodesOnlyClusterCache) GetAllServices() []*clustercache.Service { return nil }
  77. // GetAllDaemonSets returns all the cached DaemonSets
  78. func (tcc *NodesOnlyClusterCache) GetAllDaemonSets() []*clustercache.DaemonSet { return nil }
  79. // GetAllDeployments returns all the cached deployments
  80. func (tcc *NodesOnlyClusterCache) GetAllDeployments() []*clustercache.Deployment { return nil }
  81. // GetAllStatfulSets returns all the cached StatefulSets
  82. func (tcc *NodesOnlyClusterCache) GetAllStatefulSets() []*clustercache.StatefulSet { return nil }
  83. // GetAllReplicaSets returns all the cached ReplicaSets
  84. func (tcc *NodesOnlyClusterCache) GetAllReplicaSets() []*clustercache.ReplicaSet { return nil }
  85. // GetAllPersistentVolumes returns all the cached persistent volumes
  86. func (tcc *NodesOnlyClusterCache) GetAllPersistentVolumes() []*clustercache.PersistentVolume {
  87. return nil
  88. }
  89. // GetAllPersistentVolumeClaims returns all the cached persistent volume claims
  90. func (tcc *NodesOnlyClusterCache) GetAllPersistentVolumeClaims() []*clustercache.PersistentVolumeClaim {
  91. return nil
  92. }
  93. // GetAllStorageClasses returns all the cached storage classes
  94. func (tcc *NodesOnlyClusterCache) GetAllStorageClasses() []*clustercache.StorageClass { return nil }
  95. // GetAllJobs returns all the cached jobs
  96. func (tcc *NodesOnlyClusterCache) GetAllJobs() []*clustercache.Job { return nil }
  97. // GetAllPodDisruptionBudgets returns all cached pod disruption budgets
  98. func (tcc *NodesOnlyClusterCache) GetAllPodDisruptionBudgets() []*clustercache.PodDisruptionBudget {
  99. return nil
  100. }
  101. // GetAllReplicationControllers returns all cached replication controllers
  102. func (tcc *NodesOnlyClusterCache) GetAllReplicationControllers() []*clustercache.ReplicationController {
  103. return nil
  104. }