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