| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- package cloud
- import (
- "database/sql"
- "errors"
- "fmt"
- "io"
- "regexp"
- "strconv"
- "strings"
- "sync"
- "time"
- "github.com/opencost/opencost/pkg/kubecost"
- "github.com/opencost/opencost/pkg/util"
- "cloud.google.com/go/compute/metadata"
- "github.com/opencost/opencost/pkg/clustercache"
- "github.com/opencost/opencost/pkg/config"
- "github.com/opencost/opencost/pkg/env"
- "github.com/opencost/opencost/pkg/log"
- "github.com/opencost/opencost/pkg/util/watcher"
- v1 "k8s.io/api/core/v1"
- )
- const authSecretPath = "/var/secrets/service-key.json"
- const storageConfigSecretPath = "/var/azure-storage-config/azure-storage-config.json"
- const defaultShareTenancyCost = "true"
- var createTableStatements = []string{
- `CREATE TABLE IF NOT EXISTS names (
- cluster_id VARCHAR(255) NOT NULL,
- cluster_name VARCHAR(255) NULL,
- PRIMARY KEY (cluster_id)
- );`,
- }
- // ReservedInstanceData keeps record of resources on a node should be
- // priced at reserved rates
- type ReservedInstanceData struct {
- ReservedCPU int64 `json:"reservedCPU"`
- ReservedRAM int64 `json:"reservedRAM"`
- CPUCost float64 `json:"CPUHourlyCost"`
- RAMCost float64 `json:"RAMHourlyCost"`
- }
- // Node is the interface by which the provider and cost model communicate Node prices.
- // The provider will best-effort try to fill out this struct.
- type Node struct {
- Cost string `json:"hourlyCost"`
- VCPU string `json:"CPU"`
- VCPUCost string `json:"CPUHourlyCost"`
- RAM string `json:"RAM"`
- RAMBytes string `json:"RAMBytes"`
- RAMCost string `json:"RAMGBHourlyCost"`
- Storage string `json:"storage"`
- StorageCost string `json:"storageHourlyCost"`
- UsesBaseCPUPrice bool `json:"usesDefaultPrice"`
- BaseCPUPrice string `json:"baseCPUPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
- BaseRAMPrice string `json:"baseRAMPrice"` // Used to compute an implicit RAM GB/Hr price when RAM pricing is not provided.
- BaseGPUPrice string `json:"baseGPUPrice"`
- UsageType string `json:"usageType"`
- GPU string `json:"gpu"` // GPU represents the number of GPU on the instance
- GPUName string `json:"gpuName"`
- GPUCost string `json:"gpuCost"`
- InstanceType string `json:"instanceType,omitempty"`
- Region string `json:"region,omitempty"`
- Reserved *ReservedInstanceData `json:"reserved,omitempty"`
- ProviderID string `json:"providerID,omitempty"`
- PricingType PricingType `json:"pricingType,omitempty"`
- }
- // IsSpot determines whether or not a Node uses spot by usage type
- func (n *Node) IsSpot() bool {
- if n != nil {
- return strings.Contains(n.UsageType, "spot") || strings.Contains(n.UsageType, "emptible")
- } else {
- return false
- }
- }
- // LoadBalancer is the interface by which the provider and cost model communicate LoadBalancer prices.
- // The provider will best-effort try to fill out this struct.
- type LoadBalancer struct {
- IngressIPAddresses []string `json:"IngressIPAddresses"`
- Cost float64 `json:"hourlyCost"`
- }
- // TODO: used for dynamic cloud provider price fetching.
- // determine what identifies a load balancer in the json returned from the cloud provider pricing API call
- // type LBKey interface {
- // }
- // Network is the interface by which the provider and cost model communicate network egress prices.
- // The provider will best-effort try to fill out this struct.
- type Network struct {
- ZoneNetworkEgressCost float64
- RegionNetworkEgressCost float64
- InternetNetworkEgressCost float64
- }
- // PV is the interface by which the provider and cost model communicate PV prices.
- // The provider will best-effort try to fill out this struct.
- type PV struct {
- Cost string `json:"hourlyCost"`
- CostPerIO string `json:"costPerIOOperation"`
- Class string `json:"storageClass"`
- Size string `json:"size"`
- Region string `json:"region"`
- ProviderID string `json:"providerID,omitempty"`
- Parameters map[string]string `json:"parameters"`
- }
- // Key represents a way for nodes to match between the k8s API and a pricing API
- type Key interface {
- ID() string // ID represents an exact match
- Features() string // Features are a comma separated string of node metadata that could match pricing
- GPUType() string // GPUType returns "" if no GPU exists, but the name of the GPU otherwise
- }
- type PVKey interface {
- Features() string
- GetStorageClass() string
- ID() string
- }
- // OutOfClusterAllocation represents a cloud provider cost not associated with kubernetes
- type OutOfClusterAllocation struct {
- Aggregator string `json:"aggregator"`
- Environment string `json:"environment"`
- Service string `json:"service"`
- Cost float64 `json:"cost"`
- Cluster string `json:"cluster"`
- }
- type CustomPricing struct {
- Provider string `json:"provider"`
- Description string `json:"description"`
- CPU string `json:"CPU"`
- SpotCPU string `json:"spotCPU"`
- RAM string `json:"RAM"`
- SpotRAM string `json:"spotRAM"`
- GPU string `json:"GPU"`
- SpotGPU string `json:"spotGPU"`
- Storage string `json:"storage"`
- ZoneNetworkEgress string `json:"zoneNetworkEgress"`
- RegionNetworkEgress string `json:"regionNetworkEgress"`
- InternetNetworkEgress string `json:"internetNetworkEgress"`
- FirstFiveForwardingRulesCost string `json:"firstFiveForwardingRulesCost"`
- AdditionalForwardingRuleCost string `json:"additionalForwardingRuleCost"`
- LBIngressDataCost string `json:"LBIngressDataCost"`
- SpotLabel string `json:"spotLabel,omitempty"`
- SpotLabelValue string `json:"spotLabelValue,omitempty"`
- GpuLabel string `json:"gpuLabel,omitempty"`
- GpuLabelValue string `json:"gpuLabelValue,omitempty"`
- ServiceKeyName string `json:"awsServiceKeyName,omitempty"`
- ServiceKeySecret string `json:"awsServiceKeySecret,omitempty"`
- SpotDataRegion string `json:"awsSpotDataRegion,omitempty"`
- SpotDataBucket string `json:"awsSpotDataBucket,omitempty"`
- SpotDataPrefix string `json:"awsSpotDataPrefix,omitempty"`
- ProjectID string `json:"projectID,omitempty"`
- AthenaProjectID string `json:"athenaProjectID,omitempty"`
- AthenaBucketName string `json:"athenaBucketName"`
- AthenaRegion string `json:"athenaRegion"`
- AthenaDatabase string `json:"athenaDatabase"`
- AthenaTable string `json:"athenaTable"`
- AthenaWorkgroup string `json:"athenaWorkgroup"`
- MasterPayerARN string `json:"masterPayerARN"`
- BillingDataDataset string `json:"billingDataDataset,omitempty"`
- CustomPricesEnabled string `json:"customPricesEnabled"`
- DefaultIdle string `json:"defaultIdle"`
- AzureSubscriptionID string `json:"azureSubscriptionID"`
- AzureClientID string `json:"azureClientID"`
- AzureClientSecret string `json:"azureClientSecret"`
- AzureTenantID string `json:"azureTenantID"`
- AzureBillingRegion string `json:"azureBillingRegion"`
- AzureOfferDurableID string `json:"azureOfferDurableID"`
- CurrencyCode string `json:"currencyCode"`
- Discount string `json:"discount"`
- NegotiatedDiscount string `json:"negotiatedDiscount"`
- SharedOverhead string `json:"sharedOverhead"`
- ClusterName string `json:"clusterName"`
- SharedNamespaces string `json:"sharedNamespaces"`
- SharedLabelNames string `json:"sharedLabelNames"`
- SharedLabelValues string `json:"sharedLabelValues"`
- ShareTenancyCosts string `json:"shareTenancyCosts"` // TODO clean up configuration so we can use a type other that string (this should be a bool, but the app panics if it's not a string)
- ReadOnly string `json:"readOnly"`
- EditorAccess string `json:"editorAccess"`
- KubecostToken string `json:"kubecostToken"`
- GoogleAnalyticsTag string `json:"googleAnalyticsTag"`
- ExcludeProviderID string `json:"excludeProviderID"`
- }
- // GetSharedOverheadCostPerMonth parses and returns a float64 representation
- // of the configured monthly shared overhead cost. If the string version cannot
- // be parsed into a float, an error is logged and 0.0 is returned.
- func (cp *CustomPricing) GetSharedOverheadCostPerMonth() float64 {
- // Empty string should be interpreted as "no cost", i.e. 0.0
- if cp.SharedOverhead == "" {
- return 0.0
- }
- // Attempt to parse, but log and return 0.0 if that fails.
- sharedCostPerMonth, err := strconv.ParseFloat(cp.SharedOverhead, 64)
- if err != nil {
- log.Errorf("SharedOverhead: failed to parse shared overhead \"%s\": %s", cp.SharedOverhead, err)
- return 0.0
- }
- return sharedCostPerMonth
- }
- type ServiceAccountStatus struct {
- Checks []*ServiceAccountCheck `json:"checks"`
- }
- // ServiceAccountChecks is a thread safe map for holding ServiceAccountCheck objects
- type ServiceAccountChecks struct {
- sync.RWMutex
- serviceAccountChecks map[string]*ServiceAccountCheck
- }
- // NewServiceAccountChecks initialize ServiceAccountChecks
- func NewServiceAccountChecks() *ServiceAccountChecks {
- return &ServiceAccountChecks{
- serviceAccountChecks: make(map[string]*ServiceAccountCheck),
- }
- }
- func (sac *ServiceAccountChecks) set(key string, check *ServiceAccountCheck) {
- sac.Lock()
- defer sac.Unlock()
- sac.serviceAccountChecks[key] = check
- }
- // getStatus extracts ServiceAccountCheck objects into a slice and returns them in a ServiceAccountStatus
- func (sac *ServiceAccountChecks) getStatus() *ServiceAccountStatus {
- sac.Lock()
- defer sac.Unlock()
- checks := []*ServiceAccountCheck{}
- for _, v := range sac.serviceAccountChecks {
- checks = append(checks, v)
- }
- return &ServiceAccountStatus{
- Checks: checks,
- }
- }
- type ServiceAccountCheck struct {
- Message string `json:"message"`
- Status bool `json:"status"`
- AdditionalInfo string `json:"additionalInfo"`
- }
- type PricingSources struct {
- PricingSources map[string]*PricingSource
- }
- type PricingSource struct {
- Name string `json:"name"`
- Enabled bool `json:"enabled"`
- Available bool `json:"available"`
- Error string `json:"error"`
- }
- type PricingType string
- const (
- Api PricingType = "api"
- Spot PricingType = "spot"
- Reserved PricingType = "reserved"
- SavingsPlan PricingType = "savingsPlan"
- CsvExact PricingType = "csvExact"
- CsvClass PricingType = "csvClass"
- DefaultPrices PricingType = "defaultPrices"
- )
- type PricingMatchMetadata struct {
- TotalNodes int `json:"TotalNodes"`
- PricingTypeCounts map[PricingType]int `json:"PricingType"`
- }
- // Provider represents a k8s provider.
- type Provider interface {
- ClusterInfo() (map[string]string, error)
- GetAddresses() ([]byte, error)
- GetDisks() ([]byte, error)
- NodePricing(Key) (*Node, error)
- PVPricing(PVKey) (*PV, error)
- NetworkPricing() (*Network, error) // TODO: add key interface arg for dynamic price fetching
- LoadBalancerPricing() (*LoadBalancer, error) // TODO: add key interface arg for dynamic price fetching
- AllNodePricing() (interface{}, error)
- DownloadPricingData() error
- GetKey(map[string]string, *v1.Node) Key
- GetPVKey(*v1.PersistentVolume, map[string]string, string) PVKey
- UpdateConfig(r io.Reader, updateType string) (*CustomPricing, error)
- UpdateConfigFromConfigMap(map[string]string) (*CustomPricing, error)
- GetConfig() (*CustomPricing, error)
- GetManagementPlatform() (string, error)
- GetLocalStorageQuery(time.Duration, time.Duration, bool, bool) string
- ApplyReservedInstancePricing(map[string]*Node)
- ServiceAccountStatus() *ServiceAccountStatus
- PricingSourceStatus() map[string]*PricingSource
- ClusterManagementPricing() (string, float64, error)
- CombinedDiscountForNode(string, bool, float64, float64) float64
- Regions() []string
- }
- // ClusterName returns the name defined in cluster info, defaulting to the
- // CLUSTER_ID environment variable
- func ClusterName(p Provider) string {
- info, err := p.ClusterInfo()
- if err != nil {
- return env.GetClusterID()
- }
- name, ok := info["name"]
- if !ok {
- return env.GetClusterID()
- }
- return name
- }
- // CustomPricesEnabled returns the boolean equivalent of the cloup provider's custom prices flag,
- // indicating whether or not the cluster is using custom pricing.
- func CustomPricesEnabled(p Provider) bool {
- config, err := p.GetConfig()
- if err != nil {
- return false
- }
- // TODO:CLEANUP what is going on with this?
- if config.NegotiatedDiscount == "" {
- config.NegotiatedDiscount = "0%"
- }
- return config.CustomPricesEnabled == "true"
- }
- // ConfigWatcherFor returns a new ConfigWatcher instance which watches changes to the "pricing-configs"
- // configmap
- func ConfigWatcherFor(p Provider) *watcher.ConfigMapWatcher {
- return &watcher.ConfigMapWatcher{
- ConfigMapName: env.GetPricingConfigmapName(),
- WatchFunc: func(name string, data map[string]string) error {
- _, err := p.UpdateConfigFromConfigMap(data)
- return err
- },
- }
- }
- // AllocateIdleByDefault returns true if the application settings specify to allocate idle by default
- func AllocateIdleByDefault(p Provider) bool {
- config, err := p.GetConfig()
- if err != nil {
- return false
- }
- return config.DefaultIdle == "true"
- }
- // SharedNamespace returns a list of names of shared namespaces, as defined in the application settings
- func SharedNamespaces(p Provider) []string {
- namespaces := []string{}
- config, err := p.GetConfig()
- if err != nil {
- return namespaces
- }
- if config.SharedNamespaces == "" {
- return namespaces
- }
- // trim spaces so that "kube-system, kubecost" is equivalent to "kube-system,kubecost"
- for _, ns := range strings.Split(config.SharedNamespaces, ",") {
- namespaces = append(namespaces, strings.Trim(ns, " "))
- }
- return namespaces
- }
- // SharedLabel returns the configured set of shared labels as a parallel tuple of keys to values; e.g.
- // for app:kubecost,type:staging this returns (["app", "type"], ["kubecost", "staging"]) in order to
- // match the signature of the NewSharedResourceInfo
- func SharedLabels(p Provider) ([]string, []string) {
- names := []string{}
- values := []string{}
- config, err := p.GetConfig()
- if err != nil {
- return names, values
- }
- if config.SharedLabelNames == "" || config.SharedLabelValues == "" {
- return names, values
- }
- ks := strings.Split(config.SharedLabelNames, ",")
- vs := strings.Split(config.SharedLabelValues, ",")
- if len(ks) != len(vs) {
- log.Warnf("Shared labels have mis-matched lengths: %d names, %d values", len(ks), len(vs))
- return names, values
- }
- for i := range ks {
- names = append(names, strings.Trim(ks[i], " "))
- values = append(values, strings.Trim(vs[i], " "))
- }
- return names, values
- }
- // ShareTenancyCosts returns true if the application settings specify to share
- // tenancy costs by default.
- func ShareTenancyCosts(p Provider) bool {
- config, err := p.GetConfig()
- if err != nil {
- return false
- }
- return config.ShareTenancyCosts == "true"
- }
- // NewProvider looks at the nodespec or provider metadata server to decide which provider to instantiate.
- func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.ConfigFileManager) (Provider, error) {
- nodes := cache.GetAllNodes()
- if len(nodes) == 0 {
- log.Infof("Could not locate any nodes for cluster.") // valid in ETL readonly mode
- return &CustomProvider{
- Clientset: cache,
- Config: NewProviderConfig(config, "default.json"),
- }, nil
- }
- cp := getClusterProperties(nodes[0])
- switch cp.provider {
- case kubecost.CSVProvider:
- log.Infof("Using CSV Provider with CSV at %s", env.GetCSVPath())
- return &CSVProvider{
- CSVLocation: env.GetCSVPath(),
- CustomProvider: &CustomProvider{
- Clientset: cache,
- Config: NewProviderConfig(config, cp.configFileName),
- },
- }, nil
- case kubecost.GCPProvider:
- log.Info("metadata reports we are in GCE")
- if apiKey == "" {
- return nil, errors.New("Supply a GCP Key to start getting data")
- }
- return &GCP{
- Clientset: cache,
- APIKey: apiKey,
- Config: NewProviderConfig(config, cp.configFileName),
- clusterRegion: cp.region,
- clusterProjectId: cp.projectID,
- }, nil
- case kubecost.AWSProvider:
- log.Info("Found ProviderID starting with \"aws\", using AWS Provider")
- return &AWS{
- Clientset: cache,
- Config: NewProviderConfig(config, cp.configFileName),
- clusterRegion: cp.region,
- clusterAccountId: cp.accountID,
- serviceAccountChecks: NewServiceAccountChecks(),
- }, nil
- case kubecost.AzureProvider:
- log.Info("Found ProviderID starting with \"azure\", using Azure Provider")
- return &Azure{
- Clientset: cache,
- Config: NewProviderConfig(config, cp.configFileName),
- clusterRegion: cp.region,
- clusterAccountId: cp.accountID,
- serviceAccountChecks: NewServiceAccountChecks(),
- }, nil
- default:
- log.Info("Unsupported provider, falling back to default")
- return &CustomProvider{
- Clientset: cache,
- Config: NewProviderConfig(config, cp.configFileName),
- }, nil
- }
- }
- type clusterProperties struct {
- provider string
- configFileName string
- region string
- accountID string
- projectID string
- }
- func getClusterProperties(node *v1.Node) clusterProperties {
- providerID := strings.ToLower(node.Spec.ProviderID)
- region, _ := util.GetRegion(node.Labels)
- cp := clusterProperties{
- provider: "DEFAULT",
- configFileName: "default.json",
- region: region,
- accountID: "",
- projectID: "",
- }
- if metadata.OnGCE() {
- cp.provider = kubecost.GCPProvider
- cp.configFileName = "gcp.json"
- cp.projectID = parseGCPProjectID(providerID)
- } else if strings.HasPrefix(providerID, "aws") {
- cp.provider = kubecost.AWSProvider
- cp.configFileName = "aws.json"
- } else if strings.HasPrefix(providerID, "azure") {
- cp.provider = kubecost.AzureProvider
- cp.configFileName = "azure.json"
- cp.accountID = parseAzureSubscriptionID(providerID)
- }
- if env.IsUseCSVProvider() {
- cp.provider = kubecost.CSVProvider
- }
- return cp
- }
- func UpdateClusterMeta(cluster_id, cluster_name string) error {
- pw := env.GetRemotePW()
- 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()
- updateStmt := `UPDATE names SET cluster_name = $1 WHERE cluster_id = $2;`
- _, err = db.Exec(updateStmt, cluster_name, cluster_id)
- if err != nil {
- return err
- }
- return nil
- }
- func CreateClusterMeta(cluster_id, cluster_name string) error {
- pw := env.GetRemotePW()
- 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()
- for _, stmt := range createTableStatements {
- _, err := db.Exec(stmt)
- if err != nil {
- return err
- }
- }
- insertStmt := `INSERT INTO names (cluster_id, cluster_name) VALUES ($1, $2);`
- _, err = db.Exec(insertStmt, cluster_id, cluster_name)
- if err != nil {
- return err
- }
- return nil
- }
- func GetClusterMeta(cluster_id string) (string, string, error) {
- pw := env.GetRemotePW()
- address := env.GetSQLAddress()
- connStr := fmt.Sprintf("postgres://postgres:%s@%s:5432?sslmode=disable", pw, address)
- db, err := sql.Open("postgres", connStr)
- defer db.Close()
- query := `SELECT cluster_id, cluster_name
- FROM names
- WHERE cluster_id = ?`
- rows, err := db.Query(query, cluster_id)
- if err != nil {
- return "", "", err
- }
- defer rows.Close()
- var (
- sql_cluster_id string
- cluster_name string
- )
- for rows.Next() {
- if err := rows.Scan(&sql_cluster_id, &cluster_name); err != nil {
- return "", "", err
- }
- }
- return sql_cluster_id, cluster_name, nil
- }
- func GetOrCreateClusterMeta(cluster_id, cluster_name string) (string, string, error) {
- id, name, err := GetClusterMeta(cluster_id)
- if err != nil {
- err := CreateClusterMeta(cluster_id, cluster_name)
- if err != nil {
- return "", "", err
- }
- }
- if id == "" {
- err := CreateClusterMeta(cluster_id, cluster_name)
- if err != nil {
- return "", "", err
- }
- }
- return id, name, nil
- }
- var (
- // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
- providerAWSRegex = regexp.MustCompile("aws://[^/]*/[^/]*/([^/]+)")
- // gce://guestbook-227502/us-central1-a/gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
- // => gke-niko-n1-standard-2-wljla-8df8e58a-hfy7
- providerGCERegex = regexp.MustCompile("gce://[^/]*/[^/]*/([^/]+)")
- // Capture "vol-0fc54c5e83b8d2b76" from "aws://us-east-2a/vol-0fc54c5e83b8d2b76"
- persistentVolumeAWSRegex = regexp.MustCompile("aws:/[^/]*/[^/]*/([^/]+)")
- // Capture "ad9d88195b52a47c89b5055120f28c58" from "ad9d88195b52a47c89b5055120f28c58-1037804914.us-east-2.elb.amazonaws.com"
- loadBalancerAWSRegex = regexp.MustCompile("^([^-]+)-.+amazonaws\\.com$")
- )
- // ParseID attempts to parse a ProviderId from a string based on formats from the various providers and
- // returns the string as is if it cannot find a match
- func ParseID(id string) string {
- match := providerAWSRegex.FindStringSubmatch(id)
- if len(match) >= 2 {
- return match[1]
- }
- match = providerGCERegex.FindStringSubmatch(id)
- if len(match) >= 2 {
- return match[1]
- }
- // Return id for Azure Provider, CSV Provider and Custom Provider
- return id
- }
- // ParsePVID attempts to parse a PV ProviderId from a string based on formats from the various providers and
- // returns the string as is if it cannot find a match
- func ParsePVID(id string) string {
- match := persistentVolumeAWSRegex.FindStringSubmatch(id)
- if len(match) >= 2 {
- return match[1]
- }
- // Return id for GCP Provider, Azure Provider, CSV Provider and Custom Provider
- return id
- }
- // ParseLBID attempts to parse a LB ProviderId from a string based on formats from the various providers and
- // returns the string as is if it cannot find a match
- func ParseLBID(id string) string {
- match := loadBalancerAWSRegex.FindStringSubmatch(id)
- if len(match) >= 2 {
- return match[1]
- }
- // Return id for GCP Provider, Azure Provider, CSV Provider and Custom Provider
- return id
- }
|