clustercache.go 10.0 KB

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