|
17 | 17 | 'use strict'; |
18 | 18 |
|
19 | 19 | const _ = require('lodash'); |
20 | | -const Api = require('kubernetes-client'); |
21 | 20 | const BbPromise = require('bluebird'); |
22 | | -const chalk = require('chalk'); |
| 21 | +const getInfo = require('../lib/get-info'); |
23 | 22 | const helpers = require('../lib/helpers'); |
24 | 23 |
|
25 | | -function toMultipleWords(word) { |
26 | | - return word.replace(/([A-Z])/, ' $1').replace(/^./, (l) => l.toUpperCase()); |
27 | | -} |
28 | | - |
29 | 24 | class KubelessInfo { |
30 | 25 | constructor(serverless, options) { |
31 | 26 | this.serverless = serverless; |
@@ -62,136 +57,12 @@ class KubelessInfo { |
62 | 57 | return BbPromise.resolve(); |
63 | 58 | } |
64 | 59 |
|
65 | | - formatMessage(service, f, options) { |
66 | | - if (options && !options.color) chalk.enabled = false; |
67 | | - let message = ''; |
68 | | - message += `\n${chalk.yellow.underline(`Service Information "${service.name}"`)}\n`; |
69 | | - message += `${chalk.yellow('Cluster IP: ')} ${service.ip}\n`; |
70 | | - message += `${chalk.yellow('Type: ')} ${service.type}\n`; |
71 | | - message += `${chalk.yellow('Ports: ')}\n`; |
72 | | - _.each(service.ports, (port) => { |
73 | | - // Ports can have variable properties |
74 | | - _.each(port, (value, key) => { |
75 | | - message += ` ${chalk.yellow(`${toMultipleWords(key)}: `)} ${value}\n`; |
76 | | - }); |
77 | | - }); |
78 | | - if (this.options.verbose) { |
79 | | - message += `${chalk.yellow('Metadata')}\n`; |
80 | | - message += ` ${chalk.yellow('Self Link: ')} ${service.selfLink}\n`; |
81 | | - message += ` ${chalk.yellow('UID: ')} ${service.uid}\n`; |
82 | | - message += ` ${chalk.yellow('Timestamp: ')} ${service.timestamp}\n`; |
83 | | - } |
84 | | - message += `${chalk.yellow.underline('Function Info')}\n`; |
85 | | - if (f.url) { |
86 | | - message += `${chalk.yellow('URL: ')} ${f.url}\n`; |
87 | | - } |
88 | | - if (f.annotations && f.annotations['kubeless.serverless.com/description']) { |
89 | | - message += `${chalk.yellow('Description:')} ` + |
90 | | - `${f.annotations['kubeless.serverless.com/description']}\n`; |
91 | | - } |
92 | | - if (f.labels) { |
93 | | - message += `${chalk.yellow('Labels:\n')}`; |
94 | | - _.each(f.labels, (v, k) => { |
95 | | - message += `${chalk.yellow(` ${k}:`)} ${v}\n`; |
96 | | - }); |
97 | | - } |
98 | | - message += `${chalk.yellow('Handler: ')} ${f.handler}\n`; |
99 | | - message += `${chalk.yellow('Runtime: ')} ${f.runtime}\n`; |
100 | | - if (f.type === 'PubSub' && !_.isEmpty(f.topic)) { |
101 | | - message += `${chalk.yellow('Topic Trigger:')} ${f.topic}\n`; |
102 | | - } else { |
103 | | - message += `${chalk.yellow('Trigger: ')} ${f.type}\n`; |
104 | | - } |
105 | | - message += `${chalk.yellow('Dependencies: ')} ${f.deps}`; |
106 | | - if (this.options.verbose) { |
107 | | - message += `\n${chalk.yellow('Metadata:')}\n`; |
108 | | - message += ` ${chalk.yellow('Self Link: ')} ${f.selfLink}\n`; |
109 | | - message += ` ${chalk.yellow('UID: ')} ${f.uid}\n`; |
110 | | - message += ` ${chalk.yellow('Timestamp: ')} ${f.timestamp}`; |
111 | | - } |
112 | | - return message; |
113 | | - } |
114 | | - |
115 | 60 | infoFunction(options) { |
116 | | - let counter = 0; |
117 | | - let message = ''; |
118 | | - return new BbPromise((resolve) => { |
119 | | - _.each(this.serverless.service.functions, (desc, f) => { |
120 | | - const connectionOptions = helpers.getConnectionOptions(helpers.loadKubeConfig(), { |
121 | | - namespace: desc.namespace || this.serverless.service.provider.namespace, |
122 | | - }); |
123 | | - const core = new Api.Core(connectionOptions); |
124 | | - const thirdPartyResources = new Api.ThirdPartyResources(connectionOptions); |
125 | | - const extensions = new Api.Extensions(connectionOptions); |
126 | | - thirdPartyResources.addResource('functions'); |
127 | | - core.services.get((err, servicesInfo) => { |
128 | | - if (err) throw new this.serverless.classes.Error(err); |
129 | | - thirdPartyResources.ns.functions.get((ferr, functionsInfo) => { |
130 | | - if (ferr) throw new this.serverless.classes.Error(ferr); |
131 | | - extensions.ns.ingress.get((ierr, ingressInfo) => { |
132 | | - if (ierr) throw this.serverless.classes.Error(ierr); |
133 | | - const fDesc = _.find(functionsInfo.items, item => item.metadata.name === f); |
134 | | - const functionService = _.find( |
135 | | - servicesInfo.items, |
136 | | - (service) => ( |
137 | | - service.metadata.labels && |
138 | | - service.metadata.labels.function === f |
139 | | - ) |
140 | | - ); |
141 | | - if (_.isEmpty(functionService) || _.isEmpty(fDesc)) { |
142 | | - this.serverless.cli.consoleLog( |
143 | | - `Not found any information about the function "${f}"` |
144 | | - ); |
145 | | - } else { |
146 | | - const fIngress = _.find(ingressInfo.items, item => ( |
147 | | - item.metadata.labels && item.metadata.labels.function === f |
148 | | - )); |
149 | | - let url = null; |
150 | | - if (fIngress) { |
151 | | - url = `${fIngress.spec.rules[0].host || 'API_URL'}` + |
152 | | - `${fIngress.spec.rules[0].http.paths[0].path}`; |
153 | | - } |
154 | | - const service = { |
155 | | - name: functionService.metadata.name, |
156 | | - ip: functionService.spec.clusterIP, |
157 | | - type: functionService.spec.type, |
158 | | - ports: functionService.spec.ports, |
159 | | - selfLink: functionService.metadata.selfLink, |
160 | | - uid: functionService.metadata.uid, |
161 | | - timestamp: functionService.metadata.creationTimestamp, |
162 | | - }; |
163 | | - const func = { |
164 | | - name: f, |
165 | | - url, |
166 | | - handler: fDesc.spec.handler, |
167 | | - runtime: fDesc.spec.runtime, |
168 | | - topic: fDesc.spec.topic, |
169 | | - type: fDesc.spec.type, |
170 | | - deps: fDesc.spec.deps, |
171 | | - annotations: fDesc.metadata.annotations, |
172 | | - labels: fDesc.metadata.labels, |
173 | | - selfLink: fDesc.metadata.selfLink, |
174 | | - uid: fDesc.metadata.uid, |
175 | | - timestamp: fDesc.metadata.creationTimestamp, |
176 | | - }; |
177 | | - message += this.formatMessage( |
178 | | - service, |
179 | | - func, |
180 | | - _.defaults({}, options, { color: true }) |
181 | | - ); |
182 | | - } |
183 | | - counter++; |
184 | | - if (counter === _.keys(this.serverless.service.functions).length) { |
185 | | - if (!_.isEmpty(message)) { |
186 | | - this.serverless.cli.consoleLog(message); |
187 | | - } |
188 | | - resolve(message); |
189 | | - } |
190 | | - }); |
191 | | - }); |
192 | | - }); |
193 | | - }); |
194 | | - }); |
| 61 | + return getInfo(this.serverless.service.functions, _.defaults({}, options, { |
| 62 | + namespace: this.serverless.service.provider.namespace, |
| 63 | + verbose: this.options.verbose, |
| 64 | + log: this.serverless.cli.consoleLog, |
| 65 | + })); |
195 | 66 | } |
196 | 67 | } |
197 | 68 |
|
|
0 commit comments