| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- package clustercache
- import (
- "time"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/types"
- "k8s.io/utils/ptr"
- appsv1 "k8s.io/api/apps/v1"
- batchv1 "k8s.io/api/batch/v1"
- v1 "k8s.io/api/core/v1"
- policyv1 "k8s.io/api/policy/v1"
- stv1 "k8s.io/api/storage/v1"
- )
- type Namespace struct {
- Name string
- Labels map[string]string
- Annotations map[string]string
- }
- type Pod struct {
- UID types.UID
- Name string
- Namespace string
- Labels map[string]string
- Annotations map[string]string
- OwnerReferences []metav1.OwnerReference
- Status PodStatus
- Spec PodSpec
- DeletionTimestamp *time.Time
- }
- type PodStatus struct {
- PodIP string
- Phase v1.PodPhase
- ContainerStatuses []v1.ContainerStatus
- }
- type PodSpec struct {
- NodeName string
- Containers []Container
- Volumes []v1.Volume
- RestartPolicy v1.RestartPolicy
- }
- type Container struct {
- Name string
- Resources v1.ResourceRequirements
- }
- type Node struct {
- Name string
- Labels map[string]string
- Annotations map[string]string
- Status v1.NodeStatus
- SpecProviderID string
- }
- type Service struct {
- Name string
- Namespace string
- SpecSelector map[string]string
- Type v1.ServiceType
- Status v1.ServiceStatus
- ClusterIP string
- }
- type DaemonSet struct {
- Name string
- Namespace string
- Labels map[string]string
- SpecContainers []v1.Container
- }
- type Deployment struct {
- Name string
- Namespace string
- Labels map[string]string
- Annotations map[string]string
- MatchLabels map[string]string
- SpecSelector *metav1.LabelSelector
- SpecReplicas *int32
- SpecStrategy appsv1.DeploymentStrategy
- StatusAvailableReplicas int32
- PodSpec PodSpec
- }
- type StatefulSet struct {
- Name string
- Namespace string
- Labels map[string]string
- Annotations map[string]string
- SpecSelector *metav1.LabelSelector
- SpecReplicas *int32
- PodSpec PodSpec
- }
- type PersistentVolumeClaim struct {
- Name string
- Namespace string
- Spec v1.PersistentVolumeClaimSpec
- Labels map[string]string
- Annotations map[string]string
- }
- type StorageClass struct {
- Name string
- Labels map[string]string
- Annotations map[string]string
- Parameters map[string]string
- Provisioner string
- TypeMeta metav1.TypeMeta
- Size int
- }
- type Job struct {
- Name string
- Namespace string
- Status batchv1.JobStatus
- }
- type PersistentVolume struct {
- Name string
- Namespace string
- Labels map[string]string
- Annotations map[string]string
- Spec v1.PersistentVolumeSpec
- Status v1.PersistentVolumeStatus
- }
- type ReplicationController struct {
- Name string
- Namespace string
- Spec v1.ReplicationControllerSpec
- }
- type PodDisruptionBudget struct {
- Name string
- Namespace string
- Spec policyv1.PodDisruptionBudgetSpec
- Status policyv1.PodDisruptionBudgetStatus
- }
- type ReplicaSet struct {
- Name string
- Namespace string
- OwnerReferences []metav1.OwnerReference
- SpecSelector *metav1.LabelSelector
- Spec appsv1.ReplicaSetSpec
- }
- type Volume struct {
- }
- // GetControllerOf returns a pointer to a copy of the controllerRef if controllee has a controller
- func GetControllerOf(pod *Pod) *metav1.OwnerReference {
- ref := GetControllerOfNoCopy(pod)
- if ref == nil {
- return nil
- }
- cp := *ref
- cp.Controller = ptr.To(*ref.Controller)
- if ref.BlockOwnerDeletion != nil {
- cp.BlockOwnerDeletion = ptr.To(*ref.BlockOwnerDeletion)
- }
- return &cp
- }
- // GetControllerOfNoCopy returns a pointer to the controllerRef if controllee has a controller
- func GetControllerOfNoCopy(pod *Pod) *metav1.OwnerReference {
- refs := pod.OwnerReferences
- for i := range refs {
- if refs[i].Controller != nil && *refs[i].Controller {
- return &refs[i]
- }
- }
- return nil
- }
- func TransformNamespace(input *v1.Namespace) *Namespace {
- return &Namespace{
- Name: input.Name,
- Annotations: input.Annotations,
- Labels: input.Labels,
- }
- }
- func TransformPodContainer(input v1.Container) Container {
- return Container{
- Name: input.Name,
- Resources: input.Resources,
- }
- }
- func TransformPodStatus(input v1.PodStatus) PodStatus {
- return PodStatus{
- PodIP: input.PodIP,
- Phase: input.Phase,
- ContainerStatuses: input.ContainerStatuses,
- }
- }
- func TransformPodSpec(input v1.PodSpec) PodSpec {
- containers := make([]Container, len(input.Containers))
- for i, container := range input.Containers {
- containers[i] = TransformPodContainer(container)
- }
- return PodSpec{
- NodeName: input.NodeName,
- Containers: containers,
- Volumes: input.Volumes,
- RestartPolicy: input.RestartPolicy,
- }
- }
- func TransformTimestamp(input *metav1.Time) *time.Time {
- if input == nil {
- return nil
- }
- t := input.Time
- return &t
- }
- func TransformPod(input *v1.Pod) *Pod {
- return &Pod{
- UID: input.UID,
- Name: input.Name,
- Namespace: input.Namespace,
- Labels: input.Labels,
- Annotations: input.Annotations,
- OwnerReferences: input.OwnerReferences,
- Spec: TransformPodSpec(input.Spec),
- Status: TransformPodStatus(input.Status),
- DeletionTimestamp: TransformTimestamp(input.DeletionTimestamp),
- }
- }
- func TransformNode(input *v1.Node) *Node {
- return &Node{
- Name: input.Name,
- Labels: input.Labels,
- Annotations: input.Annotations,
- Status: input.Status,
- SpecProviderID: input.Spec.ProviderID,
- }
- }
- func TransformService(input *v1.Service) *Service {
- return &Service{
- Name: input.Name,
- Namespace: input.Namespace,
- SpecSelector: input.Spec.Selector,
- Type: input.Spec.Type,
- Status: input.Status,
- ClusterIP: input.Spec.ClusterIP,
- }
- }
- func TransformDaemonSet(input *appsv1.DaemonSet) *DaemonSet {
- return &DaemonSet{
- Name: input.Name,
- Namespace: input.Namespace,
- Labels: input.Labels,
- SpecContainers: input.Spec.Template.Spec.Containers,
- }
- }
- func TransformDeployment(input *appsv1.Deployment) *Deployment {
- return &Deployment{
- Name: input.Name,
- Namespace: input.Namespace,
- Labels: input.Labels,
- MatchLabels: input.Spec.Selector.MatchLabels,
- SpecReplicas: input.Spec.Replicas,
- SpecSelector: input.Spec.Selector,
- SpecStrategy: input.Spec.Strategy,
- StatusAvailableReplicas: input.Status.AvailableReplicas,
- PodSpec: TransformPodSpec(input.Spec.Template.Spec),
- }
- }
- func TransformStatefulSet(input *appsv1.StatefulSet) *StatefulSet {
- return &StatefulSet{
- Name: input.Name,
- Namespace: input.Namespace,
- SpecSelector: input.Spec.Selector,
- SpecReplicas: input.Spec.Replicas,
- PodSpec: TransformPodSpec(input.Spec.Template.Spec),
- }
- }
- func TransformPersistentVolume(input *v1.PersistentVolume) *PersistentVolume {
- return &PersistentVolume{
- Name: input.Name,
- Namespace: input.Namespace,
- Labels: input.Labels,
- Annotations: input.Annotations,
- Spec: input.Spec,
- Status: input.Status,
- }
- }
- func TransformPersistentVolumeClaim(input *v1.PersistentVolumeClaim) *PersistentVolumeClaim {
- return &PersistentVolumeClaim{
- Name: input.Name,
- Namespace: input.Namespace,
- Spec: input.Spec,
- Labels: input.Labels,
- Annotations: input.Annotations,
- }
- }
- func TransformStorageClass(input *stv1.StorageClass) *StorageClass {
- return &StorageClass{
- Name: input.Name,
- Annotations: input.Annotations,
- Labels: input.Labels,
- Parameters: input.Parameters,
- Provisioner: input.Provisioner,
- TypeMeta: input.TypeMeta,
- Size: input.Size(),
- }
- }
- func TransformJob(input *batchv1.Job) *Job {
- return &Job{
- Name: input.Name,
- Namespace: input.Namespace,
- Status: input.Status,
- }
- }
- func TransformReplicationController(input *v1.ReplicationController) *ReplicationController {
- return &ReplicationController{
- Name: input.Name,
- Namespace: input.Namespace,
- Spec: input.Spec,
- }
- }
- func TransformPodDisruptionBudget(input *policyv1.PodDisruptionBudget) *PodDisruptionBudget {
- return &PodDisruptionBudget{
- Name: input.Name,
- Namespace: input.Namespace,
- Spec: input.Spec,
- Status: input.Status,
- }
- }
- func TransformReplicaSet(input *appsv1.ReplicaSet) *ReplicaSet {
- return &ReplicaSet{
- Name: input.Name,
- Namespace: input.Namespace,
- OwnerReferences: input.OwnerReferences,
- Spec: input.Spec,
- SpecSelector: input.Spec.Selector,
- }
- }
- // ClusterCache defines an contract for an object which caches components within a cluster, ensuring
- // up to date resources using watchers
- type ClusterCache interface {
- // Run starts the watcher processes
- Run()
- // Stops the watcher processes
- Stop()
- // GetAllNamespaces returns all the cached namespaces
- GetAllNamespaces() []*Namespace
- // GetAllNodes returns all the cached nodes
- GetAllNodes() []*Node
- // GetAllPods returns all the cached pods
- GetAllPods() []*Pod
- // GetAllServices returns all the cached services
- GetAllServices() []*Service
- // GetAllDaemonSets returns all the cached DaemonSets
- GetAllDaemonSets() []*DaemonSet
- // GetAllDeployments returns all the cached deployments
- GetAllDeployments() []*Deployment
- // GetAllStatfulSets returns all the cached StatefulSets
- GetAllStatefulSets() []*StatefulSet
- // GetAllReplicaSets returns all the cached ReplicaSets
- GetAllReplicaSets() []*ReplicaSet
- // GetAllPersistentVolumes returns all the cached persistent volumes
- GetAllPersistentVolumes() []*PersistentVolume
- // GetAllPersistentVolumeClaims returns all the cached persistent volume claims
- GetAllPersistentVolumeClaims() []*PersistentVolumeClaim
- // GetAllStorageClasses returns all the cached storage classes
- GetAllStorageClasses() []*StorageClass
- // GetAllJobs returns all the cached jobs
- GetAllJobs() []*Job
- // GetAllPodDisruptionBudgets returns all cached pod disruption budgets
- GetAllPodDisruptionBudgets() []*PodDisruptionBudget
- // GetAllReplicationControllers returns all cached replication controllers
- GetAllReplicationControllers() []*ReplicationController
- }
|