瀏覽代碼

return management platform information

AjayTripathy 6 年之前
父節點
當前提交
c0702c1d37
共有 5 個文件被更改,包括 74 次插入1 次删除
  1. 18 0
      cloud/awsprovider.go
  2. 18 0
      cloud/azureprovider.go
  3. 15 0
      cloud/gcpprovider.go
  4. 9 1
      cloud/provider.go
  5. 14 0
      main.go

+ 18 - 0
cloud/awsprovider.go

@@ -234,6 +234,24 @@ type AwsAthenaInfo struct {
 	AccountID        string `json:"projectID"`
 	AccountID        string `json:"projectID"`
 }
 }
 
 
+func (aws *AWS) GetManagementPlatform() (string, error) {
+	nodes, err := aws.Clientset.CoreV1().Nodes().List(metav1.ListOptions{})
+	if err != nil {
+		return "", err
+	}
+	if len(nodes.Items) > 0 {
+		n := nodes.Items[0]
+		version := n.Status.NodeInfo.KubeletVersion
+		if strings.Contains(version, "eks") {
+			return "eks", nil
+		}
+		if _, ok := n.Labels["kops.k8s.io/instancegroup"]; ok {
+			return "kops", nil
+		}
+	}
+	return "", nil
+}
+
 func (aws *AWS) GetConfig() (*CustomPricing, error) {
 func (aws *AWS) GetConfig() (*CustomPricing, error) {
 	c, err := GetDefaultPricingData("aws.json")
 	c, err := GetDefaultPricingData("aws.json")
 	if err != nil {
 	if err != nil {

+ 18 - 0
cloud/azureprovider.go

@@ -22,6 +22,8 @@ import (
 	"github.com/Azure/go-autorest/autorest/azure"
 	"github.com/Azure/go-autorest/autorest/azure"
 	"github.com/Azure/go-autorest/autorest/azure/auth"
 	"github.com/Azure/go-autorest/autorest/azure/auth"
 	v1 "k8s.io/api/core/v1"
 	v1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/client-go/kubernetes"
 	"k8s.io/klog"
 	"k8s.io/klog"
 )
 )
 
 
@@ -163,6 +165,7 @@ func checkRegionID(regionID string, regions map[string]string) bool {
 type Azure struct {
 type Azure struct {
 	allPrices               map[string]*Node
 	allPrices               map[string]*Node
 	DownloadPricingDataLock sync.RWMutex
 	DownloadPricingDataLock sync.RWMutex
+	Clientset               *kubernetes.Clientset
 }
 }
 
 
 type azureKey struct {
 type azureKey struct {
@@ -261,6 +264,21 @@ func getMachineTypeVariants(mt string) []string {
 	return []string{}
 	return []string{}
 }
 }
 
 
+func (az *Azure) GetManagementPlatform() (string, error) {
+	nodes, err := az.Clientset.CoreV1().Nodes().List(metav1.ListOptions{})
+	if err != nil {
+		return "", err
+	}
+	if len(nodes.Items) > 0 {
+		n := nodes.Items[0]
+		providerID := n.Spec.ProviderID
+		if strings.Contains(providerID, "aks") {
+			return "aks", nil
+		}
+	}
+	return "", nil
+}
+
 // DownloadPricingData uses provided azure "best guesses" for pricing
 // DownloadPricingData uses provided azure "best guesses" for pricing
 func (az *Azure) DownloadPricingData() error {
 func (az *Azure) DownloadPricingData() error {
 
 

+ 15 - 0
cloud/gcpprovider.go

@@ -93,6 +93,21 @@ type BigQueryConfig struct {
 	Key                map[string]string `json:"key"`
 	Key                map[string]string `json:"key"`
 }
 }
 
 
+func (gcp *GCP) GetManagementPlatform() (string, error) {
+	nodes, err := gcp.Clientset.CoreV1().Nodes().List(metav1.ListOptions{})
+	if err != nil {
+		return "", err
+	}
+	if len(nodes.Items) > 0 {
+		n := nodes.Items[0]
+		version := n.Status.NodeInfo.KubeletVersion
+		if strings.Contains(version, "gke") {
+			return "gke", nil
+		}
+	}
+	return "", nil
+}
+
 func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
 func (gcp *GCP) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
 	c, err := GetDefaultPricingData("gcp.json")
 	c, err := GetDefaultPricingData("gcp.json")
 	if err != nil {
 	if err != nil {

+ 9 - 1
cloud/provider.go

@@ -86,6 +86,7 @@ type Provider interface {
 	GetPVKey(*v1.PersistentVolume, map[string]string) PVKey
 	GetPVKey(*v1.PersistentVolume, map[string]string) PVKey
 	UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error)
 	UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error)
 	GetConfig() (*CustomPricing, error)
 	GetConfig() (*CustomPricing, error)
+	GetManagementPlatform() (string, error)
 
 
 	ExternalAllocations(string, string, string) ([]*OutOfClusterAllocation, error)
 	ExternalAllocations(string, string, string) ([]*OutOfClusterAllocation, error)
 }
 }
@@ -168,6 +169,7 @@ type CustomPricing struct {
 	AzureClientSecret   string `json:"azureClientSecret"`
 	AzureClientSecret   string `json:"azureClientSecret"`
 	AzureTenantID       string `json:"azureTenantID"`
 	AzureTenantID       string `json:"azureTenantID"`
 	CurrencyCode        string `json:"currencyCode"`
 	CurrencyCode        string `json:"currencyCode"`
+	Discount            string `json:"discount"`
 }
 }
 
 
 func SetCustomPricingField(obj *CustomPricing, name string, value string) error {
 func SetCustomPricingField(obj *CustomPricing, name string, value string) error {
@@ -209,6 +211,10 @@ func (*CustomProvider) GetConfig() (*CustomPricing, error) {
 	return GetDefaultPricingData("default.json")
 	return GetDefaultPricingData("default.json")
 }
 }
 
 
+func (*CustomProvider) GetManagementPlatform() (string, error) {
+	return "", nil
+}
+
 func (*CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
 func (*CustomProvider) UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error) {
 	c, err := GetDefaultPricingData("default.json")
 	c, err := GetDefaultPricingData("default.json")
 	if err != nil {
 	if err != nil {
@@ -377,7 +383,9 @@ func NewProvider(clientset *kubernetes.Clientset, apiKey string) (Provider, erro
 		}, nil
 		}, nil
 	} else if strings.HasPrefix(provider, "azure") {
 	} else if strings.HasPrefix(provider, "azure") {
 		klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
 		klog.V(2).Info("Found ProviderID starting with \"azure\", using Azure Provider")
-		return &Azure{}, nil
+		return &Azure{
+			Clientset: clientset,
+		}, nil
 	} else {
 	} else {
 		klog.V(2).Info("Unsupported provider, falling back to default")
 		klog.V(2).Info("Unsupported provider, falling back to default")
 		return &CustomProvider{
 		return &CustomProvider{

+ 14 - 0
main.go

@@ -270,6 +270,19 @@ func (p *Accesses) UpdateConfigByKey(w http.ResponseWriter, r *http.Request, ps
 	return
 	return
 }
 }
 
 
+func (p *Accesses) ManagementPlatform(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	w.Header().Set("Content-Type", "application/json")
+	w.Header().Set("Access-Control-Allow-Origin", "*")
+
+	data, err := p.Cloud.GetManagementPlatform()
+	if err != nil {
+		w.Write(wrapData(data, err))
+		return
+	}
+	w.Write(wrapData(data, err))
+	return
+}
+
 func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 	w.WriteHeader(200)
 	w.WriteHeader(200)
 	w.Header().Set("Content-Length", "0")
 	w.Header().Set("Content-Length", "0")
@@ -504,6 +517,7 @@ func main() {
 	router.GET("/clusterCostsOverTime", a.ClusterCostsOverTime)
 	router.GET("/clusterCostsOverTime", a.ClusterCostsOverTime)
 	router.GET("/clusterCosts", a.ClusterCosts)
 	router.GET("/clusterCosts", a.ClusterCosts)
 	router.GET("/validatePrometheus", a.GetPrometheusMetadata)
 	router.GET("/validatePrometheus", a.GetPrometheusMetadata)
+	router.GET("/managementPlatform", a.ManagementPlatform)
 
 
 	rootMux := http.NewServeMux()
 	rootMux := http.NewServeMux()
 	rootMux.Handle("/", router)
 	rootMux.Handle("/", router)