2022-04-21 10:28:37 -04:00
package qemu
import (
"context"
2022-06-30 05:14:26 -04:00
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
2022-04-21 10:28:37 -04:00
"github.com/edgelesssys/constellation/coordinator/role"
2022-06-28 10:08:05 -04:00
"github.com/edgelesssys/constellation/internal/cloud/metadata"
2022-04-21 10:28:37 -04:00
)
2022-06-30 05:14:26 -04:00
const qemuMetadataEndpoint = "10.42.0.1:8080"
// Metadata implements core.ProviderMetadata interface for QEMU.
2022-04-21 10:28:37 -04:00
type Metadata struct { }
// Supported is used to determine if metadata API is implemented for this cloud provider.
func ( m * Metadata ) Supported ( ) bool {
2022-06-30 05:14:26 -04:00
return true
2022-04-21 10:28:37 -04:00
}
// List retrieves all instances belonging to the current constellation.
2022-06-28 10:08:05 -04:00
func ( m * Metadata ) List ( ctx context . Context ) ( [ ] metadata . InstanceMetadata , error ) {
2022-06-30 05:14:26 -04:00
instancesRaw , err := m . retrieveMetadata ( ctx , "/peers" )
if err != nil {
return nil , err
}
2022-06-28 10:08:05 -04:00
var instances [ ] metadata . InstanceMetadata
2022-06-30 05:14:26 -04:00
err = json . Unmarshal ( instancesRaw , & instances )
return instances , err
2022-04-21 10:28:37 -04:00
}
// Self retrieves the current instance.
2022-06-28 10:08:05 -04:00
func ( m * Metadata ) Self ( ctx context . Context ) ( metadata . InstanceMetadata , error ) {
2022-06-30 05:14:26 -04:00
instanceRaw , err := m . retrieveMetadata ( ctx , "/self" )
if err != nil {
2022-06-28 10:08:05 -04:00
return metadata . InstanceMetadata { } , err
2022-06-30 05:14:26 -04:00
}
2022-06-28 10:08:05 -04:00
var instance metadata . InstanceMetadata
2022-06-30 05:14:26 -04:00
err = json . Unmarshal ( instanceRaw , & instance )
return instance , err
2022-04-21 10:28:37 -04:00
}
// GetInstance retrieves an instance using its providerID.
2022-06-28 10:08:05 -04:00
func ( m Metadata ) GetInstance ( ctx context . Context , providerID string ) ( metadata . InstanceMetadata , error ) {
2022-06-30 05:14:26 -04:00
instances , err := m . List ( ctx )
if err != nil {
2022-06-28 10:08:05 -04:00
return metadata . InstanceMetadata { } , err
2022-06-30 05:14:26 -04:00
}
for _ , instance := range instances {
if instance . ProviderID == providerID {
return instance , nil
}
}
2022-06-28 10:08:05 -04:00
return metadata . InstanceMetadata { } , errors . New ( "instance not found" )
2022-04-21 10:28:37 -04:00
}
// SignalRole signals the constellation role via cloud provider metadata (if supported by the CSP and deployment type, otherwise does nothing).
func ( m Metadata ) SignalRole ( ctx context . Context , role role . Role ) error {
2022-06-30 05:14:26 -04:00
return nil
2022-04-21 10:28:37 -04:00
}
// SetVPNIP stores the internally used VPN IP in cloud provider metadata (if supported and required for autoscaling by the CSP, otherwise does nothing).
func ( m Metadata ) SetVPNIP ( ctx context . Context , vpnIP string ) error {
2022-06-30 05:14:26 -04:00
return nil
2022-04-21 10:28:37 -04:00
}
2022-05-24 04:04:42 -04:00
// SupportsLoadBalancer returns true if the cloud provider supports load balancers.
func ( m Metadata ) SupportsLoadBalancer ( ) bool {
return false
}
// GetLoadBalancerIP returns the IP of the load balancer.
func ( m Metadata ) GetLoadBalancerIP ( ctx context . Context ) ( string , error ) {
panic ( "function *Metadata.GetLoadBalancerIP not implemented" )
}
// GetSubnetworkCIDR retrieves the subnetwork CIDR from cloud provider metadata.
func ( m Metadata ) GetSubnetworkCIDR ( ctx context . Context ) ( string , error ) {
2022-06-30 05:14:26 -04:00
return "10.244.0.0/16" , nil
}
func ( m Metadata ) retrieveMetadata ( ctx context . Context , uri string ) ( [ ] byte , error ) {
url := & url . URL {
Scheme : "http" ,
Host : qemuMetadataEndpoint ,
Path : uri ,
}
req , err := http . NewRequestWithContext ( ctx , http . MethodGet , url . String ( ) , nil )
if err != nil {
return nil , err
}
res , err := ( & http . Client { } ) . Do ( req )
if err != nil {
return nil , err
}
defer res . Body . Close ( )
return io . ReadAll ( res . Body )
2022-05-24 04:04:42 -04:00
}