Skip to content

Commit

Permalink
feat: implement skaffold delete for docker deployer using docker labe…
Browse files Browse the repository at this point in the history
…ls (#8885)

* feat: add labeller labels to resources deployed with Docker deployer: containers and network

* feat: add labeller labels to volume created on `skaffold debug` with the Docker deployer

* feat: implement `skaffold delete` for docker deployer using docker labels

* test: integration test for delete command with docker deployer
  • Loading branch information
renzodavid9 authored Jun 26, 2023
1 parent ce682a5 commit 29ae963
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 17 deletions.
57 changes: 57 additions & 0 deletions integration/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ limitations under the License.
package integration

import (
"context"
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"

"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)

func TestDelete(t *testing.T) {
Expand Down Expand Up @@ -75,6 +81,57 @@ func TestDelete(t *testing.T) {
}
}

func TestDeleteDockerDeployer(t *testing.T) {
tests := []struct {
description string
dir string
args []string
deployedContainers []string
}{
{
description: "run with one container",
dir: "testdata/docker-deploy",
args: []string{"-p", "one-container"},
deployedContainers: []string{"docker-bert-img-1"},
},
{
description: "run with more than one container",
dir: "testdata/docker-deploy",
args: []string{"-p", "more-than-one-container"},
deployedContainers: []string{"docker-bert-img-2", "docker-ernie-img-2"},
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, CanRunWithoutGcp)
ctx := context.Background()
skaffold.Run(test.args...).InDir(test.dir).RunOrFail(t.T)
skaffold.Delete(test.args...).InDir(test.dir).RunOrFail(t.T)

client := SetupDockerClient(t.T)
cs := getContainers(ctx, t, test.deployedContainers, client)
t.CheckDeepEqual(0, len(cs))
})
}
}

func getContainers(ctx context.Context, t *testutil.T, deployedContainers []string, client docker.LocalDaemon) []types.Container {
t.Helper()

containersFilters := []filters.KeyValuePair{}
for _, c := range deployedContainers {
containersFilters = append(containersFilters, filters.Arg("name", c))
}

cl, err := client.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(containersFilters...),
})
t.CheckNoError(err)

return cl
}

func TestDeleteNonExistedHelmResource(t *testing.T) {
var tests = []struct {
description string
Expand Down
25 changes: 25 additions & 0 deletions integration/testdata/docker-deploy/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@ build:
deploy:
docker:
images: [bert, ernie]

profiles:
- name: one-container
build:
local:
push: false
artifacts:
- image: docker-bert-img-1
context: bert
deploy:
docker:
images: [docker-bert-img-1]

- name: more-than-one-container
build:
local:
push: false
artifacts:
- image: docker-bert-img-2
context: bert
- image: docker-ernie-img-2
context: ernie
deploy:
docker:
images: [docker-bert-img-2, docker-ernie-img-2]
2 changes: 1 addition & 1 deletion pkg/skaffold/actions/docker/exec_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func newExecEnv(ctx context.Context, cfg dockerutil.Config, labeller *label.Defa

func (e ExecEnv) PrepareActions(ctx context.Context, out io.Writer, allbuilds, _ []graph.Artifact, acsNames []string) ([]actions.Action, error) {
if e.shouldCreateNetwork {
if err := e.client.NetworkCreate(ctx, e.network); err != nil {
if err := e.client.NetworkCreate(ctx, e.network, nil); err != nil {
return nil, fmt.Errorf("creating skaffold network %s: %w", e.network, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/actions/docker/exec_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type fakeDockerDaemon struct {
ImgsInDaemon map[string]string
}

func (fd *fakeDockerDaemon) NetworkCreate(ctx context.Context, name string) error {
func (fd *fakeDockerDaemon) NetworkCreate(ctx context.Context, name string, labels map[string]string) error {
return nil
}

Expand Down
Loading

0 comments on commit 29ae963

Please sign in to comment.