Преглед изворни кода

Cluster Cache Optimization Effort - Additional Fields (#3008)

* merge develop

Signed-off-by: Matt Bolt <mbolt35@gmail.com>

* First iteration on adding necessary fields to flyweights.

Signed-off-by: Matt Bolt <mbolt35@gmail.com>

---------

Signed-off-by: Matt Bolt <mbolt35@gmail.com>
Signed-off-by: r2k1 <yokree@gmail.com>
Co-authored-by: r2k1 <yokree@gmail.com>
Matt Bolt пре 1 година
родитељ
комит
d123a95000
2 измењених фајлова са 117 додато и 30 уклоњено
  1. 4 4
      pkg/cloud/otc/provider.go
  2. 113 26
      pkg/clustercache/clustercache.go

+ 4 - 4
pkg/cloud/otc/provider.go

@@ -116,7 +116,7 @@ func (k *otcKey) Features() string {
 
 // Extract/generate a key that holds the data required to calculate
 // the cost of the given node (like s2.large.4).
-func (otc *OTC) GetKey(labels map[string]string, n *v1.Node) models.Key {
+func (otc *OTC) GetKey(labels map[string]string, n *clustercache.Node) models.Key {
 	return &otcKey{
 		Labels:     labels,
 		ProviderID: labels["providerID"],
@@ -133,7 +133,7 @@ func (k *otcPVKey) ID() string {
 	return k.ProviderId
 }
 
-func (otc *OTC) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (otc *OTC) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	providerID := ""
 	return &otcPVKey{
 		Labels:                 pv.Labels,
@@ -229,7 +229,7 @@ func (otc *OTC) DownloadPricingData() error {
 		fmt.Println(tmp.Parameters)
 		fmt.Println(tmp.Labels)
 		fmt.Println(tmp.TypeMeta)
-		fmt.Println(tmp.Size())
+		fmt.Println(tmp.Size)
 	}
 
 	// Slice with all persistent volumes present in the cluster
@@ -240,7 +240,7 @@ func (otc *OTC) DownloadPricingData() error {
 	inputkeys := make(map[string]bool)
 	tmp := []string{}
 	for _, node := range nodeList {
-		labels := node.GetObjectMeta().GetLabels()
+		labels := node.Labels
 		key := otc.GetKey(labels, node)
 		inputkeys[key.Features()] = true
 		tmp = append(tmp, key.Features())

+ 113 - 26
pkg/clustercache/clustercache.go

@@ -2,11 +2,13 @@ package clustercache
 
 import (
 	"sync"
+	"time"
 
 	"github.com/opencost/opencost/core/pkg/log"
 	"github.com/opencost/opencost/pkg/env"
 	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"
@@ -24,14 +26,15 @@ type Namespace struct {
 }
 
 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
+	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 {
@@ -40,9 +43,10 @@ type PodStatus struct {
 }
 
 type PodSpec struct {
-	NodeName   string
-	Containers []Container
-	Volumes    []v1.Volume
+	NodeName      string
+	Containers    []Container
+	Volumes       []v1.Volume
+	RestartPolicy v1.RestartPolicy
 }
 
 type Container struct {
@@ -77,30 +81,41 @@ 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 {
@@ -118,11 +133,52 @@ type PersistentVolume struct {
 	Status      v1.PersistentVolumeStatus
 }
 
-type ReplicationController struct{}
+type ReplicationController struct {
+	Name      string
+	Namespace string
+	Spec      v1.ReplicationControllerSpec
+}
+
+type PodDisruptionBudget struct {
+	Name      string
+	Namespace string
+	Spec      policyv1.PodDisruptionBudgetSpec
+	Status    policyv1.PodDisruptionBudgetStatus
+}
 
-type PodDisruptionBudget struct{}
 type ReplicaSet struct {
+	Name         string
+	Namespace    string
 	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 {
@@ -153,23 +209,34 @@ func transformPodSpec(input v1.PodSpec) PodSpec {
 		containers[i] = transformPodContainer(container)
 	}
 	return PodSpec{
-		NodeName:   input.NodeName,
-		Containers: containers,
-		Volumes:    input.Volumes,
+		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),
+		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),
 	}
 }
 
@@ -210,7 +277,9 @@ func transformDeployment(input *appsv1.Deployment) *Deployment {
 		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),
 	}
 }
 
@@ -219,6 +288,8 @@ func transformStatefulSet(input *appsv1.StatefulSet) *StatefulSet {
 		Name:         input.Name,
 		Namespace:    input.Namespace,
 		SpecSelector: input.Spec.Selector,
+		SpecReplicas: input.Spec.Replicas,
+		PodSpec:      transformPodSpec(input.Spec.Template.Spec),
 	}
 }
 
@@ -238,6 +309,7 @@ func transformPersistentVolumeClaim(input *v1.PersistentVolumeClaim) *Persistent
 		Name:        input.Name,
 		Namespace:   input.Namespace,
 		Spec:        input.Spec,
+		Labels:      input.Labels,
 		Annotations: input.Annotations,
 	}
 }
@@ -246,8 +318,11 @@ 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(),
 	}
 }
 
@@ -260,15 +335,27 @@ func transformJob(input *batchv1.Job) *Job {
 }
 
 func transformReplicationController(input *v1.ReplicationController) *ReplicationController {
-	return &ReplicationController{}
+	return &ReplicationController{
+		Name:      input.Name,
+		Namespace: input.Namespace,
+		Spec:      input.Spec,
+	}
 }
 
 func transformPodDisruptionBudget(input *policyv1.PodDisruptionBudget) *PodDisruptionBudget {
-	return &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,
+		Spec:         input.Spec,
 		SpecSelector: input.Spec.Selector,
 	}
 }