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"
)
2022-06-29 09:26:29 -04:00
// Code in this file is mostly copied from constellation-controlPlane
// TODO: import as package from controlPlane once it is properly refactored
2022-03-22 11:03:15 -04:00
2022-06-29 09:26:29 -04:00
func GetScalingGroupsFromConfig ( stat state . ConstellationState , config * config . Config ) ( controlPlanes , workers cloudtypes . ScalingGroup , err error ) {
2022-03-22 11:03:15 -04:00
switch {
2022-07-29 02:10:51 -04:00
case len ( stat . GCPControlPlaneInstances ) != 0 :
2022-03-22 11:03:15 -04:00
return getGCPInstances ( stat , config )
2022-07-29 02:10:51 -04:00
case len ( stat . AzureControlPlaneInstances ) != 0 :
2022-03-22 11:03:15 -04:00
return getAzureInstances ( stat , config )
2022-07-29 02:10:51 -04:00
case len ( stat . QEMUControlPlaneInstances ) != 0 :
2022-05-24 03:44:00 -04:00
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-07-20 10:44:41 -04:00
func getGCPInstances ( stat state . ConstellationState , _ * config . Config ) ( controlPlanes , workers cloudtypes . ScalingGroup , err error ) {
2022-07-29 02:10:51 -04:00
if len ( stat . GCPControlPlaneInstances ) == 0 {
2022-06-29 09:26:29 -04:00
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no control-plane workers 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-29 09:26:29 -04:00
// GroupID of controlPlanes is empty, since they currently do not scale.
2022-07-29 02:10:51 -04:00
controlPlanes = cloudtypes . ScalingGroup { Instances : stat . GCPControlPlaneInstances }
2022-03-22 11:03:15 -04:00
2022-07-29 02:10:51 -04:00
if len ( stat . GCPWorkerInstances ) == 0 {
2022-06-29 09:26:29 -04:00
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no worker workers 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-07-29 02:10:51 -04:00
workers = cloudtypes . ScalingGroup { Instances : stat . GCPWorkerInstances }
2022-03-22 11:03:15 -04:00
return
}
2022-06-29 09:26:29 -04:00
func getAzureInstances ( stat state . ConstellationState , _ * config . Config ) ( controlPlanes , workers cloudtypes . ScalingGroup , err error ) {
2022-07-29 02:10:51 -04:00
if len ( stat . AzureControlPlaneInstances ) == 0 {
2022-06-29 09:26:29 -04:00
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no control-plane workers 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-06-29 09:26:29 -04:00
// GroupID of controlPlanes is empty, since they currently do not scale.
2022-07-29 02:10:51 -04:00
controlPlanes = cloudtypes . ScalingGroup { Instances : stat . AzureControlPlaneInstances }
2022-03-22 11:03:15 -04:00
2022-07-29 02:10:51 -04:00
if len ( stat . AzureWorkerInstances ) == 0 {
2022-06-29 09:26:29 -04:00
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no worker workers 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-07-29 02:10:51 -04:00
workers = cloudtypes . ScalingGroup { Instances : stat . AzureWorkerInstances }
2022-03-22 11:03:15 -04:00
return
}
2022-05-24 03:44:00 -04:00
2022-07-20 10:44:41 -04:00
func getQEMUInstances ( stat state . ConstellationState , _ * config . Config ) ( controlPlanes , workers cloudtypes . ScalingGroup , err error ) {
2022-07-29 02:10:51 -04:00
controlPlaneMap := stat . QEMUControlPlaneInstances
2022-06-29 09:26:29 -04:00
if len ( controlPlaneMap ) == 0 {
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no controlPlanes 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
2022-07-29 02:10:51 -04:00
controlPlanes = cloudtypes . ScalingGroup { Instances : stat . QEMUControlPlaneInstances }
2022-06-07 11:15:23 -04:00
2022-07-29 02:10:51 -04:00
if len ( stat . QEMUWorkerInstances ) == 0 {
2022-06-29 09:26:29 -04:00
return cloudtypes . ScalingGroup { } , cloudtypes . ScalingGroup { } , errors . New ( "no workers 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
2022-07-29 02:10:51 -04:00
workers = cloudtypes . ScalingGroup { Instances : stat . QEMUWorkerInstances }
2022-05-24 03:44:00 -04:00
return
}