From 57779a369ec678005ae418cbe489ab281172ebef Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Thu, 27 Jan 2022 07:33:08 +1000 Subject: [PATCH] fix: Add missing implementation for function excludes --- mocks/mock_containerengine/mock_containerengine.go | 8 ++++---- pkg/build/build.go | 6 +++--- pkg/build/build_test.go | 7 ++++--- pkg/containerengine/docker.go | 7 ++++--- pkg/containerengine/podman.go | 4 ++-- pkg/containerengine/types.go | 2 +- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/mocks/mock_containerengine/mock_containerengine.go b/mocks/mock_containerengine/mock_containerengine.go index d9395e829..468e04355 100644 --- a/mocks/mock_containerengine/mock_containerengine.go +++ b/mocks/mock_containerengine/mock_containerengine.go @@ -40,17 +40,17 @@ func (m *MockContainerEngine) EXPECT() *MockContainerEngineMockRecorder { } // Build mocks base method. -func (m *MockContainerEngine) Build(arg0, arg1, arg2 string, arg3 map[string]string) error { +func (m *MockContainerEngine) Build(arg0, arg1, arg2 string, arg3 map[string]string, arg4 []string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Build", arg0, arg1, arg2, arg3) + ret := m.ctrl.Call(m, "Build", arg0, arg1, arg2, arg3, arg4) ret0, _ := ret[0].(error) return ret0 } // Build indicates an expected call of Build. -func (mr *MockContainerEngineMockRecorder) Build(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { +func (mr *MockContainerEngineMockRecorder) Build(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Build", reflect.TypeOf((*MockContainerEngine)(nil).Build), arg0, arg1, arg2, arg3) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Build", reflect.TypeOf((*MockContainerEngine)(nil).Build), arg0, arg1, arg2, arg3, arg4) } // ContainerCreate mocks base method. diff --git a/pkg/build/build.go b/pkg/build/build.go index a0f42dc55..e541223a4 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -57,7 +57,7 @@ func Create(s *stack.Stack, t *target.Target) error { fh.Close() buildArgs := map[string]string{"PROVIDER": t.Provider} - err = cr.Build(path.Base(fh.Name()), f.ContextDirectory, f.ImageTagName(s, t.Provider), buildArgs) + err = cr.Build(path.Base(fh.Name()), f.ContextDirectory, f.ImageTagName(s, t.Provider), buildArgs, f.Excludes) if err != nil { return err } @@ -65,7 +65,7 @@ func Create(s *stack.Stack, t *target.Target) error { for _, c := range s.Containers { buildArgs := map[string]string{"PROVIDER": t.Provider} - err := cr.Build(path.Join(c.Context, c.Dockerfile), c.ContextDirectory, c.ImageTagName(s, t.Provider), buildArgs) + err := cr.Build(path.Join(c.Context, c.Dockerfile), c.ContextDirectory, c.ImageTagName(s, t.Provider), buildArgs, []string{}) if err != nil { return err } @@ -105,7 +105,7 @@ func CreateBaseDev(s *stack.Stack) error { return err } - if err := ce.Build(path.Base(f.Name()), s.Dir, rt.DevImageName(), map[string]string{}); err != nil { + if err := ce.Build(path.Base(f.Name()), s.Dir, rt.DevImageName(), map[string]string{}, []string{}); err != nil { return err } imagesToBuild[lang] = rt.DevImageName() diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index 6f800a17c..2ec262193 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -42,7 +42,7 @@ func TestCreateBaseDev(t *testing.T) { s := stack.New("", dir) s.Functions = map[string]stack.Function{"foo": {Handler: "functions/list.ts"}} - me.EXPECT().Build(gomock.Any(), dir, "nitric-ts-dev", map[string]string{}) + me.EXPECT().Build(gomock.Any(), dir, "nitric-ts-dev", map[string]string{}, []string{}) containerengine.DiscoveredEngine = me @@ -54,8 +54,8 @@ func TestCreateBaseDev(t *testing.T) { func TestCreate(t *testing.T) { ctrl := gomock.NewController(t) me := mock_containerengine.NewMockContainerEngine(ctrl) - me.EXPECT().Build(gomock.Any(), "", "corp-abc-dev:123456", map[string]string{"PROVIDER": "aws"}) - me.EXPECT().Build("Dockerfile.custom", "", "corp-xyz-dev:444444", map[string]string{"PROVIDER": "aws"}) + me.EXPECT().Build(gomock.Any(), "", "corp-abc-dev:123456", map[string]string{"PROVIDER": "aws"}, []string{"data/*"}) + me.EXPECT().Build("Dockerfile.custom", "", "corp-xyz-dev:444444", map[string]string{"PROVIDER": "aws"}, []string{}) containerengine.DiscoveredEngine = me @@ -69,6 +69,7 @@ func TestCreate(t *testing.T) { Tag: "corp-abc-dev:123456", }, BuildScripts: []string{"ls"}, + Excludes: []string{"data/*"}, }, }, Containers: map[string]stack.Container{ diff --git a/pkg/containerengine/docker.go b/pkg/containerengine/docker.go index ff42cb311..79d9ad579 100644 --- a/pkg/containerengine/docker.go +++ b/pkg/containerengine/docker.go @@ -74,13 +74,14 @@ func (d *docker) Type() string { return "docker" } -func tarContextDir(relDockerfile, contextDir string) (io.ReadCloser, error) { +func tarContextDir(relDockerfile, contextDir string, extraExcludes []string) (io.ReadCloser, error) { excludes, err := build.ReadDockerignore(contextDir) if err != nil { return nil, err } excludes = append(excludes, utils.NitricLogDir(contextDir)) + excludes = append(excludes, extraExcludes...) if err := build.ValidateContextDirectory(contextDir, excludes); err != nil { return nil, errors.Errorf("error checking context: '%s'.", err) @@ -93,11 +94,11 @@ func tarContextDir(relDockerfile, contextDir string) (io.ReadCloser, error) { }) } -func (d *docker) Build(dockerfile, srcPath, imageTag string, buildArgs map[string]string) error { +func (d *docker) Build(dockerfile, srcPath, imageTag string, buildArgs map[string]string, excludes []string) error { ctx, cancel := context.WithTimeout(context.Background(), buildTimeout()) defer cancel() - dockerBuildContext, err := tarContextDir(dockerfile, srcPath) + dockerBuildContext, err := tarContextDir(dockerfile, srcPath, excludes) if err != nil { return err } diff --git a/pkg/containerengine/podman.go b/pkg/containerengine/podman.go index 38a681da5..46d19a8d3 100644 --- a/pkg/containerengine/podman.go +++ b/pkg/containerengine/podman.go @@ -82,8 +82,8 @@ func (p *podman) Type() string { return "podman" } -func (p *podman) Build(dockerfile, path, imageTag string, buildArgs map[string]string) error { - return p.docker.Build(dockerfile, path, imageTag, buildArgs) +func (p *podman) Build(dockerfile, path, imageTag string, buildArgs map[string]string, excludes []string) error { + return p.docker.Build(dockerfile, path, imageTag, buildArgs, excludes) } func (p *podman) ListImages(stackName, containerName string) ([]Image, error) { diff --git a/pkg/containerengine/types.go b/pkg/containerengine/types.go index 4b69c18be..5db3b3550 100644 --- a/pkg/containerengine/types.go +++ b/pkg/containerengine/types.go @@ -44,7 +44,7 @@ type ContainerLogger interface { type ContainerEngine interface { Type() string - Build(dockerfile, path, imageTag string, buildArgs map[string]string) error + Build(dockerfile, path, imageTag string, buildArgs map[string]string, excludes []string) error ListImages(stackName, containerName string) ([]Image, error) ImagePull(rawImage string) error NetworkCreate(name string) error