Skip to content

Commit

Permalink
feat: adds aws metadata provider
Browse files Browse the repository at this point in the history
This extends cloudmeta package with aws metadata provider.

Fixes: #4127
  • Loading branch information
VAveryanov8 committed Dec 11, 2024
1 parent c2dcb17 commit 8878021
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
50 changes: 50 additions & 0 deletions pkg/cloudmeta/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2024 ScyllaDB

package cloudmeta

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/pkg/errors"
)

// AWSMetadata is a wrapper around ec2 metadata client.
type AWSMetadata struct {
ec2meta *ec2metadata.EC2Metadata
}

// NewAWSMetadata is a constructor for AWSMetadata service.
// testEndpoint can be provided if you want to overwrite the default metadata endpoint, otherwise leave it empty.
func NewAWSMetadata(testEndpoint string) (*AWSMetadata, error) {
session, err := session.NewSession()
if err != nil {
return nil, errors.Wrap(err, "session.NewSession")
}
cfg := aws.NewConfig()
if testEndpoint != "" {
cfg = cfg.WithEndpoint(testEndpoint)
}
return &AWSMetadata{
ec2meta: ec2metadata.New(session, cfg),
}, nil
}

// Metadata return InstanceMetadata from aws if available.
func (aws *AWSMetadata) Metadata(ctx context.Context) (InstanceMetadata, error) {
if !aws.ec2meta.AvailableWithContext(ctx) {
return InstanceMetadata{}, errors.New("metadata is not available")
}

instanceData, err := aws.ec2meta.GetInstanceIdentityDocumentWithContext(ctx)
if err != nil {
return InstanceMetadata{}, errors.Wrap(err, "aws.metadataClient.GetInstanceIdentityDocument")
}

return InstanceMetadata{
CloudProvider: CloudProviderAWS,
InstanceType: instanceData.InstanceType,
}, nil
}
11 changes: 8 additions & 3 deletions pkg/cloudmeta/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ type CloudMeta struct {

// NewCloudMeta creates new CloudMeta provider.
func NewCloudMeta() (*CloudMeta, error) {
// providers will initialized here and added to CloudMeta.providers.

const defaultTimeout = 5 * time.Second

awsMeta, err := NewAWSMetadata("")
if err != nil {
return nil, err
}

return &CloudMeta{
providers: []CloudMetadataProvider{},
providers: []CloudMetadataProvider{
awsMeta,
},
providerTimeout: defaultTimeout,
}, nil
}
Expand Down

0 comments on commit 8878021

Please sign in to comment.