From 766bb202b089af822d090c93b447e4725461f9e2 Mon Sep 17 00:00:00 2001 From: Sergey Kukurudzyak Date: Tue, 16 May 2023 13:13:33 +0300 Subject: [PATCH] BKNDLSS-32460 Change statefulset to deployment (#92) --- config/config.json | 3 +- src/services/bl-containers.js | 84 ++++++++++++++----- .../install/bl/install-bl-hazelcast.js | 4 +- .../install/bl/install-bl-java-coderunner.js | 4 +- .../install/bl/install-bl-js-coderunner.js | 4 +- .../install/bl/install-bl-rt-server.js | 4 +- src/services/install/bl/install-bl-server.js | 4 +- src/services/install/bl/install-bl-taskman.js | 4 +- .../install/bl/install-bl-web-console.js | 4 +- src/services/install/index.js | 5 +- ...{bl-statefulset-status.js => bl-status.js} | 16 +++- src/services/k8s/config/console.json | 2 +- src/services/k8s/config/hazelcast.json | 2 +- src/services/k8s/config/java-coderunner.json | 2 +- src/services/k8s/config/js-coderunner.json | 2 +- src/services/k8s/config/rt-server.json | 2 +- src/services/k8s/config/server.json | 2 +- src/services/k8s/config/taskman.json | 2 +- src/services/k8s/k8s-delete-service.js | 19 ++++- ...-statefulset-restart.js => k8s-restart.js} | 26 +++++- src/services/k8s/k8s-scale.js | 26 ++++++ src/services/k8s/k8s-statefulset-scale.js | 14 ---- ...8s-statefulset-status.js => k8s-status.js} | 8 +- .../manage/loadbalancer/nginx/index.js | 4 +- src/services/manage/manage-service.js | 9 +- src/services/upgrade/index.js | 2 +- 26 files changed, 183 insertions(+), 75 deletions(-) rename src/services/k8s/{bl-statefulset-status.js => bl-status.js} (67%) rename src/services/k8s/{k8s-statefulset-restart.js => k8s-restart.js} (69%) create mode 100644 src/services/k8s/k8s-scale.js delete mode 100644 src/services/k8s/k8s-statefulset-scale.js rename src/services/k8s/{k8s-statefulset-status.js => k8s-status.js} (57%) diff --git a/config/config.json b/config/config.json index 6a4bc80..6ce01c7 100644 --- a/config/config.json +++ b/config/config.json @@ -14,7 +14,8 @@ "UpgradeService": "verbose", "install-bl-rt-server": "verbose", "install-bl-server": "verbose", - "service-log": "verbose" + "service-log": "verbose", + "k8s-scale": "debug" } }, "user": { diff --git a/src/services/bl-containers.js b/src/services/bl-containers.js index 8b84f38..cb6a79e 100644 --- a/src/services/bl-containers.js +++ b/src/services/bl-containers.js @@ -1,5 +1,5 @@ -import { blStatefulsetStatus } from './k8s/bl-statefulset-status' -import { deleteService } from './k8s/k8s-delete-service' +import { blDeploymentStatus, blStatefulsetStatus } from './k8s/bl-status' +import { deleteDeploymentAndService, deleteStatefulsetAndService } from './k8s/k8s-delete-service' import { blJobStatus } from './k8s/bl-job-status' import { installConsul } from './install/db/install-consul' import { installMysql } from './install/db/mysql' @@ -14,57 +14,73 @@ import { installBlJsCoderunner } from './install/bl/install-bl-js-coderunner' import { installBlWebConsole } from './install/bl/install-bl-web-console' import { installBlRtServer } from './install/bl/install-bl-rt-server' import { deleteJob } from './k8s/k8s-delete-job' +import { deploymentScale, statefulsetScale } from './k8s/k8s-scale' +import { deploymentRestart, statefulsetRestart } from './k8s/k8s-restart' class BlContainers { bl = { server: { name: 'bl-server', imageName: 'bl-server', - serviceStatus: () => blStatefulsetStatus('bl-server'), + serviceStatus: () => blDeploymentStatus('bl-server'), installService: installArguments => installBlServer(installArguments), - deleteService: () => deleteService('bl-server') + deleteService: () => deleteDeploymentAndService('bl-server'), + scale: replicas => deploymentScale('bl-server', replicas), + restart: () => deploymentRestart('bl-server') }, rtServer: { name: 'bl-rt-server', imageName: 'bl-rt-server', - serviceStatus: () => blStatefulsetStatus('bl-rt-server'), + serviceStatus: () => blDeploymentStatus('bl-rt-server'), installService: installArguments => installBlRtServer(installArguments), - deleteService: () => deleteService('bl-rt-server') + deleteService: () => deleteDeploymentAndService('bl-rt-server'), + scale: replicas => deploymentScale('bl-rt-server', replicas), + restart: () => deploymentRestart('bl-rt-server') }, taskman: { name: 'bl-taskman', imageName: 'bl-server', - serviceStatus: () => blStatefulsetStatus('bl-taskman'), + serviceStatus: () => blDeploymentStatus('bl-taskman'), installService: installArguments => installBlTaskman(installArguments), - deleteService: () => deleteService('bl-taskman') + deleteService: () => deleteDeploymentAndService('bl-taskman'), + scale: replicas => deploymentScale('bl-taskman', replicas), + restart: () => deploymentRestart('bl-taskman') }, hazelcast: { name: 'bl-hazelcast-3125', imageName: 'bl-hazelcast', - serviceStatus: () => blStatefulsetStatus('bl-hazelcast-3125'), + serviceStatus: () => blDeploymentStatus('bl-hazelcast-3125'), installService: installArguments => installBlHazelcast(installArguments), - deleteService: () => deleteService('bl-hazelcast-3125') + deleteService: () => deleteDeploymentAndService('bl-hazelcast-3125'), + scale: replicas => deploymentScale('bl-hazelcast-3125', replicas), + restart: () => deploymentRestart('bl-hazelcast-3125') }, javaCoderunner: { name: 'bl-coderunner-java', imageName: 'bl-coderunner-java', - serviceStatus: () => blStatefulsetStatus('bl-coderunner-java'), + serviceStatus: () => blDeploymentStatus('bl-coderunner-java'), installService: installArguments => installBlJavaCoderunner(installArguments), - deleteService: () => deleteService('bl-coderunner-java') + deleteService: () => deleteDeploymentAndService('bl-coderunner-java'), + scale: replicas => deploymentScale('bl-coderunner-java', replicas), + restart: () => deploymentRestart('bl-coderunner-java') }, jsCoderunner: { name: 'bl-coderunner-js', imageName: 'bl-coderunner-js', - serviceStatus: () => blStatefulsetStatus('bl-coderunner-js'), + serviceStatus: () => blDeploymentStatus('bl-coderunner-js'), installService: installArguments => installBlJsCoderunner(installArguments), - deleteService: () => deleteService('bl-coderunner-js') + deleteService: () => deleteDeploymentAndService('bl-coderunner-js'), + scale: replicas => deploymentScale('bl-coderunner-js', replicas), + restart: () => deploymentRestart('bl-coderunner-js') }, console: { name: 'bl-web-console', imageName: 'bl-web-console', - serviceStatus: () => blStatefulsetStatus('bl-web-console'), + serviceStatus: () => blDeploymentStatus('bl-web-console'), installService: installArguments => installBlWebConsole(installArguments), - deleteService: () => deleteService('bl-web-console') + deleteService: () => deleteDeploymentAndService('bl-web-console'), + scale: replicas => deploymentScale('bl-web-console', replicas), + restart: () => deploymentRestart('bl-web-console') }, initConfigValues: { name: 'bl-init-config-values', @@ -79,15 +95,19 @@ class BlContainers { name: 'bl-consul', serviceStatus: () => blStatefulsetStatus('bl-consul'), installService: installArguments => installConsul(installArguments), + scale: replicas => statefulsetScale('bl-consul', replicas), + restart: () => statefulsetRestart('bl-consul'), order: 0, - deleteService: () => deleteService('bl-consul') + deleteService: () => deleteStatefulsetAndService('bl-consul') }, mysql: { name: 'bl-mysql', serviceStatus: () => blStatefulsetStatus('bl-mysql'), installService: installArguments => installMysql(installArguments), + scale: replicas => statefulsetScale('bl-mysql', replicas), + restart: () => statefulsetRestart('bl-mysql'), order: 1, - deleteService: () => deleteService('bl-mysql') + deleteService: () => deleteStatefulsetAndService('bl-mysql') }, redis: { name: 'bl-redis', @@ -98,8 +118,10 @@ class BlContainers { externalPort: 32379, name: 'bl-redis' }), + scale: replicas => statefulsetScale('bl-redis', replicas), + restart: () => statefulsetRestart('bl-redis'), order: 2, - deleteService: () => deleteService('bl-redis') + deleteService: () => deleteStatefulsetAndService('bl-redis') }, debugRedis: { name: 'bl-redis-debug', @@ -110,15 +132,19 @@ class BlContainers { externalPort: 32380, name: 'bl-redis-debug' }), + scale: replicas => statefulsetScale('bl-redis-debug', replicas), + restart: () => statefulsetRestart('bl-redis-debug'), order: 3, - deleteService: () => deleteService('bl-redis-debug') + deleteService: () => deleteStatefulsetAndService('bl-redis-debug') }, mongo: { name: 'bl-mongo', serviceStatus: () => blStatefulsetStatus('bl-mongo'), installService: installArguments => installMongo(installArguments), + scale: replicas => statefulsetScale('bl-mongo', replicas), + restart: () => statefulsetRestart('bl-mongo'), order: 4, - deleteService: () => deleteService('bl-mongo') + deleteService: () => deleteStatefulsetAndService('bl-mongo') }, } @@ -128,6 +154,22 @@ class BlContainers { .sort(([key1, dependency1], [key2, dependency2]) => dependency1.order - dependency2.order) .map(([key, dependency]) => dependency) } + + findByName(name) { + for (const containerKey in this.bl) { + const container = this.bl[containerKey] + if (container.name === name) { + return container + } + } + + for (const containerKey in this.dependencies) { + const container = this.dependencies[containerKey] + if (container.name === name) { + return container + } + } + } } export const blContainers = new BlContainers() \ No newline at end of file diff --git a/src/services/install/bl/install-bl-hazelcast.js b/src/services/install/bl/install-bl-hazelcast.js index 19da351..2b248e8 100644 --- a/src/services/install/bl/install-bl-hazelcast.js +++ b/src/services/install/bl/install-bl-hazelcast.js @@ -61,8 +61,8 @@ export async function installBlHazelcast({ version }) { workload.spec.template.spec.containers[0].image = `backendless/bl-hazelcast:${version}` - installStatus.info('creating statefulset for bl-hazelcast') - const createStatefulSetResult = await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + installStatus.info('creating deployment for bl-hazelcast') + const createStatefulSetResult = await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) installStatus.info('creating service for bl-hazelcast') const createServiceResult = await k8sCoreV1Api.createNamespacedService(await k8sConfig.getNamespace(), blK8sConfig.service) diff --git a/src/services/install/bl/install-bl-java-coderunner.js b/src/services/install/bl/install-bl-java-coderunner.js index 2bd2c7b..3752fd1 100644 --- a/src/services/install/bl/install-bl-java-coderunner.js +++ b/src/services/install/bl/install-bl-java-coderunner.js @@ -25,6 +25,6 @@ export async function installBlJavaCoderunner({ mountPath, version }) { name: 'logs' }) - installStatus.info('creating statefulset for bl-coderunner-java') - return await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + installStatus.info('creating deployment for bl-coderunner-java') + return await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) } diff --git a/src/services/install/bl/install-bl-js-coderunner.js b/src/services/install/bl/install-bl-js-coderunner.js index afbf039..fc6c315 100644 --- a/src/services/install/bl/install-bl-js-coderunner.js +++ b/src/services/install/bl/install-bl-js-coderunner.js @@ -25,6 +25,6 @@ export async function installBlJsCoderunner({ mountPath, version }) { name: 'logs' }) - installStatus.info('creating statefulset for bl-coderunner-js') - return await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + installStatus.info('creating deployment for bl-coderunner-js') + return await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) } diff --git a/src/services/install/bl/install-bl-rt-server.js b/src/services/install/bl/install-bl-rt-server.js index a8cf05f..e880c36 100644 --- a/src/services/install/bl/install-bl-rt-server.js +++ b/src/services/install/bl/install-bl-rt-server.js @@ -28,9 +28,9 @@ export async function installBlRtServer({ mountPath, version }) { name: 'logs' }) - installStatus.info('creating statefulset for bl-rt-server') + installStatus.info('creating deployment for bl-rt-server') logger.verbose(`creating workload for bl-rt-server with config: ${JSON.stringify(workload)}`) - const createStatefulSetResult = await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + const createStatefulSetResult = await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) installStatus.info('creating service for bl-rt-server') const createServiceResult = await k8sCoreV1Api.createNamespacedService(await k8sConfig.getNamespace(), blK8sConfig.service) diff --git a/src/services/install/bl/install-bl-server.js b/src/services/install/bl/install-bl-server.js index d0e9783..ee76b04 100644 --- a/src/services/install/bl/install-bl-server.js +++ b/src/services/install/bl/install-bl-server.js @@ -30,9 +30,9 @@ export async function installBlServer({ mountPath, version }) { name: 'logs' }) - installStatus.info('creating statefulset for bl-server') + installStatus.info('creating deployment for bl-server') logger.verbose(`creating workload for bl-server with config: ${JSON.stringify(workload)}`) - const createStatefulSetResult = await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + const createStatefulSetResult = await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) installStatus.info('creating service for bl-server') const createServiceResult = await k8sCoreV1Api.createNamespacedService(await k8sConfig.getNamespace(), blServerK8sConfig.service) diff --git a/src/services/install/bl/install-bl-taskman.js b/src/services/install/bl/install-bl-taskman.js index e6e2fb5..8a974a6 100644 --- a/src/services/install/bl/install-bl-taskman.js +++ b/src/services/install/bl/install-bl-taskman.js @@ -25,8 +25,8 @@ export async function installBlTaskman({ mountPath, version }) { name: 'logs' }) - installStatus.info('creating statefulset for bl-taskman') - const createStatefulSetResult = await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + installStatus.info('creating deployment for bl-taskman') + const createStatefulSetResult = await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) installStatus.info('creating service for bl-taskman') const createServiceResult = await k8sCoreV1Api.createNamespacedService(await k8sConfig.getNamespace(), blK8sConfig.service) diff --git a/src/services/install/bl/install-bl-web-console.js b/src/services/install/bl/install-bl-web-console.js index c0c8a97..309e902 100644 --- a/src/services/install/bl/install-bl-web-console.js +++ b/src/services/install/bl/install-bl-web-console.js @@ -27,8 +27,8 @@ export async function installBlWebConsole({ mountPath, version }) { name: 'logs' }) - installStatus.info('creating statefulset for bl-web-console') - const createStatefulSetResult = await k8sAppsV1Api.createNamespacedStatefulSet(await k8sConfig.getNamespace(), workload) + installStatus.info('creating deployment for bl-web-console') + const createStatefulSetResult = await k8sAppsV1Api.createNamespacedDeployment(await k8sConfig.getNamespace(), workload) installStatus.info('creating service for bl-web-console') const createServiceResult = await k8sCoreV1Api.createNamespacedService(await k8sConfig.getNamespace(), blK8sConfig.service) diff --git a/src/services/install/index.js b/src/services/install/index.js index 159529a..edb56c6 100644 --- a/src/services/install/index.js +++ b/src/services/install/index.js @@ -6,7 +6,7 @@ import { initConfigMap } from './init-config-map' import { Logger } from '../../logger' import { isWin } from '../../utils/os' import * as fs from 'fs' -import { deleteService } from '../k8s/k8s-delete-service' +import { deleteStatefulsetAndService } from '../k8s/k8s-delete-service' import { ApiError } from '../../error' import { mountPathConfig } from '../../config/mount-path-config' import { registerFirstUser } from './register-first-user' @@ -168,7 +168,8 @@ class InstallService { } async deleteService(serviceName) { - return deleteService(serviceName) + const container = blContainers.findByName( serviceName ) + return container.deleteService() } /** diff --git a/src/services/k8s/bl-statefulset-status.js b/src/services/k8s/bl-status.js similarity index 67% rename from src/services/k8s/bl-statefulset-status.js rename to src/services/k8s/bl-status.js index 1f5ba61..19bbe87 100644 --- a/src/services/k8s/bl-statefulset-status.js +++ b/src/services/k8s/bl-status.js @@ -1,15 +1,23 @@ import { Logger } from '../../logger' -import { statefulsetStatus } from './k8s-statefulset-status' +import { deploymentStatus, statefulsetStatus } from './k8s-status' import { getVersionFromStatus } from './bl-get-version' const Status = require('../service-status.json') -const logger = Logger('bl-statefulset-status') +const logger = Logger('bl-status') export async function blStatefulsetStatus(containerName) { + return blStatus(containerName, () => statefulsetStatus(containerName, true)) +} + +export async function blDeploymentStatus(containerName) { + return blStatus(containerName, () => deploymentStatus(containerName, true)) +} + +async function blStatus(containerName, getK8sStatus) { const blStatus = { name: containerName } let status try { - status = await statefulsetStatus(containerName, true) + status = await getK8sStatus() } catch (err) { if (err.statusCode === 404) { blStatus.status = Status.notInstalled @@ -25,7 +33,7 @@ export async function blStatefulsetStatus(containerName) { logger.verbose(`service status for '${containerName}' is ${JSON.stringify(serviceStatus)}`) - if (serviceStatus.replicas < 1) + if (!serviceStatus.replicas) blStatus.status = Status.stopped else if (serviceStatus.readyReplicas !== serviceStatus.replicas || serviceStatus.updatedReplicas !== serviceStatus.readyReplicas) blStatus.status = Status.changing diff --git a/src/services/k8s/config/console.json b/src/services/k8s/config/console.json index a30ec2e..721f76a 100644 --- a/src/services/k8s/config/console.json +++ b/src/services/k8s/config/console.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "name": "bl-web-console" }, diff --git a/src/services/k8s/config/hazelcast.json b/src/services/k8s/config/hazelcast.json index b0d03c5..5676e5f 100644 --- a/src/services/k8s/config/hazelcast.json +++ b/src/services/k8s/config/hazelcast.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "name": "bl-hazelcast-3125" }, diff --git a/src/services/k8s/config/java-coderunner.json b/src/services/k8s/config/java-coderunner.json index 6149a10..0e008ed 100644 --- a/src/services/k8s/config/java-coderunner.json +++ b/src/services/k8s/config/java-coderunner.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "annotations": { "name": "bl-coderunner-java" diff --git a/src/services/k8s/config/js-coderunner.json b/src/services/k8s/config/js-coderunner.json index ceaa76f..db6f08f 100644 --- a/src/services/k8s/config/js-coderunner.json +++ b/src/services/k8s/config/js-coderunner.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "annotations": { "name": "bl-coderunner-js" diff --git a/src/services/k8s/config/rt-server.json b/src/services/k8s/config/rt-server.json index 95aa05e..739d8de 100644 --- a/src/services/k8s/config/rt-server.json +++ b/src/services/k8s/config/rt-server.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "annotations": { "name": "bl-rt-server" diff --git a/src/services/k8s/config/server.json b/src/services/k8s/config/server.json index 0efa918..4433153 100644 --- a/src/services/k8s/config/server.json +++ b/src/services/k8s/config/server.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "annotations": { "name": "bl-server" diff --git a/src/services/k8s/config/taskman.json b/src/services/k8s/config/taskman.json index 2cd804d..4fe3683 100644 --- a/src/services/k8s/config/taskman.json +++ b/src/services/k8s/config/taskman.json @@ -1,7 +1,7 @@ { "workload": { "apiVersion": "apps/v1", - "kind": "StatefulSet", + "kind": "Deployment", "metadata": { "annotations": { "name": "bl-taskman" diff --git a/src/services/k8s/k8s-delete-service.js b/src/services/k8s/k8s-delete-service.js index 505f945..e13dc3d 100644 --- a/src/services/k8s/k8s-delete-service.js +++ b/src/services/k8s/k8s-delete-service.js @@ -4,12 +4,27 @@ import { k8sConfig } from '../../config/k8s-config' const logger = Logger('k8s-delete-service') -export async function deleteService(name) { - logger.info(`removing ${name}...`) +export async function deleteStatefulsetAndService(name) { + logger.info(`removing statefulset ${name}...`) try { const removeStatefulSetResult = await k8sAppsV1Api.deleteNamespacedStatefulSet(name, await k8sConfig.getNamespace()) + logger.debug(`remove statefulset [${name}] result [${JSON.stringify(removeStatefulSetResult)}]`) logger.info(`removing service ${name}...`) const removeServiceResult = await k8sCoreV1Api.deleteNamespacedService(name, await k8sConfig.getNamespace()) + logger.debug(`remove service [${name}] result [${JSON.stringify(removeServiceResult)}]`) + } catch (e) { + logger.error(e) + } +} + +export async function deleteDeploymentAndService(name) { + logger.info(`removing deployment ${name}...`) + try { + const removeStatefulSetResult = await k8sAppsV1Api.deleteNamespacedDeployment(name, await k8sConfig.getNamespace()) + logger.debug(`remove deployment [${name}] result [${JSON.stringify(removeStatefulSetResult)}]`) + logger.info(`removing service ${name}...`) + const removeServiceResult = await k8sCoreV1Api.deleteNamespacedService(name, await k8sConfig.getNamespace()) + logger.debug(`remove service [${name}] result [${JSON.stringify(removeServiceResult)}]`) } catch (e) { logger.error(e) } diff --git a/src/services/k8s/k8s-statefulset-restart.js b/src/services/k8s/k8s-restart.js similarity index 69% rename from src/services/k8s/k8s-statefulset-restart.js rename to src/services/k8s/k8s-restart.js index 0c535b5..925df80 100644 --- a/src/services/k8s/k8s-statefulset-restart.js +++ b/src/services/k8s/k8s-restart.js @@ -3,9 +3,10 @@ const k8s = require('@kubernetes/client-node') import { k8sAppsV1Api } from './k8s' import { Logger } from '../../logger' import { k8sConfig } from '../../config/k8s-config' -const logger = Logger('k8s-statefulset-restart') +const logger = Logger('k8s-restart') export async function statefulsetRestart(name, resultAsIS = false) { + logger.info(`Restarting statefulset: [${name}]` ) // const statefulSet = (await k8sAppsV1Api.readNamespacedStatefulSetScale(name, await k8sConfig.getNamespace())).body // // statefulSet.spec.replicas = replicas // @@ -49,4 +50,27 @@ export async function statefulsetRestart(name, resultAsIS = false) { return resultAsIS ? res : res.body.status }) +} + +export async function deploymentRestart(name, resultAsIS = false) { + logger.info(`Restarting deployment: [${name}]` ) + const options = { 'headers': { 'Content-type': k8s.PatchUtils.PATCH_FORMAT_STRATEGIC_MERGE_PATCH } } + + const body = + { + 'spec': { + 'template': { + 'metadata': { + 'annotations': + { 'kubectl.kubernetes.io/restartedAt': new Date().toISOString() } + } + } + } + } + + return k8sAppsV1Api.patchNamespacedDeployment(name, await k8sConfig.getNamespace(), body, undefined, undefined, 'kubectl-rollout', undefined, options) + .then(res => { + return resultAsIS ? res : res.body.status + }) + } \ No newline at end of file diff --git a/src/services/k8s/k8s-scale.js b/src/services/k8s/k8s-scale.js new file mode 100644 index 0000000..8490d93 --- /dev/null +++ b/src/services/k8s/k8s-scale.js @@ -0,0 +1,26 @@ +import { k8sAppsV1Api } from './k8s' +import { Logger } from '../../logger' +import { k8sConfig } from '../../config/k8s-config' +const logger = Logger('k8s-scale') + +export async function statefulsetScale(name, replicas, resultAsIS = false) { + logger.info(`Scaling statefulset [${name}] to [${replicas}] replicas`) + const statefulSet = (await k8sAppsV1Api.readNamespacedStatefulSetScale(name, await k8sConfig.getNamespace())).body + logger.debug(`statefulset found [${JSON.stringify(statefulSet)}]`) + statefulSet.spec.replicas = replicas + + const result = await k8sAppsV1Api.replaceNamespacedStatefulSetScale(name, await k8sConfig.getNamespace(), statefulSet) + logger.debug( `result of scale statefulSet is [${result}]`) + + return resultAsIS ? result : result.body.status +} + +export async function deploymentScale(name, replicas, resultAsIS = false) { + logger.info(`Scaling deployment [${name}] to [${replicas}] replicas`) + const deployment = (await k8sAppsV1Api.readNamespacedDeploymentScale(name, await k8sConfig.getNamespace())).body + logger.debug(`deployment found [${JSON.stringify(deployment)}]`) + deployment.spec.replicas = replicas + + const result = await k8sAppsV1Api.replaceNamespacedDeploymentScale(name, await k8sConfig.getNamespace(), deployment) + return resultAsIS ? result : result.body.status +} \ No newline at end of file diff --git a/src/services/k8s/k8s-statefulset-scale.js b/src/services/k8s/k8s-statefulset-scale.js deleted file mode 100644 index 7f6f7aa..0000000 --- a/src/services/k8s/k8s-statefulset-scale.js +++ /dev/null @@ -1,14 +0,0 @@ -import { k8sAppsV1Api } from './k8s' -import { Logger } from '../../logger' -import { k8sConfig } from '../../config/k8s-config' -const logger = Logger('k8s-statefulset-scale') - -export async function statefulsetScale(name, replicas, resultAsIS = false) { - const statefulSet = (await k8sAppsV1Api.readNamespacedStatefulSetScale(name, await k8sConfig.getNamespace())).body - statefulSet.spec.replicas = replicas - - return k8sAppsV1Api.replaceNamespacedStatefulSetScale(name, await k8sConfig.getNamespace(), statefulSet) - .then(res => { - return resultAsIS ? res : res.body.status - }) -} \ No newline at end of file diff --git a/src/services/k8s/k8s-statefulset-status.js b/src/services/k8s/k8s-status.js similarity index 57% rename from src/services/k8s/k8s-statefulset-status.js rename to src/services/k8s/k8s-status.js index 58e5c6a..ec41fc2 100644 --- a/src/services/k8s/k8s-statefulset-status.js +++ b/src/services/k8s/k8s-status.js @@ -1,12 +1,18 @@ import { k8sAppsV1Api } from './k8s' import { Logger } from '../../logger' import { k8sConfig } from '../../config/k8s-config' -const logger = Logger('k8s-statefulset-status') +const logger = Logger('k8s-status') export async function statefulsetStatus(name, resultAsIS = false) { return k8sAppsV1Api.readNamespacedStatefulSetStatus( name, await k8sConfig.getNamespace()) .then(res => { return resultAsIS ? res : res.body.status }) +} +export async function deploymentStatus(name, resultAsIS = false) { + return k8sAppsV1Api.readNamespacedDeploymentStatus( name, await k8sConfig.getNamespace()) + .then(res => { + return resultAsIS ? res : res.body.status + }) } \ No newline at end of file diff --git a/src/services/manage/loadbalancer/nginx/index.js b/src/services/manage/loadbalancer/nginx/index.js index 6456e25..f21576b 100644 --- a/src/services/manage/loadbalancer/nginx/index.js +++ b/src/services/manage/loadbalancer/nginx/index.js @@ -3,7 +3,7 @@ import blK8sConfig from '../../../k8s/config/nginx.json' import { k8sAppsV1Api, k8sCoreV1Api } from '../../../k8s/k8s' import { k8sConfig } from '../../../../config/k8s-config' import { Logger } from '../../../../logger' -import { deleteService } from '../../../k8s/k8s-delete-service' +import { deleteStatefulsetAndService } from '../../../k8s/k8s-delete-service' const logger = Logger('nginx-load-balancer') @@ -27,7 +27,7 @@ class NginxLoadBalancerService{ } async delete(){ - await deleteService('bl-nginx') + await deleteStatefulsetAndService('bl-nginx') } } diff --git a/src/services/manage/manage-service.js b/src/services/manage/manage-service.js index 2a6b430..e3ccd11 100644 --- a/src/services/manage/manage-service.js +++ b/src/services/manage/manage-service.js @@ -1,7 +1,5 @@ import { blContainers } from '../bl-containers' import States from '../service-states.json' -import { statefulsetRestart } from '../k8s/k8s-statefulset-restart' -import { statefulsetScale } from '../k8s/k8s-statefulset-scale' import { Logger } from '../../logger' import { listPods } from '../k8s/k8s-list-pods' @@ -10,15 +8,16 @@ const logger = Logger('manage-service') export class ManageService { async changeState({ serviceName, state }) { + const container = blContainers.findByName(serviceName) switch (state) { case States.restart: - await statefulsetRestart(serviceName) + await container.restart() break case States.stop: - await statefulsetScale(serviceName, 0) + await container.scale(0) break case States.start: - await statefulsetScale(serviceName, 1) + await container.scale(1) break } } diff --git a/src/services/upgrade/index.js b/src/services/upgrade/index.js index 3738da3..2884c23 100644 --- a/src/services/upgrade/index.js +++ b/src/services/upgrade/index.js @@ -64,7 +64,7 @@ class UpgradeService { } } const options = { 'headers': { 'Content-type': k8s.PatchUtils.PATCH_FORMAT_STRATEGIC_MERGE_PATCH } } - return await k8sAppsV1Api.patchNamespacedStatefulSet(serviceName, await k8sConfig.getNamespace(), body, + return await k8sAppsV1Api.patchNamespacedDeployment(serviceName, await k8sConfig.getNamespace(), body, undefined, undefined, undefined, undefined, options) }