@@ -5,22 +5,20 @@ import (
55"encoding/base64"
66"encoding/json"
77"io"
8-
9- "github.com/docker/cli/cli/streams"
8+
109"github.com/distribution/reference"
10+ "github.com/docker/cli/cli/streams"
1111"github.com/docker/docker/api/types/image"
1212dockerregistry "github.com/docker/docker/api/types/registry"
13+ "github.com/docker/docker/pkg/jsonmessage"
1314"github.com/loft-sh/devspace/pkg/devspace/build/builder/helper"
1415"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1516devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
1617dockerclient "github.com/loft-sh/devspace/pkg/devspace/docker"
1718"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1819"github.com/loft-sh/devspace/pkg/devspace/pullsecrets"
1920command2 "github.com/loft-sh/utils/pkg/command"
20-
2121"github.com/pkg/errors"
22-
23- "github.com/docker/docker/pkg/jsonmessage"
2422)
2523
2624// EngineName is the name of the building engine
@@ -29,7 +27,7 @@ const EngineName = "docker"
2927// Builder holds the necessary information to build and push docker images
3028type Builder struct {
3129helper * helper.BuildHelper
32-
30+
3331authConfig * dockerregistry.AuthConfig
3432client dockerclient.Client
3533skipPush bool
@@ -57,7 +55,7 @@ func (b *Builder) ShouldRebuild(ctx devspacecontext.Context, forceRebuild bool)
5755imageCache , _ := ctx .Config ().LocalCache ().GetImageCache (b .helper .ImageConf .Name )
5856imageName := imageCache .ResolveImage () + ":" + imageCache .Tag
5957rebuild , err := b .helper .ShouldRebuild (ctx , forceRebuild )
60-
58+
6159// Check if image is present in local docker daemon
6260if ! rebuild && err == nil {
6361if b .skipPushOnLocalKubernetes && ctx .KubeClient () != nil && kubectl .IsLocalKubernetes (ctx .KubeClient ()) {
@@ -68,7 +66,7 @@ func (b *Builder) ShouldRebuild(ctx devspacecontext.Context, forceRebuild bool)
6866}
6967}
7068}
71-
69+
7270return rebuild , err
7371}
7472
@@ -79,7 +77,7 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
7977var (
8078displayRegistryURL = "hub.docker.com"
8179)
82-
80+
8381// Display nice registry name
8482registryURL , err := pullsecrets .GetRegistryFromImageName (b .helper .ImageName )
8583if err != nil {
@@ -88,12 +86,12 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
8886if registryURL != "" {
8987displayRegistryURL = registryURL
9088}
91-
89+
9290// We skip pushing when it is the minikube client
9391if b .skipPushOnLocalKubernetes && ctx .KubeClient () != nil && kubectl .IsLocalKubernetes (ctx .KubeClient ()) {
9492b .skipPush = true
9593}
96-
94+
9795// Authenticate
9896if ! b .skipPush && ! b .helper .ImageConf .SkipPush {
9997if pullsecrets .IsAzureContainerRegistry (registryURL ) {
@@ -108,18 +106,18 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
108106if err != nil {
109107return errors .Errorf ("Error during image registry authentication: %v" , err )
110108}
111-
109+
112110ctx .Log ().Done ("Authentication successful (" + displayRegistryURL + ")" )
113111}
114112}
115-
113+
116114// create context stream
117115body , writer , outStream , buildOptions , err := b .helper .CreateContextStream (contextPath , dockerfilePath , entrypoint , cmd , ctx .Log ())
118116defer writer .Close ()
119117if err != nil {
120118return err
121119}
122-
120+
123121// Should we build with cli?
124122useBuildKit := false
125123useDockerCli := b .helper .ImageConf .Docker != nil && b .helper .ImageConf .Docker .UseCLI
@@ -138,27 +136,27 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
138136} else {
139137// make sure to use the correct proxy configuration
140138buildOptions .BuildArgs = b .client .ParseProxyConfig (buildOptions .BuildArgs )
141-
139+
142140response , err := b .client .ImageBuild (ctx .Context (), body , * buildOptions )
143141if err != nil {
144142return err
145143}
146144defer response .Body .Close ()
147-
145+
148146err = jsonmessage .DisplayJSONMessagesStream (response .Body , outStream , outStream .FD (), outStream .IsTerminal (), nil )
149147if err != nil {
150148return err
151149}
152150}
153-
151+
154152// Check if we skip push
155153if ! b .skipPush && ! b .helper .ImageConf .SkipPush {
156154for _ , tag := range buildOptions .Tags {
157155err = b .pushImage (ctx .Context (), writer , tag )
158156if err != nil {
159157return errors .Errorf ("error during image push: %v" , err )
160158}
161-
159+
162160ctx .Log ().Info ("Image pushed to registry (" + displayRegistryURL + ")" )
163161}
164162} else if ctx .KubeClient () != nil && kubectl .GetKindContext (ctx .KubeClient ().CurrentContext ()) != "" {
@@ -169,14 +167,14 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
169167completeArgs = append (completeArgs , command [1 :]... )
170168err = command2 .Command (ctx .Context (), ctx .WorkingDir (), ctx .Environ (), writer , writer , nil , command [0 ], completeArgs ... )
171169if err != nil {
172- ctx . Log (). Info ( errors .Errorf ("error during image load to kind cluster: %v" , err ) )
170+ return errors .Errorf ("error during image load to kind cluster: %v" , err )
173171}
174172ctx .Log ().Info ("Image loaded to kind cluster" )
175173}
176174} else {
177175ctx .Log ().Infof ("Skip image push for %s" , b .helper .ImageName )
178176}
179-
177+
180178return nil
181179}
182180
@@ -186,12 +184,12 @@ func (b *Builder) Authenticate(ctx context.Context) (*dockerregistry.AuthConfig,
186184if err != nil {
187185return nil , err
188186}
189-
187+
190188b .authConfig , err = b .client .Login (ctx , registryURL , "" , "" , true , false , false )
191189if err != nil {
192190return nil , err
193191}
194-
192+
195193return b .authConfig , nil
196194}
197195
@@ -201,25 +199,25 @@ func (b *Builder) pushImage(ctx context.Context, writer io.Writer, imageName str
201199if err != nil {
202200return err
203201}
204-
202+
205203encodedAuth , err := encodeAuthToBase64 (* b .authConfig )
206204if err != nil {
207205return err
208206}
209-
207+
210208out , err := b .client .ImagePush (ctx , reference .FamiliarString (ref ), image.PushOptions {
211209RegistryAuth : encodedAuth ,
212210})
213211if err != nil {
214212return err
215213}
216-
214+
217215outStream := streams .NewOut (writer )
218216err = jsonmessage .DisplayJSONMessagesStream (out , outStream , outStream .FD (), outStream .IsTerminal (), nil )
219217if err != nil {
220218return err
221219}
222-
220+
223221return nil
224222}
225223
@@ -228,6 +226,6 @@ func encodeAuthToBase64(authConfig dockerregistry.AuthConfig) (string, error) {
228226if err != nil {
229227return "" , err
230228}
231-
229+
232230return base64 .URLEncoding .EncodeToString (buf ), nil
233231}
0 commit comments