2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-10-05 03:11:30 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-09-26 09:52:31 -04:00
|
|
|
"runtime"
|
2022-10-05 03:11:30 -04:00
|
|
|
"strings"
|
2022-04-13 07:01:38 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
azurecl "github.com/edgelesssys/constellation/v2/cli/internal/azure/client"
|
2022-10-05 03:11:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
|
2022-09-26 09:52:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudtypes"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/state"
|
2022-04-13 07:01:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Creator creates cloud resources.
|
|
|
|
type Creator struct {
|
2022-09-27 03:22:29 -04:00
|
|
|
out io.Writer
|
|
|
|
newTerraformClient func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error)
|
|
|
|
newAzureClient func(subscriptionID, tenantID, name, location, resourceGroup string) (azureclient, error)
|
2022-10-05 03:11:30 -04:00
|
|
|
newLibvirtRunner func() libvirtRunner
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCreator creates a new creator.
|
|
|
|
func NewCreator(out io.Writer) *Creator {
|
|
|
|
return &Creator{
|
|
|
|
out: out,
|
2022-09-27 03:22:29 -04:00
|
|
|
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
|
|
|
|
return terraform.New(ctx, provider)
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-08-25 09:12:08 -04:00
|
|
|
newAzureClient: func(subscriptionID, tenantID, name, location, resourceGroup string) (azureclient, error) {
|
|
|
|
return azurecl.NewInitialized(subscriptionID, tenantID, name, location, resourceGroup)
|
2022-04-13 07:01:38 -04:00
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
newLibvirtRunner: func() libvirtRunner {
|
|
|
|
return libvirt.New()
|
|
|
|
},
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates the handed amount of instances and all the needed resources.
|
2022-06-29 09:26:29 -04:00
|
|
|
func (c *Creator) Create(ctx context.Context, provider cloudprovider.Provider, config *config.Config, name, insType string, controlPlaneCount, workerCount int,
|
2022-04-13 07:01:38 -04:00
|
|
|
) (state.ConstellationState, error) {
|
2022-09-05 10:53:37 -04:00
|
|
|
// Use debug ingress firewall rules when debug mode / image is enabled
|
|
|
|
var ingressRules cloudtypes.Firewall
|
|
|
|
if config.IsDebugCluster() {
|
|
|
|
ingressRules = constants.IngressRulesDebug
|
|
|
|
} else {
|
|
|
|
ingressRules = constants.IngressRulesNoDebug
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
switch provider {
|
|
|
|
case cloudprovider.GCP:
|
2022-09-27 03:22:29 -04:00
|
|
|
cl, err := c.newTerraformClient(ctx, provider)
|
2022-04-13 07:01:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
2022-09-27 03:22:29 -04:00
|
|
|
defer cl.RemoveInstaller()
|
|
|
|
return c.createGCP(ctx, cl, config, name, insType, controlPlaneCount, workerCount)
|
2022-04-13 07:01:38 -04:00
|
|
|
case cloudprovider.Azure:
|
|
|
|
cl, err := c.newAzureClient(
|
2022-05-16 12:54:25 -04:00
|
|
|
config.Provider.Azure.SubscriptionID,
|
|
|
|
config.Provider.Azure.TenantID,
|
2022-04-13 07:01:38 -04:00
|
|
|
name,
|
2022-05-16 12:54:25 -04:00
|
|
|
config.Provider.Azure.Location,
|
2022-08-25 09:12:08 -04:00
|
|
|
config.Provider.Azure.ResourceGroup,
|
2022-04-13 07:01:38 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
2022-09-05 10:53:37 -04:00
|
|
|
return c.createAzure(ctx, cl, config, insType, controlPlaneCount, workerCount, ingressRules)
|
2022-09-26 09:52:31 -04:00
|
|
|
case cloudprovider.QEMU:
|
2022-09-27 03:22:29 -04:00
|
|
|
if runtime.GOARCH != "amd64" || runtime.GOOS != "linux" {
|
|
|
|
return state.ConstellationState{}, fmt.Errorf("creation of a QEMU based Constellation is not supported for %s/%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
cl, err := c.newTerraformClient(ctx, provider)
|
2022-09-26 09:52:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
|
|
|
defer cl.RemoveInstaller()
|
2022-10-05 03:11:30 -04:00
|
|
|
lv := c.newLibvirtRunner()
|
|
|
|
return c.createQEMU(ctx, cl, lv, name, config, controlPlaneCount, workerCount)
|
2022-04-13 07:01:38 -04:00
|
|
|
default:
|
|
|
|
return state.ConstellationState{}, fmt.Errorf("unsupported cloud provider: %s", provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 03:22:29 -04:00
|
|
|
func (c *Creator) createGCP(ctx context.Context, cl terraformClient, config *config.Config,
|
|
|
|
name, insType string, controlPlaneCount, workerCount int,
|
2022-04-13 07:01:38 -04:00
|
|
|
) (stat state.ConstellationState, retErr error) {
|
2022-09-27 03:22:29 -04:00
|
|
|
defer rollbackOnError(context.Background(), c.out, &retErr, &rollbackerTerraform{client: cl})
|
|
|
|
|
|
|
|
vars := &terraform.GCPVariables{
|
|
|
|
CommonVariables: terraform.CommonVariables{
|
|
|
|
Name: name,
|
|
|
|
CountControlPlanes: controlPlaneCount,
|
|
|
|
CountWorkers: workerCount,
|
|
|
|
StateDiskSizeGB: config.StateDiskSizeGB,
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
2022-09-27 03:22:29 -04:00
|
|
|
Project: config.Provider.GCP.Project,
|
|
|
|
Region: config.Provider.GCP.Region,
|
|
|
|
Zone: config.Provider.GCP.Zone,
|
|
|
|
CredentialsFile: config.Provider.GCP.ServiceAccountKeyPath,
|
|
|
|
InstanceType: insType,
|
|
|
|
StateDiskType: config.Provider.GCP.StateDiskType,
|
|
|
|
ImageID: config.Provider.GCP.Image,
|
|
|
|
Debug: config.IsDebugCluster(),
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 03:22:29 -04:00
|
|
|
if err := cl.CreateCluster(ctx, name, vars); err != nil {
|
2022-06-09 16:26:36 -04:00
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
|
|
|
|
2022-08-01 06:35:35 -04:00
|
|
|
return cl.GetState(), nil
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
|
2022-09-05 10:53:37 -04:00
|
|
|
func (c *Creator) createAzure(ctx context.Context, cl azureclient, config *config.Config, insType string, controlPlaneCount, workerCount int, ingressRules cloudtypes.Firewall,
|
2022-04-13 07:01:38 -04:00
|
|
|
) (stat state.ConstellationState, retErr error) {
|
|
|
|
defer rollbackOnError(context.Background(), c.out, &retErr, &rollbackerAzure{client: cl})
|
|
|
|
|
2022-06-15 06:19:41 -04:00
|
|
|
if err := cl.CreateApplicationInsight(ctx); err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
2022-09-05 12:14:58 -04:00
|
|
|
if err := cl.CreateExternalLoadBalancer(ctx, config.IsDebugCluster()); err != nil {
|
2022-05-24 04:04:42 -04:00
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
2022-04-13 07:01:38 -04:00
|
|
|
if err := cl.CreateVirtualNetwork(ctx); err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
2022-05-16 12:54:25 -04:00
|
|
|
|
|
|
|
if err := cl.CreateSecurityGroup(ctx, azurecl.NetworkSecurityGroupInput{
|
2022-09-05 10:53:37 -04:00
|
|
|
Ingress: ingressRules,
|
|
|
|
Egress: constants.EgressRules,
|
2022-05-16 12:54:25 -04:00
|
|
|
}); err != nil {
|
2022-04-13 07:01:38 -04:00
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
|
|
|
createInput := azurecl.CreateInstancesInput{
|
2022-06-29 09:26:29 -04:00
|
|
|
CountControlPlanes: controlPlaneCount,
|
|
|
|
CountWorkers: workerCount,
|
2022-04-13 07:01:38 -04:00
|
|
|
InstanceType: insType,
|
2022-05-16 12:54:25 -04:00
|
|
|
StateDiskSizeGB: config.StateDiskSizeGB,
|
2022-08-02 06:24:55 -04:00
|
|
|
StateDiskType: config.Provider.Azure.StateDiskType,
|
2022-05-16 12:54:25 -04:00
|
|
|
Image: config.Provider.Azure.Image,
|
|
|
|
UserAssingedIdentity: config.Provider.Azure.UserAssignedIdentity,
|
2022-08-25 09:24:31 -04:00
|
|
|
ConfidentialVM: *config.Provider.Azure.ConfidentialVM,
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
|
|
|
if err := cl.CreateInstances(ctx, createInput); err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
|
|
|
|
2022-08-01 06:35:35 -04:00
|
|
|
return cl.GetState(), nil
|
2022-04-13 07:01:38 -04:00
|
|
|
}
|
2022-09-26 09:52:31 -04:00
|
|
|
|
2022-10-05 03:11:30 -04:00
|
|
|
func (c *Creator) createQEMU(ctx context.Context, cl terraformClient, lv libvirtRunner, name string, config *config.Config,
|
2022-09-27 03:22:29 -04:00
|
|
|
controlPlaneCount, workerCount int,
|
2022-09-26 09:52:31 -04:00
|
|
|
) (stat state.ConstellationState, retErr error) {
|
2022-10-05 03:11:30 -04:00
|
|
|
defer rollbackOnError(context.Background(), c.out, &retErr, &rollbackerQEMU{client: cl, libvirt: lv})
|
|
|
|
|
|
|
|
libvirtURI := config.Provider.QEMU.LibvirtURI
|
|
|
|
libvirtSocketPath := "."
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// if no libvirt URI is specified, start a libvirt container
|
|
|
|
case libvirtURI == "":
|
|
|
|
if err := lv.Start(ctx, name, config.Provider.QEMU.LibvirtContainerImage); err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
2022-10-07 03:38:43 -04:00
|
|
|
libvirtURI = libvirt.LibvirtTCPConnectURI
|
2022-10-05 03:11:30 -04:00
|
|
|
|
|
|
|
// socket for system URI should be in /var/run/libvirt/libvirt-sock
|
|
|
|
case libvirtURI == "qemu:///system":
|
|
|
|
libvirtSocketPath = "/var/run/libvirt/libvirt-sock"
|
|
|
|
|
|
|
|
// socket for session URI should be in /run/user/<uid>/libvirt/libvirt-sock
|
|
|
|
case libvirtURI == "qemu:///session":
|
|
|
|
libvirtSocketPath = fmt.Sprintf("/run/user/%d/libvirt/libvirt-sock", os.Getuid())
|
|
|
|
|
|
|
|
// if a unix socket is specified we need to parse the URI to get the socket path
|
|
|
|
case strings.HasPrefix(libvirtURI, "qemu+unix://"):
|
|
|
|
unixURI, err := url.Parse(strings.TrimPrefix(libvirtURI, "qemu+unix://"))
|
|
|
|
if err != nil {
|
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
|
|
|
libvirtSocketPath = unixURI.Query().Get("socket")
|
|
|
|
if libvirtSocketPath == "" {
|
|
|
|
return state.ConstellationState{}, fmt.Errorf("socket path not specified in qemu+unix URI: %s", libvirtURI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataLibvirtURI := libvirtURI
|
|
|
|
if libvirtSocketPath != "." {
|
|
|
|
metadataLibvirtURI = "qemu:///system"
|
|
|
|
}
|
2022-09-27 03:22:29 -04:00
|
|
|
|
|
|
|
vars := &terraform.QEMUVariables{
|
|
|
|
CommonVariables: terraform.CommonVariables{
|
|
|
|
Name: name,
|
|
|
|
CountControlPlanes: controlPlaneCount,
|
|
|
|
CountWorkers: workerCount,
|
|
|
|
StateDiskSizeGB: config.StateDiskSizeGB,
|
2022-09-26 09:52:31 -04:00
|
|
|
},
|
2022-10-05 03:11:30 -04:00
|
|
|
LibvirtURI: libvirtURI,
|
|
|
|
LibvirtSocketPath: libvirtSocketPath,
|
|
|
|
ImagePath: config.Provider.QEMU.Image,
|
|
|
|
ImageFormat: config.Provider.QEMU.ImageFormat,
|
|
|
|
CPUCount: config.Provider.QEMU.VCPUs,
|
|
|
|
MemorySizeMiB: config.Provider.QEMU.Memory,
|
|
|
|
MetadataAPIImage: config.Provider.QEMU.MetadataAPIImage,
|
|
|
|
MetadataLibvirtURI: metadataLibvirtURI,
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 03:22:29 -04:00
|
|
|
if err := cl.CreateCluster(ctx, name, vars); err != nil {
|
2022-09-26 09:52:31 -04:00
|
|
|
return state.ConstellationState{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cl.GetState(), nil
|
|
|
|
}
|