@@ -20,14 +20,30 @@ import (
20
20
)
21
21
22
22
const (
23
- exitCodeDiffDetection = 2
23
+ exitCodeDiffDetection = 2
24
+ defaultFetchedKongVersion = "2.8.0"
24
25
)
25
26
26
27
var (
27
28
dumpConfig dump.Config
28
29
assumeYes bool
29
30
)
30
31
32
+ type mode int
33
+
34
+ const (
35
+ modeKonnect = iota
36
+ modeKong
37
+ modeKongEnterprise
38
+ )
39
+
40
+ func getMode (targetContent * file.Content ) mode {
41
+ if inKonnectMode (targetContent ) {
42
+ return modeKonnect
43
+ }
44
+ return modeKong
45
+ }
46
+
31
47
// workspaceExists checks if workspace exists in Kong.
32
48
func workspaceExists (ctx context.Context , config utils.KongClientConfig , workspaceName string ) (bool , error ) {
33
49
rootConfig := config .ForWorkspace ("" )
@@ -73,6 +89,34 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
73
89
if dumpConfig .SkipConsumers {
74
90
targetContent .Consumers = []file.FConsumer {}
75
91
}
92
+ if dumpConfig .SkipCACerts {
93
+ targetContent .CACertificates = []file.FCACertificate {}
94
+ }
95
+
96
+ cmd := "sync"
97
+ if dry {
98
+ cmd = "diff"
99
+ }
100
+
101
+ var kongClient * kong.Client
102
+ mode := getMode (targetContent )
103
+ if mode == modeKonnect {
104
+ if targetContent .Konnect != nil {
105
+ if konnectRuntimeGroup != "" &&
106
+ targetContent .Konnect .RuntimeGroupName != konnectRuntimeGroup {
107
+ return fmt .Errorf ("warning: runtime group '%v' specified via " +
108
+ "--konnect-runtime-group flag is " +
109
+ "different from '%v' found in state file(s)" ,
110
+ konnectRuntimeGroup , targetContent .Konnect .RuntimeGroupName )
111
+ }
112
+ konnectRuntimeGroup = targetContent .Konnect .RuntimeGroupName
113
+ }
114
+ kongClient , err = getKonnectClient (ctx )
115
+ if err != nil {
116
+ return err
117
+ }
118
+ dumpConfig .KonnectRuntimeGroup = konnectRuntimeGroup
119
+ }
76
120
77
121
rootClient , err := utils .GetKongClient (rootConfig )
78
122
if err != nil {
@@ -85,32 +129,34 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
85
129
wsConfig = rootConfig .ForWorkspace (workspaceName )
86
130
87
131
// load Kong version after workspace
88
- kongVersion , err := fetchKongVersion (ctx , wsConfig )
89
- if err != nil {
90
- return fmt .Errorf ("reading Kong version: %w" , err )
132
+ kongVersion := defaultFetchedKongVersion
133
+ var parsedKongVersion semver.Version
134
+ if mode == modeKong {
135
+ kongVersion , err = fetchKongVersion (ctx , wsConfig )
136
+ if err != nil {
137
+ return fmt .Errorf ("reading Kong version: %w" , err )
138
+ }
91
139
}
92
- parsedKongVersion , err : = parseKongVersion (kongVersion )
140
+ parsedKongVersion , err = parseKongVersion (kongVersion )
93
141
if err != nil {
94
142
return fmt .Errorf ("parsing Kong version: %w" , err )
95
143
}
96
144
97
145
// TODO: instead of guessing the cobra command here, move the sendAnalytics
98
146
// call to the RunE function. That is not trivial because it requires the
99
147
// workspace name and kong client to be present on that level.
100
- cmd := "sync"
101
- if dry {
102
- cmd = "diff"
103
- }
104
- _ = sendAnalytics (cmd , kongVersion )
148
+ _ = sendAnalytics (cmd , kongVersion , mode )
105
149
106
150
workspaceExists , err := workspaceExists (ctx , rootConfig , workspaceName )
107
151
if err != nil {
108
152
return err
109
153
}
110
154
111
- wsClient , err := utils .GetKongClient (wsConfig )
112
- if err != nil {
113
- return err
155
+ if kongClient == nil {
156
+ kongClient , err = utils .GetKongClient (wsConfig )
157
+ if err != nil {
158
+ return err
159
+ }
114
160
}
115
161
116
162
dumpConfig .SelectorTags , err = determineSelectorTag (* targetContent , dumpConfig )
@@ -121,7 +167,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
121
167
// read the current state
122
168
var currentState * state.KongState
123
169
if workspaceExists {
124
- currentState , err = fetchCurrentState (ctx , wsClient , dumpConfig )
170
+ currentState , err = fetchCurrentState (ctx , kongClient , dumpConfig )
125
171
if err != nil {
126
172
return err
127
173
}
@@ -145,7 +191,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
145
191
rawState , err := file .Get (ctx , targetContent , file.RenderConfig {
146
192
CurrentState : currentState ,
147
193
KongVersion : parsedKongVersion ,
148
- }, dumpConfig , wsClient )
194
+ }, dumpConfig , kongClient )
149
195
if err != nil {
150
196
return err
151
197
}
@@ -157,7 +203,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
157
203
return err
158
204
}
159
205
160
- totalOps , err := performDiff (ctx , currentState , targetState , dry , parallelism , delay , wsClient )
206
+ totalOps , err := performDiff (ctx , currentState , targetState , dry , parallelism , delay , kongClient )
161
207
if err != nil {
162
208
return err
163
209
}
@@ -310,9 +356,27 @@ func containsRBACConfiguration(content utils.KongRawState) bool {
310
356
return len (content .RBACRoles ) != 0
311
357
}
312
358
313
- func sendAnalytics (cmd , kongVersion string ) error {
359
+ func sendAnalytics (cmd , kongVersion string , mode mode ) error {
314
360
if disableAnalytics {
315
361
return nil
316
362
}
317
- return utils .SendAnalytics (cmd , VERSION , kongVersion )
363
+ var modeStr string
364
+ switch mode {
365
+ case modeKong :
366
+ modeStr = "kong"
367
+ case modeKonnect :
368
+ modeStr = "konnect"
369
+ case modeKongEnterprise :
370
+ modeStr = "enterprise"
371
+ }
372
+ return utils .SendAnalytics (cmd , VERSION , kongVersion , modeStr )
373
+ }
374
+
375
+ func inKonnectMode (targetContent * file.Content ) bool {
376
+ if (targetContent != nil && targetContent .Konnect != nil ) ||
377
+ konnectConfig .Email != "" ||
378
+ konnectConfig .Password != "" {
379
+ return true
380
+ }
381
+ return false
318
382
}
0 commit comments