2
0

clustercache.go 13 KB

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