From ea91a4449d49c75fe3dd6c61abdc5de19f91f51e Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Mon, 1 Mar 2021 15:00:18 -0500 Subject: [PATCH 01/19] added secret to create remote resource method --- lib/remoteResource.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 6270b96..f74a9ca 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -14,7 +14,12 @@ const requestsTemplate = `{ "options": { "url": "{{{url}}}", "headers": { - "razee-org-key": "{{orgKey}}" + "razee-org-key": + "valueFrom": + "secretKeyRef": + "name": "clustersubscription-{{subscriptionUuid}}-secret" + "namespace": "razeedeploy" + "key": "razee-api-org-key" } } }`; @@ -50,7 +55,27 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'requests': [] } }; + const resourceSecret = { + 'apiVersion': v1, + 'kind': Secret, + 'metadata': { + 'namespace': NAMESPACE, + 'name': resourceName, + 'annotations': { + 'deploy.razee.io/clustersubscription': sub.subscriptionUuid, + 'deploy.razee.io/clusterid': clusterId + }, + 'labels': { + 'razee/watch-resource': 'lite' + } + }, + 'data': { + 'razee-api-org-key': apiKey + } + }; + resourceTemplate.spec.requests.push(parsed); + resourceSecret.spec.requests.push(parsed); const opt = { simple: false, resolveWithFullResponse: true }; From d33069f8389e978b446138320f6304cc19c21d80 Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Mon, 1 Mar 2021 16:01:43 -0500 Subject: [PATCH 02/19] broke up methods to apply resource and secret and delete --- lib/remoteResource.js | 73 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 7 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index f74a9ca..8d86873 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -10,6 +10,9 @@ const API_VERSION = 'deploy.razee.io/v1alpha2'; const KIND = 'RemoteResource'; const NAMESPACE = process.env.NAMESPACE; +const API_VERSION_SECRET = 'v1'; +const KIND_SECRET = 'Secret'; + const requestsTemplate = `{ "options": { "url": "{{{url}}}", @@ -33,6 +36,7 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey }); const parsed = JSON.parse(rendered); const resourceName = `clustersubscription-${sub.subscriptionUuid}`; + const resourceSecretName = `clustersubscription-${sub.subscriptionUuid}-secret`; const userName = (sub.kubeOwnerName && typeof sub.kubeOwnerName === 'string') ? sub.kubeOwnerName : 'razeedeploy'; const resourceTemplate = { 'apiVersion': API_VERSION, @@ -60,7 +64,7 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'kind': Secret, 'metadata': { 'namespace': NAMESPACE, - 'name': resourceName, + 'name': resourceSecretName, 'annotations': { 'deploy.razee.io/clustersubscription': sub.subscriptionUuid, 'deploy.razee.io/clusterid': clusterId @@ -76,9 +80,18 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) resourceTemplate.spec.requests.push(parsed); resourceSecret.spec.requests.push(parsed); + })); + + } catch (error) { + log.error('There was an error creating remote resources', { error }); + } +}; - const opt = { simple: false, resolveWithFullResponse: true }; +const applyResource = async (razeeApi, apiKey, subscriptions, clusterId) =>{ + createRemoteResources(razeeApi, apiKey, subscriptions, clusterId); + const opt = { simple: false, resolveWithFullResponse: true }; + const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); const uri = krm.uri({ name: resourceName, namespace: NAMESPACE }); log.debug(resourceName); const get = await krm.get(resourceName, NAMESPACE, opt); @@ -103,16 +116,60 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } else { log.error(`Get ${get.statusCode} ${uri}`); } - })); + const krm_secret = await kc.getKubeResourceMeta(API_VERSION_SECRET, KIND_SECRET, 'update'); + const uri = krm.uri({ name: resourceSecretName, namespace: NAMESPACE }); + log.debug(resourceSecretName); + const get = await krm.get(resourceSecretName, NAMESPACE, opt); + if (get.statusCode === 200) { + // the remote resource already exists so use mergePatch to apply the resource + log.info(`Attempting mergePatch for an existing resource ${uri}`); + const mergeResult = await krm.mergePatch(resourceSecretName, NAMESPACE, resourceSecretTemplate, opt); + if (mergeResult.statusCode === 200) { + log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); + } else { + log.error('mergePatch error', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); + } + } else if (get.statusCode === 404) { + // the secret does not exist so use post to apply the secret + log.info(`Attempting post for a new resource ${uri}`); + const postResult = await krm.post(resourceSecretTemplate, opt); + if (postResult.statusCode === 200 || postResult.statusCode === 201) { + log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); + } else { + log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); + } + } else { + log.error(`Get ${get.statusCode} ${uri}`); + } +} + +const deleteRemoteResources = async (resources) => { + const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); + const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); + log.debug('Deleting', { selfLinks }); + try { + selfLinks.map(async (selfLink) => { + log.info(`Delete ${selfLink}`); + const opt = { uri: selfLink, simple: false, resolveWithFullResponse: true, method: 'DELETE' }; + const res = await krm.request(opt); + if (res.statusCode === 404) { + log.info(`Delete ${res.statusCode} ${opt.uri}`); + return { statusCode: res.statusCode, body: res.body }; + } else if (res.statusCode !== 200) { + log.info(`Delete ${res.statusCode} ${opt.uri}`); + return Promise.reject({ statusCode: res.statusCode, body: res.body }); + } + log.info(`Delete ${res.statusCode} ${opt.uri}`); + return { statusCode: res.statusCode, body: res.body }; + }); } catch (error) { - log.error('There was an error creating remote resources', { error }); + log.error(error); } - }; -const deleteRemoteResources = async (resources) => { - const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); +const deleteSecret = async (resources) => { + const krm = await kc.getKubeResourceMeta(API_VERSION_SECRET, KIND_SECRET, 'update'); const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); log.debug('Deleting', { selfLinks }); try { @@ -165,3 +222,5 @@ const getRemoteResources = async (clusterId) => { exports.createRemoteResources = createRemoteResources; exports.getRemoteResources = getRemoteResources; exports.deleteRemoteResources = deleteRemoteResources; +exports.applyResource = applyResource; +exports.deleteSecret = deleteSecret; \ No newline at end of file From ea485916512485b5eabda7de2861e351b0a48e35 Mon Sep 17 00:00:00 2001 From: Alexander Lewitt Date: Tue, 2 Mar 2021 14:14:49 -0500 Subject: [PATCH 03/19] add function template --- lib/remoteResource.js | 135 +++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 66 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 8d86873..82fad53 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -6,13 +6,9 @@ const { KubeClass, KubeApiConfig } = require('@razee/kubernetes-util'); const kubeApiConfig = KubeApiConfig(); const kc = new KubeClass(kubeApiConfig); -const API_VERSION = 'deploy.razee.io/v1alpha2'; -const KIND = 'RemoteResource'; +const RR_API_VERSION = 'deploy.razee.io/v1alpha2'; const NAMESPACE = process.env.NAMESPACE; -const API_VERSION_SECRET = 'v1'; -const KIND_SECRET = 'Secret'; - const requestsTemplate = `{ "options": { "url": "{{{url}}}", @@ -35,15 +31,15 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) const url = `${razeeApi}/${sub.url}`; const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey }); const parsed = JSON.parse(rendered); - const resourceName = `clustersubscription-${sub.subscriptionUuid}`; - const resourceSecretName = `clustersubscription-${sub.subscriptionUuid}-secret`; + const remoteResourceName = `clustersubscription-${sub.subscriptionUuid}`; + const secretName = `clustersubscription-${sub.subscriptionUuid}-secret`; const userName = (sub.kubeOwnerName && typeof sub.kubeOwnerName === 'string') ? sub.kubeOwnerName : 'razeedeploy'; - const resourceTemplate = { + const remoteResourceJson = { 'apiVersion': API_VERSION, - 'kind': KIND, + 'kind': 'RemoteResource', 'metadata': { 'namespace': NAMESPACE, - 'name': resourceName, + 'name': remoteResourceName, 'annotations': { 'deploy.razee.io/clustersubscription': sub.subscriptionUuid, 'deploy.razee.io/clusterid': clusterId @@ -59,12 +55,12 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'requests': [] } }; - const resourceSecret = { - 'apiVersion': v1, - 'kind': Secret, + const secretJson = { + 'apiVersion': 'v1', + 'kind': 'Secret', 'metadata': { 'namespace': NAMESPACE, - 'name': resourceSecretName, + 'name': secretName, 'annotations': { 'deploy.razee.io/clustersubscription': sub.subscriptionUuid, 'deploy.razee.io/clusterid': clusterId @@ -77,9 +73,12 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'razee-api-org-key': apiKey } }; - + resourceTemplate.spec.requests.push(parsed); resourceSecret.spec.requests.push(parsed); + + await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceName, remoteResourceJson); + await applyResource('v1', 'Secret', secretName, secretJson); })); } catch (error) { @@ -87,60 +86,64 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } }; +const applyResource = async (apiVersion, kind, resourceName, resourceJson) => { + +} -const applyResource = async (razeeApi, apiKey, subscriptions, clusterId) =>{ + +const applyResource = async (razeeApi, apiKey, subscriptions, clusterId) => { createRemoteResources(razeeApi, apiKey, subscriptions, clusterId); const opt = { simple: false, resolveWithFullResponse: true }; const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); - const uri = krm.uri({ name: resourceName, namespace: NAMESPACE }); - log.debug(resourceName); - const get = await krm.get(resourceName, NAMESPACE, opt); - if (get.statusCode === 200) { - // the remote resource already exists so use mergePatch to apply the resource - log.info(`Attempting mergePatch for an existing resource ${uri}`); - const mergeResult = await krm.mergePatch(resourceName, NAMESPACE, resourceTemplate, opt); - if (mergeResult.statusCode === 200) { - log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); - } else { - log.error('mergePatch error', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); - } - } else if (get.statusCode === 404) { - // the remote resource does not exist so use post to apply the resource - log.info(`Attempting post for a new resource ${uri}`); - const postResult = await krm.post(resourceTemplate, opt); - if (postResult.statusCode === 200 || postResult.statusCode === 201) { - log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); - } else { - log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); - } - } else { - log.error(`Get ${get.statusCode} ${uri}`); - } - const krm_secret = await kc.getKubeResourceMeta(API_VERSION_SECRET, KIND_SECRET, 'update'); - const uri = krm.uri({ name: resourceSecretName, namespace: NAMESPACE }); - log.debug(resourceSecretName); - const get = await krm.get(resourceSecretName, NAMESPACE, opt); - if (get.statusCode === 200) { - // the remote resource already exists so use mergePatch to apply the resource - log.info(`Attempting mergePatch for an existing resource ${uri}`); - const mergeResult = await krm.mergePatch(resourceSecretName, NAMESPACE, resourceSecretTemplate, opt); - if (mergeResult.statusCode === 200) { - log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); - } else { - log.error('mergePatch error', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); - } - } else if (get.statusCode === 404) { - // the secret does not exist so use post to apply the secret - log.info(`Attempting post for a new resource ${uri}`); - const postResult = await krm.post(resourceSecretTemplate, opt); - if (postResult.statusCode === 200 || postResult.statusCode === 201) { - log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); - } else { - log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); - } - } else { - log.error(`Get ${get.statusCode} ${uri}`); - } + const uri = krm.uri({ name: resourceName, namespace: NAMESPACE }); + log.debug(resourceName); + const get = await krm.get(resourceName, NAMESPACE, opt); + if (get.statusCode === 200) { + // the remote resource already exists so use mergePatch to apply the resource + log.info(`Attempting mergePatch for an existing resource ${uri}`); + const mergeResult = await krm.mergePatch(resourceName, NAMESPACE, resourceTemplate, opt); + if (mergeResult.statusCode === 200) { + log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); + } else { + log.error('mergePatch error', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); + } + } else if (get.statusCode === 404) { + // the remote resource does not exist so use post to apply the resource + log.info(`Attempting post for a new resource ${uri}`); + const postResult = await krm.post(resourceTemplate, opt); + if (postResult.statusCode === 200 || postResult.statusCode === 201) { + log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); + } else { + log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); + } + } else { + log.error(`Get ${get.statusCode} ${uri}`); + } + const krm_secret = await kc.getKubeResourceMeta(API_VERSION_SECRET, KIND_SECRET, 'update'); + const uri = krm.uri({ name: resourceSecretName, namespace: NAMESPACE }); + log.debug(resourceSecretName); + const get = await krm.get(resourceSecretName, NAMESPACE, opt); + if (get.statusCode === 200) { + // the remote resource already exists so use mergePatch to apply the resource + log.info(`Attempting mergePatch for an existing resource ${uri}`); + const mergeResult = await krm.mergePatch(resourceSecretName, NAMESPACE, resourceSecretTemplate, opt); + if (mergeResult.statusCode === 200) { + log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); + } else { + log.error('mergePatch error', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); + } + } else if (get.statusCode === 404) { + // the secret does not exist so use post to apply the secret + log.info(`Attempting post for a new resource ${uri}`); + const postResult = await krm.post(resourceSecretTemplate, opt); + if (postResult.statusCode === 200 || postResult.statusCode === 201) { + log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); + } else { + log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); + } + } else { + log.error(`Get ${get.statusCode} ${uri}`); + } } @@ -223,4 +226,4 @@ exports.createRemoteResources = createRemoteResources; exports.getRemoteResources = getRemoteResources; exports.deleteRemoteResources = deleteRemoteResources; exports.applyResource = applyResource; -exports.deleteSecret = deleteSecret; \ No newline at end of file +exports.deleteSecret = deleteSecret; From 5d172d2a7a465988b9d38ec636c0f71bc0297d53 Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Tue, 2 Mar 2021 15:59:04 -0500 Subject: [PATCH 04/19] updated apply resources method, broke up delete method --- lib/remoteResource.js | 84 +++++++++---------------------------------- 1 file changed, 17 insertions(+), 67 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 82fad53..26e1183 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -26,7 +26,7 @@ const requestsTemplate = `{ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) => { log.info('create remote resources subscription list', { subscriptions }); try { - const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); + const krm = await kc.getKubeResourceMeta(RP_API_VERSION, KIND, 'update'); return Promise.all(subscriptions.map(async sub => { const url = `${razeeApi}/${sub.url}`; const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey }); @@ -35,7 +35,7 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) const secretName = `clustersubscription-${sub.subscriptionUuid}-secret`; const userName = (sub.kubeOwnerName && typeof sub.kubeOwnerName === 'string') ? sub.kubeOwnerName : 'razeedeploy'; const remoteResourceJson = { - 'apiVersion': API_VERSION, + 'apiVersion': RP_API_VERSION, 'kind': 'RemoteResource', 'metadata': { 'namespace': NAMESPACE, @@ -75,10 +75,10 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) }; resourceTemplate.spec.requests.push(parsed); - resourceSecret.spec.requests.push(parsed); + - await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceName, remoteResourceJson); - await applyResource('v1', 'Secret', secretName, secretJson); + await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceName, remoteResourceJson, NAMESPACE); + await applyResource('v1', 'Secret', secretName, secretJson, NAMESPACE); })); } catch (error) { @@ -86,22 +86,16 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } }; -const applyResource = async (apiVersion, kind, resourceName, resourceJson) => { - -} - - -const applyResource = async (razeeApi, apiKey, subscriptions, clusterId) => { - createRemoteResources(razeeApi, apiKey, subscriptions, clusterId); +const applyResource = async (apiVersion, kind, resourceName, resourceJson, namespace) => { const opt = { simple: false, resolveWithFullResponse: true }; - const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); - const uri = krm.uri({ name: resourceName, namespace: NAMESPACE }); + const krm = await kc.getKubeResourceMeta(apiVersion, kind, 'update'); + const uri = krm.uri({ name: resourceName, namespace: namespace }); log.debug(resourceName); - const get = await krm.get(resourceName, NAMESPACE, opt); + const get = await krm.get(resourceName, namespace, opt); if (get.statusCode === 200) { // the remote resource already exists so use mergePatch to apply the resource log.info(`Attempting mergePatch for an existing resource ${uri}`); - const mergeResult = await krm.mergePatch(resourceName, NAMESPACE, resourceTemplate, opt); + const mergeResult = await krm.mergePatch(resourceName, namespace, resourceJson, opt); if (mergeResult.statusCode === 200) { log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); } else { @@ -110,32 +104,7 @@ const applyResource = async (razeeApi, apiKey, subscriptions, clusterId) => { } else if (get.statusCode === 404) { // the remote resource does not exist so use post to apply the resource log.info(`Attempting post for a new resource ${uri}`); - const postResult = await krm.post(resourceTemplate, opt); - if (postResult.statusCode === 200 || postResult.statusCode === 201) { - log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); - } else { - log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); - } - } else { - log.error(`Get ${get.statusCode} ${uri}`); - } - const krm_secret = await kc.getKubeResourceMeta(API_VERSION_SECRET, KIND_SECRET, 'update'); - const uri = krm.uri({ name: resourceSecretName, namespace: NAMESPACE }); - log.debug(resourceSecretName); - const get = await krm.get(resourceSecretName, NAMESPACE, opt); - if (get.statusCode === 200) { - // the remote resource already exists so use mergePatch to apply the resource - log.info(`Attempting mergePatch for an existing resource ${uri}`); - const mergeResult = await krm.mergePatch(resourceSecretName, NAMESPACE, resourceSecretTemplate, opt); - if (mergeResult.statusCode === 200) { - log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); - } else { - log.error('mergePatch error', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); - } - } else if (get.statusCode === 404) { - // the secret does not exist so use post to apply the secret - log.info(`Attempting post for a new resource ${uri}`); - const postResult = await krm.post(resourceSecretTemplate, opt); + const postResult = await krm.post(resourceJson, opt); if (postResult.statusCode === 200 || postResult.statusCode === 201) { log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); } else { @@ -148,33 +117,14 @@ const applyResource = async (razeeApi, apiKey, subscriptions, clusterId) => { const deleteRemoteResources = async (resources) => { - const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'update'); - const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); + log.debug('Deleting', { selfLinks }); - try { - selfLinks.map(async (selfLink) => { - log.info(`Delete ${selfLink}`); - const opt = { uri: selfLink, simple: false, resolveWithFullResponse: true, method: 'DELETE' }; - const res = await krm.request(opt); - if (res.statusCode === 404) { - log.info(`Delete ${res.statusCode} ${opt.uri}`); - return { statusCode: res.statusCode, body: res.body }; - } else if (res.statusCode !== 200) { - log.info(`Delete ${res.statusCode} ${opt.uri}`); - return Promise.reject({ statusCode: res.statusCode, body: res.body }); - } - log.info(`Delete ${res.statusCode} ${opt.uri}`); - return { statusCode: res.statusCode, body: res.body }; - }); - } catch (error) { - log.error(error); - } + await deleteResource(resources); }; -const deleteSecret = async (resources) => { - const krm = await kc.getKubeResourceMeta(API_VERSION_SECRET, KIND_SECRET, 'update'); +const deleteResource = async(resources) => { + const krm = await kc.getKubeResourceMeta(RP_API_VERSION, KIND, 'update'); const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); - log.debug('Deleting', { selfLinks }); try { selfLinks.map(async (selfLink) => { log.info(`Delete ${selfLink}`); @@ -193,7 +143,8 @@ const deleteSecret = async (resources) => { } catch (error) { log.error(error); } -}; + +} const getRemoteResources = async (clusterId) => { log.debug('Getting a list of clustersubscription remote resources on this cluster'); @@ -226,4 +177,3 @@ exports.createRemoteResources = createRemoteResources; exports.getRemoteResources = getRemoteResources; exports.deleteRemoteResources = deleteRemoteResources; exports.applyResource = applyResource; -exports.deleteSecret = deleteSecret; From 3dd5c9d9d1bfc3b39c1f84e99339aad3d6b7ba33 Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Tue, 2 Mar 2021 16:24:29 -0500 Subject: [PATCH 05/19] updated delete methods and applyresource --- lib/remoteResource.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 26e1183..2562cb3 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -26,7 +26,6 @@ const requestsTemplate = `{ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) => { log.info('create remote resources subscription list', { subscriptions }); try { - const krm = await kc.getKubeResourceMeta(RP_API_VERSION, KIND, 'update'); return Promise.all(subscriptions.map(async sub => { const url = `${razeeApi}/${sub.url}`; const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey }); @@ -117,14 +116,17 @@ const applyResource = async (apiVersion, kind, resourceName, resourceJson, names const deleteRemoteResources = async (resources) => { - + const krm = await kc.getKubeResourceMeta(RR_API_VERSION, 'RemoteResource', 'update'); + const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); + log.debug('Deleting', { selfLinks }); + await deleteResource(resources, krm); + const krm = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); + const selfLinks = resources.map((resource) => krm.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace })); log.debug('Deleting', { selfLinks }); - await deleteResource(resources); + await deleteResource(resources, krm); }; -const deleteResource = async(resources) => { - const krm = await kc.getKubeResourceMeta(RP_API_VERSION, KIND, 'update'); - const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); +const deleteResource = async(selfLinks, krm) => { try { selfLinks.map(async (selfLink) => { log.info(`Delete ${selfLink}`); From f7a7ad21f3a90e303b771ee743a85a6c74fe5a31 Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 14:31:11 -0500 Subject: [PATCH 06/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 2562cb3..bccf553 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -28,7 +28,7 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) try { return Promise.all(subscriptions.map(async sub => { const url = `${razeeApi}/${sub.url}`; - const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey }); + const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey, namespace: NAMESPACE }); const parsed = JSON.parse(rendered); const remoteResourceName = `clustersubscription-${sub.subscriptionUuid}`; const secretName = `clustersubscription-${sub.subscriptionUuid}-secret`; From b2a962830cdd8fab79769b6e6cdeb5e559986c31 Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 14:32:05 -0500 Subject: [PATCH 07/19] Update lib/remoteResource.js namespace update Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index bccf553..39acd46 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -17,7 +17,7 @@ const requestsTemplate = `{ "valueFrom": "secretKeyRef": "name": "clustersubscription-{{subscriptionUuid}}-secret" - "namespace": "razeedeploy" + "namespace": {{namespace}} "key": "razee-api-org-key" } } From 0ba8b4b1f2d37275e48f4409bbe3cd5680c6b4e2 Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 14:46:48 -0500 Subject: [PATCH 08/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 39acd46..fc554ec 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -73,8 +73,6 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } }; - resourceTemplate.spec.requests.push(parsed); - await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceName, remoteResourceJson, NAMESPACE); await applyResource('v1', 'Secret', secretName, secretJson, NAMESPACE); From 964bc58f0dc142226e4dcef20e2646973e110fa4 Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 14:46:55 -0500 Subject: [PATCH 09/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index fc554ec..a59334b 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -54,6 +54,8 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'requests': [] } }; + resourceTemplate.spec.requests.push(parsed); + const secretJson = { 'apiVersion': 'v1', 'kind': 'Secret', From 1243df30bdd880a8e934902e286982296a5b489e Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 15:11:26 -0500 Subject: [PATCH 10/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index a59334b..12c7936 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -54,7 +54,7 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'requests': [] } }; - resourceTemplate.spec.requests.push(parsed); + remoteResourceJson.spec.requests.push(parsed); const secretJson = { 'apiVersion': 'v1', From ee0f0fc2ac442f5b96fc8e2f707e2bbeaccd1a5b Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 15:11:47 -0500 Subject: [PATCH 11/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 12c7936..d1a7c48 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -76,8 +76,8 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) }; - await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceName, remoteResourceJson, NAMESPACE); - await applyResource('v1', 'Secret', secretName, secretJson, NAMESPACE); + await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceJson); + await applyResource('v1', 'Secret', secretJson); })); } catch (error) { From 675c1b5d3c2c629469e27e2eeda97b1bf323badc Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 15:18:50 -0500 Subject: [PATCH 12/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index d1a7c48..0241043 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -85,7 +85,9 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } }; -const applyResource = async (apiVersion, kind, resourceName, resourceJson, namespace) => { +const applyResource = async (apiVersion, kind, resourceJson) => { +const resourceName = objectPath.get(resourceJson, 'metadata.name', ''); +const resourceNamespace = objectPath.get(resourceJson, 'metadata.namespace', NAMESPACE); const opt = { simple: false, resolveWithFullResponse: true }; const krm = await kc.getKubeResourceMeta(apiVersion, kind, 'update'); const uri = krm.uri({ name: resourceName, namespace: namespace }); From ca12e9015e6be388cf6b9fc8a31206e7e782df40 Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 15:19:18 -0500 Subject: [PATCH 13/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 0241043..a026782 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -90,7 +90,7 @@ const resourceName = objectPath.get(resourceJson, 'metadata.name', ''); const resourceNamespace = objectPath.get(resourceJson, 'metadata.namespace', NAMESPACE); const opt = { simple: false, resolveWithFullResponse: true }; const krm = await kc.getKubeResourceMeta(apiVersion, kind, 'update'); - const uri = krm.uri({ name: resourceName, namespace: namespace }); + const uri = krm.uri({ name: resourceName, namespace: resourceNamespace }); log.debug(resourceName); const get = await krm.get(resourceName, namespace, opt); if (get.statusCode === 200) { From 1494da31daeb89b5ee5def3ecc2ef04aaf8922d1 Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Wed, 3 Mar 2021 16:25:50 -0500 Subject: [PATCH 14/19] updated applyresource to take fields from json --- lib/remoteResource.js | 19 +++++++++++-------- package-lock.json | 4 ++-- package.json | 1 + 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index a026782..e789b5e 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -3,6 +3,7 @@ const Mustache = require('mustache'); const log = require('./log'); const { KubeClass, KubeApiConfig } = require('@razee/kubernetes-util'); +const objectPath = require('object-path'); const kubeApiConfig = KubeApiConfig(); const kc = new KubeClass(kubeApiConfig); @@ -76,8 +77,8 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) }; - await applyResource(RR_API_VERSION, 'RemoteResource', remoteResourceJson); - await applyResource('v1', 'Secret', secretJson); + await applyResource(remoteResourceJson); + await applyResource(secretJson); })); } catch (error) { @@ -85,18 +86,20 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } }; -const applyResource = async (apiVersion, kind, resourceJson) => { +const applyResource = async (resourceJson) => { const resourceName = objectPath.get(resourceJson, 'metadata.name', ''); const resourceNamespace = objectPath.get(resourceJson, 'metadata.namespace', NAMESPACE); +const resourceApiVersion = objectPath.get(resourceJson, 'metadata.apiVersion', RP_API_VERSION); +const resourceKind = objectPath.get(resourceJson, 'metadata.kind', ''); const opt = { simple: false, resolveWithFullResponse: true }; - const krm = await kc.getKubeResourceMeta(apiVersion, kind, 'update'); + const krm = await kc.getKubeResourceMeta(resourceApiVersion, resourceKind, 'update'); const uri = krm.uri({ name: resourceName, namespace: resourceNamespace }); log.debug(resourceName); - const get = await krm.get(resourceName, namespace, opt); + const get = await krm.get(resourceName, resourceNamespace, opt); if (get.statusCode === 200) { // the remote resource already exists so use mergePatch to apply the resource log.info(`Attempting mergePatch for an existing resource ${uri}`); - const mergeResult = await krm.mergePatch(resourceName, namespace, resourceJson, opt); + const mergeResult = await krm.mergePatch(resourceName, resourceNamespace, resourceJson, opt); if (mergeResult.statusCode === 200) { log.info('mergePatch successful', { 'statusCode': mergeResult.statusCode, 'statusMessage': mergeResult.statusMessage }); } else { @@ -125,7 +128,7 @@ const deleteRemoteResources = async (resources) => { const krm = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); const selfLinks = resources.map((resource) => krm.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace })); log.debug('Deleting', { selfLinks }); - await deleteResource(resources, krm); + await deleteResource(selfLinks, krm); }; const deleteResource = async(selfLinks, krm) => { @@ -154,7 +157,7 @@ const getRemoteResources = async (clusterId) => { log.debug('Getting a list of clustersubscription remote resources on this cluster'); let remoteResources = []; try { - const krm = await kc.getKubeResourceMeta(API_VERSION, KIND, 'get'); + const krm = await kc.getKubeResourceMeta(RR_API_VERSION, 'RemoteResource', 'get'); const opt = { simple: false, resolveWithFullResponse: true }; const get = await krm.get('', NAMESPACE, opt); if (get.statusCode === 200) { diff --git a/package-lock.json b/package-lock.json index 95042c5..7588268 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3275,8 +3275,8 @@ }, "object-path": { "version": "0.11.5", - "resolved": "https://registry.npmjs.org/object-path/-/object-path-0.11.5.tgz", - "integrity": "sha512-jgSbThcoR/s+XumvGMTMf81QVBmah+/Q7K7YduKeKVWL7N111unR2d6pZZarSk6kY/caeNxUDyxOvMWyzoU2eg==" + "resolved": "https://na.artifactory.swg-devops.com/artifactory/api/npm/wcp-alchemy-containers-team-npm-virtual/object-path/-/object-path-0.11.5.tgz?dl=https%3A%2F%2Fregistry.npmjs.org%2Fobject-path%2F-%2Fobject-path-0.11.5.tgz", + "integrity": "sha1-1OPPGWAaUUClWhatcSAZqcULV3o=" }, "oidc-token-hash": { "version": "5.0.1", diff --git a/package.json b/package.json index 923c014..8fb2da1 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "graphql": "^15.5.0", "graphql-tag": "^2.11.0", "mustache": "^4.1.0", + "object-path": "^0.11.5", "subscriptions-transport-ws": "^0.9.18", "touch": "^3.1.0", "winston": "^3.3.3" From c4b7d3beda3019e1a6bfda34ac847ecc18305f16 Mon Sep 17 00:00:00 2001 From: nderibe <42783987+nderibe@users.noreply.github.com> Date: Wed, 3 Mar 2021 16:26:39 -0500 Subject: [PATCH 15/19] Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> --- lib/remoteResource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index e789b5e..7a50e64 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -124,7 +124,7 @@ const deleteRemoteResources = async (resources) => { const krm = await kc.getKubeResourceMeta(RR_API_VERSION, 'RemoteResource', 'update'); const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); log.debug('Deleting', { selfLinks }); - await deleteResource(resources, krm); + await deleteResource(selfLinks, krm); const krm = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); const selfLinks = resources.map((resource) => krm.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace })); log.debug('Deleting', { selfLinks }); From f3e836de4f281ce29af9e6dec876ba396ec19a0c Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Wed, 3 Mar 2021 16:38:20 -0500 Subject: [PATCH 16/19] fixed error with krm being declared twice in deleteremote resources --- lib/remoteResource.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/remoteResource.js b/lib/remoteResource.js index e789b5e..f3b793c 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -125,10 +125,10 @@ const deleteRemoteResources = async (resources) => { const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); log.debug('Deleting', { selfLinks }); await deleteResource(resources, krm); - const krm = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); - const selfLinks = resources.map((resource) => krm.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace })); - log.debug('Deleting', { selfLinks }); - await deleteResource(selfLinks, krm); + const krm_secret = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); + const selfLinks2 = resources.map((resource) => krm_secret.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace, })); + log.debug('Deleting', { selfLinks2 }); + await deleteResource(selfLinks2, krm); }; const deleteResource = async(selfLinks, krm) => { From 3ac290088e5c4fa8076fc354abfe7d7e08c89a4a Mon Sep 17 00:00:00 2001 From: NAOD DERIBE Date: Wed, 3 Mar 2021 16:45:48 -0500 Subject: [PATCH 17/19] updated delete resources updated create Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> Update lib/remoteResource.js Co-authored-by: Alex Lewitt <48691328+alewitt2@users.noreply.github.com> removed --- lib/log.js | 2 +- lib/remoteResource.js | 69 ++++++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/lib/log.js b/lib/log.js index 43c3e0d..574ca42 100644 --- a/lib/log.js +++ b/lib/log.js @@ -20,7 +20,7 @@ const winston = require('winston'); const log = winston.createLogger({ level: (process.env.LOG_LEVEL || 'info'), - format: winston.format.json(), + format: winston.format.prettyPrint(), defaultMeta: { service: 'cluster-subscription' }, transports: [ new winston.transports.Console(), diff --git a/lib/remoteResource.js b/lib/remoteResource.js index 10e940d..726ed78 100644 --- a/lib/remoteResource.js +++ b/lib/remoteResource.js @@ -14,12 +14,15 @@ const requestsTemplate = `{ "options": { "url": "{{{url}}}", "headers": { - "razee-org-key": - "valueFrom": - "secretKeyRef": - "name": "clustersubscription-{{subscriptionUuid}}-secret" - "namespace": {{namespace}} + "razee-org-key": { + "valueFrom": { + "secretKeyRef":{ + "name": "{{{secretName}}}", + "namespace": "{{namespace}}", "key": "razee-api-org-key" + } + } + } } } }`; @@ -28,14 +31,16 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) log.info('create remote resources subscription list', { subscriptions }); try { return Promise.all(subscriptions.map(async sub => { + const apiKeyBase64 = Buffer.from(apiKey).toString('base64'); const url = `${razeeApi}/${sub.url}`; - const rendered = Mustache.render(requestsTemplate, { url: url, orgKey: apiKey, namespace: NAMESPACE }); + const secretName = `clustersubscription-${sub.subscriptionUuid}-secret`; + const rendered = Mustache.render(requestsTemplate, { url: url, secretName: secretName, namespace: NAMESPACE }); const parsed = JSON.parse(rendered); + //console.log(parsed); const remoteResourceName = `clustersubscription-${sub.subscriptionUuid}`; - const secretName = `clustersubscription-${sub.subscriptionUuid}-secret`; const userName = (sub.kubeOwnerName && typeof sub.kubeOwnerName === 'string') ? sub.kubeOwnerName : 'razeedeploy'; const remoteResourceJson = { - 'apiVersion': RP_API_VERSION, + 'apiVersion': RR_API_VERSION, 'kind': 'RemoteResource', 'metadata': { 'namespace': NAMESPACE, @@ -52,11 +57,12 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) 'clusterAuth': { 'impersonateUser': userName }, - 'requests': [] + 'requests': + [] } }; remoteResourceJson.spec.requests.push(parsed); - + const secretJson = { 'apiVersion': 'v1', 'kind': 'Secret', @@ -72,13 +78,13 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) } }, 'data': { - 'razee-api-org-key': apiKey + 'razee-api-org-key': apiKeyBase64 } }; - - await applyResource(remoteResourceJson); await applyResource(secretJson); + await applyResource(remoteResourceJson); + })); } catch (error) { @@ -87,12 +93,13 @@ const createRemoteResources = async (razeeApi, apiKey, subscriptions, clusterId) }; const applyResource = async (resourceJson) => { -const resourceName = objectPath.get(resourceJson, 'metadata.name', ''); -const resourceNamespace = objectPath.get(resourceJson, 'metadata.namespace', NAMESPACE); -const resourceApiVersion = objectPath.get(resourceJson, 'metadata.apiVersion', RP_API_VERSION); -const resourceKind = objectPath.get(resourceJson, 'metadata.kind', ''); + const resourceName = objectPath.get(resourceJson, 'metadata.name', ''); + const resourceNamespace = objectPath.get(resourceJson, 'metadata.namespace', NAMESPACE); + const resourceApiVersion = objectPath.get(resourceJson, 'apiVersion', RR_API_VERSION); + const resourceKind = objectPath.get(resourceJson, 'kind', ''); const opt = { simple: false, resolveWithFullResponse: true }; const krm = await kc.getKubeResourceMeta(resourceApiVersion, resourceKind, 'update'); + const uri = krm.uri({ name: resourceName, namespace: resourceNamespace }); log.debug(resourceName); const get = await krm.get(resourceName, resourceNamespace, opt); @@ -112,38 +119,32 @@ const resourceKind = objectPath.get(resourceJson, 'metadata.kind', ''); if (postResult.statusCode === 200 || postResult.statusCode === 201) { log.info('post successful', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); } else { + console.log(postResult); log.error('post error', { 'statusCode': postResult.statusCode, 'statusMessage': postResult.statusMessage }); } } else { log.error(`Get ${get.statusCode} ${uri}`); } -} +}; const deleteRemoteResources = async (resources) => { const krm = await kc.getKubeResourceMeta(RR_API_VERSION, 'RemoteResource', 'update'); - const selfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); - log.debug('Deleting', { selfLinks }); -<<<<<<< HEAD - await deleteResource(resources, krm); + const rrSelfLinks = resources.map((resource) => krm.uri({ name: resource.metadata.name, namespace: resource.metadata.namespace })); + log.debug('Deleting', { rrSelfLinks }); + await deleteResource(rrSelfLinks, krm); const krm_secret = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); - const selfLinks2 = resources.map((resource) => krm_secret.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace, })); - log.debug('Deleting', { selfLinks2 }); - await deleteResource(selfLinks2, krm); -======= - await deleteResource(selfLinks, krm); - const krm = await kc.getKubeResourceMeta('v1', 'Secret', 'update'); - const selfLinks = resources.map((resource) => krm.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace })); - log.debug('Deleting', { selfLinks }); - await deleteResource(selfLinks, krm); ->>>>>>> c4b7d3beda3019e1a6bfda34ac847ecc18305f16 + const secretSelfLinks = resources.map((resource) => krm_secret.uri({ name: `${resource.metadata.name}-secret`, namespace: resource.metadata.namespace, })); + log.debug('Deleting', { secretSelfLinks }); + await deleteResource(secretSelfLinks, krm_secret); }; -const deleteResource = async(selfLinks, krm) => { +const deleteResource = async (selfLinks, krm) => { try { selfLinks.map(async (selfLink) => { log.info(`Delete ${selfLink}`); const opt = { uri: selfLink, simple: false, resolveWithFullResponse: true, method: 'DELETE' }; + console.log(opt); const res = await krm.request(opt); if (res.statusCode === 404) { log.info(`Delete ${res.statusCode} ${opt.uri}`); @@ -159,7 +160,7 @@ const deleteResource = async(selfLinks, krm) => { log.error(error); } -} +}; const getRemoteResources = async (clusterId) => { log.debug('Getting a list of clustersubscription remote resources on this cluster'); From ebacf4290d92d3af0be1b6fe4662b2a6e7527e6f Mon Sep 17 00:00:00 2001 From: Alexander Lewitt Date: Tue, 16 Mar 2021 17:24:55 -0400 Subject: [PATCH 18/19] Update log.js --- lib/log.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/log.js b/lib/log.js index 574ca42..1485b9a 100644 --- a/lib/log.js +++ b/lib/log.js @@ -19,8 +19,8 @@ const winston = require('winston'); const log = winston.createLogger({ - level: (process.env.LOG_LEVEL || 'info'), - format: winston.format.prettyPrint(), + level: (process.env.LOG_LEVEL || 'info'), + format: winston.format.json(), defaultMeta: { service: 'cluster-subscription' }, transports: [ new winston.transports.Console(), From bce5d2ccb31f514b7b264ba833f5adb7ea749c15 Mon Sep 17 00:00:00 2001 From: Alexander Lewitt Date: Wed, 17 Mar 2021 15:29:12 -0400 Subject: [PATCH 19/19] publish release candidates --- .travis.yml | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2db6850..731426b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,31 +12,41 @@ script: # Audit npm packages. Fail build whan a PR audit fails, otherwise report the vulnerability and proceed. - if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then npm audit; else npm audit || true; fi - npm run lint + - if [[ "${TRAVIS_TAG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$ ]]; then npm version --no-git-tag-version "${TRAVIS_TAG}"; fi - docker build --rm -t "quay.io/razee/clustersubscription:${TRAVIS_COMMIT}" . - if [ -n "${TRAVIS_TAG}" ]; then docker tag quay.io/razee/clustersubscription:${TRAVIS_COMMIT} quay.io/razee/clustersubscription:${TRAVIS_TAG}; fi - docker images - ./build/process-template.sh kubernetes/ClusterSubscription/resource.yaml >/tmp/resource.yaml - - if [[ "${TRAVIS_TAG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then npm version --no-git-tag-version "${TRAVIS_TAG}"; fi before_deploy: - docker login -u="${QUAY_ID}" -p="${QUAY_TOKEN}" quay.io deploy: - # Deploy alpha builds - - provider: script - script: docker push "quay.io/razee/clustersubscription:${TRAVIS_TAG}" + # Publish npm package with tag "next" on release candidates + - provider: npm + email: "${NPMJS_EMAIL}" + api_key: "${NPMJS_API_KEY}" + tag: next skip_cleanup: true on: tags: true - condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+_[0-9]{3}$ - - # Deploy released builds + condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)$ + # Publish docker image on release and release candidates - provider: script script: docker push "quay.io/razee/clustersubscription:${TRAVIS_TAG}" skip_cleanup: true + on: + tags: true + condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$ + # Publish npm package as "latest" on release + - provider: npm + email: "${NPMJS_EMAIL}" + api_key: "${NPMJS_API_KEY}" + skip_cleanup: true on: tags: true condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ + # Publish GitHub release assets on release - provider: releases file: /tmp/resource.yaml skip_cleanup: true @@ -45,10 +55,3 @@ deploy: on: tags: true condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ - - provider: npm - email: "${NPMJS_EMAIL}" - api_key: "${NPMJS_API_KEY}" - skip_cleanup: true - on: - tags: true - condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+$