2022-10-07 03:38:43 -04:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
package cmd
import (
"context"
"errors"
"fmt"
"net"
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
2023-06-28 04:28:48 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/featureset"
2023-08-04 04:42:09 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/helm"
2022-10-07 03:38:43 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
2023-04-14 08:15:07 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2023-06-07 10:16:32 -04:00
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
2023-05-03 05:11:53 -04:00
"github.com/edgelesssys/constellation/v2/internal/atls"
2022-10-07 03:38:43 -04:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/config"
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
"github.com/edgelesssys/constellation/v2/internal/license"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
func newMiniUpCmd ( ) * cobra . Command {
cmd := & cobra . Command {
Use : "up" ,
2022-10-14 04:48:20 -04:00
Short : "Create and initialize a new MiniConstellation cluster" ,
2023-01-17 08:01:56 -05:00
Long : "Create and initialize a new MiniConstellation cluster.\n\n" +
"A mini cluster consists of a single control-plane and worker node, hosted using QEMU/KVM." ,
2022-10-07 03:38:43 -04:00
Args : cobra . ExactArgs ( 0 ) ,
RunE : runUp ,
}
// override global flag so we don't have a default value for the config
2023-01-17 08:01:56 -05:00
cmd . Flags ( ) . String ( "config" , "" , "path to the configuration file to use for the cluster" )
2023-03-08 09:48:36 -05:00
cmd . Flags ( ) . Bool ( "merge-kubeconfig" , true , "merge Constellation kubeconfig file with default kubeconfig file in $HOME/.kube/config" )
2022-10-07 03:38:43 -04:00
return cmd
}
2023-01-04 04:46:29 -05:00
type miniUpCmd struct {
2023-06-01 07:55:46 -04:00
log debugLog
2023-06-07 10:16:32 -04:00
configFetcher attestationconfigapi . Fetcher
2023-01-04 04:46:29 -05:00
}
2023-03-20 06:03:36 -04:00
func runUp ( cmd * cobra . Command , _ [ ] string ) error {
2023-01-04 04:46:29 -05:00
log , err := newCLILogger ( cmd )
if err != nil {
return fmt . Errorf ( "creating logger: %w" , err )
}
defer log . Sync ( )
2023-01-18 07:10:24 -05:00
spinner , err := newSpinnerOrStderr ( cmd )
if err != nil {
return err
}
2022-10-07 13:35:07 -04:00
defer spinner . Stop ( )
2022-10-21 08:26:42 -04:00
creator := cloudcmd . NewCreator ( spinner )
2022-10-07 13:35:07 -04:00
2023-06-07 10:16:32 -04:00
m := & miniUpCmd { log : log , configFetcher : attestationconfigapi . NewFetcher ( ) }
2023-01-04 04:46:29 -05:00
return m . up ( cmd , creator , spinner )
2022-10-07 13:35:07 -04:00
}
2023-01-04 04:46:29 -05:00
func ( m * miniUpCmd ) up ( cmd * cobra . Command , creator cloudCreator , spinner spinnerInterf ) error {
if err := m . checkSystemRequirements ( cmd . ErrOrStderr ( ) ) ; err != nil {
2022-10-07 03:38:43 -04:00
return fmt . Errorf ( "system requirements not met: %w" , err )
}
2023-04-14 08:15:07 -04:00
flags , err := m . parseUpFlags ( cmd )
if err != nil {
return fmt . Errorf ( "parsing flags: %w" , err )
}
2022-10-07 03:38:43 -04:00
fileHandler := file . NewHandler ( afero . NewOsFs ( ) )
// create config if not passed as flag and set default values
2023-04-14 08:15:07 -04:00
config , err := m . prepareConfig ( cmd , fileHandler , flags )
2022-10-07 03:38:43 -04:00
if err != nil {
return fmt . Errorf ( "preparing config: %w" , err )
}
// create cluster
2022-10-07 13:35:07 -04:00
spinner . Start ( "Creating cluster in QEMU " , false )
2023-04-14 08:15:07 -04:00
err = m . createMiniCluster ( cmd . Context ( ) , fileHandler , creator , config , flags . tfLogLevel )
2022-10-07 03:38:43 -04:00
spinner . Stop ( )
if err != nil {
return fmt . Errorf ( "creating cluster: %w" , err )
}
cmd . Println ( "Cluster successfully created." )
connectURI := config . Provider . QEMU . LibvirtURI
2023-01-04 04:46:29 -05:00
m . log . Debugf ( "Using connect URI %s" , connectURI )
2022-10-07 03:38:43 -04:00
if connectURI == "" {
connectURI = libvirt . LibvirtTCPConnectURI
}
cmd . Println ( "Connect to the VMs by executing:" )
cmd . Printf ( "\tvirsh -c %s\n\n" , connectURI )
// initialize cluster
2023-01-04 04:46:29 -05:00
if err := m . initializeMiniCluster ( cmd , fileHandler , spinner ) ; err != nil {
2022-10-07 03:38:43 -04:00
return fmt . Errorf ( "initializing cluster: %w" , err )
}
2023-01-04 04:46:29 -05:00
m . log . Debugf ( "Initialized cluster" )
2022-10-07 03:38:43 -04:00
return nil
}
// prepareConfig reads a given config, or creates a new minimal QEMU config.
2023-04-14 08:15:07 -04:00
func ( m * miniUpCmd ) prepareConfig ( cmd * cobra . Command , fileHandler file . Handler , flags upFlags ) ( * config . Config , error ) {
2022-10-07 03:38:43 -04:00
// check for existing config
2023-04-14 08:15:07 -04:00
if flags . configPath != "" {
2023-06-01 07:55:46 -04:00
conf , err := config . New ( fileHandler , flags . configPath , m . configFetcher , flags . force )
2023-02-07 06:56:25 -05:00
var configValidationErr * config . ValidationError
if errors . As ( err , & configValidationErr ) {
cmd . PrintErrln ( configValidationErr . LongMessage ( ) )
}
2022-10-07 03:38:43 -04:00
if err != nil {
2023-02-07 06:56:25 -05:00
return nil , err
2022-10-07 03:38:43 -04:00
}
2022-11-15 09:40:49 -05:00
if conf . GetProvider ( ) != cloudprovider . QEMU {
2022-10-14 04:48:20 -04:00
return nil , errors . New ( "invalid provider for MiniConstellation cluster" )
2022-10-07 03:38:43 -04:00
}
2022-11-15 09:40:49 -05:00
return conf , nil
2022-10-07 03:38:43 -04:00
}
2023-04-14 08:15:07 -04:00
m . log . Debugf ( "Configuration path is %q" , flags . configPath )
2022-10-20 07:36:20 -04:00
if err := cmd . Flags ( ) . Set ( "config" , constants . ConfigFilename ) ; err != nil {
return nil , err
}
2023-04-14 08:15:07 -04:00
_ , err := fileHandler . Stat ( constants . ConfigFilename )
2022-10-20 07:36:20 -04:00
if err == nil {
// config already exists, prompt user to overwrite
2023-02-21 10:46:47 -05:00
cmd . PrintErrln ( "A config file already exists in the current workspace. Use --config to use an existing config file." )
2022-10-20 07:36:20 -04:00
ok , err := askToConfirm ( cmd , "Do you want to overwrite it?" )
if err != nil {
return nil , err
}
2023-02-21 10:46:47 -05:00
2022-10-20 07:36:20 -04:00
if ! ok {
return nil , errors . New ( "not overwriting existing config" )
}
}
2023-06-28 04:28:48 -04:00
if ! featureset . CanUseEmbeddedMeasurmentsAndImage {
cmd . PrintErrln ( "Generating a valid default config is not supported in the OSS build of the Constellation CLI. Consult the documentation for instructions on where to download the enterprise version." )
return nil , errors . New ( "cannot create a mini cluster without a config file in the OSS build" )
}
config , err := config . MiniDefault ( )
if err != nil {
return nil , fmt . Errorf ( "mini default config is invalid: %v" , err )
2023-05-12 11:14:32 -04:00
}
2023-01-18 07:10:24 -05:00
m . log . Debugf ( "Prepared configuration" )
2022-10-07 03:38:43 -04:00
return config , fileHandler . WriteYAML ( constants . ConfigFilename , config , file . OptOverwrite )
}
// createMiniCluster creates a new cluster using the given config.
2023-04-14 08:15:07 -04:00
func ( m * miniUpCmd ) createMiniCluster ( ctx context . Context , fileHandler file . Handler , creator cloudCreator , config * config . Config , tfLogLevel terraform . LogLevel ) error {
2023-01-04 04:46:29 -05:00
m . log . Debugf ( "Creating mini cluster" )
2023-04-14 08:15:07 -04:00
opts := cloudcmd . CreateOptions {
2023-08-02 04:36:55 -04:00
Provider : cloudprovider . QEMU ,
Config : config ,
TFLogLevel : tfLogLevel ,
2023-04-14 08:15:07 -04:00
}
idFile , err := creator . Create ( ctx , opts )
2022-10-07 03:38:43 -04:00
if err != nil {
return err
}
2023-01-25 08:42:52 -05:00
idFile . UID = constants . MiniConstellationUID // use UID "mini" to identify MiniConstellation clusters.
2023-01-04 04:46:29 -05:00
m . log . Debugf ( "Cluster id file contains %v" , idFile )
2022-10-11 06:24:33 -04:00
return fileHandler . WriteJSON ( constants . ClusterIDsFileName , idFile , file . OptNone )
2022-10-07 03:38:43 -04:00
}
// initializeMiniCluster initializes a QEMU cluster.
2023-01-04 04:46:29 -05:00
func ( m * miniUpCmd ) initializeMiniCluster ( cmd * cobra . Command , fileHandler file . Handler , spinner spinnerInterf ) ( retErr error ) {
m . log . Debugf ( "Initializing mini cluster" )
2022-10-07 03:38:43 -04:00
// clean up cluster resources if initialization fails
defer func ( ) {
if retErr != nil {
2022-11-10 04:27:24 -05:00
cmd . PrintErrf ( "An error occurred: %s\n" , retErr )
cmd . PrintErrln ( "Attempting to roll back." )
2022-10-07 03:38:43 -04:00
_ = runDown ( cmd , [ ] string { } )
2022-11-10 04:27:24 -05:00
cmd . PrintErrf ( "Rollback succeeded.\n\n" )
2022-10-07 03:38:43 -04:00
}
} ( )
2023-05-03 05:11:53 -04:00
newDialer := func ( validator atls . Validator ) * dialer . Dialer {
return dialer . New ( nil , validator , & net . Dialer { } )
2022-10-07 03:38:43 -04:00
}
2023-01-04 04:46:29 -05:00
m . log . Debugf ( "Created new dialer" )
2022-10-07 03:38:43 -04:00
cmd . Flags ( ) . String ( "master-secret" , "" , "" )
cmd . Flags ( ) . String ( "endpoint" , "" , "" )
cmd . Flags ( ) . Bool ( "conformance" , false , "" )
2023-07-10 09:16:45 -04:00
cmd . Flags ( ) . Bool ( "skip-helm-wait" , false , "install helm charts without waiting for deployments to be ready" )
2023-01-04 04:46:29 -05:00
log , err := newCLILogger ( cmd )
if err != nil {
return fmt . Errorf ( "creating logger: %w" , err )
}
m . log . Debugf ( "Created new logger" )
defer log . Sync ( )
2023-08-04 04:42:09 -04:00
helmInstaller , err := helm . NewInitializer ( log )
if err != nil {
return fmt . Errorf ( "creating Helm installer: %w" , err )
}
tfClient , err := terraform . New ( cmd . Context ( ) , constants . TerraformWorkingDir )
if err != nil {
return fmt . Errorf ( "creating Terraform client: %w" , err )
}
i := newInitCmd ( tfClient , helmInstaller , fileHandler , spinner , & kubeconfigMerger { log : log } , log )
if err := i . initialize ( cmd , newDialer , license . NewClient ( ) , m . configFetcher ) ; err != nil {
2022-10-07 03:38:43 -04:00
return err
}
2023-01-04 04:46:29 -05:00
m . log . Debugf ( "Initialized mini cluster" )
2022-10-07 03:38:43 -04:00
return nil
}
2023-04-14 08:15:07 -04:00
type upFlags struct {
configPath string
force bool
tfLogLevel terraform . LogLevel
}
func ( m * miniUpCmd ) parseUpFlags ( cmd * cobra . Command ) ( upFlags , error ) {
m . log . Debugf ( "Preparing configuration" )
configPath , err := cmd . Flags ( ) . GetString ( "config" )
if err != nil {
return upFlags { } , fmt . Errorf ( "parsing config string: %w" , err )
}
m . log . Debugf ( "Configuration path is %q" , configPath )
force , err := cmd . Flags ( ) . GetBool ( "force" )
if err != nil {
return upFlags { } , fmt . Errorf ( "parsing force bool: %w" , err )
}
m . log . Debugf ( "force flag is %q" , configPath )
logLevelString , err := cmd . Flags ( ) . GetString ( "tf-log" )
if err != nil {
return upFlags { } , fmt . Errorf ( "parsing tf-log string: %w" , err )
}
logLevel , err := terraform . ParseLogLevel ( logLevelString )
if err != nil {
return upFlags { } , fmt . Errorf ( "parsing Terraform log level %s: %w" , logLevelString , err )
}
m . log . Debugf ( "Terraform logs will be written into %s at level %s" , constants . TerraformLogFile , logLevel . String ( ) )
return upFlags {
configPath : configPath ,
force : force ,
tfLogLevel : logLevel ,
} , nil
}