Skip to content

Commit

Permalink
chore: add new config to control the pull behaviour for verify (#9150)
Browse files Browse the repository at this point in the history
* chore: add new config to control the pull behaviour for verify to check or not the local daemon before pull

* test: unit test to check behaviour with new flag
  • Loading branch information
renzodavid9 authored Oct 27, 2023
1 parent 6f12afe commit d47c33d
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/skaffold/runner/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func GetVerifier(ctx context.Context, runCtx *runcontext.RunContext, labeller *l
kubernetesTestCases = append(kubernetesTestCases, tc)
continue
}

if tc.ExecutionMode.LocalExecutionMode == nil {
tc.ExecutionMode.LocalExecutionMode = &latest.LocalVerifier{}
}

localTestCases = append(localTestCases, tc)
}
}
Expand Down
20 changes: 18 additions & 2 deletions pkg/skaffold/verify/docker/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func (v *Verifier) Verify(ctx context.Context, out io.Writer, allbuilds []graph.
var na graph.Artifact
foundArtifact := false
testCase := tc
useLocalImages := testCase.ExecutionMode.LocalExecutionMode.UseLocalImages

for _, b := range allbuilds {
if tc.Container.Image == b.ImageName {
foundArtifact = true
Expand All @@ -158,10 +160,24 @@ func (v *Verifier) Verify(ctx context.Context, out io.Writer, allbuilds []graph.
break
}
}

if !foundArtifact {
if err := v.client.Pull(ctx, out, tc.Container.Image, v1.Platform{}); err != nil {
return err
pullArtifact := true

if useLocalImages {
imageID, err := v.client.ImageID(ctx, tc.Container.Image)
if err != nil {
return fmt.Errorf("getting imageID for %q: %w", tc.Container.Image, err)
}
pullArtifact = imageID == ""
}

if pullArtifact {
if err := v.client.Pull(ctx, out, tc.Container.Image, v1.Platform{}); err != nil {
return err
}
}

na = graph.Artifact{
ImageName: tc.Container.Image,
Tag: tc.Container.Image,
Expand Down
151 changes: 151 additions & 0 deletions pkg/skaffold/verify/docker/verify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright 2023 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package docker

import (
"context"
"io"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/deploy/label"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
testEvent "github.com/GoogleContainerTools/skaffold/v2/testutil/event"
)

type fakeDockerDaemon struct {
docker.LocalDaemon

PulledImages []string
ImgsInDaemon map[string]string
}

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

func (fd *fakeDockerDaemon) Pull(ctx context.Context, out io.Writer, ref string, platform v1.Platform) error {
fd.PulledImages = append(fd.PulledImages, ref)
return nil
}

func (fd *fakeDockerDaemon) ImageID(ctx context.Context, ref string) (string, error) {
img := fd.ImgsInDaemon[ref]
return img, nil
}

func (fd *fakeDockerDaemon) Run(ctx context.Context, out io.Writer, opts docker.ContainerCreateOpts) (<-chan container.WaitResponse, <-chan error, string, error) {
statusCh := make(chan container.WaitResponse)
go func() {
statusCh <- container.WaitResponse{Error: nil, StatusCode: 0}
}()
errCh := make(<-chan error)
return statusCh, errCh, "", nil
}

func (fd *fakeDockerDaemon) ImageInspectWithRaw(ctx context.Context, image string) (types.ImageInspect, []byte, error) {
return types.ImageInspect{
Config: &container.Config{},
}, []byte{}, nil
}

func (fd *fakeDockerDaemon) ContainerExists(ctx context.Context, name string) bool {
return false
}

func Test_UseLocalImages(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
testEvent.InitializeState([]latest.Pipeline{{}})
ctx := context.TODO()
runCtx := &runcontext.RunContext{}

fDockerDaemon := &fakeDockerDaemon{
LocalDaemon: docker.NewLocalDaemon(&testutil.FakeAPIClient{}, nil, false, nil),

ImgsInDaemon: map[string]string{
"gcr.io/img1:latest": "id111",
"gcr.io/img3:latest": "id111",
},
}

t.Override(&docker.NewAPIClient, func(context.Context, docker.Config) (docker.LocalDaemon, error) {
return fDockerDaemon, nil
})

testCases := []*latest.VerifyTestCase{
{
Name: "t1",
Config: latest.VerifyConfig{},
ExecutionMode: latest.VerifyExecutionModeConfig{
VerifyExecutionModeType: latest.VerifyExecutionModeType{
LocalExecutionMode: &latest.LocalVerifier{
UseLocalImages: true,
},
},
},
Container: latest.VerifyContainer{
Name: "container1",
Image: "gcr.io/img1:latest",
},
},
{
Name: "t2",
Config: latest.VerifyConfig{},
ExecutionMode: latest.VerifyExecutionModeConfig{
VerifyExecutionModeType: latest.VerifyExecutionModeType{
LocalExecutionMode: &latest.LocalVerifier{
UseLocalImages: true,
},
},
},
Container: latest.VerifyContainer{
Name: "container2",
Image: "gcr.io/img2:latest",
},
},
{
Name: "t3",
Config: latest.VerifyConfig{},
ExecutionMode: latest.VerifyExecutionModeConfig{
VerifyExecutionModeType: latest.VerifyExecutionModeType{
LocalExecutionMode: &latest.LocalVerifier{},
},
},
Container: latest.VerifyContainer{
Name: "container3",
Image: "gcr.io/img3:latest",
},
},
}

verifier, err := NewVerifier(ctx, runCtx, &label.DefaultLabeller{}, testCases, nil, "", nil)
t.CheckError(false, err)

err = verifier.Verify(ctx, nil, nil)
t.CheckError(false, err)

expectedPullImgs := []string{"gcr.io/img2:latest", "gcr.io/img3:latest"}

t.CheckDeepEqual(expectedPullImgs, fDockerDaemon.PulledImages)
})
}

0 comments on commit d47c33d

Please sign in to comment.