Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cleanerupper/cleanerupper.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,31 @@ func WorkflowPolicy(id string) PolicyFunc {
}
}

// DeprecatePolicy returns a PolicyFunc which indicates to delete any image whose
// deprecation state is DEPRECATED. Also contains a safeguard against deleting
// resources with a "do-not-delete" label.
func DeprecatePolicy() PolicyFunc {
return func(resource any) bool {
var labels map[string]string
var desc, name string
switch r := resource.(type) {
case *compute.Image:
if r.Deprecated == nil || r.Deprecated.State != "DEPRECATED" {
return false
}
name = r.Name
desc = r.Description
labels = r.Labels
default:
return false
}
if _, keep := labels[keepLabel]; keep {
return false
}
return !strings.Contains(desc, keepLabel) && !strings.Contains(name, keepLabel)
}
}

// CleanInstances deletes all instances indicated, returning a slice of deleted
// instance partial URLs and a slice of errors encountered. On dry run, returns
// what would have been deleted.
Expand Down
93 changes: 93 additions & 0 deletions cleanerupper/cleanerupper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,99 @@ func TestWorkflowPolicy(t *testing.T) {
}
}

func TestDeprecatePolicy(t *testing.T) {
testcases := []struct {
name string

resource any
output bool
}{
{
name: "Unknown Resource",
resource: struct{}{},
output: false,
},
{
name: "Deprecated Image",
resource: &compute.Image{Deprecated: &compute.DeprecationStatus{State: "DEPRECATED"}},
output: true,
},
{
name: "Image with No Deprecation Status",
resource: &compute.Image{},
output: false,
},
{
name: "Deprecated Image with Malformed Timestamp",
resource: &compute.Image{Name: "image", Deprecated: &compute.DeprecationStatus{State: "DEPRECATED", Deprecated: "1970-01-01"}},
output: true,
},
{
name: "Active Image",
resource: &compute.Image{Deprecated: &compute.DeprecationStatus{State: "ACTIVE"}},
output: false,
},
{
name: "Obsolete Image",
resource: &compute.Image{Deprecated: &compute.DeprecationStatus{State: "OBSOLETE"}},
output: false,
},
{
name: "Deleted Image",
resource: &compute.Image{Deprecated: &compute.DeprecationStatus{State: "DELETED"}},
output: false,
},
{
name: "Keep Label in Labels",
resource: &compute.Image{Name: "image", Labels: map[string]string{keepLabel: ""}, Deprecated: &compute.DeprecationStatus{State: "DEPRECATED"}},
output: false,
},
{
name: "Keep Label in Name",
resource: &compute.Image{Name: "image-" + keepLabel, Deprecated: &compute.DeprecationStatus{State: "DEPRECATED"}},
output: false,
},
{
name: "Keep Label in Description",
resource: &compute.Image{Name: "image", Description: "image. do-not-delete", Deprecated: &compute.DeprecationStatus{State: "DEPRECATED"}},
output: false,
},
{
name: "Machine Image",
resource: &compute.MachineImage{Name: "machineimage"},
output: false,
},
{
name: "Disk",
resource: &compute.Disk{Name: "disk"},
output: false,
},
{
name: "Snapshot",
resource: &compute.Snapshot{Name: "snapshot"},
output: false,
},
{
name: "Instance",
resource: &compute.Instance{Name: "instance"},
output: false,
},
{
name: "Network",
resource: &compute.Network{Name: "network"},
output: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
o := DeprecatePolicy()(tc.resource)
if o != tc.output {
t.Errorf("Unexpected output from DeprecatePolicy(%v), got %v but want %v", tc.resource, o, tc.output)
}
})
}
}

func TestCleanInstances(t *testing.T) {
_, daisyFake, err := computeDaisy.NewTestClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.String() == fmt.Sprintf("/projects/%s/aggregated/instances?alt=json&pageToken=&prettyPrint=false", "test-project") {
Expand Down
Loading