@@ -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
@@ -151,7 +152,7 @@ func ApplyInclude(ctx context.Context, workingDir string, environment types.Mapp
151152 if err != nil {
152153 return err
153154 }
154- err = importResources(imported, model)
155+ err = importResources(imported, model, processor )
155156 if err != nil {
156157 return err
157158 }
@@ -161,29 +162,29 @@ func ApplyInclude(ctx context.Context, workingDir string, environment types.Mapp
161162}
162163
163164// importResources import into model all resources defined by imported, and report error on conflict
164- func importResources(source map[string]any, target map[string]any) error {
165- if err := importResource(source, target, "services"); err != nil {
165+ func importResources(source map[string]any, target map[string]any, processor PostProcessor ) error {
166+ if err := importResource(source, target, "services", processor ); err != nil {
166167 return err
167168 }
168- if err := importResource(source, target, "volumes"); err != nil {
169+ if err := importResource(source, target, "volumes", processor ); err != nil {
169170 return err
170171 }
171- if err := importResource(source, target, "networks"); err != nil {
172+ if err := importResource(source, target, "networks", processor ); err != nil {
172173 return err
173174 }
174- if err := importResource(source, target, "secrets"); err != nil {
175+ if err := importResource(source, target, "secrets", processor ); err != nil {
175176 return err
176177 }
177- if err := importResource(source, target, "configs"); err != nil {
178+ if err := importResource(source, target, "configs", processor ); err != nil {
178179 return err
179180 }
180- if err := importResource(source, target, "models"); err != nil {
181+ if err := importResource(source, target, "models", processor ); err != nil {
181182 return err
182183 }
183184 return nil
184185}
185186
186- func importResource(source map[string]any, target map[string]any, key string) error {
187+ func importResource(source map[string]any, target map[string]any, key string, processor PostProcessor ) error {
187188 from := source[key]
188189 if from != nil {
189190 var to map[string]any
@@ -193,13 +194,25 @@ func importResource(source map[string]any, target map[string]any, key string) er
193194 to = map[string]any{}
194195 }
195196 for name, a := range from.(map[string]any) {
196- if conflict, ok := to[name]; ok {
197- if reflect.DeepEqual(a, conflict) {
198- continue
199- }
200- return fmt.Errorf("%s.%s conflicts with imported resource", key, name)
197+ conflict, ok := to[name]
198+ if !ok {
199+ to[name] = a
200+ continue
201+ }
202+ err := processor.Apply(map[string]any{
203+ key: map[string]any{
204+ name: a,
205+ },
206+ })
207+ if err != nil {
208+ return err
209+ }
210+
211+ merged, err := override.MergeYaml(a, conflict, tree.NewPath(key, name))
212+ if err != nil {
213+ return err
201214 }
202- to[name] = a
215+ to[name] = merged
203216 }
204217 target[key] = to
205218 }
0 commit comments