clustercache.go 10 KB

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