clustercache.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package clustercache
  2. import (
  3. "time"
  4. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  5. "k8s.io/apimachinery/pkg/types"
  6. "k8s.io/utils/ptr"
  7. appsv1 "k8s.io/api/apps/v1"
  8. batchv1 "k8s.io/api/batch/v1"
  9. v1 "k8s.io/api/core/v1"
  10. policyv1 "k8s.io/api/policy/v1"
  11. stv1 "k8s.io/api/storage/v1"
  12. )
  13. type Namespace struct {
  14. UID types.UID
  15. Name string
  16. Labels map[string]string
  17. Annotations map[string]string
  18. }
  19. type Pod struct {
  20. UID types.UID
  21. Name string
  22. Namespace string
  23. Labels map[string]string
  24. Annotations map[string]string
  25. OwnerReferences []metav1.OwnerReference
  26. Status PodStatus
  27. Spec PodSpec
  28. DeletionTimestamp *time.Time
  29. }
  30. type PodStatus struct {
  31. PodIP string
  32. Phase v1.PodPhase
  33. ContainerStatuses []v1.ContainerStatus
  34. }
  35. type PodSpec struct {
  36. NodeName string
  37. Containers []Container
  38. Volumes []v1.Volume
  39. RestartPolicy v1.RestartPolicy
  40. }
  41. type Container struct {
  42. Name string
  43. Resources v1.ResourceRequirements
  44. }
  45. type Node struct {
  46. UID types.UID
  47. Name string
  48. Labels map[string]string
  49. Annotations map[string]string
  50. Status v1.NodeStatus
  51. SpecProviderID string
  52. }
  53. type Service struct {
  54. UID types.UID
  55. Name string
  56. Namespace string
  57. SpecSelector map[string]string
  58. Type v1.ServiceType
  59. Status v1.ServiceStatus
  60. ClusterIP string
  61. }
  62. type DaemonSet struct {
  63. Name string
  64. Namespace string
  65. Labels map[string]string
  66. SpecContainers []v1.Container
  67. }
  68. type Deployment struct {
  69. UID types.UID
  70. Name string
  71. Namespace string
  72. Labels map[string]string
  73. Annotations map[string]string
  74. MatchLabels map[string]string
  75. SpecSelector *metav1.LabelSelector
  76. SpecReplicas *int32
  77. SpecStrategy appsv1.DeploymentStrategy
  78. StatusAvailableReplicas int32
  79. PodSpec PodSpec
  80. }
  81. type StatefulSet struct {
  82. UID types.UID
  83. Name string
  84. Namespace string
  85. Labels map[string]string
  86. Annotations map[string]string
  87. SpecSelector *metav1.LabelSelector
  88. SpecReplicas *int32
  89. PodSpec PodSpec
  90. }
  91. type PersistentVolumeClaim struct {
  92. UID types.UID
  93. Name string
  94. Namespace string
  95. Spec v1.PersistentVolumeClaimSpec
  96. Labels map[string]string
  97. Annotations map[string]string
  98. }
  99. type StorageClass struct {
  100. Name string
  101. Labels map[string]string
  102. Annotations map[string]string
  103. Parameters map[string]string
  104. Provisioner string
  105. TypeMeta metav1.TypeMeta
  106. Size int
  107. }
  108. type Job struct {
  109. UID types.UID
  110. Name string
  111. Namespace string
  112. Status batchv1.JobStatus
  113. }
  114. type PersistentVolume struct {
  115. UID types.UID
  116. Name string
  117. Namespace string
  118. Labels map[string]string
  119. Annotations map[string]string
  120. Spec v1.PersistentVolumeSpec
  121. Status v1.PersistentVolumeStatus
  122. }
  123. type ReplicationController struct {
  124. Name string
  125. Namespace string
  126. Spec v1.ReplicationControllerSpec
  127. }
  128. type PodDisruptionBudget struct {
  129. Name string
  130. Namespace string
  131. Spec policyv1.PodDisruptionBudgetSpec
  132. Status policyv1.PodDisruptionBudgetStatus
  133. }
  134. type ReplicaSet struct {
  135. UID types.UID
  136. Name string
  137. Namespace string
  138. OwnerReferences []metav1.OwnerReference
  139. SpecSelector *metav1.LabelSelector
  140. Spec appsv1.ReplicaSetSpec
  141. }
  142. type ResourceQuota struct {
  143. UID types.UID
  144. Name string
  145. Namespace string
  146. Spec v1.ResourceQuotaSpec
  147. Status v1.ResourceQuotaStatus
  148. }
  149. type Volume struct {
  150. }
  151. // GetPublicIPAddresses returns all external IP addresses associated with the node
  152. func (n *Node) GetPublicIPAddresses() []string {
  153. var publicIPs []string
  154. for _, addr := range n.Status.Addresses {
  155. if addr.Type == v1.NodeExternalIP {
  156. publicIPs = append(publicIPs, addr.Address)
  157. }
  158. }
  159. return publicIPs
  160. }
  161. // GetPublicIPCount returns the count of external IP addresses associated with the node
  162. func (n *Node) GetPublicIPCount() int {
  163. count := 0
  164. for _, addr := range n.Status.Addresses {
  165. if addr.Type == v1.NodeExternalIP {
  166. count++
  167. }
  168. }
  169. return count
  170. }
  171. // GetControllerOf returns a pointer to a copy of the controllerRef if controllee has a controller
  172. func GetControllerOf(pod *Pod) *metav1.OwnerReference {
  173. ref := GetControllerOfNoCopy(pod)
  174. if ref == nil {
  175. return nil
  176. }
  177. cp := *ref
  178. cp.Controller = ptr.To(*ref.Controller)
  179. if ref.BlockOwnerDeletion != nil {
  180. cp.BlockOwnerDeletion = ptr.To(*ref.BlockOwnerDeletion)
  181. }
  182. return &cp
  183. }
  184. // GetControllerOfNoCopy returns a pointer to the controllerRef if controllee has a controller
  185. func GetControllerOfNoCopy(pod *Pod) *metav1.OwnerReference {
  186. refs := pod.OwnerReferences
  187. for i := range refs {
  188. if refs[i].Controller != nil && *refs[i].Controller {
  189. return &refs[i]
  190. }
  191. }
  192. return nil
  193. }
  194. func TransformNamespace(input *v1.Namespace) *Namespace {
  195. return &Namespace{
  196. UID: input.UID,
  197. Name: input.Name,
  198. Annotations: input.Annotations,
  199. Labels: input.Labels,
  200. }
  201. }
  202. func TransformPodContainer(input v1.Container) Container {
  203. return Container{
  204. Name: input.Name,
  205. Resources: input.Resources,
  206. }
  207. }
  208. func TransformPodStatus(input v1.PodStatus) PodStatus {
  209. return PodStatus{
  210. PodIP: input.PodIP,
  211. Phase: input.Phase,
  212. ContainerStatuses: input.ContainerStatuses,
  213. }
  214. }
  215. func TransformPodSpec(input v1.PodSpec) PodSpec {
  216. containers := make([]Container, len(input.Containers))
  217. for i, container := range input.Containers {
  218. containers[i] = TransformPodContainer(container)
  219. }
  220. return PodSpec{
  221. NodeName: input.NodeName,
  222. Containers: containers,
  223. Volumes: input.Volumes,
  224. RestartPolicy: input.RestartPolicy,
  225. }
  226. }
  227. func TransformTimestamp(input *metav1.Time) *time.Time {
  228. if input == nil {
  229. return nil
  230. }
  231. t := input.Time
  232. return &t
  233. }
  234. func TransformPod(input *v1.Pod) *Pod {
  235. return &Pod{
  236. UID: input.UID,
  237. Name: input.Name,
  238. Namespace: input.Namespace,
  239. Labels: input.Labels,
  240. Annotations: input.Annotations,
  241. OwnerReferences: input.OwnerReferences,
  242. Spec: TransformPodSpec(input.Spec),
  243. Status: TransformPodStatus(input.Status),
  244. DeletionTimestamp: TransformTimestamp(input.DeletionTimestamp),
  245. }
  246. }
  247. func TransformNode(input *v1.Node) *Node {
  248. return &Node{
  249. UID: input.UID,
  250. Name: input.Name,
  251. Labels: input.Labels,
  252. Annotations: input.Annotations,
  253. Status: input.Status,
  254. SpecProviderID: input.Spec.ProviderID,
  255. }
  256. }
  257. func TransformService(input *v1.Service) *Service {
  258. return &Service{
  259. UID: input.UID,
  260. Name: input.Name,
  261. Namespace: input.Namespace,
  262. SpecSelector: input.Spec.Selector,
  263. Type: input.Spec.Type,
  264. Status: input.Status,
  265. ClusterIP: input.Spec.ClusterIP,
  266. }
  267. }
  268. func TransformDaemonSet(input *appsv1.DaemonSet) *DaemonSet {
  269. return &DaemonSet{
  270. Name: input.Name,
  271. Namespace: input.Namespace,
  272. Labels: input.Labels,
  273. SpecContainers: input.Spec.Template.Spec.Containers,
  274. }
  275. }
  276. func TransformDeployment(input *appsv1.Deployment) *Deployment {
  277. return &Deployment{
  278. UID: input.UID,
  279. Name: input.Name,
  280. Namespace: input.Namespace,
  281. Labels: input.Labels,
  282. MatchLabels: input.Spec.Selector.MatchLabels,
  283. SpecReplicas: input.Spec.Replicas,
  284. SpecSelector: input.Spec.Selector,
  285. SpecStrategy: input.Spec.Strategy,
  286. StatusAvailableReplicas: input.Status.AvailableReplicas,
  287. PodSpec: TransformPodSpec(input.Spec.Template.Spec),
  288. }
  289. }
  290. func TransformStatefulSet(input *appsv1.StatefulSet) *StatefulSet {
  291. return &StatefulSet{
  292. Name: input.Name,
  293. Namespace: input.Namespace,
  294. SpecSelector: input.Spec.Selector,
  295. SpecReplicas: input.Spec.Replicas,
  296. PodSpec: TransformPodSpec(input.Spec.Template.Spec),
  297. Labels: input.Labels,
  298. Annotations: input.Annotations,
  299. UID: input.UID,
  300. }
  301. }
  302. func TransformPersistentVolume(input *v1.PersistentVolume) *PersistentVolume {
  303. return &PersistentVolume{
  304. UID: input.UID,
  305. Name: input.Name,
  306. Namespace: input.Namespace,
  307. Labels: input.Labels,
  308. Annotations: input.Annotations,
  309. Spec: input.Spec,
  310. Status: input.Status,
  311. }
  312. }
  313. func TransformPersistentVolumeClaim(input *v1.PersistentVolumeClaim) *PersistentVolumeClaim {
  314. return &PersistentVolumeClaim{
  315. UID: input.UID,
  316. Name: input.Name,
  317. Namespace: input.Namespace,
  318. Spec: input.Spec,
  319. Labels: input.Labels,
  320. Annotations: input.Annotations,
  321. }
  322. }
  323. func TransformStorageClass(input *stv1.StorageClass) *StorageClass {
  324. return &StorageClass{
  325. Name: input.Name,
  326. Annotations: input.Annotations,
  327. Labels: input.Labels,
  328. Parameters: input.Parameters,
  329. Provisioner: input.Provisioner,
  330. TypeMeta: input.TypeMeta,
  331. Size: input.Size(),
  332. }
  333. }
  334. func TransformJob(input *batchv1.Job) *Job {
  335. return &Job{
  336. UID: input.UID,
  337. Name: input.Name,
  338. Namespace: input.Namespace,
  339. Status: input.Status,
  340. }
  341. }
  342. func TransformReplicationController(input *v1.ReplicationController) *ReplicationController {
  343. return &ReplicationController{
  344. Name: input.Name,
  345. Namespace: input.Namespace,
  346. Spec: input.Spec,
  347. }
  348. }
  349. func TransformPodDisruptionBudget(input *policyv1.PodDisruptionBudget) *PodDisruptionBudget {
  350. return &PodDisruptionBudget{
  351. Name: input.Name,
  352. Namespace: input.Namespace,
  353. Spec: input.Spec,
  354. Status: input.Status,
  355. }
  356. }
  357. func TransformReplicaSet(input *appsv1.ReplicaSet) *ReplicaSet {
  358. return &ReplicaSet{
  359. UID: input.UID,
  360. Name: input.Name,
  361. Namespace: input.Namespace,
  362. OwnerReferences: input.OwnerReferences,
  363. Spec: input.Spec,
  364. SpecSelector: input.Spec.Selector,
  365. }
  366. }
  367. func TransformResourceQuota(input *v1.ResourceQuota) *ResourceQuota {
  368. return &ResourceQuota{
  369. UID: input.UID,
  370. Name: input.Name,
  371. Namespace: input.Namespace,
  372. Spec: input.Spec,
  373. Status: input.Status,
  374. }
  375. }
  376. // ClusterCache defines an contract for an object which caches components within a cluster, ensuring
  377. // up to date resources using watchers
  378. type ClusterCache interface {
  379. // Run starts the watcher processes
  380. Run()
  381. // Stops the watcher processes
  382. Stop()
  383. // GetAllNamespaces returns all the cached namespaces
  384. GetAllNamespaces() []*Namespace
  385. // GetAllNodes returns all the cached nodes
  386. GetAllNodes() []*Node
  387. // GetAllPods returns all the cached pods
  388. GetAllPods() []*Pod
  389. // GetAllServices returns all the cached services
  390. GetAllServices() []*Service
  391. // GetAllDaemonSets returns all the cached DaemonSets
  392. GetAllDaemonSets() []*DaemonSet
  393. // GetAllDeployments returns all the cached deployments
  394. GetAllDeployments() []*Deployment
  395. // GetAllStatfulSets returns all the cached StatefulSets
  396. GetAllStatefulSets() []*StatefulSet
  397. // GetAllReplicaSets returns all the cached ReplicaSets
  398. GetAllReplicaSets() []*ReplicaSet
  399. // GetAllPersistentVolumes returns all the cached persistent volumes
  400. GetAllPersistentVolumes() []*PersistentVolume
  401. // GetAllPersistentVolumeClaims returns all the cached persistent volume claims
  402. GetAllPersistentVolumeClaims() []*PersistentVolumeClaim
  403. // GetAllStorageClasses returns all the cached storage classes
  404. GetAllStorageClasses() []*StorageClass
  405. // GetAllJobs returns all the cached jobs
  406. GetAllJobs() []*Job
  407. // GetAllPodDisruptionBudgets returns all cached pod disruption budgets
  408. GetAllPodDisruptionBudgets() []*PodDisruptionBudget
  409. // GetAllReplicationControllers returns all cached replication controllers
  410. GetAllReplicationControllers() []*ReplicationController
  411. // GetAllResourceQuotas returns all cached resource quotas
  412. GetAllResourceQuotas() []*ResourceQuota
  413. }