Explorar o código

use reduced daemonset struct

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 %!s(int64=2) %!d(string=hai) anos
pai
achega
9014b34fad
Modificáronse 3 ficheiros con 22 adicións e 7 borrados
  1. 20 4
      pkg/clustercache/clustercache.go
  2. 1 1
      pkg/costmodel/costmodel.go
  3. 1 2
      test/cloud_test.go

+ 20 - 4
pkg/clustercache/clustercache.go

@@ -134,6 +134,22 @@ func transformService(input *v1.Service) *Service {
 	}
 }
 
+type DaemonSet struct {
+	Name           string
+	Namespace      string
+	Labels         map[string]string
+	SpecContainers []v1.Container
+}
+
+func transformDaemonSet(input *appsv1.DaemonSet) *DaemonSet {
+	return &DaemonSet{
+		Name:           input.Name,
+		Namespace:      input.Namespace,
+		Labels:         input.Labels,
+		SpecContainers: input.Spec.Template.Spec.Containers,
+	}
+}
+
 // ClusterCache defines an contract for an object which caches components within a cluster, ensuring
 // up to date resources using watchers
 type ClusterCache interface {
@@ -156,7 +172,7 @@ type ClusterCache interface {
 	GetAllServices() []*Service
 
 	// GetAllDaemonSets returns all the cached DaemonSets
-	GetAllDaemonSets() []*appsv1.DaemonSet
+	GetAllDaemonSets() []*DaemonSet
 
 	// GetAllDeployments returns all the cached deployments
 	GetAllDeployments() []*appsv1.Deployment
@@ -347,11 +363,11 @@ func (kcc *KubernetesClusterCache) GetAllServices() []*Service {
 	return services
 }
 
-func (kcc *KubernetesClusterCache) GetAllDaemonSets() []*appsv1.DaemonSet {
-	var daemonsets []*appsv1.DaemonSet
+func (kcc *KubernetesClusterCache) GetAllDaemonSets() []*DaemonSet {
+	var daemonsets []*DaemonSet
 	items := kcc.daemonsetsWatch.GetAll()
 	for _, daemonset := range items {
-		daemonsets = append(daemonsets, daemonset.(*appsv1.DaemonSet))
+		daemonsets = append(daemonsets, daemonset.(*DaemonSet))
 	}
 	return daemonsets
 }

+ 1 - 1
pkg/costmodel/costmodel.go

@@ -2357,7 +2357,7 @@ func getAllocatableVGPUs(cache clustercache.ClusterCache) (float64, error) {
 	daemonsets := cache.GetAllDaemonSets()
 	vgpuCount := 0.0
 	for _, ds := range daemonsets {
-		dsContainerList := &ds.Spec.Template.Spec.Containers
+		dsContainerList := &ds.SpecContainers
 		for _, ctnr := range *dsContainerList {
 			if ctnr.Args != nil {
 				for _, arg := range ctnr.Args {

+ 1 - 2
test/cloud_test.go

@@ -16,7 +16,6 @@ import (
 	"github.com/opencost/opencost/pkg/config"
 	"github.com/opencost/opencost/pkg/costmodel"
 
-	appsv1 "k8s.io/api/apps/v1"
 	v1 "k8s.io/api/core/v1"
 	"k8s.io/apimachinery/pkg/api/resource"
 )
@@ -445,7 +444,7 @@ func (f FakeCache) GetAllNodes() []*clustercache.Node {
 	return f.nodes
 }
 
-func (f FakeCache) GetAllDaemonSets() []*appsv1.DaemonSet {
+func (f FakeCache) GetAllDaemonSets() []*clustercache.DaemonSet {
 	return nil
 }