Skip to content

Commit 24ea069

Browse files
a-r-ncopybara-github
authored andcommitted
Add MachineMaintenancePolicy utility
This is a helper which recommends a maintenance policy as a function of the given machine type. For example, most accelerator and metal machines require the `TERMINATE` policy, while most other machines default to `MIGRATE`. PiperOrigin-RevId: 934421171
1 parent b955f9d commit 24ea069

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

fixtures.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"os/exec"
2222
"path/filepath"
23+
"regexp"
2324
"strings"
2425

2526
daisy "github.com/GoogleCloudPlatform/compute-daisy"
@@ -53,6 +54,24 @@ const (
5354
DefaultMachineType = "n1-standard-1"
5455
)
5556

57+
var (
58+
// knownTerminateOnlyMachineRegexes is a list of regexes for machine types that must use the
59+
// TERMINATE maintenance policy.
60+
knownTerminateOnlyMachineRegexes = []*regexp.Regexp{
61+
// Accelerator families.
62+
regexp.MustCompile(`^a\d+[x]?-.*`),
63+
regexp.MustCompile(`^g\d+-.*`),
64+
65+
// TPU families.
66+
regexp.MustCompile(`^ct\d.*`),
67+
regexp.MustCompile(`^tpu\d.*`),
68+
69+
// Special cases.
70+
regexp.MustCompile(`^h4d-.*`),
71+
regexp.MustCompile(`^z3-.*`),
72+
}
73+
)
74+
5675
// TestVM is a test VM.
5776
type TestVM struct {
5877
name string
@@ -147,6 +166,23 @@ func DiskTypeNeeded(machineType string) string {
147166
return HyperdiskBalanced
148167
}
149168

169+
// MachineMaintenancePolicy returns the recommended maintenance policy for the given machine type.
170+
// For instance, metal and accelerator machines must use TERMINATE, while most others can use
171+
// MIGRATE.
172+
func MachineMaintenancePolicy(machineType string) string {
173+
if IsMetal(machineType) {
174+
return "TERMINATE"
175+
}
176+
177+
for _, regex := range knownTerminateOnlyMachineRegexes {
178+
if regex.MatchString(machineType) {
179+
return "TERMINATE"
180+
}
181+
}
182+
183+
return "MIGRATE"
184+
}
185+
150186
// CreateTestVM adds the necessary steps to create a VM with the specified
151187
// name to the workflow.
152188
func (t *TestWorkflow) CreateTestVM(name string) (*TestVM, error) {

fixtures_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,3 +979,50 @@ func TestIsMetal(t *testing.T) {
979979
})
980980
}
981981
}
982+
983+
func TestMachineMaintenancePolicy(t *testing.T) {
984+
cases := []struct {
985+
name string
986+
machineType string
987+
want string
988+
}{
989+
{
990+
name: "regular machine",
991+
machineType: "c3-highcpu-176",
992+
want: "MIGRATE",
993+
},
994+
{
995+
name: "metal machine",
996+
machineType: "c3-highcpu-192-metal",
997+
want: "TERMINATE",
998+
},
999+
{
1000+
name: "accelerator machine",
1001+
machineType: "a2-highgpu-1g",
1002+
want: "TERMINATE",
1003+
},
1004+
{
1005+
name: "h4d special case",
1006+
machineType: "h4d-standard-4",
1007+
want: "TERMINATE",
1008+
},
1009+
{
1010+
name: "z3 special case",
1011+
machineType: "z3-highmem-176-standardlssd",
1012+
want: "TERMINATE",
1013+
},
1014+
{
1015+
name: "unknown machine",
1016+
machineType: "unknown-machine",
1017+
want: "MIGRATE",
1018+
},
1019+
}
1020+
for _, tc := range cases {
1021+
t.Run(tc.machineType, func(t *testing.T) {
1022+
got := MachineMaintenancePolicy(tc.machineType)
1023+
if got != tc.want {
1024+
t.Errorf("MachineMaintenancePolicy(%q) = %v, want %v", tc.machineType, got, tc.want)
1025+
}
1026+
})
1027+
}
1028+
}

0 commit comments

Comments
 (0)