clustercache.go 13 KB

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