Skip to content

Commit a5057a6

Browse files
committed
progress
1 parent d886313 commit a5057a6

File tree

17 files changed

+8679
-114
lines changed

17 files changed

+8679
-114
lines changed

.DS_Store

8 KB
Binary file not shown.

cmd/common.go

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

convert/testdata/5/output.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
_format_version: "3.0"
2+
plugins:
3+
- enabled: true
4+
name: prometheus
5+
protocols:
6+
- http
7+
- https
8+
run_on: first
9+
services:
10+
- connect_timeout: 60000
11+
host: mockbin.org
12+
name: svc1
13+
protocol: http
14+
read_timeout: 60000
15+
routes:
16+
- https_redirect_status_code: 301
17+
name: r1
18+
paths:
19+
- /r1
20+
preserve_host: false
21+
protocols:
22+
- http
23+
- https
24+
regex_priority: 0
25+
strip_path: true
26+
tags:
27+
- team-svc1
28+
write_timeout: 60000
29+
- connect_timeout: 60000
30+
host: mockbin.org
31+
name: svc2
32+
protocol: http
33+
read_timeout: 60000
34+
routes:
35+
- https_redirect_status_code: 301
36+
name: r2
37+
paths:
38+
- /r2
39+
preserve_host: false
40+
protocols:
41+
- http
42+
- https
43+
regex_priority: 0
44+
strip_path: true
45+
write_timeout: 60000
46+
- connect_timeout: 60000
47+
host: mockbin.org
48+
name: svc3
49+
port: 80
50+
protocol: http
51+
read_timeout: 60000
52+
routes:
53+
- https_redirect_status_code: 301
54+
methods:
55+
- GET
56+
name: r3
57+
paths:
58+
- /r3
59+
preserve_host: false
60+
protocols:
61+
- http
62+
- https
63+
regex_priority: 0
64+
strip_path: true
65+
write_timeout: 60000

convert/testdata/6/output.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
_format_version: "3.0"
2+
plugins:
3+
- enabled: true
4+
name: prometheus
5+
protocols:
6+
- http
7+
- https
8+
run_on: first
9+
services:
10+
- connect_timeout: 30000
11+
host: mockbin.org
12+
name: svc1
13+
protocol: http
14+
read_timeout: 60000
15+
routes:
16+
- https_redirect_status_code: 301
17+
name: r1
18+
paths:
19+
- /r1
20+
preserve_host: false
21+
protocols:
22+
- https
23+
regex_priority: 0
24+
strip_path: true
25+
tags:
26+
- team-svc1
27+
write_timeout: 30000
28+
- connect_timeout: 30000
29+
host: mockbin.org
30+
name: svc2
31+
protocol: http
32+
read_timeout: 60000
33+
routes:
34+
- https_redirect_status_code: 301
35+
name: r2
36+
paths:
37+
- /r2
38+
preserve_host: false
39+
protocols:
40+
- https
41+
regex_priority: 0
42+
strip_path: true
43+
write_timeout: 30000
44+
- connect_timeout: 30000
45+
host: mockbin.org
46+
name: svc3
47+
port: 80
48+
protocol: http
49+
read_timeout: 60000
50+
routes:
51+
- https_redirect_status_code: 301
52+
methods:
53+
- GET
54+
name: r3
55+
paths:
56+
- /r3
57+
preserve_host: false
58+
protocols:
59+
- https
60+
regex_priority: 0
61+
strip_path: true
62+
write_timeout: 30000

convert/testdata/7/output.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
_format_version: "3.0"
2+
plugins:
3+
- enabled: true
4+
name: prometheus
5+
protocols:
6+
- http
7+
- https
8+
run_on: first
9+
services:
10+
- connect_timeout: 60000
11+
host: mockbin.org
12+
name: svc1
13+
protocol: http
14+
read_timeout: 60000
15+
routes:
16+
- https_redirect_status_code: 301
17+
name: r1
18+
paths:
19+
- /r1
20+
preserve_host: false
21+
protocols:
22+
- http
23+
- https
24+
regex_priority: 0
25+
strip_path: true
26+
tags:
27+
- team-svc1
28+
write_timeout: 60000
29+
- connect_timeout: 60000
30+
host: mockbin.org
31+
name: svc2
32+
protocol: http
33+
read_timeout: 60000
34+
routes:
35+
- https_redirect_status_code: 301
36+
name: r2
37+
paths:
38+
- /r2
39+
preserve_host: false
40+
protocols:
41+
- http
42+
- https
43+
regex_priority: 0
44+
strip_path: true
45+
write_timeout: 60000
46+
- connect_timeout: 60000
47+
host: mockbin.org
48+
name: svc3
49+
port: 80
50+
protocol: http
51+
read_timeout: 60000
52+
routes:
53+
- https_redirect_status_code: 301
54+
methods:
55+
- GET
56+
name: r3
57+
paths:
58+
- /r3
59+
preserve_host: false
60+
protocols:
61+
- http
62+
- https
63+
regex_priority: 0
64+
strip_path: true
65+
write_timeout: 60000

convert/testdata/8/output.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
_format_version: "3.0"
2+
plugins:
3+
- config:
4+
foo: 666
5+
name: foofloat
6+
services:
7+
- connect_timeout: 60000
8+
enabled: true
9+
host: mockbin.org
10+
name: svc1
11+
protocol: http
12+
read_timeout: 60000
13+
write_timeout: 777

convert/testdata/9/output.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
_format_version: "3.0"
2+
plugins:
3+
- config:
4+
foo: 42
5+
name: foofloat
6+
services:
7+
- connect_timeout: 60000
8+
enabled: false
9+
host: DECK_MOCKBIN_HOST
10+
name: svc1
11+
protocol: http
12+
read_timeout: 60000
13+
write_timeout: 42

0 commit comments

Comments
 (0)