From 77d2429a8c4ea0fd1c2779991715bb584141fac6 Mon Sep 17 00:00:00 2001 From: yiyione Date: Fri, 7 Aug 2020 15:17:08 +0800 Subject: [PATCH] Webportal update k8s proxy api (#4791) --- src/webportal/package.json | 2 +- .../app/cluster-view/services/service-info.js | 56 +++++++------------ src/webportal/yarn.lock | 8 +-- 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/webportal/package.json b/src/webportal/package.json index 5e1d475b3e..9b4677afa9 100644 --- a/src/webportal/package.json +++ b/src/webportal/package.json @@ -33,7 +33,7 @@ "dependencies": { "@hapi/joi": "~15.1.0", "@loadable/component": "^5.10.3", - "@microsoft/openpai-js-sdk": "^0.1.3", + "@microsoft/openpai-js-sdk": "^0.1.4", "@svgr/webpack": "^4.3.3", "@webcomponents/custom-elements": "^1.2.1", "admin-lte": "~2.4.2", diff --git a/src/webportal/src/app/cluster-view/services/service-info.js b/src/webportal/src/app/cluster-view/services/service-info.js index 6f1f6168dd..8fdc9754c5 100644 --- a/src/webportal/src/app/cluster-view/services/service-info.js +++ b/src/webportal/src/app/cluster-view/services/service-info.js @@ -1,42 +1,22 @@ -// Copyright (c) Microsoft Corporation -// All rights reserved. -// -// MIT License -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated -// documentation files (the "Software"), to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and -// to permit persons to whom the Software is furnished to do so, subject to the following conditions: -// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING -// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. +const { PAIV2 } = require('@microsoft/openpai-js-sdk'); const userAuth = require('../../user/user-auth/user-auth.component'); const loading = require('../../job/loading/loading.component'); const webportalConfig = require('../../config/webportal.config.js'); const { clearToken } = require('../../user/user-logout/user-logout.component'); -const fetchWrapper = async url => { - const token = userAuth.checkToken(); - const res = await fetch(url, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); - if (res.ok) { - const data = await res.json(); +const wrapper = async func => { + try { + const data = await func(); return data.items; - } else { + } catch (err) { loading.hideLoading(); - if (res.status === 401) { + if (err.data.code === 'UnauthorizedUserError') { clearToken(); } else { - const data = await res.json(); - alert(data.message); + alert(err.data.message); } } }; @@ -49,13 +29,19 @@ export const getServiceView = async callback => { if (!restServerUrl.pathname.endsWith('/')) { restServerUrl.pathname += '/'; } - const nodeUrl = new URL('api/v1/kubernetes/nodes', restServerUrl); - const podUrl = new URL('api/v1/kubernetes/pods', restServerUrl); - podUrl.searchParams.set('namespace', 'default'); - podUrl.searchParams.set('labelSelector', 'app'); + const client = new PAIV2.OpenPAIClient({ + token: userAuth.checkToken(), + rest_server_uri: restServerUrl.href, + https: window.location.protocol === 'https:', + }); const [nodes, pods] = await Promise.all([ - fetchWrapper(nodeUrl), - fetchWrapper(podUrl), + wrapper(async () => client.kubernetes.getK8sNodes()), + wrapper(async () => + client.kubernetes.getK8sPods({ + namespace: 'default', + labelSelector: 'app', + }), + ), ]); const resultDict = {}; for (const node of nodes) { diff --git a/src/webportal/yarn.lock b/src/webportal/yarn.lock index f6a7d13ab1..b56cbc9394 100644 --- a/src/webportal/yarn.lock +++ b/src/webportal/yarn.lock @@ -1527,10 +1527,10 @@ resolved "https://registry.yarnpkg.com/@microsoft/load-themed-styles/-/load-themed-styles-1.9.7.tgz#4f28d7097802317d2a491888ba65fac617c56dd4" integrity sha512-8Iua4vPmffV2tT1nR/E55GSbUkYRPD9xFfa6v8zJ89EiiDrlyO2OGuXAtC6/nWxLTeVRA3GhtQuU2SApD2sQAA== -"@microsoft/openpai-js-sdk@^0.1.3": - version "0.1.3" - resolved "https://registry.yarnpkg.com/@microsoft/openpai-js-sdk/-/openpai-js-sdk-0.1.3.tgz#9aefccbce928c248e28c03a7a2dc6b8f70877183" - integrity sha512-++9KUVqPFTO/IP9Fnm973oM9KcTCaqCSg/Ug9WluXVL2CmUK/fg+H4Icn/8DHGql7SFeOJxW1Le49SJN+pxcZQ== +"@microsoft/openpai-js-sdk@^0.1.4": + version "0.1.4" + resolved "https://registry.yarnpkg.com/@microsoft/openpai-js-sdk/-/openpai-js-sdk-0.1.4.tgz#99084c2e984dcdf171f9ea1901c750b48c390827" + integrity sha512-QD0QdeC4JWvRG5r3BqcX3WMQB5Xq3ENsh1iFfj32N7nloQIVo3iDJAHzQaGNr5vvMPaW08HY36OYTHO2OcfG6g== dependencies: "@apidevtools/swagger-parser" "^9.0.1" "@azure/storage-blob" "^12.1.0"