Remove metering ConfigMap dependency.
Signed-off-by: Rao Yunkun <yunkunrao@yunify.com>
This commit is contained in:
@@ -14,16 +14,6 @@ type PriceResponse struct {
|
||||
PriceInfo `json:",inline"`
|
||||
}
|
||||
|
||||
// currently init method fill illegal value to hint that metering config file was not mounted yet
|
||||
func (p *PriceInfo) Init() {
|
||||
p.Currency = ""
|
||||
p.CpuPerCorePerHour = -1
|
||||
p.MemPerGigabytesPerHour = -1
|
||||
p.IngressNetworkTrafficPerMegabytesPerHour = -1
|
||||
p.EgressNetworkTrafficPerMegabytesPerHour = -1
|
||||
p.PvcPerGigabytesPerHour = -1
|
||||
}
|
||||
|
||||
type PodStatistic struct {
|
||||
CPUUsage float64 `json:"cpu_usage" description:"cpu_usage"`
|
||||
MemoryUsageWoCache float64 `json:"memory_usage_wo_cache" description:"memory_usage_wo_cache"`
|
||||
|
||||
@@ -41,6 +41,7 @@ import (
|
||||
resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
|
||||
"kubesphere.io/kubesphere/pkg/server/errors"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
meteringclient "kubesphere.io/kubesphere/pkg/simple/client/metering"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/monitoring"
|
||||
)
|
||||
|
||||
@@ -57,8 +58,8 @@ type MonitoringOperator interface {
|
||||
GetWorkspaceStats(workspace string) Metrics
|
||||
|
||||
// meter
|
||||
GetNamedMetersOverTime(metrics []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) (Metrics, error)
|
||||
GetNamedMeters(metrics []string, time time.Time, opt monitoring.QueryOption) (Metrics, error)
|
||||
GetNamedMetersOverTime(metrics []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption, priceInfo meteringclient.PriceInfo) (Metrics, error)
|
||||
GetNamedMeters(metrics []string, time time.Time, opt monitoring.QueryOption, priceInfo meteringclient.PriceInfo) (Metrics, error)
|
||||
GetAppWorkloads(ns string, apps []string) map[string][]string
|
||||
GetSerivePodsMap(ns string, services []string) map[string][]string
|
||||
}
|
||||
@@ -421,7 +422,7 @@ func generateScalingFactorMap(step time.Duration) map[string]float64 {
|
||||
return scalingMap
|
||||
}
|
||||
|
||||
func (mo monitoringOperator) GetNamedMetersOverTime(meters []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) (metrics Metrics, err error) {
|
||||
func (mo monitoringOperator) GetNamedMetersOverTime(meters []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption, priceInfo meteringclient.PriceInfo) (metrics Metrics, err error) {
|
||||
|
||||
if step.Hours() < 1 {
|
||||
klog.Warning("step should be longer than one hour")
|
||||
@@ -458,13 +459,13 @@ func (mo monitoringOperator) GetNamedMetersOverTime(meters []string, start, end
|
||||
sMap := generateScalingFactorMap(step)
|
||||
|
||||
for i, _ := range ress {
|
||||
ress[i].MetricData = updateMetricStatData(ress[i], sMap)
|
||||
ress[i].MetricData = updateMetricStatData(ress[i], sMap, priceInfo)
|
||||
}
|
||||
|
||||
return Metrics{Results: ress}, nil
|
||||
}
|
||||
|
||||
func (mo monitoringOperator) GetNamedMeters(meters []string, time time.Time, opt monitoring.QueryOption) (Metrics, error) {
|
||||
func (mo monitoringOperator) GetNamedMeters(meters []string, time time.Time, opt monitoring.QueryOption, priceInfo meteringclient.PriceInfo) (Metrics, error) {
|
||||
|
||||
metersPerHour := mo.getNamedMetersWithHourInterval(meters, time, opt)
|
||||
|
||||
@@ -472,7 +473,7 @@ func (mo monitoringOperator) GetNamedMeters(meters []string, time time.Time, opt
|
||||
|
||||
res := metersPerHour.Results[metricIndex]
|
||||
|
||||
metersPerHour.Results[metricIndex].MetricData = updateMetricStatData(res, nil)
|
||||
metersPerHour.Results[metricIndex].MetricData = updateMetricStatData(res, nil, priceInfo)
|
||||
}
|
||||
|
||||
return metersPerHour, nil
|
||||
|
||||
@@ -3,12 +3,10 @@ package monitoring
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/yaml"
|
||||
"k8s.io/klog"
|
||||
|
||||
meteringclient "kubesphere.io/kubesphere/pkg/simple/client/metering"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/monitoring"
|
||||
)
|
||||
|
||||
@@ -76,56 +74,6 @@ var MeterResourceMap = map[string]int{
|
||||
"meter_pod_pvc_bytes_total": METER_RESOURCE_TYPE_PVC,
|
||||
}
|
||||
|
||||
type PriceInfo struct {
|
||||
CpuPerCorePerHour float64 `json:"cpuPerCorePerHour" yaml:"cpuPerCorePerHour"`
|
||||
MemPerGigabytesPerHour float64 `json:"memPerGigabytesPerHour" yaml:"memPerGigabytesPerHour"`
|
||||
IngressNetworkTrafficPerMegabytesPerHour float64 `json:"ingressNetworkTrafficPerMegabytesPerHour" yaml:"ingressNetworkTrafficPerGiagabytesPerHour"`
|
||||
EgressNetworkTrafficPerMegabytesPerHour float64 `json:"egressNetworkTrafficPerMegabytesPerHour" yaml:"egressNetworkTrafficPerGigabytesPerHour"`
|
||||
PvcPerGigabytesPerHour float64 `json:"pvcPerGigabytesPerHour" yaml:"pvcPerGigabytesPerHour"`
|
||||
CurrencyUnit string `json:"currencyUnit" yaml:"currencyUnit"`
|
||||
}
|
||||
|
||||
type Billing struct {
|
||||
PriceInfo PriceInfo `json:"priceInfo" yaml:"priceInfo"`
|
||||
}
|
||||
|
||||
type MeterConfig struct {
|
||||
RetentionDay string `json:"retentionDay" yaml:"retentionDay"`
|
||||
Billing Billing `json:"billing" yaml:"billing"`
|
||||
}
|
||||
|
||||
func (mc MeterConfig) GetPriceInfo() PriceInfo {
|
||||
return mc.Billing.PriceInfo
|
||||
}
|
||||
|
||||
func LoadYaml() (*MeterConfig, error) {
|
||||
|
||||
var meterConfig MeterConfig
|
||||
var mf *os.File
|
||||
var err error
|
||||
|
||||
if _, err := os.Stat(meteringConfigName); os.IsNotExist(err) {
|
||||
mf, err = os.Open(filepath.Join(meteringConfigDir, meteringConfigName))
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
mf, err = os.Open(meteringConfigName)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err = yaml.NewYAMLOrJSONDecoder(mf, 1024).Decode(&meterConfig); err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &meterConfig, nil
|
||||
}
|
||||
|
||||
func getMaxPointValue(points []monitoring.Point) string {
|
||||
var max *big.Float
|
||||
for i, p := range points {
|
||||
@@ -181,16 +129,6 @@ func getAvgPointValue(points []monitoring.Point) string {
|
||||
return fmt.Sprintf(generateFloatFormat(meteringDefaultPrecision), sum.Quo(sum, length))
|
||||
}
|
||||
|
||||
func getCurrencyUnit() string {
|
||||
meterConfig, err := LoadYaml()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return meterConfig.GetPriceInfo().CurrencyUnit
|
||||
}
|
||||
|
||||
func generateFloatFormat(precision int) string {
|
||||
return "%." + fmt.Sprintf("%d", precision) + "f"
|
||||
}
|
||||
@@ -204,7 +142,7 @@ func getResourceUnit(meterName string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func getFeeWithMeterName(meterName string, sum string) string {
|
||||
func getFeeWithMeterName(meterName string, sum string, priceInfo meteringclient.PriceInfo) string {
|
||||
|
||||
s, ok := new(big.Float).SetString(sum)
|
||||
if !ok {
|
||||
@@ -212,13 +150,6 @@ func getFeeWithMeterName(meterName string, sum string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
meterConfig, err := LoadYaml()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return ""
|
||||
}
|
||||
priceInfo := meterConfig.GetPriceInfo()
|
||||
|
||||
if resourceType, ok := MeterResourceMap[meterName]; !ok {
|
||||
klog.Errorf("invlaid meter %v", meterName)
|
||||
return ""
|
||||
@@ -267,7 +198,7 @@ func getFeeWithMeterName(meterName string, sum string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func updateMetricStatData(metric monitoring.Metric, scalingMap map[string]float64) monitoring.MetricData {
|
||||
func updateMetricStatData(metric monitoring.Metric, scalingMap map[string]float64, priceInfo meteringclient.PriceInfo) monitoring.MetricData {
|
||||
metricName := metric.MetricName
|
||||
metricData := metric.MetricData
|
||||
for index, metricValue := range metricData.MetricValues {
|
||||
@@ -293,14 +224,14 @@ func updateMetricStatData(metric monitoring.Metric, scalingMap map[string]float6
|
||||
if metricData.MetricType == monitoring.MetricTypeMatrix {
|
||||
sum := getSumPointValue(metricData.MetricValues[index].Series)
|
||||
metricData.MetricValues[index].SumValue = sum
|
||||
metricData.MetricValues[index].Fee = getFeeWithMeterName(metricName, sum)
|
||||
metricData.MetricValues[index].Fee = getFeeWithMeterName(metricName, sum, priceInfo)
|
||||
} else {
|
||||
sum := getSumPointValue([]monitoring.Point{*metricValue.Sample})
|
||||
metricData.MetricValues[index].SumValue = sum
|
||||
metricData.MetricValues[index].Fee = getFeeWithMeterName(metricName, sum)
|
||||
metricData.MetricValues[index].Fee = getFeeWithMeterName(metricName, sum, priceInfo)
|
||||
}
|
||||
|
||||
metricData.MetricValues[index].CurrencyUnit = getCurrencyUnit()
|
||||
metricData.MetricValues[index].CurrencyUnit = priceInfo.CurrencyUnit
|
||||
metricData.MetricValues[index].ResourceUnit = getResourceUnit(metricName)
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
monitoringmodel "kubesphere.io/kubesphere/pkg/models/monitoring"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
meteringclient "kubesphere.io/kubesphere/pkg/simple/client/metering"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/monitoring"
|
||||
)
|
||||
|
||||
@@ -538,7 +539,7 @@ func (t *tenantOperator) makeQueryOptions(user user.Info, q meteringv1alpha1.Que
|
||||
return qo, nil
|
||||
}
|
||||
|
||||
func (t *tenantOperator) ProcessNamedMetersQuery(q QueryOptions) (metrics monitoringmodel.Metrics, err error) {
|
||||
func (t *tenantOperator) ProcessNamedMetersQuery(q QueryOptions, priceInfo meteringclient.PriceInfo) (metrics monitoringmodel.Metrics, err error) {
|
||||
var meters []string
|
||||
for _, meter := range q.NamedMetrics {
|
||||
if !strings.HasPrefix(meter, monitoringmodel.MetricMeterPrefix) {
|
||||
@@ -559,20 +560,20 @@ func (t *tenantOperator) ProcessNamedMetersQuery(q QueryOptions) (metrics monito
|
||||
|
||||
_, ok := q.Option.(monitoring.ApplicationsOption)
|
||||
if ok {
|
||||
metrics, err = t.processApplicationMetersQuery(meters, q)
|
||||
metrics, err = t.processApplicationMetersQuery(meters, q, priceInfo)
|
||||
return
|
||||
}
|
||||
|
||||
_, ok = q.Option.(monitoring.ServicesOption)
|
||||
if ok {
|
||||
metrics, err = t.processServiceMetersQuery(meters, q)
|
||||
metrics, err = t.processServiceMetersQuery(meters, q, priceInfo)
|
||||
return
|
||||
}
|
||||
|
||||
if q.isRangeQuery() {
|
||||
metrics, err = t.mo.GetNamedMetersOverTime(meters, q.Start, q.End, q.Step, q.Option)
|
||||
metrics, err = t.mo.GetNamedMetersOverTime(meters, q.Start, q.End, q.Step, q.Option, priceInfo)
|
||||
} else {
|
||||
metrics, err = t.mo.GetNamedMeters(meters, q.Time, q.Option)
|
||||
metrics, err = t.mo.GetNamedMeters(meters, q.Time, q.Option, priceInfo)
|
||||
if q.shouldSort() {
|
||||
metrics = *metrics.Sort(q.Target, q.Order, q.Identifier).Page(q.Page, q.Limit)
|
||||
}
|
||||
@@ -591,7 +592,7 @@ func getMetricPosMap(metrics []monitoring.Metric) map[string]int {
|
||||
return metricMap
|
||||
}
|
||||
|
||||
func (t *tenantOperator) processApplicationMetersQuery(meters []string, q QueryOptions) (res monitoringmodel.Metrics, err error) {
|
||||
func (t *tenantOperator) processApplicationMetersQuery(meters []string, q QueryOptions, priceInfo meteringclient.PriceInfo) (res monitoringmodel.Metrics, err error) {
|
||||
var metricMap = make(map[string]int)
|
||||
var current_res monitoringmodel.Metrics
|
||||
|
||||
@@ -612,9 +613,9 @@ func (t *tenantOperator) processApplicationMetersQuery(meters []string, q QueryO
|
||||
}
|
||||
|
||||
if q.isRangeQuery() {
|
||||
current_res, err = t.mo.GetNamedMetersOverTime(meters, q.Start, q.End, q.Step, opt)
|
||||
current_res, err = t.mo.GetNamedMetersOverTime(meters, q.Start, q.End, q.Step, opt, priceInfo)
|
||||
} else {
|
||||
current_res, err = t.mo.GetNamedMeters(meters, q.Time, opt)
|
||||
current_res, err = t.mo.GetNamedMeters(meters, q.Time, opt, priceInfo)
|
||||
}
|
||||
|
||||
if res.Results == nil {
|
||||
@@ -639,7 +640,7 @@ func (t *tenantOperator) processApplicationMetersQuery(meters []string, q QueryO
|
||||
return
|
||||
}
|
||||
|
||||
func (t *tenantOperator) processServiceMetersQuery(meters []string, q QueryOptions) (res monitoringmodel.Metrics, err error) {
|
||||
func (t *tenantOperator) processServiceMetersQuery(meters []string, q QueryOptions, priceInfo meteringclient.PriceInfo) (res monitoringmodel.Metrics, err error) {
|
||||
var metricMap = make(map[string]int)
|
||||
var current_res monitoringmodel.Metrics
|
||||
|
||||
@@ -659,9 +660,9 @@ func (t *tenantOperator) processServiceMetersQuery(meters []string, q QueryOptio
|
||||
}
|
||||
|
||||
if q.isRangeQuery() {
|
||||
current_res, err = t.mo.GetNamedMetersOverTime(meters, q.Start, q.End, q.Step, opt)
|
||||
current_res, err = t.mo.GetNamedMetersOverTime(meters, q.Start, q.End, q.Step, opt, priceInfo)
|
||||
} else {
|
||||
current_res, err = t.mo.GetNamedMeters(meters, q.Time, opt)
|
||||
current_res, err = t.mo.GetNamedMeters(meters, q.Time, opt, priceInfo)
|
||||
}
|
||||
|
||||
if res.Results == nil {
|
||||
|
||||
@@ -63,6 +63,7 @@ import (
|
||||
auditingclient "kubesphere.io/kubesphere/pkg/simple/client/auditing"
|
||||
eventsclient "kubesphere.io/kubesphere/pkg/simple/client/events"
|
||||
loggingclient "kubesphere.io/kubesphere/pkg/simple/client/logging"
|
||||
meteringclient "kubesphere.io/kubesphere/pkg/simple/client/metering"
|
||||
monitoringclient "kubesphere.io/kubesphere/pkg/simple/client/monitoring"
|
||||
"kubesphere.io/kubesphere/pkg/utils/stringutils"
|
||||
)
|
||||
@@ -90,8 +91,8 @@ type Interface interface {
|
||||
PatchNamespace(workspace string, namespace *corev1.Namespace) (*corev1.Namespace, error)
|
||||
PatchWorkspace(workspace string, data json.RawMessage) (*tenantv1alpha2.WorkspaceTemplate, error)
|
||||
ListClusters(info user.Info) (*api.ListResult, error)
|
||||
Metering(user user.Info, queryParam *meteringv1alpha1.Query) (monitoring.Metrics, error)
|
||||
MeteringHierarchy(user user.Info, queryParam *meteringv1alpha1.Query) (metering.ResourceStatistic, error)
|
||||
Metering(user user.Info, queryParam *meteringv1alpha1.Query, priceInfo meteringclient.PriceInfo) (monitoring.Metrics, error)
|
||||
MeteringHierarchy(user user.Info, queryParam *meteringv1alpha1.Query, priceInfo meteringclient.PriceInfo) (metering.ResourceStatistic, error)
|
||||
CreateWorkspaceResourceQuota(workspace string, resourceQuota *quotav1alpha2.ResourceQuota) (*quotav1alpha2.ResourceQuota, error)
|
||||
DeleteWorkspaceResourceQuota(workspace string, resourceQuotaName string) error
|
||||
UpdateWorkspaceResourceQuota(workspace string, resourceQuota *quotav1alpha2.ResourceQuota) (*quotav1alpha2.ResourceQuota, error)
|
||||
@@ -991,7 +992,7 @@ func (t *tenantOperator) Auditing(user user.Info, queryParam *auditingv1alpha1.Q
|
||||
})
|
||||
}
|
||||
|
||||
func (t *tenantOperator) Metering(user user.Info, query *meteringv1alpha1.Query) (metrics monitoring.Metrics, err error) {
|
||||
func (t *tenantOperator) Metering(user user.Info, query *meteringv1alpha1.Query, priceInfo meteringclient.PriceInfo) (metrics monitoring.Metrics, err error) {
|
||||
|
||||
var opt QueryOptions
|
||||
|
||||
@@ -999,13 +1000,13 @@ func (t *tenantOperator) Metering(user user.Info, query *meteringv1alpha1.Query)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
metrics, err = t.ProcessNamedMetersQuery(opt)
|
||||
metrics, err = t.ProcessNamedMetersQuery(opt, priceInfo)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (t *tenantOperator) MeteringHierarchy(user user.Info, queryParam *meteringv1alpha1.Query) (metering.ResourceStatistic, error) {
|
||||
res, err := t.Metering(user, queryParam)
|
||||
func (t *tenantOperator) MeteringHierarchy(user user.Info, queryParam *meteringv1alpha1.Query, priceInfo meteringclient.PriceInfo) (metering.ResourceStatistic, error) {
|
||||
res, err := t.Metering(user, queryParam, priceInfo)
|
||||
if err != nil {
|
||||
return metering.ResourceStatistic{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user