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: adds gcp metadata provider support to cloudmeta package #4155

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pkg/cloudmeta/gcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2024 ScyllaDB

package cloudmeta

import (
"context"
"strings"

"cloud.google.com/go/compute/metadata"
"github.com/pkg/errors"
)

// GCPMetadata is a wrapper around gcp metadata client.
type GCPMetadata struct {
meta *metadata.Client
}

// NewGCPMetadata returns gcp metadata provider.
func NewGCPMetadata() *GCPMetadata {
return &GCPMetadata{
meta: metadata.NewClient(nil),
}
}

// Metadata returns InstanceMetadata from gcp if available.
func (gcp *GCPMetadata) Metadata(ctx context.Context) (InstanceMetadata, error) {
machineType, err := gcp.getMachineType(ctx)
if err != nil {
return InstanceMetadata{}, errors.Wrap(err, "gcp.meta.GetWithContext")
}
return InstanceMetadata{
CloudProvider: CloudProviderGCP,
InstanceType: machineType,
}, nil
}

func (gcp *GCPMetadata) getMachineType(ctx context.Context) (string, error) {
// The machine type for this VM. This value has the following format: projects/PROJECT_NUM/machineTypes/MACHINE_TYPE.
machineType, err := gcp.meta.GetWithContext(ctx, "instance/machine-type")
if err != nil {
return "", errors.Wrap(err, "gcp.meta.GetWithContext")
}

parts := strings.Split(machineType, "/")
if len(parts) < 2 {
return "", errors.Errorf("unexpected machine-type format: %s", machineType)
}

return parts[len(parts)-1], nil
Michal-Leszczynski marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 9 additions & 2 deletions pkg/cloudmeta/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ type InstanceMetadata struct {
// CloudProvider is enum of supported cloud providers.
type CloudProvider string

// CloudProviderAWS represents aws provider.
var CloudProviderAWS CloudProvider = "aws"
var (
Michal-Leszczynski marked this conversation as resolved.
Show resolved Hide resolved
// CloudProviderAWS represents aws provider.
CloudProviderAWS CloudProvider = "aws"
// CloudProviderGCP represents gcp provider.
CloudProviderGCP CloudProvider = "gcp"
)

// CloudMetadataProvider interface that each metadata provider should implement.
type CloudMetadataProvider interface {
Expand All @@ -43,9 +47,12 @@ func NewCloudMeta() (*CloudMeta, error) {
return nil, err
}

gcpMeta := NewGCPMetadata()

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