|
| 1 | +package azure |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 8 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/costmanagement/armcostmanagement" |
| 9 | + "github.com/flanksource/commons/logger" |
| 10 | + v1 "github.com/flanksource/config-db/api/v1" |
| 11 | + "github.com/flanksource/duty" |
| 12 | +) |
| 13 | + |
| 14 | +type CostScraper struct { |
| 15 | + cred *azidentity.ClientSecretCredential |
| 16 | +} |
| 17 | + |
| 18 | +func (t CostScraper) CanScrape(config v1.ConfigScraper) bool { |
| 19 | + // At least one of the azure configuration must have the subscription ID set |
| 20 | + for _, c := range config.Azure { |
| 21 | + if c.SubscriptionID != "" { |
| 22 | + return true |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return false |
| 27 | +} |
| 28 | + |
| 29 | +func (t CostScraper) Scrape(ctx *v1.ScrapeContext, config v1.ConfigScraper) v1.ScrapeResults { |
| 30 | + var results v1.ScrapeResults |
| 31 | + for _, config := range config.Azure { |
| 32 | + clientId, err := duty.GetEnvValueFromCache(ctx.Kubernetes, config.ClientID, ctx.Namespace) |
| 33 | + if err != nil { |
| 34 | + results.Errorf(err, "failed to get client id") |
| 35 | + continue |
| 36 | + } |
| 37 | + |
| 38 | + clientSecret, err := duty.GetEnvValueFromCache(ctx.Kubernetes, config.ClientSecret, ctx.Namespace) |
| 39 | + if err != nil { |
| 40 | + results.Errorf(err, "failed to get client secret") |
| 41 | + continue |
| 42 | + } |
| 43 | + |
| 44 | + cred, err := azidentity.NewClientSecretCredential(config.TenantID, clientId, clientSecret, nil) |
| 45 | + if err != nil { |
| 46 | + results.Errorf(err, "failed to get credentials for azure") |
| 47 | + continue |
| 48 | + } |
| 49 | + t.cred = cred |
| 50 | + |
| 51 | + if err := t.GetCost(ctx.Context, config.SubscriptionID); err != nil { |
| 52 | + results.Errorf(err, "failed to get cost") |
| 53 | + continue |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return results |
| 58 | +} |
| 59 | + |
| 60 | +func (t *CostScraper) GetCost(ctx context.Context, subscriptionID string) error { |
| 61 | + costClient, err := armcostmanagement.NewQueryClient(t.cred, nil) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to create cost client: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + var ( |
| 67 | + scope = fmt.Sprintf("/subscriptions/%s", subscriptionID) |
| 68 | + timeFrame = armcostmanagement.TimeframeTypeTheLastBillingMonth |
| 69 | + queryType = armcostmanagement.ExportTypeActualCost |
| 70 | + granularity = armcostmanagement.GranularityTypeDaily |
| 71 | + ) |
| 72 | + queryDef := armcostmanagement.QueryDefinition{ |
| 73 | + Dataset: &armcostmanagement.QueryDataset{ |
| 74 | + Granularity: &granularity, |
| 75 | + }, |
| 76 | + Timeframe: &timeFrame, |
| 77 | + Type: &queryType, |
| 78 | + } |
| 79 | + usageRes, err := costClient.Usage(ctx, scope, queryDef, nil) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to get usage: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + logger.Debugf("usage: %v", usageRes) |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
0 commit comments