@@ -84,15 +84,15 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
8484 delay int , workspace string ,
8585) error {
8686 // read target file
87- targetContent , err := file .GetContentFromFiles (filenames )
87+ inputFileContent , err := file .GetContentFromFiles (filenames )
8888 if err != nil {
8989 return err
9090 }
9191 if dumpConfig .SkipConsumers {
92- targetContent .Consumers = []file.FConsumer {}
92+ inputFileContent .Consumers = []file.FConsumer {}
9393 }
9494 if dumpConfig .SkipCACerts {
95- targetContent .CACertificates = []file.FCACertificate {}
95+ inputFileContent .CACertificates = []file.FCACertificate {}
9696 }
9797
9898 cmd := "sync"
@@ -101,26 +101,26 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
101101 }
102102
103103 var kongClient * kong.Client
104- mode := getMode (targetContent )
104+ mode := getMode (inputFileContent )
105105 if mode == modeKonnect {
106- if targetContent .Workspace != "" {
106+ if inputFileContent .Workspace != "" {
107107 return fmt .Errorf ("_workspace set in config file.\n " +
108108 "Workspaces are not supported in Konnect. " +
109109 "Please remove '_workspace: %s' from your " +
110- "configuration and try again" , targetContent .Workspace )
110+ "configuration and try again" , inputFileContent .Workspace )
111111 }
112112 if workspace != "" {
113113 return fmt .Errorf ("--workspace flag is not supported when running against Konnect" )
114114 }
115- if targetContent .Konnect != nil {
115+ if inputFileContent .Konnect != nil {
116116 if konnectRuntimeGroup != "" &&
117- targetContent .Konnect .RuntimeGroupName != konnectRuntimeGroup {
117+ inputFileContent .Konnect .RuntimeGroupName != konnectRuntimeGroup {
118118 return fmt .Errorf ("warning: runtime group '%v' specified via " +
119119 "--konnect-runtime-group flag is " +
120120 "different from '%v' found in state file(s)" ,
121- konnectRuntimeGroup , targetContent .Konnect .RuntimeGroupName )
121+ konnectRuntimeGroup , inputFileContent .Konnect .RuntimeGroupName )
122122 }
123- konnectRuntimeGroup = targetContent .Konnect .RuntimeGroupName
123+ konnectRuntimeGroup = inputFileContent .Konnect .RuntimeGroupName
124124 }
125125 kongClient , err = GetKongClientForKonnectMode (ctx , & konnectConfig )
126126 if err != nil {
@@ -136,7 +136,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
136136
137137 // prepare to read the current state from Kong
138138 var wsConfig utils.KongClientConfig
139- workspaceName := getWorkspaceName (workspace , targetContent )
139+ workspaceName := getWorkspaceName (workspace , inputFileContent )
140140 wsConfig = rootConfig .ForWorkspace (workspaceName )
141141
142142 // load Kong version after workspace
@@ -156,8 +156,8 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
156156 }
157157
158158 if parsedKongVersion .GTE (utils .Kong300Version ) &&
159- targetContent .FormatVersion != formatVersion30 {
160- formatVersion := targetContent .FormatVersion
159+ inputFileContent .FormatVersion != formatVersion30 {
160+ formatVersion := inputFileContent .FormatVersion
161161 if formatVersion == "" {
162162 formatVersion = defaultFormatVersion
163163 }
@@ -183,21 +183,21 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
183183 }
184184 }
185185
186- dumpConfig .SelectorTags , err = determineSelectorTag (* targetContent , dumpConfig )
186+ dumpConfig .SelectorTags , err = determineSelectorTag (* inputFileContent , dumpConfig )
187187 if err != nil {
188188 return err
189189 }
190190
191191 // read the current state
192- var currentState * state.KongState
192+ var currentKongState * state.KongState
193193 if workspaceExists {
194- currentState , err = fetchCurrentState (ctx , kongClient , dumpConfig )
194+ currentKongState , err = fetchCurrentState (ctx , kongClient , dumpConfig )
195195 if err != nil {
196196 return err
197197 }
198198 } else {
199199 // inject empty state
200- currentState , err = state .NewKongState ()
200+ currentKongState , err = state .NewKongState ()
201201 if err != nil {
202202 return err
203203 }
@@ -211,9 +211,33 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
211211 }
212212 }
213213
214+ // check entities with possible references.
215+ for _ , route := range inputFileContent .Routes {
216+ if route .Service != nil {
217+ if route .Service .Name != nil {
218+ var found bool
219+ for _ , service := range inputFileContent .Services {
220+ if * service .Name == * route .Service .Name {
221+ found = true
222+ break
223+ }
224+ }
225+ if ! found {
226+ var foundInCurrentState bool
227+ for _ , service := range currentKongState .Services {
228+ if * service .Name == * route .Service .Name {
229+ foundInCurrentState = true
230+ break
231+ }
232+ }
233+ }
234+ }
235+ }
236+ }
237+
214238 // read the target state
215- rawState , err := file .Get (ctx , targetContent , file.RenderConfig {
216- CurrentState : currentState ,
239+ rawState , err := file .Get (ctx , inputFileContent , file.RenderConfig {
240+ CurrentState : currentKongState ,
217241 KongVersion : parsedKongVersion ,
218242 }, dumpConfig , kongClient )
219243 if err != nil {
@@ -228,7 +252,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
228252 }
229253
230254 totalOps , err := performDiff (
231- ctx , currentState , targetState , dry , parallelism , delay , kongClient , mode == modeKonnect )
255+ ctx , currentKongState , targetState , dry , parallelism , delay , kongClient , mode == modeKonnect )
232256 if err != nil {
233257 return err
234258 }
0 commit comments