diff --git a/controllers/machineset_controller.go b/controllers/machineset_controller.go index dbeba3f..befc96f 100644 --- a/controllers/machineset_controller.go +++ b/controllers/machineset_controller.go @@ -31,6 +31,7 @@ const ( memoryKey = "machine.openshift.io/memoryMb" gpuKey = "machine.openshift.io/GPU" labelsKey = "capacity.cluster-autoscaler.kubernetes.io/labels" + diskKey = "capacity.cluster-autoscaler.kubernetes.io/ephemeral-disk" gpuKeyValue = "0" arch = "kubernetes.io/arch=amd64" @@ -67,9 +68,15 @@ func (r *MachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, fmt.Errorf("failed to parse flavor %q: %w", spec.Flavor, err) } + if spec.RootVolumeSizeGB == 0 { + return ctrl.Result{}, fmt.Errorf("root volume size is not set") + } + machineSet.Annotations[cpuKey] = strconv.Itoa(flavor.CPU) machineSet.Annotations[memoryKey] = strconv.Itoa(flavor.MemGB * 1024) machineSet.Annotations[gpuKey] = gpuKeyValue + // According to https://www.cloudscale.ch/en/api/v1#create-a-server GB here means GiB + machineSet.Annotations[diskKey] = fmt.Sprintf("%dGi", spec.RootVolumeSizeGB) // We guarantee that any existing labels provided via the capacity annotations are preserved. // See https://github.com/kubernetes/autoscaler/pull/5382 and https://github.com/kubernetes/autoscaler/pull/5697 diff --git a/controllers/machineset_controller_test.go b/controllers/machineset_controller_test.go index 3b3d42d..f6a6bd0 100644 --- a/controllers/machineset_controller_test.go +++ b/controllers/machineset_controller_test.go @@ -14,6 +14,8 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" + + csv1beta1 "github.com/appuio/machine-api-provider-cloudscale/api/cloudscale/provider/v1beta1" ) func Test_MachineSetReconciler_Reconcile(t *testing.T) { @@ -37,7 +39,12 @@ func Test_MachineSetReconciler_Reconcile(t *testing.T) { Spec: machinev1beta1.MachineSetSpec{}, } - setFlavorOnMachineSet(ms, "plus-4-2") + providerData := csv1beta1.CloudscaleMachineProviderSpec{ + Flavor: "plus-4-2", + RootVolumeSizeGB: 50, + } + + setMachineSetProviderData(ms, &providerData) c := fake.NewClientBuilder(). WithScheme(scheme). @@ -57,10 +64,11 @@ func Test_MachineSetReconciler_Reconcile(t *testing.T) { assert.Equal(t, "4096", updated.Annotations[memoryKey]) assert.Equal(t, "0", updated.Annotations[gpuKey]) assert.Equal(t, "a=a,b=b,kubernetes.io/arch=amd64", updated.Annotations[labelsKey]) + assert.Equal(t, "50Gi", updated.Annotations[diskKey]) } -func setFlavorOnMachineSet(machine *machinev1beta1.MachineSet, flavor string) { +func setMachineSetProviderData(machine *machinev1beta1.MachineSet, providerData *csv1beta1.CloudscaleMachineProviderSpec) { machine.Spec.Template.Spec.ProviderSpec.Value = &runtime.RawExtension{ - Raw: []byte(fmt.Sprintf(`{"flavor": "%s"}`, flavor)), + Raw: []byte(fmt.Sprintf(`{"flavor": "%s", "rootVolumeSizeGB": %d}`, providerData.Flavor, providerData.RootVolumeSizeGB)), } }