clustercache.go 11 KB

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