constellation/coordinator/cloudprovider/gcp/metadata.go

165 lines
5.5 KiB
Go
Raw Normal View History

package gcp
import (
"context"
"fmt"
2022-05-24 08:04:42 +00:00
"github.com/edgelesssys/constellation/coordinator/cloudprovider/cloudtypes"
"github.com/edgelesssys/constellation/coordinator/core"
"github.com/edgelesssys/constellation/coordinator/role"
"github.com/edgelesssys/constellation/internal/gcpshared"
)
// API handles all GCP API requests.
type API interface {
// RetrieveInstances retrieves a list of all accessible GCP instances with their metadata.
2022-05-24 08:04:42 +00:00
RetrieveInstances(ctx context.Context, project, zone string) ([]cloudtypes.Instance, error)
// RetrieveInstances retrieves a single GCP instances with its metadata.
2022-05-24 08:04:42 +00:00
RetrieveInstance(ctx context.Context, project, zone, instanceName string) (cloudtypes.Instance, error)
// RetrieveInstanceMetadata retrieves the GCP instance metadata of the current instance.
RetrieveInstanceMetadata(attr string) (string, error)
// RetrieveProjectID retrieves the GCP projectID containing the current instance.
RetrieveProjectID() (string, error)
// RetrieveZone retrieves the GCP zone containing the current instance.
RetrieveZone() (string, error)
// RetrieveInstanceName retrieves the instance name of the current instance.
RetrieveInstanceName() (string, error)
2022-05-24 08:04:42 +00:00
// RetrieveSubnetworkAliasCIDR retrieves the subnetwork CIDR of the current instance.
RetrieveSubnetworkAliasCIDR(ctx context.Context, project, zone, instanceName string) (string, error)
2022-06-09 20:26:36 +00:00
// RetrieveLoadBalancerIP retrieves the load balancer IP of the current instance.
RetrieveLoadBalancerIP(ctx context.Context, project, zone string) (string, error)
// SetInstanceMetadata sets metadata key: value of the instance specified by project, zone and instanceName.
SetInstanceMetadata(ctx context.Context, project, zone, instanceName, key, value string) error
// UnsetInstanceMetadata removes a metadata key-value pair of the instance specified by project, zone and instanceName.
UnsetInstanceMetadata(ctx context.Context, project, zone, instanceName, key string) error
}
// Metadata implements core.ProviderMetadata interface.
type Metadata struct {
api API
}
// New creates a new Provider with real API and FS.
func New(api API) *Metadata {
return &Metadata{
api: api,
}
}
// List retrieves all instances belonging to the current constellation.
2022-05-24 08:04:42 +00:00
func (m *Metadata) List(ctx context.Context) ([]cloudtypes.Instance, error) {
project, err := m.api.RetrieveProjectID()
if err != nil {
return nil, err
}
zone, err := m.api.RetrieveZone()
if err != nil {
return nil, err
}
instances, err := m.api.RetrieveInstances(ctx, project, zone)
if err != nil {
return nil, fmt.Errorf("retrieving instances list from GCP api: %w", err)
}
return instances, nil
}
// Self retrieves the current instance.
2022-05-24 08:04:42 +00:00
func (m *Metadata) Self(ctx context.Context) (cloudtypes.Instance, error) {
project, err := m.api.RetrieveProjectID()
if err != nil {
2022-05-24 08:04:42 +00:00
return cloudtypes.Instance{}, err
}
zone, err := m.api.RetrieveZone()
if err != nil {
2022-05-24 08:04:42 +00:00
return cloudtypes.Instance{}, err
}
instanceName, err := m.api.RetrieveInstanceName()
if err != nil {
2022-05-24 08:04:42 +00:00
return cloudtypes.Instance{}, err
}
return m.api.RetrieveInstance(ctx, project, zone, instanceName)
}
// GetInstance retrieves an instance using its providerID.
2022-05-24 08:04:42 +00:00
func (m *Metadata) GetInstance(ctx context.Context, providerID string) (cloudtypes.Instance, error) {
project, zone, instanceName, err := gcpshared.SplitProviderID(providerID)
if err != nil {
2022-05-24 08:04:42 +00:00
return cloudtypes.Instance{}, fmt.Errorf("invalid providerID: %w", err)
}
return m.api.RetrieveInstance(ctx, project, zone, instanceName)
}
// SignalRole signals the constellation role via cloud provider metadata.
func (m *Metadata) SignalRole(ctx context.Context, role role.Role) error {
project, err := m.api.RetrieveProjectID()
if err != nil {
return err
}
zone, err := m.api.RetrieveZone()
if err != nil {
return err
}
instanceName, err := m.api.RetrieveInstanceName()
if err != nil {
return err
}
return m.api.SetInstanceMetadata(ctx, project, zone, instanceName, core.RoleMetadataKey, role.String())
}
// SetVPNIP stores the internally used VPN IP in cloud provider metadata.
func (m *Metadata) SetVPNIP(ctx context.Context, vpnIP string) error {
project, err := m.api.RetrieveProjectID()
if err != nil {
return err
}
zone, err := m.api.RetrieveZone()
if err != nil {
return err
}
instanceName, err := m.api.RetrieveInstanceName()
if err != nil {
return err
}
return m.api.SetInstanceMetadata(ctx, project, zone, instanceName, core.VPNIPMetadataKey, vpnIP)
}
2022-05-24 08:04:42 +00:00
// GetSubnetworkCIDR returns the subnetwork CIDR of the current instance.
func (m *Metadata) GetSubnetworkCIDR(ctx context.Context) (string, error) {
project, err := m.api.RetrieveProjectID()
if err != nil {
return "", err
}
zone, err := m.api.RetrieveZone()
if err != nil {
return "", err
}
instanceName, err := m.api.RetrieveInstanceName()
if err != nil {
return "", err
}
return m.api.RetrieveSubnetworkAliasCIDR(ctx, project, zone, instanceName)
}
// SupportsLoadBalancer returns true if the cloud provider supports load balancers.
func (m *Metadata) SupportsLoadBalancer() bool {
2022-06-09 20:26:36 +00:00
return true
2022-05-24 08:04:42 +00:00
}
// GetLoadBalancerIP returns the IP of the load balancer.
func (m *Metadata) GetLoadBalancerIP(ctx context.Context) (string, error) {
2022-06-09 20:26:36 +00:00
project, err := m.api.RetrieveProjectID()
if err != nil {
return "", err
}
zone, err := m.api.RetrieveZone()
if err != nil {
return "", err
}
return m.api.RetrieveLoadBalancerIP(ctx, project, zone)
2022-05-24 08:04:42 +00:00
}
// Supported is used to determine if metadata API is implemented for this cloud provider.
func (m *Metadata) Supported() bool {
return true
}