Skip to content

Commit

Permalink
feat: Add flag to disable cleanups (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarek-kindred authored Feb 28, 2024
1 parent 2180978 commit 3f315b6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
1 change: 1 addition & 0 deletions k8s-deployer/scripts/start-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ LAUNCH_ARGS="$PROJECT_ROOT/dist/src/index.js \
--lock-manager-mock $LOCK_MANAGER_MOCK \
--use-kube-proxy $USE_KUBE_PROXY \
--cluster-url $CLUSTER_URL \
--enable-cleanups false \
--lock-manager-api-retries $LOCK_MANAGER_API_RETRIES"

if [ "${USER_NAME}" != "" ];
Expand Down
11 changes: 10 additions & 1 deletion k8s-deployer/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Config,
DEFAULT_CLUSTER_URL,
DEFAULT_DEPLOY_CHECK_FREQUENCY,
DEFAULT_NAMESPACE_TIMEOUT,
DEFAULT_SUB_NAMESPACE_GENERATOR_TYPE,
DEFAULT_SUB_NAMESPACE_PREFIX,
DEFAULT_TEST_STATUS_POLL_FREQUENCY,
DEFAULT_TEST_TIMEOUT,
SUB_NAMESPACE_GENERATOR_TYPE_COMMITSHA,
SUB_NAMESPACE_GENERATOR_TYPE_DATE,
TestReportConfig } from "./config.js"
Expand All @@ -25,6 +28,7 @@ export const PARAM_CLUSTER_URL = "--cluster-url"
export const PARAM_LOCK_MANAGER_MOCK = "--lock-manager-mock"
export const PARAM_USE_KUBE_PROXY = "--use-kube-proxy"
export const PARAM_LOCK_MANAGER_API_RETRIES = "--lock-manager-api-retries"
export const PARAM_ENABLE_CLEANUPS = "--enable-cleanups"

const readParams = (): Config => {
logger.debug("readParams()... \n%s", JSON.stringify(process.argv, null, 2))
Expand Down Expand Up @@ -82,6 +86,7 @@ const readParams = (): Config => {
}

const useKubeProxy = !params.has(PARAM_USE_KUBE_PROXY) ? true : params.get(PARAM_USE_KUBE_PROXY) === "true"
const enableCleanups = !params.has(PARAM_ENABLE_CLEANUPS) ? true : params.get(PARAM_ENABLE_CLEANUPS) === "true"
return new Config(
commitSha,
workspace,
Expand All @@ -100,7 +105,11 @@ const readParams = (): Config => {
params,
useKubeProxy,
useMockLockManager,
lockManagerApiRetries
lockManagerApiRetries,
DEFAULT_TEST_STATUS_POLL_FREQUENCY,
DEFAULT_DEPLOY_CHECK_FREQUENCY,
DEFAULT_TEST_TIMEOUT,
enableCleanups
)
}

Expand Down
10 changes: 7 additions & 3 deletions k8s-deployer/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export const DEFAULT_SUB_NAMESPACE_PREFIX = "pit"
export const SUB_NAMESPACE_GENERATOR_TYPE_COMMITSHA = "COMMITSHA"
export const SUB_NAMESPACE_GENERATOR_TYPE_DATE = "DATE"
export const DEFAULT_SUB_NAMESPACE_GENERATOR_TYPE = SUB_NAMESPACE_GENERATOR_TYPE_COMMITSHA
export const DEFAULT_TEST_STATUS_POLL_FREQUENCY = 15_000
export const DEFAULT_DEPLOY_CHECK_FREQUENCY = 5_000
export const DEFAULT_TEST_TIMEOUT = 60_000

export class TestReportConfig {
constructor(
Expand All @@ -30,8 +33,9 @@ export class Config {
readonly servicesAreExposedViaProxy: boolean = true,
readonly useMockLockManager: boolean = false,
readonly lockManagerApiRetries: number = 3,
readonly testStatusPollFrequencyMs: number = 15_000,
readonly deployCheckFrequencyMs: number = 5_000,
readonly testTimeoutMs: number = 60_000,
readonly testStatusPollFrequencyMs: number = DEFAULT_TEST_STATUS_POLL_FREQUENCY,
readonly deployCheckFrequencyMs: number = DEFAULT_DEPLOY_CHECK_FREQUENCY,
readonly testTimeoutMs: number = DEFAULT_TEST_TIMEOUT,
readonly enableCleanups: boolean = true,
) {}
}
9 changes: 7 additions & 2 deletions k8s-deployer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ const main = async () => {
logger.info("")
logger.info("--------------------- Cleaning up --------------------- ")
logger.info("")
for (let deployments of artefacts) {
await SuiteHandler.undeployAll(config, file, deployments)
if (config.enableCleanups) {
for (let deployments of artefacts) {
await SuiteHandler.undeployAll(config, file, deployments)
}
} else {
logger.info("The cleanups are intentionally disabled.")
}

logger.info("")
logger.info("DONE")
}

Expand Down
5 changes: 5 additions & 0 deletions k8s-deployer/src/test-suite-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const deployGraph = async (config: Config, workspace: string, testSuiteId: strin
const deployments: Array<DeployedComponent> = new Array()
for (let i = 0; i < graph.components.length; i++) {
const componentSpec = graph.components[i]
logger.info("")
logger.info("Deploying graph component (%s of %s) \"%s\"...", i + 1, graph.components.length, componentSpec.name)
logger.info("")
const commitSha = await Deployer.deployComponent(config, workspace, componentSpec, namespace)
Expand Down Expand Up @@ -53,7 +54,9 @@ const downloadPitFile = async (testSuite: Schema.TestSuite, destination: string)
}

const createWorkspace = async (path: string) => {
logger.info("")
logger.info("Creating workspace '%s'", path)
logger.info("")
let directoryCreated = false
try {
await fs.promises.access(path, fs.constants.W_OK)
Expand Down Expand Up @@ -112,6 +115,7 @@ const deployLocal = async (
logger.info("NAMEPSACE IN USE=%s, process.env.MOCK_NS=%s", ns, process.env.MOCK_NS)

await deployLockManager(config, workspace, pitfile.lockManager.enabled, ns)
logger.info("")

const deployedGraph = await deployGraph(config, workspace, testSuite.id, testSuite.deployment.graph, ns, testAppDirForRemoteTestSuite)

Expand All @@ -129,6 +133,7 @@ const deployRemote = async (
const destination = `${ workspace }/remotesuite_${ testSuite.id }`
fs.mkdirSync(destination, { recursive: true })

logger.info("Downloading remote pitfile for '%s'", testSuite.id)
const remotePitFile = await downloadPitFile(testSuite, destination)

// Extract test suites from remote file where IDs are matching definition of local ones
Expand Down
10 changes: 9 additions & 1 deletion k8s-deployer/test/bootstrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
PARAM_REPORT_REPOSITORY,
PARAM_REPORT_USER_EMAIL,
PARAM_REPORT_USER_NAME,
PARAM_ENABLE_CLEANUPS,
readParams,
} from "../src/bootstrap.js"
import {
Expand Down Expand Up @@ -44,7 +45,8 @@ describe("bootstrap with correct configs", () => {
PARAM_REPORT_BRANCH_NAME, "service-branch",
PARAM_REPORT_REPOSITORY, "http://some-host.name/service.git",
PARAM_REPORT_USER_EMAIL, "some-user@some-host.name",
PARAM_REPORT_USER_NAME, "some-user"
PARAM_REPORT_USER_NAME, "some-user",
PARAM_ENABLE_CLEANUPS, "false"
])
})

Expand All @@ -63,6 +65,7 @@ describe("bootstrap with correct configs", () => {
chai.expect(config.report.gitRepository).be.eq("http://some-host.name/service.git")
chai.expect(config.report.gitUserEmail).be.eq("some-user@some-host.name")
chai.expect(config.report.gitUserName).be.eq("some-user")
chai.expect(config.enableCleanups).be.false
})

afterEach(() => {
Expand Down Expand Up @@ -124,6 +127,11 @@ describe("bootstrap with invalid configs", () => {
chai.expect(config.servicesAreExposedViaProxy).be.true
chai.expect(config.useMockLockManager).be.false
chai.expect(config.lockManagerApiRetries).be.eq(3)
chai.expect(config.report.gitRepository).be.undefined
chai.expect(config.report.branchName).be.undefined
chai.expect(config.report.gitUserName).be.undefined
chai.expect(config.report.gitUserEmail).be.undefined
chai.expect(config.enableCleanups).be.true
sandbox.restore()
})

Expand Down
41 changes: 19 additions & 22 deletions k8s-deployer/test/test-suite-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ describe("Deployment happy path", async () => {
testTimeoutMs: 2_000,
deployCheckFrequencyMs: 500,
params: new Map(),
useMockLockManager: true,
useMockLockManager: false,
servicesAreExposedViaProxy: false,
lockManagerApiRetries: 3
lockManagerApiRetries: 3,
enableCleanups: true,
}

const testSuiteNumber = "1"
Expand Down Expand Up @@ -161,19 +162,17 @@ describe("Deployment happy path", async () => {

// check verification for expected directories and shell executables

chai.expect(fsAccessStubs.callCount).eq(5)

// restore line below once lock manager is stable
//chai.expect(fsAccessStubs.callCount).eq(7)
chai.expect(fsAccessStubs.callCount).eq(7)

// check for presense of workspace direectory on the disk
chai.expect(fsAccessStubs.getCall(0).calledWith(workspace)).be.true
chai.expect(fsAccessStubs.getCall(1).calledWith("lock-manager/deployment/pit/deploy.sh")).be.false
chai.expect(fsAccessStubs.getCall(2).calledWith("lock-manager/deployment/pit/is-deployment-ready.sh")).be.false
chai.expect(fsAccessStubs.getCall(1).calledWith("comp-1/deployment/pit/deploy.sh")).be.true
chai.expect(fsAccessStubs.getCall(2).calledWith("comp-1/deployment/pit/is-deployment-ready.sh")).be.true
chai.expect(fsAccessStubs.getCall(3).calledWith("comp-1-test-app/deployment/pit/deploy.sh")).be.true
chai.expect(fsAccessStubs.getCall(4).calledWith("comp-1-test-app/deployment/pit/is-deployment-ready.sh")).be.true
chai.expect(fsAccessStubs.getCall(1).calledWith("lock-manager/deployment/pit/deploy.sh")).be.true
chai.expect(fsAccessStubs.getCall(2).calledWith("lock-manager/deployment/pit/is-deployment-ready.sh")).be.true
chai.expect(fsAccessStubs.getCall(3).calledWith("comp-1/deployment/pit/deploy.sh")).be.true
chai.expect(fsAccessStubs.getCall(4).calledWith("comp-1/deployment/pit/is-deployment-ready.sh")).be.true
chai.expect(fsAccessStubs.getCall(5).calledWith("comp-1-test-app/deployment/pit/deploy.sh")).be.true
chai.expect(fsAccessStubs.getCall(6).calledWith("comp-1-test-app/deployment/pit/is-deployment-ready.sh")).be.true

// check shell calls

Expand All @@ -185,9 +184,7 @@ describe("Deployment happy path", async () => {
// chai.expect(execStub.getCall(9).args[0]).eq(`deployment/pit/deploy.sh nsChild t1`)
// chai.expect(execStub.getCall(9).args[1]).eq(param2)

// restore line below once lock manager is stable
//chai.expect(execStub.callCount).eq(11)
chai.expect(execStub.callCount).eq(9)
chai.expect(execStub.callCount).eq(11)
chai.expect(execStub.getCall(0).calledWith(`mkdir -p 12345_t1/logs`)).be.true
chai.expect(execStub.getCall(1).calledWith(`mkdir -p 12345_t1/reports`)).be.true

Expand All @@ -199,32 +196,32 @@ describe("Deployment happy path", async () => {
chai.expect(execStub.getCall(3).calledWith(
`deployment/pit/deploy.sh nsChild lock-manager`,
{ homeDir: "lock-manager", logFileName: `12345_t1/logs/deploy-nsChild-lock-manager.log`, tailTarget: sinon.match.any })
).be.false
).be.true

chai.expect(execStub.getCall(4).calledWith(
`deployment/pit/is-deployment-ready.sh nsChild`,
{ homeDir: "lock-manager" })
).be.false
).be.true

chai.expect(execStub.getCall(3).calledWith(`cd comp-1 && git log --pretty=format:"%h" -1`)).be.true
chai.expect(execStub.getCall(5).calledWith(`cd comp-1 && git log --pretty=format:"%h" -1`)).be.true

chai.expect(execStub.getCall(4).calledWith(
chai.expect(execStub.getCall(6).calledWith(
`deployment/pit/deploy.sh nsChild`,
{ homeDir: "comp-1", logFileName: `12345_t1/logs/deploy-nsChild-comp-1.log`, tailTarget: sinon.match.any })
).be.true

chai.expect(execStub.getCall(5).calledWith(
chai.expect(execStub.getCall(7).calledWith(
`deployment/pit/is-deployment-ready.sh nsChild`,
{ homeDir: "comp-1" }
)).be.true

chai.expect(execStub.getCall(6).calledWith(`cd comp-1-test-app && git log --pretty=format:"%h" -1`)).be.true
chai.expect(execStub.getCall(7).calledWith(
chai.expect(execStub.getCall(8).calledWith(`cd comp-1-test-app && git log --pretty=format:"%h" -1`)).be.true
chai.expect(execStub.getCall(9).calledWith(
`deployment/pit/deploy.sh nsChild t1`,
{ homeDir: "comp-1-test-app", logFileName: `12345_t1/logs/deploy-nsChild-comp-1-test-app.log`, tailTarget: sinon.match.any })
).be.true

chai.expect(execStub.getCall(8).calledWith(
chai.expect(execStub.getCall(10).calledWith(
`deployment/pit/is-deployment-ready.sh nsChild`,
{ homeDir: "comp-1-test-app" })
).be.true
Expand Down

0 comments on commit 3f315b6

Please sign in to comment.