clustercache.go 11 KB

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