Skip to content

Commit

Permalink
fix: Make project deployable to local k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarek-kindred committed Feb 2, 2024
1 parent b8d5d1a commit c54b5e3
Show file tree
Hide file tree
Showing 46 changed files with 691 additions and 829 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ deploy.lock-manager:
--atomic \
--timeout 60s \
--namespace $$K8S_NAMESPACE \
--set image.tag=$$IMAGE_TAG \
--set EXTERNAL_SECRET_DB_PATH=$$EXTERNAL_SECRET_DB_PATH \
--set IMAGE_TAG=$$IMAGE_TAG \
--set SERVICE_NAME=$$SERVICE_NAME \
--set SERVICE_PORT=$$SERVICE_PORT \
--set PG_MIN_POOL_SIZE=$$PG_MIN_POOL_SIZE \
--set PGDATABASE=$$PGDATABASE \
--set PGHOST=$$PGHOST \
--set PGPORT=$$PGPORT \
--set PGUSER=$$PGUSER \
--set PGPASSWORD=$$PGPASSWORD \
--set pod.repository=$$REGISTRY_URL/$$SERVICE_NAME \
--set service.port=$$SERVICE_PORT \
--set webApp.contextRoot=$$K8S_NAMESPACE.$$SERVICE_NAME \
--set webApp.contextRoot=$$K8S_NAMESPACE.$$WEB_APP_CONTEXT_ROOT"
$$SERVICE_NAME ./$$CHART_PACKAGE; \
rm $$CHART_PACKAGE; \
kubectl -n $$K8S_NAMESPACE port-forward service/$$SERVICE_NAME $$SERVICE_PORT:http'
Expand Down
31 changes: 31 additions & 0 deletions k8s-deployer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions k8s-deployer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@types/mocha": "^10.0.6",
"@types/node": "^20.8.9",
"@types/sinon": "^17.0.2",
"@types/uuid": "^9.0.8",
"c8": "^8.0.1",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
Expand All @@ -35,6 +36,7 @@
},
"dependencies": {
"node-fetch": "^3.3.2",
"uuid": "^9.0.1",
"winston": "^3.11.0",
"yaml": "^2.3.3"
}
Expand Down
6 changes: 3 additions & 3 deletions k8s-deployer/scripts/k8s-manage-namespace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ then
if [ "${existsAlready}" == "" ];
then
echo "Creating namespace: ${NS} under parent ${PARENT_NS}, this may take some time..."
kubectl hns create $NS -n $PARENT_NS

kubectl hns create $NS -n $PARENT_NS
returnStatus=$(($?+0))

if [ $returnStatus -ne 0 ];
then
echo "$STATUS_ERROR"
echo "$STATUS_ERROR"
exit $returnStatus
fi

Expand Down
18 changes: 13 additions & 5 deletions k8s-deployer/scripts/start-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
# Input:
# 1. Workspace directory for deployer. Should be empty.
# 2. Path to the directory where we have source code of the PROJECT.
#
# 3. Flag for lock manager mock
# 4. Lock manager retries count
# 5. Flag for kube proxy
# 6. Flag for generator which makes a new sub-namespace
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

echo "Starting k8s-deployer application..."
Expand All @@ -22,6 +25,13 @@ LOCK_MANAGER_API_RETRIES=$4
USE_KUBE_PROXY=$5
if [ "${USE_KUBE_PROXY}" == "" ]; then USE_KUBE_PROXY="false"; fi

# Can be "DATE" or "COMMITSHA", using "DATE" makes local development faster because resulting name
# will be less random and will need to be generated only in the morning
SUB_NS_NAME_GENERATOR_TYPE=$6
if [ "${SUB_NS_NAME_GENERATOR_TYPE}" == "" ]; then SUB_NS_NAME_GENERATOR_TYPE="DATE"; fi

TEST_SESSION=$7

PROJECT_ROOT=$(pwd)
APP_NAME=$(basename $PROJECT_DIR)
PARENT_NS=dev
Expand All @@ -33,10 +43,8 @@ then
CLUSTER_URL="http://127.0.0.1:8001"
fi

# Can be "date" or "commit-sha", using "date" makes locl development faster because resulting name
# will be less random and will need to be generated only in the morning
SUB_NS_NAME_GENERATOR_TYPE="DATE"
COMMIT_SHA=$(git rev-parse --short HEAD)
COMMIT_SHA="${COMMIT_SHA}${TEST_SESSION}"

echo "EXAMPLES_TEMP_DIR=${EXAMPLES_TEMP_DIR}"
echo "PROJECT_DIR=${PROJECT_DIR}"
Expand Down Expand Up @@ -131,7 +139,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 \
--lock_manager_api_retries $LOCK_MANAGER_API_RETRIES"
--lock-manager-api-retries $LOCK_MANAGER_API_RETRIES"


echo "LAUNCH_ARGS="
Expand Down
16 changes: 9 additions & 7 deletions k8s-deployer/src/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,20 @@ export const deployApplication = async (
const undeployApplication = async (
workspace: string,
namespace: Namespace,
appId: string,
appDirectory: string,
instructions: Schema.DeployInstructions) => {
appId: string,
appDirectory: string,
instructions: Schema.DeployInstructions,
logsDir?: string) => {
await isExecutable(`${appDirectory}/${instructions.command}`)
try {
logger.info("Invoking: '%s/%s'", appDirectory, instructions.command)
const timeoutMs = instructions.timeoutSeconds * 1_000

const logFileName = `${ workspace }/logs/undeploy-${ namespace }-${ appId }.log`
logsDir = logsDir || `${ workspace }/logs`
const logFileName = `${ logsDir }/undeploy-${ namespace }-${ appId }.log`
const command = `${ instructions.command } ${ namespace }`

const opts: any = { homeDir: appDirectory, logFileName, tailTarget: (line: string) => {
const opts: any = { homeDir: appDirectory, logFileName, timeoutMs, tailTarget: (line: string) => {
if (line.toLowerCase().startsWith("error:")) {
logger.error("%s", line)
} else {
Expand Down Expand Up @@ -190,8 +192,8 @@ export const undeployLockManager = async (config: Config, workspace: string, nam
if (config.useMockLockManager) {
logger.info("Lock manager mock is enabled. Skipping undeployment of LockManager app.")
} else {
let appDir = `${ workspace }/${ spec.id }`
await undeployApplication(spec.id, workspace, namespace, appDir, spec.undeploy)
let appDir = spec.id
await undeployApplication(workspace, namespace, spec.id, appDir, spec.undeploy, appDir)
}
}

Expand Down
23 changes: 8 additions & 15 deletions k8s-deployer/src/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const generateNamespaceName = async (config: cfg.Config, seqNumber: strin

if (config.subNamespaceGeneratorType == cfg.SUB_NAMESPACE_GENERATOR_TYPE_DATE) {
const date = new Date()

namespaceName = `${ namespaceName }${ pad(date.getUTCMonth() + 1, 2) }`
namespaceName = `${ namespaceName }${ pad(date.getUTCDate(), 2) }`
namespaceName = `${ namespaceName }-${ seqNumber }-${ attempt }`
} else if (config.subNamespaceGeneratorType == cfg.SUB_NAMESPACE_GENERATOR_TYPE_COMMITSHA) {
namespaceName = `${ namespaceName }-${ config.commitSha }-${ seqNumber }-${ attempt }`
}

return namespaceName
return namespaceName
}

export const createNamespace = async (workspace: string, rootNamespace: Namespace, namespace: Namespace, timeoutSeconds: number) => {
Expand All @@ -45,26 +45,19 @@ export const deleteNamespace = async (rootNamespace: Namespace, namespace: Names
await Shell.exec(command, { logFileName: logFile, tailTarget: logger.info, timeoutMs })
}


export class ServiceUrlOptions {
exposedViaProxy?: boolean
servicePort?: number
}

export const makeServiceUrl = (clusterUrl: string, namespace: Namespace, service: string, testId?: string, options?: ServiceUrlOptions) => {
let url
if (options?.exposedViaProxy) {
url = `${ clusterUrl }/api/v1/namespaces/${ namespace }/services/${ service }`
let servicePort = 80
if (options.servicePort) {
servicePort = options.servicePort
}
url = `${ url }:${ servicePort }/proxy`
} else {
let serviceName = testId || service
// This is exposed via NGINX Ingress, "service" here is test suite id
url = `${ clusterUrl }/${ namespace }.${ serviceName }`
const url = `${ clusterUrl }/api/v1/namespaces/${ namespace }/services/${ service }`
const servicePort = options.servicePort || 80
return `${ url }:${ servicePort }/proxy`
}

return url
let serviceName = testId || service
// This is exposed via NGINX Ingress, "service" here is test suite id
return `${ clusterUrl }/${ namespace }.${ serviceName }`
}
55 changes: 0 additions & 55 deletions k8s-deployer/src/locks/lock-api-fetch.ts

This file was deleted.

42 changes: 42 additions & 0 deletions k8s-deployer/src/locks/lock-manager-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fetch from "node-fetch"

import { logger } from "../logger.js"

export type FetchParams = {
endpoint: string,
options: Object
}

export type RetryOptions = {
retries: number,
retryDelay: number,
fetchParams: FetchParams
}

export const invoke = async (options: RetryOptions, apiBody: Object) => {
let attempts = 1
while (options.retries > 0) {
logger.info("lock-manager-api-client.invoke(): attempt %s of %s invoking %s", attempts, options.retries, JSON.stringify(options.fetchParams))
try {
return await invokeApi(options.fetchParams, apiBody)
} catch (error) {
logger.warn("lock-manager-api-client.invoke(): attempt %s of %s invoking %s. Error: %s", attempts, options.retries, JSON.stringify(options.fetchParams), error)
if (attempts < options.retries) {
attempts++
await sleep(options.retryDelay)
continue
}

logger.warn("lock-manager-api-client.invoke(): failed to invoke %s. No more attempts left, giving up", JSON.stringify(options.fetchParams))
throw new Error(`Failed to fetch ${ options.fetchParams.endpoint } after ${ attempts } attempts`, { cause: error })
}
}
}

const invokeApi = async (params: FetchParams, apiBody: Object) =>{
let resp = await fetch(params.endpoint, { ...params.options, body: JSON.stringify(apiBody) })
// TODO: check for JSON header before decoding
return await resp.json()
}

export const sleep = (time: number) => new Promise(resolve => setTimeout(resolve, time))
Loading

0 comments on commit c54b5e3

Please sign in to comment.