@@ -29,6 +29,7 @@ import (
2929 "github.com/containerd/platforms"
3030 dref "github.com/distribution/reference"
3131
32+ "github.com/apple/container-builder-shim/pkg/build/utils"
3233 "github.com/moby/buildkit/client/llb"
3334 "github.com/moby/buildkit/client/llb/sourceresolver"
3435 "github.com/moby/buildkit/exporter/containerimage/exptypes"
@@ -42,8 +43,6 @@ import (
4243 "github.com/moby/buildkit/util/progress/progresswriter"
4344 ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
4445 "github.com/sirupsen/logrus"
45-
46- "github.com/apple/container-builder-shim/pkg/build/utils"
4746)
4847
4948func frontend (ctx context.Context , c gateway.Client ) (* gateway.Result , error ) {
@@ -131,6 +130,79 @@ func resolveStates(ctx context.Context, bopts *BOpts, platform ocispecs.Platform
131130 stateLock := sync.Mutex {}
132131
133132 resolveSource := func (resolvedBaseStageName string , sourcePlatform ocispecs.Platform ) error {
133+
134+ saveState := func (img []byte , fqdn string , ref string , storeName string , opts ... llb.OCILayoutOption ) error {
135+ opts = append (opts , llb .Platform (sourcePlatform ))
136+ opts = append (opts , llb .OCIStore ("" , storeName ))
137+
138+ st := llb .OCILayout (fqdn , opts ... )
139+
140+ st , err = st .WithImageConfig (img )
141+ if err != nil {
142+ return err
143+ }
144+
145+ named , err := dref .ParseNormalizedNamed (ref )
146+ if err != nil {
147+ return fmt .Errorf ("invalid context name %s %v" , ref , err )
148+ }
149+ // pname constructs a platform-qualified image reference in the format buildkit requires for digest resolution
150+ name := strings .TrimSuffix (dref .FamiliarString (named ), ":latest" )
151+ pname := name + "::" + platforms .FormatAll (platforms .Normalize (sourcePlatform ))
152+
153+ imgMetaMap := map [string ][]byte {
154+ exptypes .ExporterImageConfigKey : img ,
155+ }
156+ imgMeta , err := json .Marshal (imgMetaMap )
157+ if err != nil {
158+ return err
159+ }
160+
161+ stateLock .Lock ()
162+ states [pname ] = stateMeta {
163+ state : st .Platform (sourcePlatform ),
164+ imgMeta : imgMeta ,
165+ }
166+ stateLock .Unlock ()
167+ return nil
168+ }
169+
170+ // handle build context
171+ if val , ok := bopts .BuildContexts [resolvedBaseStageName ]; ok {
172+ // oci-layout requires custom handling as namedContext cannot load from client correctly
173+ if strings .SplitN (val , ":" , 2 )[0 ] != "oci-layout" {
174+ return nil
175+ }
176+ // passing in "oci-layout" as they are so client side knows that it is oci from build-context
177+ resolverOpts := sourceresolver.Opt {}
178+ resolverOpts .ImageOpt = & sourceresolver.ResolveImageOpt {
179+ Platform : & sourcePlatform ,
180+ ResolveMode : llb .ResolveModePreferLocal .String (),
181+ }
182+ resolverOpts .OCILayoutOpt = & sourceresolver.ResolveOCILayoutOpt {
183+ Store : sourceresolver.ResolveImageConfigOptStore {
184+ StoreID : resolvedBaseStageName ,
185+ SessionID : "" ,
186+ },
187+ }
188+ _ , digest , img , err := bopts .Resolver .ResolveImageConfig (ctx , val , resolverOpts )
189+ if err != nil {
190+ if err == reference .ErrObjectRequired {
191+ return nil
192+ }
193+ return err
194+ }
195+
196+ // not using the returning `ref` here as the `ref` will be the local path like /User/path/to/oci-layout
197+ // However, when using resolvedBaseStageName directly (ex: deps),
198+ // and if we don't add some dummy host like "docker.io/library/".
199+ // we will get an error like following
200+ // Error: unknown: "failed to solve: failed to load cache key: parse "dummy://deps@sha256:xxx": invalid port ":xxx" after host"
201+ ref := resolvedBaseStageName
202+ fqdn := "docker.io/library/" + ref + "@" + digest .String ()
203+ return saveState (img , fqdn , ref , resolvedBaseStageName , llb .WithCustomName ("[context " + resolvedBaseStageName + "] OCI load from client" ))
204+ }
205+
134206 if strings .EqualFold (resolvedBaseStageName , "scratch" ) || strings .EqualFold (resolvedBaseStageName , "context" ) {
135207 return nil
136208 }
@@ -152,7 +224,7 @@ func resolveStates(ctx context.Context, bopts *BOpts, platform ocispecs.Platform
152224 }
153225 resolverOpts .OCILayoutOpt = & sourceresolver.ResolveOCILayoutOpt {
154226 Store : sourceresolver.ResolveImageConfigOptStore {
155- StoreID : "container" ,
227+ StoreID : KeyContentStoreName ,
156228 SessionID : "" ,
157229 },
158230 }
@@ -175,31 +247,7 @@ func resolveStates(ctx context.Context, bopts *BOpts, platform ocispecs.Platform
175247 if _ , ok := ref .(dref.Digested ); ! ok {
176248 fqdn += "@" + digest .String ()
177249 }
178- st := llb .OCILayout (fqdn , llb .OCIStore ("" , "container" ), llb .Platform (sourcePlatform ))
179-
180- named , err := dref .ParseNormalizedNamed (ref .String ())
181- if err != nil {
182- return fmt .Errorf ("invalid context name %s %v" , ref .String (), err )
183- }
184- // pname constructs a platform-qualified image reference in the format buildkit requires for digest resolution
185- name := strings .TrimSuffix (dref .FamiliarString (named ), ":latest" )
186- pname := name + "::" + platforms .FormatAll (platforms .Normalize (sourcePlatform ))
187-
188- imgMetaMap := map [string ][]byte {
189- exptypes .ExporterImageConfigKey : img ,
190- }
191- imgMeta , err := json .Marshal (imgMetaMap )
192- if err != nil {
193- return err
194- }
195-
196- stateLock .Lock ()
197- states [pname ] = stateMeta {
198- state : st .Platform (sourcePlatform ),
199- imgMeta : imgMeta ,
200- }
201- stateLock .Unlock ()
202- return nil
250+ return saveState (img , fqdn , ref .String (), KeyContentStoreName )
203251 }
204252
205253 for i , stage := range stages {
0 commit comments