Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/app/api/v2/builds/[uuid]/destroy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2025 GoodRx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest } from 'next/server';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildService from 'server/services/build';

/**
* @openapi
* /api/v2/builds/{uuid}/destroy:
* put:
* summary: Tear down a build environment
* description: |
* Changes the status of all Deploys, Builds and Deployables associated with the specified
* UUID to torn_down. This effectively marks the environment as deleted.
* tags:
* - Builds
* parameters:
* - in: path
* name: uuid
* required: true
* schema:
* type: string
* description: The UUID of the environment
* responses:
* 200:
* description: Build has been successfully torn down
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/TearDownBuildSuccessResponse'
* 404:
* description: Build not found or is a static environment
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 400:
* description: Bad request
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const PutHandler = async (req: NextRequest, { params }: { params: { uuid: string } }) => {
const { uuid: buildUuid } = params;

const buildService = new BuildService();

const response = await buildService.tearDownBuild(buildUuid);

if (response.status === 'success') {
return successResponse(response, { status: 200 }, req);
} else if (response.status === 'not_found') {
return errorResponse(response.message, { status: 404 }, req);
} else {
return errorResponse(response.message, { status: 400 }, req);
}
};

export const PUT = createApiHandler(PutHandler);
81 changes: 81 additions & 0 deletions src/app/api/v2/builds/[uuid]/redeploy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2025 GoodRx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest } from 'next/server';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildService from 'server/services/build';

/**
* @openapi
* /api/v2/builds/{uuid}/redeploy:
* put:
* summary: Redeploy an entire build
* description: |
* Triggers a redeployment of an entire build. The build
* will be queued for deployment and its status will be updated accordingly.
* tags:
* - Builds
* parameters:
* - in: path
* name: uuid
* required: true
* schema:
* type: string
* description: The UUID of the build
* responses:
* 200:
* description: Build has been successfully queued for redeployment
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/RedeployBuildSuccessResponse'
* 404:
* description: Build not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 400:
* description: Bad request
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const PutHandler = async (req: NextRequest, { params }: { params: { uuid: string } }) => {
const { uuid: buildUuid } = params;

const buildService = new BuildService();

const response = await buildService.redeployBuild(buildUuid);

if (response.status === 'success') {
return successResponse(response, { status: 200 }, req);
} else if (response.status === 'not_found') {
return errorResponse(response.message, { status: 404 }, req);
} else {
return errorResponse(response.message, { status: 400 }, req);
}
};

export const PUT = createApiHandler(PutHandler);
87 changes: 87 additions & 0 deletions src/app/api/v2/builds/[uuid]/services/[name]/redeploy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright 2025 GoodRx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest } from 'next/server';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildService from 'server/services/build';

/**
* @openapi
* /api/v2/builds/{uuid}/services/{name}/redeploy:
* put:
* summary: Redeploy a service within an environment
* description: |
* Triggers a redeployment of a specific service within an environment. The service
* will be queued for deployment and its status will be updated accordingly.
* tags:
* - Services
* parameters:
* - in: path
* name: uuid
* required: true
* schema:
* type: string
* description: The UUID of the environment
* - in: path
* name: name
* required: true
* schema:
* type: string
* description: The name of the service to redeploy
* responses:
* 200:
* description: Service has been successfully queued for redeployment
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/RedeployServiceSuccessResponse'
* 404:
* description: Build or service not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 400:
* description: Bad request
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const PutHandler = async (req: NextRequest, { params }: { params: { uuid: string; name: string } }) => {
const { uuid: buildUuid, name: serviceName } = params;

const buildService = new BuildService();

const response = await buildService.redeployServiceFromBuild(buildUuid, serviceName);

if (response.status === 'success') {
return successResponse(response, { status: 200 }, req);
} else if (response.status === 'not_found') {
return errorResponse(response.message, { status: 404 }, req);
} else {
return errorResponse(response.message, { status: 400 }, req);
}
};

export const PUT = createApiHandler(PutHandler);
83 changes: 83 additions & 0 deletions src/app/api/v2/builds/[uuid]/webhooks/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2025 GoodRx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest } from 'next/server';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildService from 'server/services/build';

/**
* @openapi
* /api/v2/builds/{uuid}/webhooks:
* post:
* summary: Invoke webhooks for a build
* description: |
* Triggers the execution of configured webhooks for a specific build.
* The webhooks must be defined in the build's webhooksYaml configuration.
* tags:
* - Builds
* parameters:
* - in: path
* name: uuid
* required: true
* schema:
* type: string
* description: The UUID of the build
* responses:
* 200:
* description: Webhooks successfully queued
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/InvokeWebhooksSuccessResponse'
* 404:
* description: Build not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 400:
* description: Bad request
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const PutHandler = async (req: NextRequest, { params }: { params: { uuid: string } }) => {
const { uuid: buildUuid } = params;

const buildService = new BuildService();

const response = await buildService.invokeWebhooksForBuild(buildUuid);

if (response.status === 'success') {
return successResponse(response, { status: 200 }, req);
} else if (response.status === 'not_found') {
return errorResponse(response.message, { status: 404 }, req);
} else if (response.status === 'no_content') {
return successResponse(response, { status: 200 }, req);
} else {
return errorResponse(response.message, { status: 400 }, req);
}
};

export const PUT = createApiHandler(PutHandler);
41 changes: 30 additions & 11 deletions src/server/lib/kubernetes/getDeploymentPods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,38 @@ export async function getDeploymentPods(deploymentName: string, uuid: string): P
const namespace = `env-${uuid}`;
const fullDeploymentName = `${deploymentName}-${uuid}`;

let deployment: k8s.V1Deployment;

try {
const deployResp = await appsV1.readNamespacedDeployment(fullDeploymentName, namespace);
deployment = deployResp.body;
} catch (err: any) {
if (err?.statusCode === 404) {
return [];
const workloadSelector = `app.kubernetes.io/instance=${fullDeploymentName}`;
let matchLabels: Record<string, string> | undefined;

// Try to find a Deployment using the label selector
const deployResp = await appsV1.listNamespacedDeployment(
namespace,
undefined,
undefined,
undefined,
undefined,
workloadSelector
);

if (deployResp.body.items.length > 0) {
matchLabels = deployResp.body.items[0].spec?.selector?.matchLabels;
} else {
// if no Deployment found, try to find a StatefulSet
const stsResp = await appsV1.listNamespacedStatefulSet(
namespace,
undefined,
undefined,
undefined,
undefined,
workloadSelector
);

if (stsResp.body.items.length > 0) {
matchLabels = stsResp.body.items[0].spec?.selector?.matchLabels;
}
throw err;
}

const matchLabels = deployment.spec?.selector?.matchLabels;
// If neither found or no labels to match, return empty
if (!matchLabels || Object.keys(matchLabels).length === 0) {
return [];
}
Expand Down Expand Up @@ -209,7 +228,7 @@ export async function getDeploymentPods(deploymentName: string, uuid: string): P
};
});
} catch (error) {
getLogger().error({ error }, `K8s: failed to list deployment pods service=${deploymentName}`);
getLogger().error({ error }, `K8s: failed to list workload pods service=${deploymentName}`);
throw error;
}
}
Loading