@@ -28,6 +28,7 @@ const (
2828)
2929
3030var cfgFile string
31+ var actRegionClient []exporter.AccountRegionClients
3132
3233type exporterConfig struct {
3334 Debug bool `mapstructure:"debug"`
@@ -46,6 +47,7 @@ type exporterConfig struct {
4647 CollectQuotas bool `mapstructure:"collect-quotas"`
4748 CollectUsages bool `mapstructure:"collect-usages"`
4849 OTELTracesEnabled bool `mapstructure:"enable-otel-traces"`
50+ AwsCredentials AWSCredentials
4951}
5052
5153func run (configuration exporterConfig ) {
@@ -54,24 +56,6 @@ func run(configuration exporterConfig) {
5456 fmt .Println ("ERROR: Fail to initialize logger: %w" , err )
5557 panic (err )
5658 }
57-
58- cfg , err := getAWSConfiguration (logger , configuration .AWSAssumeRoleArn , configuration .AWSAssumeRoleSession )
59- if err != nil {
60- logger .Error ("can't initialize AWS configuration" , "reason" , err )
61- os .Exit (awsErrorExitCode )
62- }
63-
64- awsAccountID , awsRegion , err := getAWSSessionInformation (cfg )
65- if err != nil {
66- logger .Error ("can't identify AWS account and/or region" , "reason" , err )
67- os .Exit (awsErrorExitCode )
68- }
69-
70- rdsClient := rds .NewFromConfig (cfg )
71- ec2Client := ec2 .NewFromConfig (cfg )
72- cloudWatchClient := cloudwatch .NewFromConfig (cfg )
73- servicequotasClient := servicequotas .NewFromConfig (cfg )
74-
7559 collectorConfiguration := exporter.Configuration {
7660 CollectInstanceMetrics : configuration .CollectInstanceMetrics ,
7761 CollectInstanceTypes : configuration .CollectInstanceTypes ,
@@ -82,16 +66,65 @@ func run(configuration exporterConfig) {
8266 CollectUsages : configuration .CollectUsages ,
8367 }
8468
85- collector := exporter .NewCollector (* logger , collectorConfiguration , awsAccountID , awsRegion , rdsClient , ec2Client , cloudWatchClient , servicequotasClient )
69+ cfgs , err := getAWSConfigurationByCredentials (logger , configuration )
70+ if err != nil {
71+ logger .Error ("can't initialize AWS configuration" , "reason" , err )
72+ os .Exit (awsErrorExitCode )
73+ }
74+ if cfgs == nil {
75+ logger .Info ("Didn't configure aws IAM User credentials in configuration file, will use default aws configuration" )
76+ cfg , err := getAWSConfiguration (logger , configuration .AWSAssumeRoleArn , configuration .AWSAssumeRoleSession )
77+ if err != nil {
78+ logger .Error ("can't initialize AWS configuration" , "reason" , err )
79+ os .Exit (awsErrorExitCode )
80+ }
81+ awsAccountID , awsRegion , err := getAWSSessionInformation (cfg )
82+ if err != nil {
83+ logger .Error ("can't identify AWS account and/or region" , "reason" , err )
84+ os .Exit (awsErrorExitCode )
85+ }
86+
87+ rdsClient := rds .NewFromConfig (cfg )
88+ ec2Client := ec2 .NewFromConfig (cfg )
89+ cloudWatchClient := cloudwatch .NewFromConfig (cfg )
90+ servicequotasClient := servicequotas .NewFromConfig (cfg )
91+
92+ collector := exporter .NewCollector (* logger , collectorConfiguration , awsAccountID , awsRegion , rdsClient , ec2Client , cloudWatchClient , servicequotasClient )
93+
94+ prometheus .MustRegister (collector )
8695
87- prometheus .MustRegister (collector )
96+ } else {
97+ for _ , cfg := range cfgs {
98+ awsAccountID , awsRegion , err := getAWSSessionInformation (cfg )
99+ if err != nil {
100+ logger .Error ("can't identify AWS account and/or region" , "reason" , err )
101+ os .Exit (awsErrorExitCode )
102+ }
103+
104+ rdsClient := rds .NewFromConfig (cfg )
105+ ec2Client := ec2 .NewFromConfig (cfg )
106+ cloudWatchClient := cloudwatch .NewFromConfig (cfg )
107+ servicequotasClient := servicequotas .NewFromConfig (cfg )
108+
109+ var accountRegionClients exporter.AccountRegionClients
110+ accountRegionClients .AwsAccountID = awsAccountID
111+ accountRegionClients .AwsRegion = awsRegion
112+ accountRegionClients .RdsClient = rdsClient
113+ accountRegionClients .Ec2Client = ec2Client
114+ accountRegionClients .CloudWatchClient = cloudWatchClient
115+ accountRegionClients .ServicequotasClient = servicequotasClient
116+ actRegionClient = append (actRegionClient , accountRegionClients )
117+ }
118+ collector := exporter .NewMultiCollector (* logger , collectorConfiguration , actRegionClient )
119+ prometheus .MustRegister (collector )
120+ }
88121
122+ // http configurations for exporter service
89123 serverConfiguration := http.Config {
90- ListenAddress : configuration .ListenAddress ,
91- MetricPath : configuration .MetricPath ,
92- TLSCertPath : configuration .TLSCertPath ,
93- TLSKeyPath : configuration .TLSKeyPath ,
94- OTELTracesEnabled : configuration .OTELTracesEnabled ,
124+ ListenAddress : configuration .ListenAddress ,
125+ MetricPath : configuration .MetricPath ,
126+ TLSCertPath : configuration .TLSCertPath ,
127+ TLSKeyPath : configuration .TLSKeyPath ,
95128 }
96129
97130 server := http .New (* logger , serverConfiguration )
@@ -118,6 +151,7 @@ func NewRootCommand() (*cobra.Command, error) {
118151
119152 return
120153 }
154+ viper .UnmarshalKey ("accounts" , & c .AwsCredentials .Accounts )
121155 run (c )
122156 },
123157 }
0 commit comments