Skip to content

Commit

Permalink
fix: reverse order of deployers during cleanup (GoogleContainerTools#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chronicc committed Oct 14, 2022
1 parent b5ce320 commit 15f2ad1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/skaffold/deploy/deploy_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func (m DeployerMux) GetDeployers() []Deployer {
return m.deployers
}

func (m DeployerMux) GetDeployersInverse() []Deployer {
inverse := m.deployers
for i, j := 0, len(inverse)-1; i < j; i, j = i+1, j-1 {
inverse[i], inverse[j] = inverse[j], inverse[i]
}
return inverse
}

func (m DeployerMux) GetAccessor() access.Accessor {
var accessors access.AccessorMux
for _, deployer := range m.deployers {
Expand Down Expand Up @@ -161,7 +169,9 @@ func (m DeployerMux) Dependencies() ([]string, error) {
}

func (m DeployerMux) Cleanup(ctx context.Context, w io.Writer, dryRun bool, manifestsByConfig manifest.ManifestListByConfig) error {
for _, deployer := range m.deployers {
// Reverse order of deployers for cleanup to ensure resources
// are removed before their definitions are removed.
for _, deployer := range m.GetDeployersInverse() {
ctx, endTrace := instrumentation.StartTrace(ctx, "Cleanup")
if dryRun {
output.Yellow.Fprintln(w, "Following resources would be deleted:")
Expand Down
44 changes: 44 additions & 0 deletions pkg/skaffold/deploy/deploy_mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"io"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
Expand Down Expand Up @@ -218,3 +220,45 @@ func TestDeployerMux_Dependencies(t *testing.T) {
})
}
}

func TestDeployerMux_GetDeployersInverse(t *testing.T) {
d1 := NewMockDeployer()
d2 := NewMockDeployer()
d3 := NewMockDeployer()
d4 := NewMockDeployer()
d5 := NewMockDeployer()

tests := []struct {
name string
args []Deployer
expected []Deployer
}{
{
name: "uneven slice",
args: []Deployer{d1, d2, d3, d4, d5},
expected: []Deployer{d5, d4, d3, d2, d1},
},
{
name: "even slice",
args: []Deployer{d1, d2, d3, d4},
expected: []Deployer{d4, d3, d2, d1},
},
{
name: "slice of one",
args: []Deployer{d1},
expected: []Deployer{d1},
},
{
name: "slice of zero",
args: []Deployer{},
expected: []Deployer{},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
deployerMux := DeployerMux{deployers: test.args, iterativeStatusCheck: false}
testutil.CheckDeepEqual(t, test.expected, deployerMux.GetDeployersInverse(), cmp.AllowUnexported(MockDeployer{}))
})
}
}

0 comments on commit 15f2ad1

Please sign in to comment.