2022-03-22 11:03:15 -04:00
package state
import (
"errors"
2022-06-08 02:17:52 -04:00
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
2022-06-07 10:50:12 -04:00
"github.com/edgelesssys/constellation/internal/config"
2022-03-22 11:03:15 -04:00
"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 ) {
2022-03-22 11:03:15 -04:00
switch {
case len ( stat . GCPCoordinators ) != 0 :
return getGCPInstances ( stat , config )
case len ( stat . AzureCoordinators ) != 0 :
return getAzureInstances ( stat , config )
2022-05-24 03:44:00 -04:00
case len ( stat . QEMUCoordinators ) != 0 :
return getQEMUInstances ( stat , config )
2022-03-22 11:03:15 -04:00
default :
2022-06-07 11:15:23 -04:00
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no instances to init" )
2022-03-22 11:03:15 -04:00
}
}
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-04-25 11:21:32 -04:00
}
2022-03-22 11:03:15 -04:00
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-03-22 11:03:15 -04:00
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" )
2022-03-22 11:03:15 -04:00
}
// 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 }
2022-03-22 11:03:15 -04:00
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-03-22 11:03:15 -04:00
}
2022-06-07 11:15:23 -04:00
2022-03-22 11:03:15 -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-03-22 11:03:15 -04:00
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" )
2022-03-22 11:03:15 -04:00
}
// 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 }
2022-03-22 11:03:15 -04:00
return
}
2022-05-24 03:44:00 -04:00
2022-06-07 11:15:23 -04:00
func getQEMUInstances ( stat state . ConstellationState , config * config . Config ) ( coordinators , nodes cloudtypes . ScalingGroup , err error ) {
2022-05-24 03:44:00 -04:00
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-05-24 03:44:00 -04:00
}
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-05-24 03:44:00 -04:00
}
2022-06-07 11:15:23 -04:00
// QEMU does not support autoscaling
nodes = cloudtypes . ScalingGroup { Instances : stat . QEMUNodes }
2022-05-24 03:44:00 -04:00
return
}