Sfoglia il codice sorgente

Merge branch 'develop' into develop

Signed-off-by: Matt Ray <github@mattray.dev>
Matt Ray 3 anni fa
parent
commit
7bf8fcb73f

+ 3 - 3
pkg/cloud/aliyunprovider.go

@@ -4,7 +4,7 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"io/ioutil"
+	"os"
 	"regexp"
 	"strings"
 	"sync"
@@ -591,7 +591,7 @@ func (alibaba *Alibaba) loadAlibabaAuthSecretAndSetEnv(force bool) error {
 		return fmt.Errorf("failed to locate service account file: %s with err: %w", authSecretPath, err)
 	}
 
-	result, err := ioutil.ReadFile(authSecretPath)
+	result, err := os.ReadFile(authSecretPath)
 	if err != nil {
 		return fmt.Errorf("failed to read service account file: %s with err: %w", authSecretPath, err)
 	}
@@ -676,7 +676,7 @@ func (alibaba *Alibaba) UpdateConfig(r io.Reader, updateType string) (*CustomPri
 				return err
 			}
 			for k, v := range a {
-				kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
+				kUpper := toTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
 				vstr, ok := v.(string)
 				if ok {
 					err := SetCustomPricingField(c, kUpper, vstr)

+ 1 - 1
pkg/cloud/awsprovider.go

@@ -591,7 +591,7 @@ func (aws *AWS) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 				return err
 			}
 			for k, v := range a {
-				kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
+				kUpper := toTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
 				vstr, ok := v.(string)
 				if ok {
 					err := SetCustomPricingField(c, kUpper, vstr)

+ 8 - 13
pkg/cloud/azureprovider.go

@@ -15,16 +15,6 @@ import (
 
 	"github.com/opencost/opencost/pkg/kubecost"
 
-	"github.com/opencost/opencost/pkg/clustercache"
-	"github.com/opencost/opencost/pkg/env"
-	"github.com/opencost/opencost/pkg/log"
-	"github.com/opencost/opencost/pkg/util"
-	"github.com/opencost/opencost/pkg/util/fileutil"
-	"github.com/opencost/opencost/pkg/util/json"
-	"github.com/opencost/opencost/pkg/util/timeutil"
-	"golang.org/x/text/cases"
-	"golang.org/x/text/language"
-
 	"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
 	"github.com/Azure/azure-sdk-for-go/services/preview/commerce/mgmt/2015-06-01-preview/commerce"
 	"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
@@ -32,6 +22,13 @@ import (
 	"github.com/Azure/go-autorest/autorest"
 	"github.com/Azure/go-autorest/autorest/azure"
 	"github.com/Azure/go-autorest/autorest/azure/auth"
+	"github.com/opencost/opencost/pkg/clustercache"
+	"github.com/opencost/opencost/pkg/env"
+	"github.com/opencost/opencost/pkg/log"
+	"github.com/opencost/opencost/pkg/util"
+	"github.com/opencost/opencost/pkg/util/fileutil"
+	"github.com/opencost/opencost/pkg/util/json"
+	"github.com/opencost/opencost/pkg/util/timeutil"
 	v1 "k8s.io/api/core/v1"
 )
 
@@ -46,8 +43,6 @@ const (
 	AzureStorageUpdateType           = "AzureStorage"
 )
 
-var toTitle = cases.Title(language.Und, cases.NoLower)
-
 var (
 	regionCodeMappings = map[string]string{
 		"ap": "asia",
@@ -1238,7 +1233,7 @@ func (az *Azure) getDisks() ([]*compute.Disk, error) {
 			d := d
 			disks = append(disks, &d)
 		}
-		err := diskPage.Next()
+		err := diskPage.NextWithContext(context.Background())
 		if err != nil {
 			return nil, fmt.Errorf("error getting next page: %v", err)
 		}

+ 1 - 2
pkg/cloud/customprovider.go

@@ -6,7 +6,6 @@ import (
 	"github.com/opencost/opencost/pkg/kubecost"
 	"io"
 	"strconv"
-	"strings"
 	"sync"
 	"time"
 
@@ -77,7 +76,7 @@ func (cp *CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomP
 	// Update Config
 	c, err := cp.Config.Update(func(c *CustomPricing) error {
 		for k, v := range a {
-			kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
+			kUpper := toTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
 			vstr, ok := v.(string)
 			if ok {
 				err := SetCustomPricingField(c, kUpper, vstr)

+ 3 - 4
pkg/cloud/gcpprovider.go

@@ -27,7 +27,6 @@ import (
 
 	"cloud.google.com/go/bigquery"
 	"cloud.google.com/go/compute/metadata"
-	"golang.org/x/oauth2"
 	"golang.org/x/oauth2/google"
 	compute "google.golang.org/api/compute/v1"
 	v1 "k8s.io/api/core/v1"
@@ -285,7 +284,7 @@ func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, er
 				return err
 			}
 			for k, v := range a {
-				kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
+				kUpper := toTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
 				vstr, ok := v.(string)
 				if ok {
 					err := SetCustomPricingField(c, kUpper, vstr)
@@ -352,7 +351,7 @@ func (gcp *GCP) getAllAddresses() (*compute.AddressAggregatedList, error) {
 		return nil, err
 	}
 
-	client, err := google.DefaultClient(oauth2.NoContext,
+	client, err := google.DefaultClient(context.TODO(),
 		"https://www.googleapis.com/auth/compute.readonly")
 	if err != nil {
 		return nil, err
@@ -392,7 +391,7 @@ func (gcp *GCP) getAllDisks() (*compute.DiskAggregatedList, error) {
 		return nil, err
 	}
 
-	client, err := google.DefaultClient(oauth2.NoContext,
+	client, err := google.DefaultClient(context.TODO(),
 		"https://www.googleapis.com/auth/compute.readonly")
 	if err != nil {
 		return nil, err

+ 7 - 0
pkg/cloud/provider.go

@@ -4,6 +4,8 @@ import (
 	"database/sql"
 	"errors"
 	"fmt"
+	"golang.org/x/text/cases"
+	"golang.org/x/text/language"
 	"io"
 	"net/http"
 	"regexp"
@@ -35,6 +37,8 @@ const defaultShareTenancyCost = "true"
 const KarpenterCapacityTypeLabel = "karpenter.sh/capacity-type"
 const KarpenterCapacitySpotTypeValue = "spot"
 
+var toTitle = cases.Title(language.Und, cases.NoLower)
+
 var createTableStatements = []string{
 	`CREATE TABLE IF NOT EXISTS names (
 		cluster_id VARCHAR(255) NOT NULL,
@@ -619,6 +623,9 @@ func GetClusterMeta(cluster_id string) (string, string, error) {
 	address := env.GetSQLAddress()
 	connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
 	db, err := sql.Open("postgres", connStr)
+	if err != nil {
+		return "", "", err
+	}
 	defer db.Close()
 	query := `SELECT cluster_id, cluster_name
 	FROM names

+ 1 - 2
pkg/cloud/providerconfig.go

@@ -5,7 +5,6 @@ import (
 	gopath "path"
 	"reflect"
 	"strconv"
-	"strings"
 	"sync"
 
 	"github.com/microcosm-cc/bluemonday"
@@ -191,7 +190,7 @@ func (pc *ProviderConfig) UpdateFromMap(a map[string]string) (*CustomPricing, er
 	return pc.Update(func(c *CustomPricing) error {
 		for k, v := range a {
 			// Just so we consistently supply / receive the same values, uppercase the first letter.
-			kUpper := strings.Title(k)
+			kUpper := toTitle.String(k)
 			if kUpper == "CPU" || kUpper == "SpotCPU" || kUpper == "RAM" || kUpper == "SpotRAM" || kUpper == "GPU" || kUpper == "Storage" {
 				val, err := strconv.ParseFloat(v, 64)
 				if err != nil {

+ 1 - 1
pkg/cloud/scalewayprovider.go

@@ -289,7 +289,7 @@ func (c *Scaleway) UpdateConfig(r io.Reader, updateType string) (*CustomPricing,
 			return err
 		}
 		for k, v := range a {
-			kUpper := strings.Title(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
+			kUpper := toTitle.String(k) // Just so we consistently supply / receive the same values, uppercase the first letter.
 			vstr, ok := v.(string)
 			if ok {
 				err := SetCustomPricingField(c, kUpper, vstr)

+ 2 - 2
pkg/clustercache/clusterexporter.go

@@ -29,8 +29,8 @@ type clusterEncoding struct {
 	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"`
+	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.

+ 1 - 1
pkg/costmodel/sql.go

@@ -95,10 +95,10 @@ func CostDataRangeFromSQL(field string, value string, window string, start strin
 	address := env.GetSQLAddress()
 	connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
 	db, err := sql.Open("postgres", connStr)
-	defer db.Close()
 	if err != nil {
 		return nil, err
 	}
+	defer db.Close()
 	nodes, err := getNodeCosts(db)
 	if err != nil {
 		return nil, err

+ 8 - 0
pkg/kubecost/allocation.go

@@ -2103,6 +2103,14 @@ func (asr *AllocationSetRange) NewAccumulation() (*AllocationSet, error) {
 	var allocSet *AllocationSet
 	var err error
 
+	if asr == nil {
+		return nil, fmt.Errorf("nil AllocationSetRange in accumulation")
+	}
+
+	if len(asr.Allocations) == 0 {
+		return nil, fmt.Errorf("AllocationSetRange has empty AssetSet in accumulation")
+	}
+
 	for _, as := range asr.Allocations {
 		if allocSet == nil {
 			allocSet = as.Clone()

+ 3 - 3
pkg/kubecost/allocation_json.go

@@ -62,9 +62,9 @@ func (aj *AllocationJSON) BuildFromAllocation(a *Allocation) {
 	aj.Name = a.Name
 	aj.Properties = a.Properties
 	aj.Window = a.Window
-	aj.Start = a.Start.Format(time.RFC3339)
-	aj.End = a.End.Format(time.RFC3339)
-	aj.Minutes = formatFloat64ForResponse(a.Minutes())
+	aj.Start = a.Start.UTC().Format(time.RFC3339)
+	aj.End = a.End.UTC().Format(time.RFC3339)
+  aj.Minutes = formatFloat64ForResponse(a.Minutes())
 	aj.CPUCores = formatFloat64ForResponse(a.CPUCores())
 	aj.CPUCoreRequestAverage = formatFloat64ForResponse(a.CPUCoreRequestAverage)
 	aj.CPUCoreUsageAverage = formatFloat64ForResponse(a.CPUCoreUsageAverage)

+ 8 - 0
pkg/kubecost/asset.go

@@ -3249,6 +3249,14 @@ func (asr *AssetSetRange) NewAccumulation() (*AssetSet, error) {
 	var assetSet *AssetSet
 	var err error
 
+	if asr == nil {
+		return nil, fmt.Errorf("nil AssetSetRange in accumulation")
+	}
+
+	if len(asr.Assets) == 0 {
+		return nil, fmt.Errorf("AssetSetRange has empty AssetSet in accumulation")
+	}
+
 	for _, as := range asr.Assets {
 		if assetSet == nil {
 			assetSet = as.Clone()

+ 8 - 8
pkg/kubecost/cloudcostitem.go

@@ -73,11 +73,11 @@ func (ccip CloudCostItemProperties) Key() string {
 // CloudCostItem represents a CUR line item, identifying a cloud resource and
 // its cost over some period of time.
 type CloudCostItem struct {
-	Properties   CloudCostItemProperties
-	IsKubernetes bool
-	Window       Window
-	Cost         float64
-	NetCost      float64
+	Properties   CloudCostItemProperties `json:"properties"`
+	IsKubernetes bool                    `json:"isKubernetes"`
+	Window       Window                  `json:"window"`
+	Cost         float64                 `json:"cost"`
+	NetCost      float64                 `json:"netCost"`
 }
 
 // NewCloudCostItem instantiates a new CloudCostItem asset
@@ -129,9 +129,9 @@ func (cci *CloudCostItem) add(that *CloudCostItem) {
 }
 
 type CloudCostItemSet struct {
-	CloudCostItems map[string]*CloudCostItem
-	Window         Window
-	Integration    string
+	CloudCostItems map[string]*CloudCostItem `json:"items"`
+	Window         Window                    `json:"window"`
+	Integration    string                    `json:"-"`
 }
 
 // NewAssetSet instantiates a new AssetSet and, optionally, inserts

+ 1 - 1
pkg/kubecost/window.go

@@ -584,7 +584,7 @@ func (w Window) Minutes() float64 {
 // 	return true
 // }
 
-func (w Window) Set(start, end *time.Time) {
+func (w *Window) Set(start, end *time.Time) {
 	w.start = start
 	w.end = end
 }

+ 1 - 1
pkg/util/mathutil/mathutil.go

@@ -7,7 +7,7 @@ func Approximately(exp, act float64) bool {
 }
 
 func ApproximatelyPct(exp, act, pct float64) bool {
-	delta := (exp * pct)
+	delta := exp * pct
 	if delta < 0.00001 {
 		delta = 0.00001
 	}