2
0

clustercache.go 10 KB

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