Jelajahi Sumber

use reduced PersistentVolume struct

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 2 tahun lalu
induk
melakukan
e7bb17cd90

+ 3 - 4
pkg/cloud/alibaba/provider.go

@@ -27,7 +27,6 @@ import (
 
 	ocenv "github.com/opencost/opencost/pkg/env"
 	"golang.org/x/exp/slices"
-	v1 "k8s.io/api/core/v1"
 )
 
 const (
@@ -920,7 +919,7 @@ type AlibabaPVKey struct {
 	SizeInGiB         string
 }
 
-func (alibaba *Alibaba) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (alibaba *Alibaba) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	regionID := defaultRegion
 	// If default Region is not passed default it to cluster region ID.
 	if defaultRegion == "" {
@@ -1294,7 +1293,7 @@ func getNumericalValueFromResourceQuantity(quantity string) (value string) {
 
 // generateSlimK8sDiskFromV1PV function generates SlimK8sDisk from v1.PersistentVolume
 // to generate slim disk type that can be used to fetch pricing information for Data disk type.
-func generateSlimK8sDiskFromV1PV(pv *v1.PersistentVolume, regionID string) *SlimK8sDisk {
+func generateSlimK8sDiskFromV1PV(pv *clustercache.PersistentVolume, regionID string) *SlimK8sDisk {
 
 	// All PVs are data disks while local disk are categorized as system disk
 	diskType := ALIBABA_DATA_DISK_CATEGORY
@@ -1340,7 +1339,7 @@ func generateSlimK8sDiskFromV1PV(pv *v1.PersistentVolume, regionID string) *Slim
 // if topology.diskplugin.csi.alibabacloud.com/zone label/annotation is passed during PV creation determine the region based on this pv label.
 // if neither of the above label/annotation is present check node affinity for the zone affinity and determine the region based on this zone.
 // if nether of the above yields a region , return empty string to default it to cluster region.
-func determinePVRegion(pv *v1.PersistentVolume) string {
+func determinePVRegion(pv *clustercache.PersistentVolume) string {
 	// if "topology.diskplugin.csi.alibabacloud.com/region" is present as a label or annotation return that as the PV region
 	if val, ok := pv.Labels[ALIBABA_DISK_TOPOLOGY_REGION_LABEL]; ok {
 		log.Debugf("determinePVRegion returned a region value of: %s through label: %s for PV name: %s", val, ALIBABA_DISK_TOPOLOGY_REGION_LABEL, pv.Name)

+ 7 - 7
pkg/cloud/alibaba/provider_test.go

@@ -658,7 +658,7 @@ func TestGenerateSlimK8sNodeFromV1Node(t *testing.T) {
 }
 
 func TestGenerateSlimK8sDiskFromV1PV(t *testing.T) {
-	testv1PV := &v1.PersistentVolume{}
+	testv1PV := &clustercache.PersistentVolume{}
 	testv1PV.Spec.Capacity = v1.ResourceList{
 		v1.ResourceStorage: *resource.NewQuantity(16*1024*1024*1024, resource.BinarySI),
 	}
@@ -672,7 +672,7 @@ func TestGenerateSlimK8sDiskFromV1PV(t *testing.T) {
 	testv1PV.Spec.StorageClassName = "testStorageClass"
 	cases := []struct {
 		name             string
-		testPV           *v1.PersistentVolume
+		testPV           *clustercache.PersistentVolume
 		expectedSlimDisk *SlimK8sDisk
 		inpRegionID      string
 	}{
@@ -773,7 +773,7 @@ func TestDeterminePVRegion(t *testing.T) {
 	}
 
 	// testPV1 contains the Label with region information as well as node affinity in spec
-	testPV1 := &v1.PersistentVolume{}
+	testPV1 := &clustercache.PersistentVolume{}
 	testPV1.Name = "testPV1"
 	testPV1.Labels = make(map[string]string)
 	testPV1.Labels[ALIBABA_DISK_TOPOLOGY_REGION_LABEL] = "us-east-1"
@@ -784,13 +784,13 @@ func TestDeterminePVRegion(t *testing.T) {
 	}
 
 	// testPV2 contains the only zone label
-	testPV2 := &v1.PersistentVolume{}
+	testPV2 := &clustercache.PersistentVolume{}
 	testPV2.Name = "testPV2"
 	testPV2.Labels = make(map[string]string)
 	testPV2.Labels[ALIBABA_DISK_TOPOLOGY_ZONE_LABEL] = "us-east-1a"
 
 	// testPV3 contains only node affinity in spec
-	testPV3 := &v1.PersistentVolume{}
+	testPV3 := &clustercache.PersistentVolume{}
 	testPV3.Name = "testPV3"
 	testPV3.Spec.NodeAffinity = &v1.VolumeNodeAffinity{
 		Required: &v1.NodeSelector{
@@ -799,12 +799,12 @@ func TestDeterminePVRegion(t *testing.T) {
 	}
 
 	// testPV4 contains no label/annotation or any node affinity
-	testPV4 := &v1.PersistentVolume{}
+	testPV4 := &clustercache.PersistentVolume{}
 	testPV4.Name = "testPV4"
 
 	cases := []struct {
 		name           string
-		inputPV        *v1.PersistentVolume
+		inputPV        *clustercache.PersistentVolume
 		expectedRegion string
 	}{
 		{

+ 1 - 3
pkg/cloud/aws/provider.go

@@ -43,8 +43,6 @@ import (
 	"github.com/aws/aws-sdk-go-v2/service/sts"
 
 	"github.com/jszwec/csvutil"
-
-	v1 "k8s.io/api/core/v1"
 )
 
 const (
@@ -705,7 +703,7 @@ type awsPVKey struct {
 	ProviderID             string
 }
 
-func (aws *AWS) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (aws *AWS) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	providerID := ""
 	if pv.Spec.AWSElasticBlockStore != nil {
 		providerID = pv.Spec.AWSElasticBlockStore.VolumeID

+ 1 - 3
pkg/cloud/azure/provider.go

@@ -31,8 +31,6 @@ import (
 	"github.com/opencost/opencost/pkg/cloud/utils"
 	"github.com/opencost/opencost/pkg/clustercache"
 	"github.com/opencost/opencost/pkg/env"
-
-	v1 "k8s.io/api/core/v1"
 )
 
 const (
@@ -1236,7 +1234,7 @@ type azurePvKey struct {
 	ProviderId             string
 }
 
-func (az *Azure) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (az *Azure) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	providerID := ""
 	if pv.Spec.AzureDisk != nil {
 		providerID = pv.Spec.AzureDisk.DiskName

+ 1 - 2
pkg/cloud/gcp/provider.go

@@ -32,7 +32,6 @@ import (
 	"cloud.google.com/go/compute/metadata"
 	"golang.org/x/oauth2/google"
 	"google.golang.org/api/compute/v1"
-	v1 "k8s.io/api/core/v1"
 )
 
 const GKE_GPU_TAG = "cloud.google.com/gke-accelerator"
@@ -1432,7 +1431,7 @@ func (key *pvKey) GetStorageClass() string {
 	return key.StorageClass
 }
 
-func (gcp *GCP) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (gcp *GCP) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	providerID := ""
 	if pv.Spec.GCEPersistentDisk != nil {
 		providerID = pv.Spec.GCEPersistentDisk.PDName

+ 2 - 4
pkg/cloud/models/models.go

@@ -10,10 +10,8 @@ import (
 	"time"
 
 	"github.com/microcosm-cc/bluemonday"
-	"github.com/opencost/opencost/pkg/clustercache"
-	v1 "k8s.io/api/core/v1"
-
 	"github.com/opencost/opencost/core/pkg/log"
+	"github.com/opencost/opencost/pkg/clustercache"
 	"github.com/opencost/opencost/pkg/config"
 )
 
@@ -317,7 +315,7 @@ type Provider interface {
 	AllNodePricing() (interface{}, error)
 	DownloadPricingData() error
 	GetKey(map[string]string, *clustercache.Node) Key
-	GetPVKey(*v1.PersistentVolume, map[string]string, string) PVKey
+	GetPVKey(*clustercache.PersistentVolume, map[string]string, string) PVKey
 	UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error)
 	UpdateConfigFromConfigMap(map[string]string) (*CustomPricing, error)
 	GetConfig() (*CustomPricing, error)

+ 1 - 2
pkg/cloud/oracle/provider.go

@@ -15,7 +15,6 @@ import (
 	"github.com/opencost/opencost/pkg/cloud/utils"
 	"github.com/opencost/opencost/pkg/clustercache"
 	"github.com/opencost/opencost/pkg/env"
-	v1 "k8s.io/api/core/v1"
 )
 
 const nodePoolIdAnnotation = "oci.oraclecloud.com/node-pool-id"
@@ -142,7 +141,7 @@ func (o *Oracle) GetKey(labels map[string]string, n *clustercache.Node) models.K
 	}
 }
 
-func (o *Oracle) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, _ string) models.PVKey {
+func (o *Oracle) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, _ string) models.PVKey {
 	var providerID string
 	var driver string
 	if pv.Spec.CSI != nil {

+ 1 - 1
pkg/cloud/oracle/provider_test.go

@@ -57,7 +57,7 @@ func TestGetKey(t *testing.T) {
 func TestGetPVKey(t *testing.T) {
 	storageClass := "xyz"
 	providerID := "ocid.abc"
-	pv := &v1.PersistentVolume{
+	pv := &clustercache.PersistentVolume{
 		Spec: v1.PersistentVolumeSpec{
 			StorageClassName: storageClass,
 			PersistentVolumeSource: v1.PersistentVolumeSource{

+ 3 - 5
pkg/cloud/provider/csvprovider.go

@@ -19,10 +19,8 @@ import (
 	"github.com/aws/aws-sdk-go/aws"
 	"github.com/aws/aws-sdk-go/aws/session"
 	"github.com/aws/aws-sdk-go/service/s3"
-	"github.com/opencost/opencost/core/pkg/log"
-	v1 "k8s.io/api/core/v1"
-
 	"github.com/jszwec/csvutil"
+	"github.com/opencost/opencost/core/pkg/log"
 )
 
 const refreshMinutes = 60
@@ -330,7 +328,7 @@ func NodeValueFromMapField(m string, n *clustercache.Node, useRegion bool) strin
 	}
 }
 
-func PVValueFromMapField(m string, n *v1.PersistentVolume) string {
+func PVValueFromMapField(m string, n *clustercache.PersistentVolume) string {
 	mf := strings.Split(m, ".")
 	if len(mf) > 1 && mf[0] == "metadata" {
 		if mf[1] == "name" {
@@ -402,7 +400,7 @@ func (key *csvPVKey) Features() string {
 	return key.ProviderID
 }
 
-func (c *CSVProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (c *CSVProvider) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	id := PVValueFromMapField(c.PVMapField, pv)
 	return &csvPVKey{
 		Labels:                 pv.Labels,

+ 1 - 3
pkg/cloud/provider/customprovider.go

@@ -16,8 +16,6 @@ import (
 	"github.com/opencost/opencost/pkg/cloud/utils"
 	"github.com/opencost/opencost/pkg/clustercache"
 	"github.com/opencost/opencost/pkg/env"
-
-	v1 "k8s.io/api/core/v1"
 )
 
 type NodePrice struct {
@@ -329,7 +327,7 @@ func (cp *CustomProvider) LoadBalancerPricing() (*models.LoadBalancer, error) {
 	}, nil
 }
 
-func (*CustomProvider) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (*CustomProvider) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	return &customPVKey{
 		Labels:                 pv.Labels,
 		StorageClassName:       pv.Spec.StorageClassName,

+ 1 - 3
pkg/cloud/scaleway/provider.go

@@ -19,8 +19,6 @@ import (
 	"github.com/opencost/opencost/pkg/env"
 
 	"github.com/opencost/opencost/core/pkg/log"
-	v1 "k8s.io/api/core/v1"
-
 	"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
 	"github.com/scaleway/scaleway-sdk-go/scw"
 )
@@ -208,7 +206,7 @@ func (key *scalewayPVKey) Features() string {
 	return key.Zone
 }
 
-func (c *Scaleway) GetPVKey(pv *v1.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
+func (c *Scaleway) GetPVKey(pv *clustercache.PersistentVolume, parameters map[string]string, defaultRegion string) models.PVKey {
 	// the csi volume handle is the form <az>/<volume-id>
 	zone := ""
 	if pv.Spec.CSI != nil {

+ 24 - 4
pkg/clustercache/clustercache.go

@@ -186,6 +186,26 @@ func transformStatefulSet(input *appsv1.StatefulSet) *StatefulSet {
 	}
 }
 
+type PersistentVolume struct {
+	Name        string
+	Namespace   string
+	Labels      map[string]string
+	Annotations map[string]string
+	Spec        v1.PersistentVolumeSpec
+	Status      v1.PersistentVolumeStatus
+}
+
+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,
+	}
+}
+
 // ClusterCache defines an contract for an object which caches components within a cluster, ensuring
 // up to date resources using watchers
 type ClusterCache interface {
@@ -217,7 +237,7 @@ type ClusterCache interface {
 	GetAllStatefulSets() []*StatefulSet
 
 	// GetAllPersistentVolumes returns all the cached persistent volumes
-	GetAllPersistentVolumes() []*v1.PersistentVolume
+	GetAllPersistentVolumes() []*PersistentVolume
 
 	// GetAllPersistentVolumeClaims returns all the cached persistent volume claims
 	GetAllPersistentVolumeClaims() []*v1.PersistentVolumeClaim
@@ -411,11 +431,11 @@ func (kcc *KubernetesClusterCache) GetAllStatefulSets() []*StatefulSet {
 	return statefulsets
 }
 
-func (kcc *KubernetesClusterCache) GetAllPersistentVolumes() []*v1.PersistentVolume {
-	var pvs []*v1.PersistentVolume
+func (kcc *KubernetesClusterCache) GetAllPersistentVolumes() []*PersistentVolume {
+	var pvs []*PersistentVolume
 	items := kcc.pvWatch.GetAll()
 	for _, pv := range items {
-		pvs = append(pvs, pv.(*v1.PersistentVolume))
+		pvs = append(pvs, transformPersistentVolume(pv.(*v1.PersistentVolume)))
 	}
 	return pvs
 }

+ 1 - 1
pkg/costmodel/costmodel.go

@@ -951,7 +951,7 @@ func addPVData(cache clustercache.ClusterCache, pvClaimMapping map[string]*Persi
 	return nil
 }
 
-func GetPVCost(pv *costAnalyzerCloud.PV, kpv *v1.PersistentVolume, cp costAnalyzerCloud.Provider, defaultRegion string) error {
+func GetPVCost(pv *costAnalyzerCloud.PV, kpv *clustercache.PersistentVolume, cp costAnalyzerCloud.Provider, defaultRegion string) error {
 	cfg, err := cp.GetConfig()
 	if err != nil {
 		return err

+ 2 - 2
test/cloud_test.go

@@ -98,7 +98,7 @@ func TestNodeValueFromMapField(t *testing.T) {
 
 func TestPVPriceFromCSV(t *testing.T) {
 	nameWant := "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d"
-	pv := &v1.PersistentVolume{}
+	pv := &clustercache.PersistentVolume{}
 	pv.Name = nameWant
 
 	confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
@@ -131,7 +131,7 @@ func TestPVPriceFromCSV(t *testing.T) {
 func TestPVPriceFromCSVStorageClass(t *testing.T) {
 	nameWant := "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d"
 	storageClassWant := "storageclass0"
-	pv := &v1.PersistentVolume{}
+	pv := &clustercache.PersistentVolume{}
 	pv.Name = nameWant
 	pv.Spec.StorageClassName = storageClassWant