Просмотр исходного кода

Merge pull request #1452 from opencost/mmd/remove-hpa-cluster-cache

Remove autoscaling/v2beta1 HPA data from cluster cache in order to support K8s v1.25
Michael Dresser 3 лет назад
Родитель
Сommit
fc946bca65

+ 1 - 19
pkg/clustercache/clustercache.go

@@ -7,7 +7,6 @@ import (
 	"github.com/opencost/opencost/pkg/log"
 
 	appsv1 "k8s.io/api/apps/v1"
-	autoscaling "k8s.io/api/autoscaling/v2beta1"
 	batchv1 "k8s.io/api/batch/v1"
 	v1 "k8s.io/api/core/v1"
 	"k8s.io/api/policy/v1beta1"
@@ -61,9 +60,6 @@ type ClusterCache interface {
 	// GetAllJobs returns all the cached jobs
 	GetAllJobs() []*batchv1.Job
 
-	// GetAllHorizontalPodAutoscalers returns all cached horizontal pod autoscalers
-	GetAllHorizontalPodAutoscalers() []*autoscaling.HorizontalPodAutoscaler
-
 	// GetAllPodDisruptionBudgets returns all cached pod disruption budgets
 	GetAllPodDisruptionBudgets() []*v1beta1.PodDisruptionBudget
 
@@ -91,7 +87,6 @@ type KubernetesClusterCache struct {
 	pvcWatch                   WatchController
 	storageClassWatch          WatchController
 	jobsWatch                  WatchController
-	hpaWatch                   WatchController
 	pdbWatch                   WatchController
 	replicationControllerWatch WatchController
 	stop                       chan struct{}
@@ -107,7 +102,6 @@ func NewKubernetesClusterCache(client kubernetes.Interface) ClusterCache {
 	appsRestClient := client.AppsV1().RESTClient()
 	storageRestClient := client.StorageV1().RESTClient()
 	batchClient := client.BatchV1().RESTClient()
-	autoscalingClient := client.AutoscalingV2beta1().RESTClient()
 	pdbClient := client.PolicyV1beta1().RESTClient()
 
 	kubecostNamespace := env.GetKubecostNamespace()
@@ -128,7 +122,6 @@ func NewKubernetesClusterCache(client kubernetes.Interface) ClusterCache {
 		pvcWatch:                   NewCachingWatcher(coreRestClient, "persistentvolumeclaims", &v1.PersistentVolumeClaim{}, "", fields.Everything()),
 		storageClassWatch:          NewCachingWatcher(storageRestClient, "storageclasses", &stv1.StorageClass{}, "", fields.Everything()),
 		jobsWatch:                  NewCachingWatcher(batchClient, "jobs", &batchv1.Job{}, "", fields.Everything()),
-		hpaWatch:                   NewCachingWatcher(autoscalingClient, "horizontalpodautoscalers", &autoscaling.HorizontalPodAutoscaler{}, "", fields.Everything()),
 		pdbWatch:                   NewCachingWatcher(pdbClient, "poddisruptionbudgets", &v1beta1.PodDisruptionBudget{}, "", fields.Everything()),
 		replicationControllerWatch: NewCachingWatcher(coreRestClient, "replicationcontrollers", &v1.ReplicationController{}, "", fields.Everything()),
 	}
@@ -140,7 +133,7 @@ func NewKubernetesClusterCache(client kubernetes.Interface) ClusterCache {
 		wg.Add(1)
 		go initializeCache(kcc.kubecostConfigMapWatch, &wg, cancel)
 	} else {
-		wg.Add(16)
+		wg.Add(15)
 		go initializeCache(kcc.kubecostConfigMapWatch, &wg, cancel)
 		go initializeCache(kcc.namespaceWatch, &wg, cancel)
 		go initializeCache(kcc.nodeWatch, &wg, cancel)
@@ -154,7 +147,6 @@ func NewKubernetesClusterCache(client kubernetes.Interface) ClusterCache {
 		go initializeCache(kcc.pvcWatch, &wg, cancel)
 		go initializeCache(kcc.storageClassWatch, &wg, cancel)
 		go initializeCache(kcc.jobsWatch, &wg, cancel)
-		go initializeCache(kcc.hpaWatch, &wg, cancel)
 		go initializeCache(kcc.podWatch, &wg, cancel)
 		go initializeCache(kcc.replicationControllerWatch, &wg, cancel)
 	}
@@ -185,7 +177,6 @@ func (kcc *KubernetesClusterCache) Run() {
 	go kcc.pvcWatch.Run(1, stopCh)
 	go kcc.storageClassWatch.Run(1, stopCh)
 	go kcc.jobsWatch.Run(1, stopCh)
-	go kcc.hpaWatch.Run(1, stopCh)
 	go kcc.pdbWatch.Run(1, stopCh)
 	go kcc.replicationControllerWatch.Run(1, stopCh)
 
@@ -309,15 +300,6 @@ func (kcc *KubernetesClusterCache) GetAllJobs() []*batchv1.Job {
 	return jobs
 }
 
-func (kcc *KubernetesClusterCache) GetAllHorizontalPodAutoscalers() []*autoscaling.HorizontalPodAutoscaler {
-	var hpas []*autoscaling.HorizontalPodAutoscaler
-	items := kcc.hpaWatch.GetAll()
-	for _, hpa := range items {
-		hpas = append(hpas, hpa.(*autoscaling.HorizontalPodAutoscaler))
-	}
-	return hpas
-}
-
 func (kcc *KubernetesClusterCache) GetAllPodDisruptionBudgets() []*v1beta1.PodDisruptionBudget {
 	var pdbs []*v1beta1.PodDisruptionBudget
 	items := kcc.pdbWatch.GetAll()

+ 28 - 31
pkg/clustercache/clusterexporter.go

@@ -9,7 +9,6 @@ import (
 	"github.com/opencost/opencost/pkg/util/json"
 
 	appsv1 "k8s.io/api/apps/v1"
-	autoscaling "k8s.io/api/autoscaling/v2beta1"
 	batchv1 "k8s.io/api/batch/v1"
 	v1 "k8s.io/api/core/v1"
 	"k8s.io/api/policy/v1beta1"
@@ -18,21 +17,20 @@ import (
 
 // clusterEncoding is used to represent the cluster objects in the encoded states.
 type clusterEncoding struct {
-	Namespaces               []*v1.Namespace                        `json:"namespaces,omitempty"`
-	Nodes                    []*v1.Node                             `json:"nodes,omitempty"`
-	Pods                     []*v1.Pod                              `json:"pods,omitempty"`
-	Services                 []*v1.Service                          `json:"services,omitempty"`
-	DaemonSets               []*appsv1.DaemonSet                    `json:"daemonSets,omitempty"`
-	Deployments              []*appsv1.Deployment                   `json:"deployments,omitempty"`
-	StatefulSets             []*appsv1.StatefulSet                  `json:"statefulSets,omitempty"`
-	ReplicaSets              []*appsv1.ReplicaSet                   `json:"replicaSets,omitempty"`
-	PersistentVolumes        []*v1.PersistentVolume                 `json:"persistentVolumes,omitempty"`
-	PersistentVolumeClaims   []*v1.PersistentVolumeClaim            `json:"persistentVolumeClaims,omitempty"`
-	StorageClasses           []*stv1.StorageClass                   `json:"storageClasses,omitempty"`
-	Jobs                     []*batchv1.Job                         `json:"jobs,omitempty"`
-	HorizontalPodAutoscalers []*autoscaling.HorizontalPodAutoscaler `json:"horizontalPodAutoscalers,omitempty"`
-	PodDisruptionBudgets     []*v1beta1.PodDisruptionBudget         `json:"podDisruptionBudgets,omitEmpty"`
-	ReplicationControllers   []*v1.ReplicationController            `json:"replicationController,omitEmpty"`
+	Namespaces             []*v1.Namespace                `json:"namespaces,omitempty"`
+	Nodes                  []*v1.Node                     `json:"nodes,omitempty"`
+	Pods                   []*v1.Pod                      `json:"pods,omitempty"`
+	Services               []*v1.Service                  `json:"services,omitempty"`
+	DaemonSets             []*appsv1.DaemonSet            `json:"daemonSets,omitempty"`
+	Deployments            []*appsv1.Deployment           `json:"deployments,omitempty"`
+	StatefulSets           []*appsv1.StatefulSet          `json:"statefulSets,omitempty"`
+	ReplicaSets            []*appsv1.ReplicaSet           `json:"replicaSets,omitempty"`
+	PersistentVolumes      []*v1.PersistentVolume         `json:"persistentVolumes,omitempty"`
+	PersistentVolumeClaims []*v1.PersistentVolumeClaim    `json:"persistentVolumeClaims,omitempty"`
+	StorageClasses         []*stv1.StorageClass           `json:"storageClasses,omitempty"`
+	Jobs                   []*batchv1.Job                 `json:"jobs,omitempty"`
+	PodDisruptionBudgets   []*v1beta1.PodDisruptionBudget `json:"podDisruptionBudgets,omitEmpty"`
+	ReplicationControllers []*v1.ReplicationController    `json:"replicationController,omitEmpty"`
 }
 
 // ClusterExporter manages and runs an file export process which dumps the local kubernetes cluster to a target location.
@@ -90,21 +88,20 @@ func (ce *ClusterExporter) Stop() {
 func (ce *ClusterExporter) Export() error {
 	c := ce.cluster
 	encoding := &clusterEncoding{
-		Namespaces:               c.GetAllNamespaces(),
-		Nodes:                    c.GetAllNodes(),
-		Pods:                     c.GetAllPods(),
-		Services:                 c.GetAllServices(),
-		DaemonSets:               c.GetAllDaemonSets(),
-		Deployments:              c.GetAllDeployments(),
-		StatefulSets:             c.GetAllStatefulSets(),
-		ReplicaSets:              c.GetAllReplicaSets(),
-		PersistentVolumes:        c.GetAllPersistentVolumes(),
-		PersistentVolumeClaims:   c.GetAllPersistentVolumeClaims(),
-		StorageClasses:           c.GetAllStorageClasses(),
-		Jobs:                     c.GetAllJobs(),
-		HorizontalPodAutoscalers: c.GetAllHorizontalPodAutoscalers(),
-		PodDisruptionBudgets:     c.GetAllPodDisruptionBudgets(),
-		ReplicationControllers:   c.GetAllReplicationControllers(),
+		Namespaces:             c.GetAllNamespaces(),
+		Nodes:                  c.GetAllNodes(),
+		Pods:                   c.GetAllPods(),
+		Services:               c.GetAllServices(),
+		DaemonSets:             c.GetAllDaemonSets(),
+		Deployments:            c.GetAllDeployments(),
+		StatefulSets:           c.GetAllStatefulSets(),
+		ReplicaSets:            c.GetAllReplicaSets(),
+		PersistentVolumes:      c.GetAllPersistentVolumes(),
+		PersistentVolumeClaims: c.GetAllPersistentVolumeClaims(),
+		StorageClasses:         c.GetAllStorageClasses(),
+		Jobs:                   c.GetAllJobs(),
+		PodDisruptionBudgets:   c.GetAllPodDisruptionBudgets(),
+		ReplicationControllers: c.GetAllReplicationControllers(),
 	}
 
 	data, err := json.Marshal(encoding)

+ 0 - 16
pkg/clustercache/clusterimporter.go

@@ -7,7 +7,6 @@ import (
 	"github.com/opencost/opencost/pkg/log"
 	"github.com/opencost/opencost/pkg/util/json"
 	appsv1 "k8s.io/api/apps/v1"
-	autoscaling "k8s.io/api/autoscaling/v2beta1"
 	batchv1 "k8s.io/api/batch/v1"
 	v1 "k8s.io/api/core/v1"
 	"k8s.io/api/policy/v1beta1"
@@ -271,21 +270,6 @@ func (ci *ClusterImporter) GetAllJobs() []*batchv1.Job {
 	return cloneList
 }
 
-// GetAllHorizontalPodAutoscalers() returns all cached horizontal pod autoscalers
-func (ci *ClusterImporter) GetAllHorizontalPodAutoscalers() []*autoscaling.HorizontalPodAutoscaler {
-	ci.dataLock.Lock()
-	defer ci.dataLock.Unlock()
-
-	// Deep copy here to avoid callers from corrupting the cache
-	// This also mimics the behavior of the default cluster cache impl.
-	hpas := ci.data.HorizontalPodAutoscalers
-	cloneList := make([]*autoscaling.HorizontalPodAutoscaler, 0, len(hpas))
-	for _, v := range hpas {
-		cloneList = append(cloneList, v.DeepCopy())
-	}
-	return cloneList
-}
-
 // GetAllPodDisruptionBudgets returns all cached pod disruption budgets
 func (ci *ClusterImporter) GetAllPodDisruptionBudgets() []*v1beta1.PodDisruptionBudget {
 	ci.dataLock.Lock()