diff --git a/cli/cloud/cloudtypes/instance.go b/cli/cloud/cloudtypes/instance.go index f303bc069..901cec7c4 100644 --- a/cli/cloud/cloudtypes/instance.go +++ b/cli/cloud/cloudtypes/instance.go @@ -56,3 +56,9 @@ func (i Instances) GetOthers(id string) Instances { } return others } + +// ScalingGroup is a group of instances, with an identifying group ID. +type ScalingGroup struct { + Instances + GroupID string +} diff --git a/cli/cmd/init.go b/cli/cmd/init.go index 5a37658f9..cd2016e4b 100644 --- a/cli/cmd/init.go +++ b/cli/cmd/init.go @@ -12,6 +12,7 @@ import ( "text/tabwriter" "github.com/edgelesssys/constellation/cli/cloud/cloudcmd" + "github.com/edgelesssys/constellation/cli/cloud/cloudtypes" "github.com/edgelesssys/constellation/cli/internal/azure" "github.com/edgelesssys/constellation/cli/internal/gcp" "github.com/edgelesssys/constellation/cli/internal/proto" @@ -395,7 +396,7 @@ func readOrGeneratedMasterSecret(w io.Writer, fileHandler file.Handler, filename return masterSecret, nil } -func getScalingGroupsFromConfig(stat state.ConstellationState, config *config.Config) (coordinators, nodes ScalingGroup, err error) { +func getScalingGroupsFromConfig(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { switch { case len(stat.GCPCoordinators) != 0: return getGCPInstances(stat, config) @@ -404,103 +405,76 @@ func getScalingGroupsFromConfig(stat state.ConstellationState, config *config.Co case len(stat.QEMUCoordinators) != 0: return getQEMUInstances(stat, config) default: - return ScalingGroup{}, ScalingGroup{}, errors.New("no instances to initialize") + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no instances to initialize") } } -func getGCPInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes ScalingGroup, err error) { - coordinatorMap := stat.GCPCoordinators - if len(coordinatorMap) == 0 { - return ScalingGroup{}, ScalingGroup{}, errors.New("no control-plane nodes available, can't create Constellation without any instance") - } - var coordinatorInstances Instances - for _, node := range coordinatorMap { - coordinatorInstances = append(coordinatorInstances, Instance(node)) +func getGCPInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { + if len(stat.GCPCoordinators) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no control-plane nodes available, can't create Constellation without any instance") } + // GroupID of coordinators is empty, since they currently do not scale. - coordinators = ScalingGroup{ - Instances: coordinatorInstances, + coordinators = cloudtypes.ScalingGroup{ + Instances: stat.GCPCoordinators, GroupID: "", } - nodeMap := stat.GCPNodes - if len(nodeMap) == 0 { - return ScalingGroup{}, ScalingGroup{}, errors.New("no worker nodes available, can't create Constellation with one instance") - } - - var nodeInstances Instances - for _, node := range nodeMap { - nodeInstances = append(nodeInstances, Instance(node)) + if len(stat.GCPNodes) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no worker nodes available, can't create Constellation with one instance") } // TODO: make min / max configurable and abstract autoscaling for different cloud providers - nodes = ScalingGroup{ - Instances: nodeInstances, + nodes = cloudtypes.ScalingGroup{ + Instances: stat.GCPNodes, GroupID: gcp.AutoscalingNodeGroup(stat.GCPProject, stat.GCPZone, stat.GCPNodeInstanceGroup, config.AutoscalingNodeGroupMin, config.AutoscalingNodeGroupMax), } return } -func getAzureInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes ScalingGroup, err error) { - coordinatorMap := stat.AzureCoordinators - if len(coordinatorMap) == 0 { - return ScalingGroup{}, ScalingGroup{}, errors.New("no control-plane nodes available, can't create Constellation cluster without any instance") - } - var coordinatorInstances Instances - for _, node := range coordinatorMap { - coordinatorInstances = append(coordinatorInstances, Instance(node)) - } - // GroupID of coordinators is empty, since they currently do not scale. - coordinators = ScalingGroup{ - Instances: coordinatorInstances, - GroupID: "", - } - nodeMap := stat.AzureNodes - if len(nodeMap) == 0 { - return ScalingGroup{}, ScalingGroup{}, errors.New("no worker nodes available, can't create Constellation cluster with one instance") +func getAzureInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { + if len(stat.AzureCoordinators) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no control-plane nodes available, can't create Constellation cluster without any instance") } - var nodeInstances Instances - for _, node := range nodeMap { - nodeInstances = append(nodeInstances, Instance(node)) + // GroupID of coordinators is empty, since they currently do not scale. + coordinators = cloudtypes.ScalingGroup{ + Instances: stat.AzureCoordinators, + GroupID: "", + } + + if len(stat.AzureNodes) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no worker nodes available, can't create Constellation cluster with one instance") } // TODO: make min / max configurable and abstract autoscaling for different cloud providers - nodes = ScalingGroup{ - Instances: nodeInstances, + nodes = cloudtypes.ScalingGroup{ + Instances: stat.AzureNodes, GroupID: azure.AutoscalingNodeGroup(stat.AzureNodesScaleSet, config.AutoscalingNodeGroupMin, config.AutoscalingNodeGroupMax), } return } -func getQEMUInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes ScalingGroup, err error) { +func getQEMUInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { coordinatorMap := stat.QEMUCoordinators if len(coordinatorMap) == 0 { - return ScalingGroup{}, ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance") - } - var coordinatorInstances Instances - for _, node := range coordinatorMap { - coordinatorInstances = append(coordinatorInstances, Instance(node)) + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance") } + // QEMU does not support autoscaling - coordinators = ScalingGroup{ - Instances: coordinatorInstances, + coordinators = cloudtypes.ScalingGroup{ + Instances: stat.QEMUCoordinators, GroupID: "", } - nodeMap := stat.QEMUNodes - if len(nodeMap) == 0 { - return ScalingGroup{}, ScalingGroup{}, errors.New("no nodes available, can't create Constellation with one instance") - } - var nodeInstances Instances - for _, node := range nodeMap { - nodeInstances = append(nodeInstances, Instance(node)) + if len(stat.QEMUNodes) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no nodes available, can't create Constellation with one instance") } // QEMU does not support autoscaling - nodes = ScalingGroup{ - Instances: nodeInstances, + nodes = cloudtypes.ScalingGroup{ + Instances: stat.QEMUNodes, GroupID: "", } return @@ -514,38 +488,3 @@ func initCompletion(cmd *cobra.Command, args []string, toComplete string) ([]str } return []string{}, cobra.ShellCompDirectiveDefault } - -// -// TODO: Code below is target of multicloud refactoring. -// - -// Instance is a cloud instance. -type Instance struct { - PublicIP string - PrivateIP string -} - -type Instances []Instance - -type ScalingGroup struct { - Instances - GroupID string -} - -// PublicIPs returns the public IPs of all the instances. -func (i Instances) PublicIPs() []string { - var ips []string - for _, instance := range i { - ips = append(ips, instance.PublicIP) - } - return ips -} - -// PrivateIPs returns the private IPs of all the instances of the Constellation. -func (i Instances) PrivateIPs() []string { - var ips []string - for _, instance := range i { - ips = append(ips, instance.PrivateIP) - } - return ips -} diff --git a/debugd/cdbg/state/state.go b/debugd/cdbg/state/state.go index 17391a7d4..c8c7a8946 100644 --- a/debugd/cdbg/state/state.go +++ b/debugd/cdbg/state/state.go @@ -3,7 +3,7 @@ package state import ( "errors" - cmdc "github.com/edgelesssys/constellation/cli/cmd" + "github.com/edgelesssys/constellation/cli/cloud/cloudtypes" "github.com/edgelesssys/constellation/internal/config" "github.com/edgelesssys/constellation/internal/state" ) @@ -11,7 +11,7 @@ import ( // Code in this file is mostly copied from constellation-coordinator // TODO: import as package from coordinator once it is properly refactored -func GetScalingGroupsFromConfig(stat state.ConstellationState, config *config.Config) (coordinators, nodes cmdc.ScalingGroup, err error) { +func GetScalingGroupsFromConfig(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { switch { case len(stat.GCPCoordinators) != 0: return getGCPInstances(stat, config) @@ -20,92 +20,59 @@ func GetScalingGroupsFromConfig(stat state.ConstellationState, config *config.Co case len(stat.QEMUCoordinators) != 0: return getQEMUInstances(stat, config) default: - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no instances to init") + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no instances to init") } } -func getGCPInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cmdc.ScalingGroup, err error) { - coordinatorMap := stat.GCPCoordinators - if len(coordinatorMap) == 0 { - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance") - } - var coordinatorInstances cmdc.Instances - for _, node := range coordinatorMap { - coordinatorInstances = append(coordinatorInstances, cmdc.Instance(node)) +func getGCPInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { + if len(stat.GCPCoordinators) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no control-plane nodes available, can't create Constellation without any instance") } + // GroupID of coordinators is empty, since they currently do not scale. - coordinators = cmdc.ScalingGroup{ - Instances: coordinatorInstances, - GroupID: "", - } + coordinators = cloudtypes.ScalingGroup{Instances: stat.GCPCoordinators} - nodeMap := stat.GCPNodes - if len(nodeMap) == 0 { - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no nodes available, can't create Constellation with one instance") - } - - var nodeInstances cmdc.Instances - for _, node := range nodeMap { - nodeInstances = append(nodeInstances, cmdc.Instance(node)) + if len(stat.GCPNodes) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no worker nodes available, can't create Constellation with one instance") } // TODO: make min / max configurable and abstract autoscaling for different cloud providers - nodes = cmdc.ScalingGroup{Instances: nodeInstances} + nodes = cloudtypes.ScalingGroup{Instances: stat.GCPNodes} return } -func getAzureInstances(stat state.ConstellationState, _ *config.Config) (coordinators, nodes cmdc.ScalingGroup, err error) { - coordinatorMap := stat.AzureCoordinators - if len(coordinatorMap) == 0 { - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance") - } - var coordinatorInstances cmdc.Instances - for _, node := range coordinatorMap { - coordinatorInstances = append(coordinatorInstances, cmdc.Instance(node)) - } - // GroupID of coordinators is empty, since they currently do not scale. - coordinators = cmdc.ScalingGroup{ - Instances: coordinatorInstances, - GroupID: "", - } - nodeMap := stat.AzureNodes - if len(nodeMap) == 0 { - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no nodes available, can't create Constellation with one instance") +func getAzureInstances(stat state.ConstellationState, _ *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { + if len(stat.AzureCoordinators) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no control-plane nodes available, can't create Constellation cluster without any instance") } - var nodeInstances cmdc.Instances - for _, node := range nodeMap { - nodeInstances = append(nodeInstances, cmdc.Instance(node)) + // GroupID of coordinators is empty, since they currently do not scale. + coordinators = cloudtypes.ScalingGroup{Instances: stat.AzureCoordinators} + + if len(stat.AzureNodes) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no worker nodes available, can't create Constellation cluster with one instance") } // TODO: make min / max configurable and abstract autoscaling for different cloud providers - nodes = cmdc.ScalingGroup{Instances: nodeInstances} + nodes = cloudtypes.ScalingGroup{Instances: stat.AzureNodes} return } -func getQEMUInstances(stat state.ConstellationState, _ *config.Config) (coordinators, nodes cmdc.ScalingGroup, err error) { +func getQEMUInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) { coordinatorMap := stat.QEMUCoordinators if len(coordinatorMap) == 0 { - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance") + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance") } - var coordinatorInstances cmdc.Instances - for _, node := range coordinatorMap { - coordinatorInstances = append(coordinatorInstances, cmdc.Instance(node)) + + // QEMU does not support autoscaling + coordinators = cloudtypes.ScalingGroup{Instances: stat.QEMUCoordinators} + + if len(stat.QEMUNodes) == 0 { + return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no nodes available, can't create Constellation with one instance") } - // GroupID of coordinators is empty, since they currently do not scale. - coordinators = cmdc.ScalingGroup{ - Instances: coordinatorInstances, - GroupID: "", - } - nodeMap := stat.QEMUNodes - if len(nodeMap) == 0 { - return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no nodes available, can't create Constellation with one instance") - } - var nodeInstances cmdc.Instances - for _, node := range nodeMap { - nodeInstances = append(nodeInstances, cmdc.Instance(node)) - } - nodes = cmdc.ScalingGroup{Instances: nodeInstances} + + // QEMU does not support autoscaling + nodes = cloudtypes.ScalingGroup{Instances: stat.QEMUNodes} return }