github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/buildpacks/fetcher.go (about) 1 /* 2 Copyright 2020 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package buildpacks 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 24 "github.com/buildpacks/imgutil" 25 "github.com/buildpacks/imgutil/local" 26 pack "github.com/buildpacks/pack/pkg/client" 27 packimg "github.com/buildpacks/pack/pkg/image" 28 29 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" 30 ) 31 32 var _ pack.ImageFetcher = (*fetcher)(nil) 33 34 type fetcher struct { 35 out io.Writer 36 docker docker.LocalDaemon 37 } 38 39 func newFetcher(out io.Writer, docker docker.LocalDaemon) *fetcher { 40 return &fetcher{ 41 out: out, 42 docker: docker, 43 } 44 } 45 46 func (f *fetcher) Fetch(ctx context.Context, name string, options packimg.FetchOptions) (imgutil.Image, error) { 47 if options.PullPolicy == packimg.PullAlways || (options.PullPolicy == packimg.PullIfNotPresent && !f.docker.ImageExists(ctx, name)) { 48 if err := f.docker.Pull(ctx, f.out, name); err != nil { 49 return nil, err 50 } 51 } 52 53 image, err := local.NewImage(name, f.docker.RawClient(), local.FromBaseImage(name)) 54 if err != nil { 55 return nil, err 56 } 57 58 if !image.Found() { 59 return nil, fmt.Errorf("image %s does not exist on the daemon", name) 60 } 61 return image, nil 62 }