clustercache2.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package clustercache
  2. import (
  3. "sync"
  4. appsv1 "k8s.io/api/apps/v1"
  5. batchv1 "k8s.io/api/batch/v1"
  6. v1 "k8s.io/api/core/v1"
  7. policyv1 "k8s.io/api/policy/v1"
  8. stv1 "k8s.io/api/storage/v1"
  9. "k8s.io/client-go/kubernetes"
  10. )
  11. type KubernetesClusterCacheV2 struct {
  12. namespaceStore *GenericStore[*v1.Namespace, *Namespace]
  13. nodeStore *GenericStore[*v1.Node, *Node]
  14. podStore *GenericStore[*v1.Pod, *Pod]
  15. serviceStore *GenericStore[*v1.Service, *Service]
  16. daemonSetStore *GenericStore[*appsv1.DaemonSet, *DaemonSet]
  17. deploymentStore *GenericStore[*appsv1.Deployment, *Deployment]
  18. statefulSetStore *GenericStore[*appsv1.StatefulSet, *StatefulSet]
  19. persistentVolumeStore *GenericStore[*v1.PersistentVolume, *PersistentVolume]
  20. persistentVolumeClaimStore *GenericStore[*v1.PersistentVolumeClaim, *PersistentVolumeClaim]
  21. storageClassStore *GenericStore[*stv1.StorageClass, *StorageClass]
  22. jobStore *GenericStore[*batchv1.Job, *Job]
  23. replicationControllerStore *GenericStore[*v1.ReplicationController, *ReplicationController]
  24. replicaSetStore *GenericStore[*appsv1.ReplicaSet, *ReplicaSet]
  25. pdbStore *GenericStore[*policyv1.PodDisruptionBudget, *PodDisruptionBudget]
  26. stopCh chan struct{}
  27. }
  28. func NewKubernetesClusterCacheV2(clientset kubernetes.Interface) *KubernetesClusterCacheV2 {
  29. return &KubernetesClusterCacheV2{
  30. namespaceStore: CreateStore(clientset.CoreV1().RESTClient(), "namespaces", transformNamespace),
  31. nodeStore: CreateStore(clientset.CoreV1().RESTClient(), "nodes", transformNode),
  32. persistentVolumeClaimStore: CreateStore(clientset.CoreV1().RESTClient(), "persistentvolumeclaims", transformPersistentVolumeClaim),
  33. persistentVolumeStore: CreateStore(clientset.CoreV1().RESTClient(), "persistentvolumes", transformPersistentVolume),
  34. podStore: CreateStore(clientset.CoreV1().RESTClient(), "pods", transformPod),
  35. replicationControllerStore: CreateStore(clientset.CoreV1().RESTClient(), "replicationcontrollers", transformReplicationController),
  36. serviceStore: CreateStore(clientset.CoreV1().RESTClient(), "services", transformService),
  37. daemonSetStore: CreateStore(clientset.AppsV1().RESTClient(), "daemonsets", transformDaemonSet),
  38. deploymentStore: CreateStore(clientset.AppsV1().RESTClient(), "deployments", transformDeployment),
  39. replicaSetStore: CreateStore(clientset.AppsV1().RESTClient(), "replicasets", transformReplicaSet),
  40. statefulSetStore: CreateStore(clientset.AppsV1().RESTClient(), "statefulsets", transformStatefulSet),
  41. storageClassStore: CreateStore(clientset.StorageV1().RESTClient(), "storageclasses", transformStorageClass),
  42. jobStore: CreateStore(clientset.BatchV1().RESTClient(), "jobs", transformJob),
  43. pdbStore: CreateStore(clientset.PolicyV1().RESTClient(), "poddisruptionbudgets", transformPodDisruptionBudget),
  44. stopCh: make(chan struct{}),
  45. }
  46. }
  47. func (kcc *KubernetesClusterCacheV2) Run() {
  48. var wg sync.WaitGroup
  49. wg.Add(14)
  50. kcc.namespaceStore.Watch(kcc.stopCh, wg.Done)
  51. kcc.nodeStore.Watch(kcc.stopCh, wg.Done)
  52. kcc.persistentVolumeClaimStore.Watch(kcc.stopCh, wg.Done)
  53. kcc.persistentVolumeStore.Watch(kcc.stopCh, wg.Done)
  54. kcc.podStore.Watch(kcc.stopCh, wg.Done)
  55. kcc.replicationControllerStore.Watch(kcc.stopCh, wg.Done)
  56. kcc.serviceStore.Watch(kcc.stopCh, wg.Done)
  57. kcc.daemonSetStore.Watch(kcc.stopCh, wg.Done)
  58. kcc.deploymentStore.Watch(kcc.stopCh, wg.Done)
  59. kcc.replicaSetStore.Watch(kcc.stopCh, wg.Done)
  60. kcc.statefulSetStore.Watch(kcc.stopCh, wg.Done)
  61. kcc.storageClassStore.Watch(kcc.stopCh, wg.Done)
  62. kcc.jobStore.Watch(kcc.stopCh, wg.Done)
  63. kcc.pdbStore.Watch(kcc.stopCh, wg.Done)
  64. wg.Wait()
  65. }
  66. func (kcc *KubernetesClusterCacheV2) Stop() {
  67. if kcc.stopCh != nil {
  68. close(kcc.stopCh)
  69. kcc.stopCh = nil
  70. }
  71. }
  72. func (kcc *KubernetesClusterCacheV2) GetAllNamespaces() []*Namespace {
  73. return kcc.namespaceStore.GetAll()
  74. }
  75. func (kcc *KubernetesClusterCacheV2) GetAllNodes() []*Node {
  76. return kcc.nodeStore.GetAll()
  77. }
  78. func (kcc *KubernetesClusterCacheV2) GetAllPods() []*Pod {
  79. return kcc.podStore.GetAll()
  80. }
  81. func (kcc *KubernetesClusterCacheV2) GetAllServices() []*Service {
  82. return kcc.serviceStore.GetAll()
  83. }
  84. func (kcc *KubernetesClusterCacheV2) GetAllDaemonSets() []*DaemonSet {
  85. return kcc.daemonSetStore.GetAll()
  86. }
  87. func (kcc *KubernetesClusterCacheV2) GetAllDeployments() []*Deployment {
  88. return kcc.deploymentStore.GetAll()
  89. }
  90. func (kcc *KubernetesClusterCacheV2) GetAllStatefulSets() []*StatefulSet {
  91. return kcc.statefulSetStore.GetAll()
  92. }
  93. func (kcc *KubernetesClusterCacheV2) GetAllPersistentVolumes() []*PersistentVolume {
  94. return kcc.persistentVolumeStore.GetAll()
  95. }
  96. func (kcc *KubernetesClusterCacheV2) GetAllPersistentVolumeClaims() []*PersistentVolumeClaim {
  97. return kcc.persistentVolumeClaimStore.GetAll()
  98. }
  99. func (kcc *KubernetesClusterCacheV2) GetAllStorageClasses() []*StorageClass {
  100. return kcc.storageClassStore.GetAll()
  101. }
  102. func (kcc *KubernetesClusterCacheV2) GetAllJobs() []*Job {
  103. return kcc.jobStore.GetAll()
  104. }
  105. func (kcc *KubernetesClusterCacheV2) GetAllReplicationControllers() []*ReplicationController {
  106. return kcc.replicationControllerStore.GetAll()
  107. }
  108. func (kcc *KubernetesClusterCacheV2) GetAllReplicaSets() []*ReplicaSet {
  109. return kcc.replicaSetStore.GetAll()
  110. }
  111. func (kcc *KubernetesClusterCacheV2) GetAllPodDisruptionBudgets() []*PodDisruptionBudget {
  112. return kcc.pdbStore.GetAll()
  113. }