constellation/debugd/cdbg/state/state.go

79 lines
3.2 KiB
Go
Raw Normal View History

package state
import (
"errors"
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
2022-06-07 10:50:12 -04:00
"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
2022-06-07 11:15:23 -04:00
func GetScalingGroupsFromConfig(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) {
switch {
case len(stat.GCPCoordinators) != 0:
return getGCPInstances(stat, config)
case len(stat.AzureCoordinators) != 0:
return getAzureInstances(stat, config)
case len(stat.QEMUCoordinators) != 0:
return getQEMUInstances(stat, config)
default:
2022-06-07 11:15:23 -04:00
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no instances to init")
}
}
2022-06-07 11:15:23 -04:00
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")
}
2022-06-07 11:15:23 -04:00
// GroupID of coordinators is empty, since they currently do not scale.
coordinators = cloudtypes.ScalingGroup{Instances: stat.GCPCoordinators}
2022-06-07 11:15:23 -04:00
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
2022-06-07 11:15:23 -04:00
nodes = cloudtypes.ScalingGroup{Instances: stat.GCPNodes}
return
}
2022-06-07 11:15:23 -04:00
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")
}
2022-06-07 11:15:23 -04:00
// GroupID of coordinators is empty, since they currently do not scale.
2022-06-07 11:15:23 -04:00
coordinators = cloudtypes.ScalingGroup{Instances: stat.AzureCoordinators}
2022-06-07 11:15:23 -04:00
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
2022-06-07 11:15:23 -04:00
nodes = cloudtypes.ScalingGroup{Instances: stat.AzureNodes}
return
}
2022-06-07 11:15:23 -04:00
func getQEMUInstances(stat state.ConstellationState, config *config.Config) (coordinators, nodes cloudtypes.ScalingGroup, err error) {
coordinatorMap := stat.QEMUCoordinators
if len(coordinatorMap) == 0 {
2022-06-07 11:15:23 -04:00
return cloudtypes.ScalingGroup{}, cloudtypes.ScalingGroup{}, errors.New("no coordinators available, can't create Constellation without any instance")
}
2022-06-07 11:15:23 -04:00
// 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")
}
2022-06-07 11:15:23 -04:00
// QEMU does not support autoscaling
nodes = cloudtypes.ScalingGroup{Instances: stat.QEMUNodes}
return
}