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

feat: Support AWS usage marketplace #1770

Merged
merged 30 commits into from
Aug 2, 2024
Merged

feat: Support AWS usage marketplace #1770

merged 30 commits into from
Aug 2, 2024

Conversation

bbernays
Copy link
Contributor

@bbernays bbernays commented Jun 21, 2024

This adds support for using CloudQuery plugins as part of the AWS Marketplace by changing the metering implementation to use the Marketplace instead, if specified via environment variables.

Copy link

github-actions bot commented Jun 24, 2024

⏱️ Benchmark results

Comparing with cf5b6b7

  • Glob-8 ns/op: 91.41 ⬇️ 1.06% decrease vs. cf5b6b7

@bbernays bbernays changed the title wip: Support AWS usage marketplace feat: Support AWS usage marketplace Jul 16, 2024
@bbernays bbernays marked this pull request as ready for review July 16, 2024 03:25
@bbernays bbernays requested a review from a team as a code owner July 16, 2024 03:25
@bbernays bbernays requested a review from erezrokah July 16, 2024 03:25
@github-actions github-actions bot added the feat label Jul 16, 2024
Copy link
Member

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Looks good, added a questions and comments.
How can I test this?

premium/usage.go Outdated
@@ -200,6 +206,20 @@ func NewUsageClient(meta plugin.Meta, ops ...UsageClientOptions) (UsageClient, e
TeamNameValue: u.teamName,
}, nil
}
// If user wants to use the AWS Marketplace for billing, don't even try to communicate with CQ API
if isAWSMarketplace() {
cfg, err := awsConfig.LoadDefaultConfig(context.TODO())
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for using context.TODO() in favor of context.Background()? Maybe we should add timeout as well for loading the config?

Also, can we put this logic in a setupAWSMarketplace function as NewUsageClient is getting quite long

Copy link
Contributor Author

@bbernays bbernays Jul 16, 2024

Choose a reason for hiding this comment

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

context.TODO() is supposed to be used when the surrounding function doesn't have a context available to use, so that is why I used it here. I am not sure the value of a timeout would add here.

I have moved the logic to setupAWSMarketplace in 566bbc0

premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated
u.awsMarketPlaceClient = marketplacemetering.NewFromConfig(cfg)
u.teamName = "AWS_MARKETPLACE"
// This needs to be larger than normal, because we can only send a single usage record per second (from each compute node)
u.batchLimit = 1000000000
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this is necessary, but would need to verify. I think how it should work is that there is a minimum flush time that should come into effect (by default this is 10s)

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 think there is a race condition, if a batch is flushed and within the same second the sync ends then the timestamp will have the same value as timestamps are truncated to the second

Copy link
Member

@hermanschaaf hermanschaaf Jul 16, 2024

Choose a reason for hiding this comment

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

Ah, good catch... ok this should work for now but we should probably try and think of a better solution in the long term, otherwise we won't count any records if the sync is abruptly stopped

go.mod Outdated Show resolved Hide resolved
Copy link
Member

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Code looks great, how can I test this?

@github-actions github-actions bot added feat and removed feat labels Jul 17, 2024
}

func isAWSMarketplace() bool {
return os.Getenv("CQ_AWS_MARKETPLACE_CONTAINER") == "true"
Copy link
Member

Choose a reason for hiding this comment

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

Where these envs are being set? If it's something user settable (and not a published image of some sort) strconv.ParseBool would've been better, but I'm guessing it's an image?

Comment on lines +48 to +51
//go:generate mockgen -package=mocks -destination=../premium/mocks/marketplacemetering.go -source=usage.go AWSMarketplaceClientInterface
type AWSMarketplaceClientInterface interface {
MeterUsage(ctx context.Context, params *marketplacemetering.MeterUsageInput, optFns ...func(*marketplacemetering.Options)) (*marketplacemetering.MeterUsageOutput, error)
}
Copy link
Contributor Author

@bbernays bbernays Aug 2, 2024

Choose a reason for hiding this comment

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

This was added to ensure that we could mock the AWS API calls like we do the API Server calls

premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
premium/usage.go Outdated Show resolved Hide resolved
Copy link
Member

@hermanschaaf hermanschaaf left a comment

Choose a reason for hiding this comment

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

🚀

@kodiakhq kodiakhq bot merged commit 1eb6d1a into main Aug 2, 2024
8 checks passed
@kodiakhq kodiakhq bot deleted the aws-usage-mktplace branch August 2, 2024 15:29
kodiakhq bot pushed a commit that referenced this pull request Aug 2, 2024
🤖 I have created a release *beep* *boop*
---


## [4.58.0](v4.57.1...v4.58.0) (2024-08-02)


### Features

* Support AWS usage marketplace ([#1770](#1770)) ([1eb6d1a](1eb6d1a))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
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.

None yet

4 participants