mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-02-24 00:40:11 -05:00

Co-authored-by: Malte Poll <mp@edgeless.systems> Co-authored-by: katexochen <katexochen@users.noreply.github.com> Co-authored-by: Daniel Weiße <dw@edgeless.systems> Co-authored-by: Thomas Tendyck <tt@edgeless.systems> Co-authored-by: Benedict Schlueter <bs@edgeless.systems> Co-authored-by: leongross <leon.gross@rub.de> Co-authored-by: Moritz Eckert <m1gh7ym0@gmail.com>
105 lines
3.7 KiB
Go
105 lines
3.7 KiB
Go
package state
|
|
|
|
import (
|
|
"errors"
|
|
|
|
cmdc "github.com/edgelesssys/constellation/cli/cmd"
|
|
"github.com/edgelesssys/constellation/cli/gcp"
|
|
configc "github.com/edgelesssys/constellation/internal/config"
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
)
|
|
|
|
// 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 *configc.Config) (coordinators, nodes cmdc.ScalingGroup, err error) {
|
|
switch {
|
|
case len(stat.EC2Instances) != 0:
|
|
return getAWSInstances(stat, config)
|
|
case len(stat.GCPCoordinators) != 0:
|
|
return getGCPInstances(stat, config)
|
|
case len(stat.AzureCoordinators) != 0:
|
|
return getAzureInstances(stat, config)
|
|
default:
|
|
return cmdc.ScalingGroup{}, cmdc.ScalingGroup{}, errors.New("no instances to init")
|
|
}
|
|
}
|
|
|
|
func getAWSInstances(stat state.ConstellationState, config *configc.Config) (coordinators, nodes cmdc.ScalingGroup, err error) {
|
|
coordinatorID, coordinator, err := stat.EC2Instances.GetOne()
|
|
if err != nil {
|
|
return
|
|
}
|
|
// GroupID of coordinators is empty, since they currently do not scale.
|
|
coordinators = cmdc.ScalingGroup{Instances: cmdc.Instances{cmdc.Instance(coordinator)}, GroupID: ""}
|
|
|
|
nodeMap := stat.EC2Instances.GetOthers(coordinatorID)
|
|
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))
|
|
}
|
|
|
|
// TODO: make min / max configurable and abstract autoscaling for different cloud providers
|
|
// TODO: GroupID of nodes is empty, since they currently do not scale.
|
|
nodes = cmdc.ScalingGroup{Instances: nodeInstances, GroupID: ""}
|
|
|
|
return
|
|
}
|
|
|
|
func getGCPInstances(stat state.ConstellationState, config *configc.Config) (coordinators, nodes cmdc.ScalingGroup, err error) {
|
|
_, coordinator, err := stat.GCPCoordinators.GetOne()
|
|
if err != nil {
|
|
return
|
|
}
|
|
// GroupID of coordinators is empty, since they currently do not scale.
|
|
coordinators = cmdc.ScalingGroup{Instances: cmdc.Instances{cmdc.Instance(coordinator)}, GroupID: ""}
|
|
|
|
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))
|
|
}
|
|
|
|
// TODO: make min / max configurable and abstract autoscaling for different cloud providers
|
|
nodes = cmdc.ScalingGroup{
|
|
Instances: nodeInstances,
|
|
GroupID: gcp.AutoscalingNodeGroup(stat.GCPProject, stat.GCPZone, stat.GCPNodeInstanceGroup, *config.AutoscalingNodeGroupsMin, *config.AutoscalingNodeGroupsMax),
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func getAzureInstances(stat state.ConstellationState, config *configc.Config) (coordinators, nodes cmdc.ScalingGroup, err error) {
|
|
_, coordinator, err := stat.AzureCoordinators.GetOne()
|
|
if err != nil {
|
|
return
|
|
}
|
|
// GroupID of coordinators is empty, since they currently do not scale.
|
|
coordinators = cmdc.ScalingGroup{Instances: cmdc.Instances{cmdc.Instance(coordinator)}, 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")
|
|
}
|
|
|
|
var nodeInstances cmdc.Instances
|
|
for _, node := range nodeMap {
|
|
nodeInstances = append(nodeInstances, cmdc.Instance(node))
|
|
}
|
|
|
|
// TODO: make min / max configurable and abstract autoscaling for different cloud providers
|
|
nodes = cmdc.ScalingGroup{
|
|
Instances: nodeInstances,
|
|
GroupID: "",
|
|
}
|
|
return
|
|
}
|