Skip to content

Commit

Permalink
feat: update custom resource on PUT request
Browse files Browse the repository at this point in the history
  • Loading branch information
karanwadhwa committed Nov 15, 2023
1 parent 3f40f65 commit 29444fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/api/controllers/httpcheck.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
ServiceUnavailableError,
} from '../utils/error.util'
import { httpStatusCodes } from '../../constants/responseCodes'
import { createCustomResource, deleteCustomResource } from '../utils/k8s.util'
import {
createCustomResource,
deleteCustomResource,
patchCustomResource,
} from '../utils/k8s.util'

/**
* `get` controller for GET `/v1/http-check/:id` endpoint
Expand Down Expand Up @@ -50,7 +54,7 @@ export const create = async (req, res, next) => {
if (!httpCheck) throw new ServiceUnavailableError()

createCustomResource({
name: `${httpCheck.name}-${httpCheck.id}`,
name: httpCheck.id,
url: httpCheck.uri,
retries: httpCheck.num_retries,
res_code: httpCheck.response_status_code,
Expand Down Expand Up @@ -81,7 +85,12 @@ export const update = async (req, res, next) => {
const updatedHttpCheck = await service.update(req.params.id, req.body)
if (!updatedHttpCheck) throw new ServiceUnavailableError()

console.log('updatedHttpcheck', JSON.stringify(updatedHttpCheck))
patchCustomResource(updatedHttpCheck.id, {
name: updatedHttpCheck.id,
url: updatedHttpCheck.uri,
retries: updatedHttpCheck.num_retries,
res_code: updatedHttpCheck.response_status_code,
})

return res.sendStatus(204)
} catch (error) {
Expand All @@ -106,7 +115,7 @@ export const remove = async (req, res, next) => {
if (!deletedCount)
throw new ResourceNotFoundError("HttpCheck doesn't exist")

deleteCustomResource(`${httpCheck.name}-${httpCheck.id}`)
deleteCustomResource(httpCheck.id)

return res.sendStatus(204)
} catch (error) {
Expand Down
21 changes: 19 additions & 2 deletions src/api/utils/k8s.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ export const createCustomResource = (params) => {
}

export const patchCustomResource = (cr_name, params) => {
const body = createCustomResourceBody(params)
const options = {
headers: { 'Content-type': k8s.PatchUtils.PATCH_FORMAT_JSON_PATCH },
}
const patch = [
{
op: 'replace',
path: '/spec',
value: {
url: params.url,
retries: params.retries,
res_code: params.res_code,
},
},
]

k8sCustomApi
.patchNamespacedCustomObject(
Expand All @@ -62,7 +75,11 @@ export const patchCustomResource = (cr_name, params) => {
NAMESPACE,
KIND_PLURAL,
cr_name,
body
patch,
undefined,
undefined,
undefined,
options
)
.then((res) => {
console.log('patchNamespacedCustomObject res:', JSON.stringify(res))
Expand Down

0 comments on commit 29444fd

Please sign in to comment.