Skip to content

Commit

Permalink
add node template
Browse files Browse the repository at this point in the history
  • Loading branch information
kon-angelo committed Apr 27, 2024
1 parent 09a5dc7 commit 9c45f4c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/gcp/types_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gcp

import (
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -31,6 +32,9 @@ type WorkerConfig struct {
// instance.
// This service account should be created in advance.
ServiceAccount *ServiceAccount

// NodeTemplate contains resource information of the machine which is used by Cluster Autoscaler to generate nodeTemplate during scaling a nodeGroup from zero.
NodeTemplate *extensionsv1alpha1.NodeTemplate
}

// Volume contains configuration for the additional disks attached to VMs.
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/gcp/v1alpha1/types_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package v1alpha1

import (
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -34,6 +35,10 @@ type WorkerConfig struct {
// This service account should be created in advance.
// +optional
ServiceAccount *ServiceAccount `json:"serviceAccount,omitempty"`

// NodeTemplate contains resource information of the machine which is used by Cluster Autoscaler to generate nodeTemplate during scaling a nodeGroup from zero.
// +optional
NodeTemplate *extensionsv1alpha1.NodeTemplate `json:"nodeTemplate,omitempty"`
}

// Volume contains configuration for the disks attached to VMs.
Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/gcp/validation/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"strings"

"github.com/gardener/gardener/pkg/apis/core"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"

Expand Down Expand Up @@ -38,6 +41,7 @@ func ValidateWorkerConfig(workerConfig *gcp.WorkerConfig, dataVolumes []core.Dat
if workerConfig.Volume != nil {
allErrs = append(allErrs, validateDiskEncryption(workerConfig.Volume.Encryption, volumeFldPath.Child("encryption"))...)
}
allErrs = append(allErrs, validateNodeTemplate(workerConfig.NodeTemplate, providerFldPath.Child("nodeTemplate"))...)
}

return allErrs
Expand Down Expand Up @@ -137,3 +141,32 @@ func validateDataVolume(workerConfig *gcp.WorkerConfig, volume core.DataVolume,

return allErrs
}

func validateNodeTemplate(nt *extensionsv1alpha1.NodeTemplate, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}

if nt == nil {
return allErrs
}

for _, capacityAttribute := range []corev1.ResourceName{"cpu", "gpu", "memory"} {
value, ok := nt.Capacity[capacityAttribute]
if !ok {
allErrs = append(allErrs, field.Required(fldPath.Child("nodeTemplate").Child("capacity"), fmt.Sprintf("%s is a mandatory field", capacityAttribute)))
continue
}
allErrs = append(allErrs, validateResourceQuantityValue(capacityAttribute, value, fldPath.Child("nodeTemplate").Child("capacity").Child(string(capacityAttribute)))...)
}

return allErrs
}

func validateResourceQuantityValue(key corev1.ResourceName, value resource.Quantity, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}

if value.Cmp(resource.Quantity{}) < 0 {
allErrs = append(allErrs, field.Invalid(fldPath, value.String(), fmt.Sprintf("%s value must not be negative", key)))
}

return allErrs
}
12 changes: 8 additions & 4 deletions pkg/controller/worker/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,19 @@ func (w *workerDelegate) generateMachineConfig(_ context.Context) error {
machineClassSpec["minCpuPlatform"] = *workerConfig.MinCpuPlatform
}

if pool.NodeTemplate != nil {
nodeTemplate := pool.NodeTemplate
if workerConfig.NodeTemplate != nil {
nodeTemplate = workerConfig.NodeTemplate
}
if nodeTemplate != nil {
machineClassSpec["nodeTemplate"] = machinev1alpha1.NodeTemplate{
Capacity: initializeCapacity(pool.NodeTemplate.Capacity, gpuCount),
// always overwrite the GPU count if it was provided in the WorkerConfig.
Capacity: initializeCapacity(nodeTemplate.Capacity, gpuCount),
InstanceType: pool.MachineType,
Region: w.worker.Spec.Region,
Zone: zone,
}

numGpus := pool.NodeTemplate.Capacity[ResourceGPU]
numGpus := workerConfig.NodeTemplate.Capacity[ResourceGPU]
if !numGpus.IsZero() {
isLiveMigrationAllowed = false
}
Expand Down

0 comments on commit 9c45f4c

Please sign in to comment.