@@ -63,9 +63,18 @@ func GetKongClientForKonnectMode(
6363 }
6464
6565 if konnectConfig .Token != "" {
66- konnectConfig .Headers = append (
67- konnectConfig .Headers , "Authorization:Bearer " + konnectConfig .Token ,
68- )
66+ found := false
67+ for _ , h := range konnectConfig .Headers {
68+ if strings .HasPrefix (h , "Authorization:" ) {
69+ found = true
70+ break
71+ }
72+ }
73+ if ! found {
74+ konnectConfig .Headers = append (
75+ konnectConfig .Headers , "Authorization:Bearer " + konnectConfig .Token ,
76+ )
77+ }
6978 }
7079
7180 if konnectConfig .Address == "" {
@@ -101,6 +110,7 @@ func GetKongClientForKonnectMode(
101110 Headers : konnectConfig .Headers ,
102111 Retryable : true ,
103112 TLSConfig : konnectConfig .TLSConfig ,
113+ Workspace : konnectConfig .WorkspaceName ,
104114 })
105115}
106116
@@ -113,22 +123,57 @@ func resetKonnectV2(ctx context.Context) error {
113123 }
114124 dumpConfig .KonnectControlPlane = konnectControlPlane
115125 konnectConfig .TLSConfig = rootConfig .TLSConfig
116- client , err := GetKongClientForKonnectMode (ctx , & konnectConfig )
117- if err != nil {
118- return err
119- }
120- currentState , err := fetchCurrentState (ctx , client , dumpConfig )
121- if err != nil {
122- return err
123- }
124- targetState , err := state .NewKongState ()
125- if err != nil {
126- return err
127- }
128- _ , err = performDiff (ctx , currentState , targetState , false , 10 , 0 , client , true , resetJSONOutput , ApplyTypeFull )
129- if err != nil {
130- return err
126+ baseKonnectClient , err := GetKongClientForKonnectMode (ctx , & konnectConfig )
127+
128+ var workspaces []string
129+ if resetAllWorkspaces {
130+ if err != nil {
131+ return fmt .Errorf ("getting initial konnect client: %w" , err )
132+ }
133+ workspaces , err = listWorkspaces (ctx , baseKonnectClient )
134+ if err != nil {
135+ return fmt .Errorf ("listing Konnect workspaces: %w" , err )
136+ }
137+ } else if resetWorkspace != "" {
138+ workspaces = append (workspaces , resetWorkspace )
139+ } else {
140+ // No workspace provided: reset global entities only
141+ workspaces = append (workspaces , "" )
142+ }
143+ for _ , ws := range workspaces {
144+ // Update the config for this specific workspace iteration
145+ exists , err := workspaceExists (ctx , rootConfig , ws , true )
146+ if err != nil {
147+ return err
148+ }
149+ if ! exists {
150+ return fmt .Errorf ("workspace '%v' does not exist in Konnect" , ws )
151+ }
152+
153+ konnectConfig .WorkspaceName = ws
154+
155+ client , err := GetKongClientForKonnectMode (ctx , & konnectConfig )
156+ if err != nil {
157+ return fmt .Errorf ("getting client for workspace '%s': %w" , ws , err )
158+ }
159+
160+ currentState , err := fetchCurrentState (ctx , client , dumpConfig )
161+ if err != nil {
162+ return fmt .Errorf ("fetching state for workspace '%s': %w" , ws , err )
163+ }
164+
165+ targetState , err := state .NewKongState ()
166+ if err != nil {
167+ return err
168+ }
169+
170+ // Perform the diff/reset
171+ _ , err = performDiff (ctx , currentState , targetState , false , 10 , 0 , client , true , resetJSONOutput , ApplyTypeFull )
172+ if err != nil {
173+ return fmt .Errorf ("resetting workspace '%s': %w" , ws , err )
174+ }
131175 }
176+
132177 return nil
133178}
134179
@@ -141,34 +186,86 @@ func dumpKonnectV2(ctx context.Context) error {
141186 }
142187 dumpConfig .KonnectControlPlane = konnectControlPlane
143188 konnectConfig .TLSConfig = rootConfig .TLSConfig
189+
190+ format := file .Format (strings .ToUpper (dumpCmdStateFormat ))
191+ writeConfig := file.WriteConfig {
192+ SelectTags : dumpConfig .SelectorTags ,
193+ FileFormat : format ,
194+ WithID : dumpWithID ,
195+ ControlPlaneName : konnectControlPlane ,
196+ KongVersion : fetchKonnectKongVersion (),
197+ SanitizeContent : dumpConfig .SanitizeContent ,
198+ }
199+
200+ var workspacesList []string
201+
202+ if dumpWorkspace != "" {
203+ workspacesList = append (workspacesList , dumpWorkspace )
204+ }
205+
206+ if dumpAllWorkspaces {
207+ baseClient , err := GetKongClientForKonnectMode (ctx , & konnectConfig )
208+ if err != nil {
209+ return err
210+ }
211+ workspaces , err := listWorkspaces (ctx , baseClient )
212+ if err != nil {
213+ return err
214+ }
215+ workspacesList = append (workspacesList , workspaces ... )
216+ }
217+
218+ for _ , workspace := range workspacesList {
219+ konnectConfig .WorkspaceName = workspace
220+ exists , err := workspaceExists (ctx , rootConfig , workspace , true )
221+ if err != nil {
222+ return err
223+ }
224+ if ! exists {
225+ return fmt .Errorf ("workspace '%v' does not exist in Konnect" , workspace )
226+ }
227+ wsKonnectClient , err := GetKongClientForKonnectMode (ctx , & konnectConfig )
228+ if err != nil {
229+ return fmt .Errorf ("getting client for workspace %s: %w" , workspace , err )
230+ }
231+ writeConfig .Workspace = workspace
232+ writeConfig .Filename = workspace
233+ if err := performKonnectDump (ctx , wsKonnectClient , workspace , writeConfig ); err != nil {
234+ return err
235+ }
236+ }
237+
238+ // No workspace (default)
239+ // If dumpWorkspace is empty, GetKongClientForKonnectMode builds the CP-level URL
144240 client , err := GetKongClientForKonnectMode (ctx , & konnectConfig )
145241 if err != nil {
146242 return err
147243 }
244+
245+ return performKonnectDump (ctx , client , dumpWorkspace , writeConfig )
246+ }
247+
248+ func performKonnectDump (ctx context.Context , client * kong.Client , wsName string , cfg file.WriteConfig ) error {
148249 rawState , err := dump .Get (ctx , client , dumpConfig )
149250 if err != nil {
150- return fmt .Errorf ("reading configuration from Kong : %w" , err )
251+ return fmt .Errorf ("reading configuration: %w" , err )
151252 }
152253 ks , err := state .Get (rawState )
153254 if err != nil {
154255 return fmt .Errorf ("building state: %w" , err )
155256 }
156257
157- writeConfig := file.WriteConfig {
158- SelectTags : dumpConfig .SelectorTags ,
159- Filename : dumpCmdKongStateFile ,
160- FileFormat : file .Format (strings .ToUpper (dumpCmdStateFormat )),
161- WithID : dumpWithID ,
162- ControlPlaneName : konnectControlPlane ,
163- KongVersion : fetchKonnectKongVersion (),
164- SanitizeContent : dumpConfig .SanitizeContent ,
258+ cfg .Workspace = wsName
259+ if dumpAllWorkspaces {
260+ cfg .Filename = wsName
261+ } else {
262+ cfg .Filename = dumpCmdKongStateFile
165263 }
166264
167- if dumpConfig .SanitizeContent {
168- return sanitizeContent (ctx , client , ks , writeConfig , true )
265+ if cfg .SanitizeContent {
266+ return sanitizeContent (ctx , client , ks , cfg , true )
169267 }
170-
171- return file .KongStateToFile (ks , writeConfig )
268+ return file .KongStateToFile (ks , cfg )
172269}
173270
174271func syncKonnect (ctx context.Context ,
0 commit comments