@@ -21,11 +21,12 @@ import (
2121 "fmt"
2222 "os"
2323 "path/filepath"
24- "reflect"
2524 "strings"
2625
2726 "github.com/compose-spec/compose-go/v2/dotenv"
2827 interp "github.com/compose-spec/compose-go/v2/interpolation"
28+ "github.com/compose-spec/compose-go/v2/override"
29+ "github.com/compose-spec/compose-go/v2/tree"
2930 "github.com/compose-spec/compose-go/v2/types"
3031)
3132
@@ -50,7 +51,7 @@ func loadIncludeConfig(source any) ([]types.IncludeConfig, error) {
5051 return requires , err
5152}
5253
53- func ApplyInclude (ctx context.Context , workingDir string , environment types.Mapping , model map [string ]any , options * Options , included []string ) error {
54+ func ApplyInclude (ctx context.Context , workingDir string , environment types.Mapping , model map [string ]any , options * Options , included []string , processor PostProcessor ) error {
5455 includeConfig , err := loadIncludeConfig (model ["include" ])
5556 if err != nil {
5657 return err
@@ -154,7 +155,7 @@ func ApplyInclude(ctx context.Context, workingDir string, environment types.Mapp
154155 if err != nil {
155156 return err
156157 }
157- err = importResources (imported , model )
158+ err = importResources (imported , model , processor )
158159 if err != nil {
159160 return err
160161 }
@@ -164,29 +165,29 @@ func ApplyInclude(ctx context.Context, workingDir string, environment types.Mapp
164165}
165166
166167// importResources import into model all resources defined by imported, and report error on conflict
167- func importResources (source map [string ]any , target map [string ]any ) error {
168- if err := importResource (source , target , "services" ); err != nil {
168+ func importResources (source map [string ]any , target map [string ]any , processor PostProcessor ) error {
169+ if err := importResource (source , target , "services" , processor ); err != nil {
169170 return err
170171 }
171- if err := importResource (source , target , "volumes" ); err != nil {
172+ if err := importResource (source , target , "volumes" , processor ); err != nil {
172173 return err
173174 }
174- if err := importResource (source , target , "networks" ); err != nil {
175+ if err := importResource (source , target , "networks" , processor ); err != nil {
175176 return err
176177 }
177- if err := importResource (source , target , "secrets" ); err != nil {
178+ if err := importResource (source , target , "secrets" , processor ); err != nil {
178179 return err
179180 }
180- if err := importResource (source , target , "configs" ); err != nil {
181+ if err := importResource (source , target , "configs" , processor ); err != nil {
181182 return err
182183 }
183- if err := importResource (source , target , "models" ); err != nil {
184+ if err := importResource (source , target , "models" , processor ); err != nil {
184185 return err
185186 }
186187 return nil
187188}
188189
189- func importResource (source map [string ]any , target map [string ]any , key string ) error {
190+ func importResource (source map [string ]any , target map [string ]any , key string , processor PostProcessor ) error {
190191 from := source [key ]
191192 if from != nil {
192193 var to map [string ]any
@@ -196,13 +197,25 @@ func importResource(source map[string]any, target map[string]any, key string) er
196197 to = map [string ]any {}
197198 }
198199 for name , a := range from .(map [string ]any ) {
199- if conflict , ok := to [name ]; ok {
200- if reflect .DeepEqual (a , conflict ) {
201- continue
202- }
203- return fmt .Errorf ("%s.%s conflicts with imported resource" , key , name )
200+ conflict , ok := to [name ]
201+ if ! ok {
202+ to [name ] = a
203+ continue
204+ }
205+ err := processor .Apply (map [string ]any {
206+ key : map [string ]any {
207+ name : a ,
208+ },
209+ })
210+ if err != nil {
211+ return err
212+ }
213+
214+ merged , err := override .MergeYaml (a , conflict , tree .NewPath (key , name ))
215+ if err != nil {
216+ return err
204217 }
205- to [name ] = a
218+ to [name ] = merged
206219 }
207220 target [key ] = to
208221 }
0 commit comments