Skip to content

Commit

Permalink
Expose google provisioning model to be able to acquire spot instances (
Browse files Browse the repository at this point in the history
…#1252)

* expose google provisioning model to be able to acquire spot instances

Signed-off-by: Tomas Lamr <[email protected]>

reformat sources

Signed-off-by: Tomas Lamr <[email protected]>

Make automaticRestart and provisioningModel Optional in pkg/cloudprovider/provider/gce/types/types.go

Co-authored-by: Waleed Malik <[email protected]>

lint

Signed-off-by: Tomas Lamr <[email protected]>

* incorporate PR changes

Signed-off-by: Tomas Lamr <[email protected]>

* thanks ahmedwaleedmalik for being patient with my commits :)

Signed-off-by: Tomas Lamr <[email protected]>

* refactor: improve handling for GCE config

Signed-off-by: Waleed Malik <[email protected]>

Co-authored-by: Tomas Lamr <[email protected]>
Co-authored-by: Waleed Malik <[email protected]>
  • Loading branch information
3 people authored Apr 21, 2022
1 parent a0be5f2 commit c4a4228
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
22 changes: 22 additions & 0 deletions pkg/cloudprovider/provider/gce/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type config struct {
network string
subnetwork string
preemptible bool
automaticRestart *bool
provisioningModel *string
labels map[string]string
tags []string
jwtConfig *jwt.Config
Expand Down Expand Up @@ -167,6 +169,26 @@ func newConfig(resolver *providerconfig.ConfigVarResolver, spec v1alpha1.Provide
return nil, fmt.Errorf("cannot retrieve preemptible: %v", err)
}

if cpSpec.AutomaticRestart != nil {
automaticRestart, _, err := resolver.GetConfigVarBoolValue(*cpSpec.AutomaticRestart)
if err != nil {
return nil, fmt.Errorf("cannot retrieve automaticRestart: %v", err)
}
cfg.automaticRestart = &automaticRestart

if *cfg.automaticRestart && cfg.preemptible {
return nil, fmt.Errorf("automatic restart option can only be enabled for standard instances. Preemptible instances cannot be automatically restarted")
}
}

if cpSpec.ProvisioningModel != nil {
provisioningModel, err := resolver.GetConfigVarStringValue(*cpSpec.ProvisioningModel)
if err != nil {
return nil, fmt.Errorf("cannot retrieve provisioningModel: %v", err)
}
cfg.provisioningModel = &provisioningModel
}

// make it true by default
cfg.assignPublicIPAddress = true

Expand Down
9 changes: 9 additions & 0 deletions pkg/cloudprovider/provider/gce/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ func (p *Provider) Create(machine *clusterv1alpha1.Machine, data *cloudprovidert
Items: cfg.tags,
},
}

if cfg.automaticRestart != nil {
inst.Scheduling.AutomaticRestart = cfg.automaticRestart
}

if cfg.provisioningModel != nil {
inst.Scheduling.ProvisioningModel = *cfg.provisioningModel
}

op, err := svc.Instances.Insert(cfg.projectID, cfg.zone, inst).Do()
if err != nil {
return nil, newError(common.InvalidConfigurationMachineError, errInsertInstance, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudprovider/provider/gce/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func testProviderSpec() map[string]interface{} {
"multizone": false,
"network": "global/networks/default",
"preemptible": false,
"provisioningModel": "STANDARD",
"regional": false,
"serviceAccount": "",
"subnetwork": "",
Expand Down
30 changes: 16 additions & 14 deletions pkg/cloudprovider/provider/gce/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ import (
// CloudProviderSpec contains the specification of the cloud provider taken
// from the provider configuration.
type CloudProviderSpec struct {
ServiceAccount providerconfigtypes.ConfigVarString `json:"serviceAccount,omitempty"`
Zone providerconfigtypes.ConfigVarString `json:"zone"`
MachineType providerconfigtypes.ConfigVarString `json:"machineType"`
DiskSize int64 `json:"diskSize"`
DiskType providerconfigtypes.ConfigVarString `json:"diskType"`
Network providerconfigtypes.ConfigVarString `json:"network"`
Subnetwork providerconfigtypes.ConfigVarString `json:"subnetwork"`
Preemptible providerconfigtypes.ConfigVarBool `json:"preemptible"`
Labels map[string]string `json:"labels,omitempty"`
Tags []string `json:"tags,omitempty"`
AssignPublicIPAddress *providerconfigtypes.ConfigVarBool `json:"assignPublicIPAddress,omitempty"`
MultiZone providerconfigtypes.ConfigVarBool `json:"multizone"`
Regional providerconfigtypes.ConfigVarBool `json:"regional"`
CustomImage providerconfigtypes.ConfigVarString `json:"customImage,omitempty"`
ServiceAccount providerconfigtypes.ConfigVarString `json:"serviceAccount,omitempty"`
Zone providerconfigtypes.ConfigVarString `json:"zone"`
MachineType providerconfigtypes.ConfigVarString `json:"machineType"`
DiskSize int64 `json:"diskSize"`
DiskType providerconfigtypes.ConfigVarString `json:"diskType"`
Network providerconfigtypes.ConfigVarString `json:"network"`
Subnetwork providerconfigtypes.ConfigVarString `json:"subnetwork"`
Preemptible providerconfigtypes.ConfigVarBool `json:"preemptible"`
AutomaticRestart *providerconfigtypes.ConfigVarBool `json:"automaticRestart,omitempty"`
ProvisioningModel *providerconfigtypes.ConfigVarString `json:"provisioningModel,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Tags []string `json:"tags,omitempty"`
AssignPublicIPAddress *providerconfigtypes.ConfigVarBool `json:"assignPublicIPAddress,omitempty"`
MultiZone providerconfigtypes.ConfigVarBool `json:"multizone"`
Regional providerconfigtypes.ConfigVarBool `json:"regional"`
CustomImage providerconfigtypes.ConfigVarString `json:"customImage,omitempty"`
}

// UpdateProviderSpec updates the given provider spec with changed
Expand Down

0 comments on commit c4a4228

Please sign in to comment.