This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
/
list_ingresses.go
424 lines (405 loc) · 16.1 KB
/
list_ingresses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package image
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"github.com/ghodss/yaml"
"github.com/olekukonko/tablewriter"
"cellery.io/cellery/components/cli/cli"
"cellery.io/cellery/components/cli/pkg/constants"
errorpkg "cellery.io/cellery/components/cli/pkg/error"
"cellery.io/cellery/components/cli/pkg/image"
"cellery.io/cellery/components/cli/pkg/kubernetes"
)
func RunListIngresses(cli cli.Cli, name string) error {
instancePattern, err := regexp.MatchString(fmt.Sprintf("^%s$", constants.CelleryIdPattern), name)
if err != nil {
return fmt.Errorf("%s is neither instance nor an image", name)
}
if instancePattern {
return displayInstanceApisTable(cli, name)
} else {
return displayImageApisTable(cli, name)
}
}
func displayInstanceApisTable(cli cli.Cli, instanceName string) error {
var canBeComposite bool
cell, err := cli.KubeCli().GetCell(instanceName)
if err != nil {
if cellNotFound, _ := errorpkg.IsCellInstanceNotFoundError(instanceName, err); cellNotFound {
canBeComposite = true
} else {
return fmt.Errorf("failed to check available Cells, %v", err)
}
} else {
displayCellInstanceApisTable(cli, cell, instanceName)
}
if canBeComposite {
composite, err := cli.KubeCli().GetComposite(instanceName)
if err != nil {
if compositeNotFound, _ := errorpkg.IsCompositeInstanceNotFoundError(instanceName, err); compositeNotFound {
return fmt.Errorf(fmt.Sprintf("failed to retrieve ingresses of %s, %v ", instanceName, errors.New(instanceName+" instance not available in the runtime")))
} else {
return fmt.Errorf("failed to check available Composites, %v", err)
}
} else {
displayCompositeInstanceApisTable(cli, composite, instanceName)
}
}
return nil
}
func displayCompositeInstanceApisTable(cli cli.Cli, composite kubernetes.Composite, compositeInstance string) {
var tableData [][]string
for _, component := range composite.CompositeSpec.ComponentTemplates {
for _, port := range component.Spec.Ports {
tableRecord := []string{component.Metadata.Name, port.Protocol, fmt.Sprint(port.Port)}
tableData = append(tableData, tableRecord)
}
}
if len(tableData) > 0 {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"COMPONENT", "INGRESS TYPE", "INGRESS PORT"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetAlignment(3)
table.SetRowSeparator("-")
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold})
table.SetColumnColor(
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{})
table.AppendBulk(tableData)
table.Render()
} else {
fmt.Fprintln(cli.Out(), fmt.Sprintf("No ingresses found for composite instance %s", compositeInstance))
}
}
func displayCellInstanceApisTable(cli cli.Cli, cell kubernetes.Cell, cellInstanceName string) {
apiArray := cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.HttpApis
var ingressType = "web"
globalContext := ""
globalVersion := ""
if len(cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.Extensions.ClusterIngress.Host) == 0 {
ingressType = "http"
}
if cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.Extensions.ApiPublisher.Context != "" {
globalContext = cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.Extensions.ApiPublisher.Context
}
if cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.Extensions.ApiPublisher.Version != "" {
globalVersion = cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.Extensions.ApiPublisher.Version
}
var tableData [][]string
for i := 0; i < len(apiArray); i++ {
url := cellInstanceName + "--gateway-service"
context := apiArray[i].Context
version := getPublishedApiVersion(globalVersion, apiArray[i].Version)
// Add the context of the Cell
if !strings.HasPrefix(context, "/") {
url += "/"
}
url += context
if len(apiArray[i].Definitions) == 0 {
tableRecord := []string{context, ingressType, version, "", "", url, cell.CellSpec.GateWayTemplate.GatewaySpec.Ingress.Extensions.ClusterIngress.Host}
tableData = append(tableData, tableRecord)
}
for j := 0; j < len(apiArray[i].Definitions); j++ {
gatewayUrl := url
path := apiArray[i].Definitions[j].Path
method := apiArray[i].Definitions[j].Method
// Add the path of the API definition
if path != "/" {
if !strings.HasSuffix(gatewayUrl, "/") {
if !strings.HasPrefix(path, "/") {
gatewayUrl += "/"
}
} else {
if strings.HasPrefix(path, "/") {
gatewayUrl = strings.TrimSuffix(gatewayUrl, "/")
}
}
gatewayUrl += path
}
// Add the global api url if globally exposed
globalUrl := ""
globalUrlContext := getPublishedApiRootContext(globalContext, cellInstanceName)
globalUrlVersion := getPublishedApiVersion(globalVersion, version)
if apiArray[i].Global {
if path != "/" {
globalUrl = constants.Wso2ApimHost + strings.Replace("/"+globalUrlContext+"/"+context+path+"/"+globalUrlVersion, "//", "/", -1)
} else {
globalUrl = constants.Wso2ApimHost + strings.Replace("/"+globalUrlContext+"/"+context+"/"+globalUrlVersion, "//", "/", -1)
}
}
tableRecord := []string{context, ingressType, version, method, path, gatewayUrl, globalUrl}
tableData = append(tableData, tableRecord)
}
}
if len(tableData) > 0 {
table := tablewriter.NewWriter(os.Stdout)
if ingressType == "http" {
table.SetHeader([]string{"CONTEXT", "INGRESS TYPE", "VERSION", "METHOD", "RESOURCE", "LOCAL CELL GATEWAY", "GLOBAL API URL"})
} else {
table.SetHeader([]string{"CONTEXT", "INGRESS TYPE", "VERSION", "METHOD", "RESOURCE", "LOCAL CELL GATEWAY", "VHOST"})
}
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetAlignment(3)
table.SetRowSeparator("-")
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold})
table.SetColumnColor(
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{})
table.AppendBulk(tableData)
table.Render()
} else {
fmt.Fprintln(cli.Out(), fmt.Sprintf("No ingresses found for cell instance, %s", cellInstanceName))
}
}
func displayImageApisTable(cli cli.Cli, imageName string) error {
cellYamlContent, err := image.ReadCellImageYaml(cli.FileSystem().Repository(), imageName)
if err != nil {
return fmt.Errorf("error while reading cell image content, %v", err)
}
cellImageContent := &image.Cell{}
err = yaml.Unmarshal(cellYamlContent, cellImageContent)
if err != nil {
return fmt.Errorf("error while unmarshalling cell image content, %v", err)
}
if cellImageContent.Kind == "Cell" {
if err := displayCellImageApisTable(cli, imageName); err != nil {
return fmt.Errorf("error displaying cell image apis table, %v", err)
}
} else if cellImageContent.Kind == "Composite" {
if err := displayCompositeImageApisTable(cli, imageName); err != nil {
return fmt.Errorf("error displaying composite image apis table, %v", err)
}
}
return nil
}
func displayCompositeImageApisTable(cli cli.Cli, compositeImageContent string) error {
cell, err := getIngressValues(cli, compositeImageContent)
if err != nil {
return fmt.Errorf("error occurred while displaying composite image ingress, %v", err)
}
var tableData [][]string
for _, componentDetail := range cell.Component {
for ingressKey, ingressInfo := range componentDetail.Ingress {
var ingressData []string
ingressData = append(ingressData, componentDetail.ComponentName)
if ingressInfo.IngressTypeTCP == constants.TcpIngress && ingressInfo.IngressType != constants.GrpcIngress {
ingressData = append(ingressData, ingressInfo.IngressTypeTCP)
} else {
ingressData = append(ingressData, ingressInfo.IngressType)
}
if (ingressInfo.Port) == 0 {
ingressData = append(ingressData, "--")
} else {
ingressData = append(ingressData, strconv.Itoa(int(ingressInfo.Port)))
}
if ingressInfo.IngressTypeTCP == constants.TcpIngress {
ingressData = append(ingressData, fmt.Sprintf("%s_%s, %s_%s_tcp_%s", componentDetail.ComponentName,
constants.HOST, componentDetail.ComponentName, ingressKey, constants.PORT))
} else {
ingressData = append(ingressData, fmt.Sprintf("%s_%s, %s_%s_%s", componentDetail.ComponentName,
constants.HOST, componentDetail.ComponentName, ingressKey, constants.PORT))
}
tableData = append(tableData, ingressData)
}
}
if len(tableData) > 0 {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"COMPONENT", "INGRESS TYPE", "INGRESS PORT", "INGRESS_KEY"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetAlignment(3)
table.SetRowSeparator("-")
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold})
table.SetColumnColor(
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{})
table.AppendBulk(tableData)
table.Render()
} else {
fmt.Fprintln(cli.Out(), fmt.Sprintf("No ingresses found for composite image, %s", compositeImageContent))
}
return nil
}
func displayCellImageApisTable(cli cli.Cli, cellImageContent string) error {
cell, err := getIngressValues(cli, cellImageContent)
if err != nil {
return fmt.Errorf("error occurred while displaying cell image ingress, %v", err)
}
var tableData [][]string
for _, componentDetail := range cell.Component {
for ingress, ingressInfo := range componentDetail.Ingress {
var ingressData []string
if !(ingressInfo.Expose == "global" || ingressInfo.Expose == "local") {
ingressInfo.Expose = "false"
}
if ingressInfo.IngressType == constants.HttpApiIngress && ingressInfo.Context != "" {
if len(ingressInfo.Definition) == 0 {
ingressData = []string{componentDetail.ComponentName, ingressInfo.IngressType,
ingressInfo.Context, ingressInfo.ApiVersion, strconv.Itoa(int(ingressInfo.Port)),
constants.NA, constants.NA, ingressInfo.Expose, constants.NA,
fmt.Sprintf("%s_%s_%s", componentDetail.ComponentName, ingress, "api_url")}
tableData = append(tableData, ingressData)
} else {
for _, resourcesValue := range ingressInfo.Definition {
for _, resource := range resourcesValue {
ingressData = []string{componentDetail.ComponentName, ingressInfo.IngressType,
ingressInfo.Context, ingressInfo.ApiVersion, strconv.Itoa(int(ingressInfo.Port)),
resource.Path, resource.Method, ingressInfo.Expose, constants.NA,
fmt.Sprintf("%s_%s_%s", componentDetail.ComponentName, ingress, "api_url")}
tableData = append(tableData, ingressData)
}
}
}
} else if ingressInfo.IngressType == constants.WebIngress {
ingressData = []string{componentDetail.ComponentName, ingressInfo.IngressType,
ingressInfo.GatewayConfig.Context, ingressInfo.ApiVersion, strconv.Itoa(int(ingressInfo.Port)),
constants.NA, constants.NA, ingressInfo.Expose, ingressInfo.GatewayConfig.Vhost, constants.NA}
tableData = append(tableData, ingressData)
} else if ingressInfo.IngressType == constants.GrpcIngress {
ingressData = []string{componentDetail.ComponentName, ingressInfo.IngressType, constants.NA,
constants.NA, strconv.Itoa(ingressInfo.GatewayPort), constants.NA, constants.NA, constants.NA,
ingressInfo.GatewayConfig.Vhost,
fmt.Sprintf("%s, %s_%s_%s", constants.GatewayHost, componentDetail.ComponentName,
ingress, "grpc_port")}
tableData = append(tableData, ingressData)
} else if ingressInfo.IngressTypeTCP == constants.TcpIngress {
if (ingressInfo.GatewayPort) == 0 {
ingressData = []string{componentDetail.ComponentName, ingressInfo.IngressTypeTCP, constants.NA,
constants.NA, "--", constants.NA, constants.NA, ingressInfo.Expose,
ingressInfo.GatewayConfig.Vhost, fmt.Sprintf("%s, %s_%s_tcp_%s", constants.GatewayHost,
componentDetail.ComponentName, ingress, constants.PORT)}
} else {
ingressData = []string{componentDetail.ComponentName, ingressInfo.IngressTypeTCP, constants.NA,
constants.NA, strconv.Itoa(ingressInfo.GatewayPort), constants.NA, constants.NA,
ingressInfo.Expose, ingressInfo.GatewayConfig.Vhost,
fmt.Sprintf("%s, %s_%s_tcp_%s", constants.GatewayHost, componentDetail.ComponentName,
ingress, constants.PORT)}
}
tableData = append(tableData, ingressData)
}
}
}
if len(tableData) > 0 {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"COMPONENT", "INGRESS TYPE", "INGRESS CONTEXT", "INGRESS_VERSION", "INGRESS PORT", "RESOURCE", "METHOD", "EXPOSED", "VHOST", "INGRESS_KEY"})
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetAlignment(3)
table.SetRowSeparator("-")
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold})
table.SetColumnColor(
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{})
table.AppendBulk(tableData)
table.Render()
} else {
fmt.Fprintln(cli.Out(), fmt.Sprintf("No ingresses found for cell image, %s", cellImageContent))
}
return nil
}
func getIngressValues(cli cli.Cli, cellImageContent string) (kubernetes.Cell, error) {
parsedCellImage, err := image.ParseImageTag(cellImageContent)
imageDir, err := ExtractImage(cli, parsedCellImage, false)
if err != nil {
return kubernetes.Cell{}, fmt.Errorf("error occurred while extracting image: %s", err)
}
jsonFile, err := os.Open(fmt.Sprintf("%s/%s/%s/%s%s%s", imageDir, constants.ZipArtifacts, constants.CELLERY,
parsedCellImage.ImageName, constants.ZipMetaSuffix, constants.JsonExt))
if err != nil {
return kubernetes.Cell{}, fmt.Errorf("error occurred while reading image_meta file: %s", err)
}
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return kubernetes.Cell{}, fmt.Errorf("error occurred while reading json data from image_meta file: %s", err)
}
cell := kubernetes.Cell{}
err = json.Unmarshal(byteValue, &cell)
if err != nil {
return kubernetes.Cell{}, fmt.Errorf("error occurred while unmarshalling json data "+
"from image_meta file: %s", err)
}
return cell, nil
}
func getPublishedApiRootContext(globalContext string, cellInstanceName string) string {
if globalContext != "" {
return globalContext
} else {
return cellInstanceName
}
}
func getPublishedApiVersion(globalVersion string, apiVersion string) string {
if globalVersion != "" {
return globalVersion
} else {
return apiVersion
}
}