Explorar el Código

use reduced statefulset struct

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 hace 2 años
padre
commit
b034f72ab8

+ 18 - 4
pkg/clustercache/clustercache.go

@@ -172,6 +172,20 @@ func transformDeployment(input *appsv1.Deployment) *Deployment {
 	}
 }
 
+type StatefulSet struct {
+	Name         string
+	Namespace    string
+	SpecSelector *metav1.LabelSelector
+}
+
+func transformStatefulSet(input *appsv1.StatefulSet) *StatefulSet {
+	return &StatefulSet{
+		Name:         input.Name,
+		Namespace:    input.Namespace,
+		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 {
@@ -200,7 +214,7 @@ type ClusterCache interface {
 	GetAllDeployments() []*Deployment
 
 	// GetAllStatfulSets returns all the cached StatefulSets
-	GetAllStatefulSets() []*appsv1.StatefulSet
+	GetAllStatefulSets() []*StatefulSet
 
 	// GetAllReplicaSets returns all the cached ReplicaSets
 	GetAllReplicaSets() []*appsv1.ReplicaSet
@@ -403,11 +417,11 @@ func (kcc *KubernetesClusterCache) GetAllDeployments() []*Deployment {
 	return deployments
 }
 
-func (kcc *KubernetesClusterCache) GetAllStatefulSets() []*appsv1.StatefulSet {
-	var statefulsets []*appsv1.StatefulSet
+func (kcc *KubernetesClusterCache) GetAllStatefulSets() []*StatefulSet {
+	var statefulsets []*StatefulSet
 	items := kcc.statefulsetWatch.GetAll()
 	for _, statefulset := range items {
-		statefulsets = append(statefulsets, statefulset.(*appsv1.StatefulSet))
+		statefulsets = append(statefulsets, transformStatefulSet(statefulset.(*appsv1.StatefulSet)))
 	}
 	return statefulsets
 }

+ 3 - 3
pkg/costmodel/costmodel.go

@@ -1437,14 +1437,14 @@ func getPodStatefulsets(cache clustercache.ClusterCache, podList []*clustercache
 	ssList := cache.GetAllStatefulSets()
 	podSSMapping := make(map[string]map[string][]string) // namespace: podName: [deploymentNames]
 	for _, ss := range ssList {
-		namespace := ss.GetObjectMeta().GetNamespace()
-		name := ss.GetObjectMeta().GetName()
+		namespace := ss.Namespace
+		name := ss.Name
 
 		key := namespace + "," + clusterID
 		if _, ok := podSSMapping[key]; !ok {
 			podSSMapping[key] = make(map[string][]string)
 		}
-		s, err := metav1.LabelSelectorAsSelector(ss.Spec.Selector)
+		s, err := metav1.LabelSelectorAsSelector(ss.SpecSelector)
 		if err != nil {
 			log.Errorf("Error doing deployment label conversion: " + err.Error())
 		}

+ 2 - 3
pkg/costmodel/router.go

@@ -53,7 +53,6 @@ import (
 	"github.com/opencost/opencost/pkg/thanos"
 	prometheus "github.com/prometheus/client_golang/api"
 	prometheusAPI "github.com/prometheus/client_golang/api/prometheus/v1"
-	appsv1 "k8s.io/api/apps/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 
 	"github.com/patrickmn/go-cache"
@@ -1008,11 +1007,11 @@ func (a *Accesses) GetAllStatefulSets(w http.ResponseWriter, r *http.Request, ps
 	statefulSetsList := a.ClusterCache.GetAllStatefulSets()
 
 	// filter for provided namespace
-	var statefulSets []*appsv1.StatefulSet
+	var statefulSets []*clustercache.StatefulSet
 	if namespace == "" {
 		statefulSets = statefulSetsList
 	} else {
-		statefulSets = []*appsv1.StatefulSet{}
+		statefulSets = []*clustercache.StatefulSet{}
 
 		for _, ss := range statefulSetsList {
 			if ss.Namespace == namespace {

+ 3 - 3
pkg/metrics/statefulsetmetrics.go

@@ -38,10 +38,10 @@ func (sc KubecostStatefulsetCollector) Collect(ch chan<- prometheus.Metric) {
 
 	ds := sc.KubeClusterCache.GetAllStatefulSets()
 	for _, statefulset := range ds {
-		statefulsetName := statefulset.GetName()
-		statefulsetNS := statefulset.GetNamespace()
+		statefulsetName := statefulset.Name
+		statefulsetNS := statefulset.Namespace
 
-		labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(statefulset.Spec.Selector.MatchLabels))
+		labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(statefulset.SpecSelector.MatchLabels))
 		if len(labels) > 0 {
 			m := newStatefulsetMatchLabelsMetric(statefulsetName, statefulsetNS, "statefulSet_match_labels", labels, values)
 			ch <- m