clustercache.go 11 KB

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