Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudWatch: Remove dependencies on grafana/pkg/setting #81208

Merged
merged 14 commits into from
Feb 5, 2024
Merged

Conversation

iwysiu
Copy link
Contributor

@iwysiu iwysiu commented Jan 24, 2024

What is this feature?

Removes references to the grafana/pkg/setting package from the CloudWatch plugin. Currently, CloudWatch is passed the settings when the service is started up and stores the settings on the executor, and after this change it will get the settings from the instance context and store them on the instance. It also passes those settings to GetSession in the grafana-aws-sdk so it doesn't need to use environment variables for its session settings. Depends on grafana/grafana-aws-sdk#118

I also found that AWSListMetricsPageLimit was only in settings.Cfg and not in config.Cfg so I copied it over. In the opposite direction, I found that there was an AWS Session Duration that was only being set through an environment variable and added it to the aws config settings

Why do we need this feature?

It's required as part of the decoupling work for CloudWatch

Which issue(s) does this PR fix?:

Fixes #79324

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@@ -874,6 +874,10 @@ list_metrics_page_limit = 500
# Experimental, for use in Grafana Cloud only. Please do not set.
external_id =

# Set the plugins that will receive AWS settings for each request (via plugin context)
# By default this will include all Grafana Labs owned AWS plugins, or those that make use of AWS settings (ElasticSearch, Prometheus).
forward_settings_to_plugins = cloudwatch, grafana-athena-datasource, grafana-redshift-datasource, grafana-x-ray-datasource, grafana-timestream-datasource, grafana-iot-sitewise-datasource, grafana-iot-twinmaker-app, grafana-opensearch-datasource, aws-datasource-provisioner, elasticsearch, prometheus
Copy link
Contributor Author

@iwysiu iwysiu Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added all the external AWS plugins (even though they're not using the context yet), the AWS provisioner app, and ElasticSearch and Prometheus (which support sigv4) to the list along with CloudWatch. I think that's all of them, unless something else uses sigv4?

AWSAllowedAuthProviders []string
AWSAssumeRoleEnabled bool
AWSExternalId string
AWSSessionDuration string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grafana-aws-sdk had session duration settable as a secret environment variable setting before this, and the AWSListMetricsPageLimit was settable but not propagated to Cfg.

@@ -35,6 +35,9 @@ func ProvideConfig(settingProvider setting.Provider, grafanaCfg *setting.Cfg, fe
allowedAuth,
aws.KeyValue("assume_role_enabled").MustBool(grafanaCfg.AWSAssumeRoleEnabled),
aws.KeyValue("external_id").Value(),
aws.KeyValue("session_duration").Value(),
grafanaCfg.AWSListMetricsPageLimit,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason that the old aws variables get values off settingsProvider instead of grafanaCfg? I wasn't sure what the difference was

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settingsProvider can be backed by a database instead of a file - which I believe AWS originally wanted so that config could be reloaded on the fly. When not used, the settingsProvider just falls back to the file anyway. That was the original motivation there!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see! Should I update the new settings to be gotten off settingsProvider? It's a little weird because allowedAuth is actually being gotten from grafanaCfg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah TBH I'm not sure we've done a great job here of making it clear what should be happening (and I don't think many know the actual details of settingsProvider). I just remember needing to do this for plugin settings originally - but I think any config that people would like to potentially be updated via API/backend by the DB/can change at runtime, should be fetched from the settingsProvider!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it so that the aws settings (including allowedAuth) are gotten from the settingsProvider and added a comment so hopefully we remember

@@ -1333,11 +1339,20 @@ func (cfg *Cfg) handleAWSConfig() {
cfg.Logger.Error(fmt.Sprintf("could not set environment variable '%s'", awsds.AllowedAuthProvidersEnvVarKeyName), err)
}

cfg.AWSExternalId = awsPluginSec.Key("external_id").Value()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this to group all the cfg setting together

@@ -874,6 +874,14 @@ list_metrics_page_limit = 500
# Experimental, for use in Grafana Cloud only. Please do not set.
external_id =

# Sets the expiry duration of an assumed role.
# This setting should be expressed as a duration. Examples: 6h (hours), 10d (days), 2w (weeks), 1M (month).
session_duration = "15m"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a secret/experimental setting for grafana-aws-sdk that was being fetched from the environment variables

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Settings_LoadCloudWatchSettings(t *testing.T) {
settingCtx := backend.WithGrafanaConfig(context.Background(), backend.NewGrafanaCfg(map[string]string{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This context just exists so that the tests not testing the context settings don't produce a bunch of warning messages

@iwysiu iwysiu added the no-backport Skip backport of PR label Jan 31, 2024
@iwysiu iwysiu changed the title CloudWatch: remove dependencies on grafana/pkg/setting CloudWatch: Remove dependencies on grafana/pkg/setting Jan 31, 2024
@iwysiu iwysiu marked this pull request as ready for review January 31, 2024 23:02
@iwysiu iwysiu requested review from torkelo and a team as code owners January 31, 2024 23:02
@iwysiu iwysiu requested review from wbrowne and removed request for a team January 31, 2024 23:02
@iwysiu iwysiu requested review from marefr, oshirohugo, sarahzinger, idastambuk, undef1nd, suntala and idafurjes and removed request for a team January 31, 2024 23:02
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes from the plugins management perspective LGTM 👍

Copy link
Contributor Author

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After double checking, I confirmed that if it's not set to forward the setting to cloudwatch we use the environment variables. Note that forward_settings_to_plugins has to be set to something for it to overwrite the default, and it can't just be an empty string.

@@ -35,6 +35,9 @@ func ProvideConfig(settingProvider setting.Provider, grafanaCfg *setting.Cfg, fe
allowedAuth,
aws.KeyValue("assume_role_enabled").MustBool(grafanaCfg.AWSAssumeRoleEnabled),
aws.KeyValue("external_id").Value(),
aws.KeyValue("session_duration").Value(),
grafanaCfg.AWSListMetricsPageLimit,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see! Should I update the new settings to be gotten off settingsProvider? It's a little weird because allowedAuth is actually being gotten from grafanaCfg

// }

if slices.Contains[[]string, string](s.cfg.AWSForwardSettingsPlugins, pluginID) {
if !s.cfg.AWSAssumeRoleEnabled {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value defaults to true, so it actually should be added if its false

Copy link
Contributor

@kevinwcyu kevinwcyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Did a quick test with CloudWatch as well as Athena.

Copy link
Member

@sarahzinger sarahzinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Also I manually tested this with grafana assume role and it worked for me!

@iwysiu iwysiu merged commit 81da3ff into main Feb 5, 2024
12 checks passed
@iwysiu iwysiu deleted the iwysiu/79324 branch February 5, 2024 18:59
@aangelisc aangelisc modified the milestones: 10.4.x, 10.4.0 Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove core imports from grafana/pkg/setting in CloudWatch
6 participants