Sitelet https://github.com/kubernetes/kops/pull/18004/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions upup/pkg/fi/cloudup/gcetasks/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,13 @@ func scopeToShortForm(s string) string {
func (e *Instance) mapToGCE(project string, ipAddressResolver func(*Address) (*string, error)) (*compute.Instance, error) {
zone := *e.Zone

var scheduling *compute.Scheduling
if fi.ValueOf(e.Preemptible) {
scheduling = &compute.Scheduling{
OnHostMaintenance: "TERMINATE",
Preemptible: true,
}
} else {
scheduling = &compute.Scheduling{
AutomaticRestart: fi.PtrTo(true),
// TODO: Migrate or terminate?
OnHostMaintenance: "MIGRATE",
Preemptible: false,
}
machineTypeInfo, err := guessMachineTypeInfo(fi.ValueOf(e.MachineType))
if err != nil {
return nil, fmt.Errorf("getting machine type info: %w", err)
}

scheduling := buildScheduling(machineTypeInfo, e.Preemptible, nil /* e.GCPProvisioningModel */, nil /* e.GuestAccelerators*/)

var disks []*compute.AttachedDisk
disks = append(disks, &compute.AttachedDisk{
InitializeParams: &compute.AttachedDiskInitializeParams{
Expand Down
57 changes: 50 additions & 7 deletions upup/pkg/fi/cloudup/gcetasks/instancetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,32 +257,75 @@ func (*InstanceTemplate) CheckChanges(a, e, changes *InstanceTemplate) error {
return nil
}

func (e *InstanceTemplate) mapToGCE(project string, region string) (*compute.InstanceTemplate, error) {
// TODO: This is similar to Instance...
type MachineTypeInfo struct {
SupportsMigration bool
}

func buildScheduling(machineTypeInfo *MachineTypeInfo, preemptible *bool, gcpProvisioningModel *string, guestAccelerators []AcceleratorConfig) *compute.Scheduling {
var scheduling *compute.Scheduling

if fi.ValueOf(e.Preemptible) {
if fi.ValueOf(preemptible) {
scheduling = &compute.Scheduling{
AutomaticRestart: fi.PtrTo(false),
OnHostMaintenance: "TERMINATE",
ProvisioningModel: fi.ValueOf(e.GCPProvisioningModel),
ProvisioningModel: fi.ValueOf(gcpProvisioningModel),
Preemptible: true,
}
} else {
// We default to allowing migration, as it gives higher uptime.
// However, if we figure out that the instance does not support migration, we will set this to TERMINATE (so we can create the instance at all).
scheduling = &compute.Scheduling{
AutomaticRestart: fi.PtrTo(true),
// TODO: Migrate or terminate?
AutomaticRestart: fi.PtrTo(true),
OnHostMaintenance: "MIGRATE",
ProvisioningModel: "STANDARD",
Preemptible: false,
}
}

if len(e.GuestAccelerators) > 0 {
if len(guestAccelerators) > 0 {
// Instances with accelerators cannot be migrated.
scheduling.OnHostMaintenance = "TERMINATE"
}

if machineTypeInfo != nil {
if !machineTypeInfo.SupportsMigration {
scheduling.OnHostMaintenance = "TERMINATE"
}
}
return scheduling
}

// guessMachineTypeInfo returns information about the machine type, such as whether it supports live migration.
// We use this to determine the correct scheduling options for non-preemptible VMs.
// If the machine type is not found, we return placeholder information, as we want to be tolerant of missing machine types, and just default to the safest scheduling options.
func guessMachineTypeInfo(machineType string) (*MachineTypeInfo, error) {
machineTypeInfo := &MachineTypeInfo{
SupportsMigration: true,
}
if machineType == "" {
return machineTypeInfo, nil
}

family := strings.Split(machineType, "-")[0]

switch family {
case "a4x", "a4", "a3", "a2", "g2", "g4":
// VMs with GPUs attached do not support live migration.
// https://docs.cloud.google.com/compute/docs/instances/live-migration-process#limitations
machineTypeInfo.SupportsMigration = false
}

return machineTypeInfo, nil
}

func (e *InstanceTemplate) mapToGCE(project string, region string) (*compute.InstanceTemplate, error) {
machineTypeInfo, err := guessMachineTypeInfo(fi.ValueOf(e.MachineType))
if err != nil {
return nil, fmt.Errorf("getting machine type info: %w", err)
}

scheduling := buildScheduling(machineTypeInfo, e.Preemptible, e.GCPProvisioningModel, e.GuestAccelerators)

var disks []*compute.AttachedDisk
disks = append(disks, &compute.AttachedDisk{
Kind: "compute#attachedDisk",
Expand Down
Loading