Skip to content

Commit

Permalink
refactor: introduce parameter object getForwardablePorts
Browse files Browse the repository at this point in the history
  • Loading branch information
vvagaytsev committed Aug 23, 2023
1 parent 250ffd0 commit c7c1256
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/helm/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const helmDeploy: DeployActionHandler<"deploy", HelmDeployAction> = async
})

// Local mode has its own port-forwarding configuration
const forwardablePorts = getForwardablePorts(manifests, action, mode)
const forwardablePorts = getForwardablePorts({ resources: manifests, parentAction: action, mode })

// Make sure port forwards work after redeployment
killPortForwards(action, forwardablePorts || [], log)
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/helm/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const getHelmDeployStatus: DeployActionHandler<"getStatus", HelmDeployAct
if (state !== "missing") {
const deployedResources = await getDeployedChartResources({ ctx: k8sCtx, action, releaseName, log })

forwardablePorts = getForwardablePorts(deployedResources, action, deployedMode)
forwardablePorts = getForwardablePorts({ resources: deployedResources, parentAction: action, mode: deployedMode })
ingresses = getK8sIngresses(deployedResources)

if (state === "ready") {
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/kubernetes-type/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const getKubernetesDeployStatus: DeployActionHandler<"getStatus", Kuberne

if (remoteResources && remoteResources.length > 0) {
try {
forwardablePorts = getForwardablePorts(remoteResources, action, deployedMode)
forwardablePorts = getForwardablePorts({ resources: remoteResources, parentAction: action, mode: deployedMode })
} catch (error) {
log.debug({ msg: `Unable to extract forwardable ports: ${error.message}`, error })
}
Expand Down
12 changes: 8 additions & 4 deletions core/src/plugins/kubernetes/port-forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,15 @@ function getTargetResourceName(action: SupportedRuntimeAction, targetName?: stri
/**
* Returns a list of forwardable ports based on the specified resources.
*/
export function getForwardablePorts(
resources: KubernetesResource[],
parentAction: Resolved<KubernetesDeployAction | HelmDeployAction> | undefined,
export function getForwardablePorts({
resources,
parentAction,
mode,
}: {
resources: KubernetesResource[]
parentAction: Resolved<KubernetesDeployAction | HelmDeployAction> | undefined
mode: ActionMode
): ForwardablePort[] {
}): ForwardablePort[] {
const spec = parentAction?.getSpec()

// Note: Local mode has its own port-forwarding configuration
Expand Down
50 changes: 25 additions & 25 deletions core/test/unit/src/plugins/kubernetes/port-forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { ResolvedDeployAction } from "../../../../../src/actions/deploy"

describe("getForwardablePorts", () => {
it("returns all ports for Service resources", () => {
const ports = getForwardablePorts(
[
const ports = getForwardablePorts({
resources: [
{
apiVersion: "v1",
kind: "Service",
Expand All @@ -29,9 +29,9 @@ describe("getForwardablePorts", () => {
},
},
],
undefined,
"default"
)
parentAction: undefined,
mode: "default",
})

expect(ports).to.eql([
{
Expand Down Expand Up @@ -64,8 +64,8 @@ describe("getForwardablePorts", () => {
},
}

const ports = getForwardablePorts(
[
const ports = getForwardablePorts({
resources: [
{
apiVersion: "v1",
kind: "Service",
Expand All @@ -77,9 +77,9 @@ describe("getForwardablePorts", () => {
},
},
],
action,
"default"
)
parentAction: action,
mode: "default",
})

expect(ports).to.eql([
{
Expand All @@ -93,8 +93,8 @@ describe("getForwardablePorts", () => {
})

it("returns all ports for Deployment resources", () => {
const ports = getForwardablePorts(
[
const ports = getForwardablePorts({
resources: [
{
apiVersion: "apps/v1",
kind: "Deployment",
Expand All @@ -114,9 +114,9 @@ describe("getForwardablePorts", () => {
},
},
],
undefined,
"default"
)
parentAction: undefined,
mode: "default",
})

expect(ports).to.eql([
{
Expand All @@ -129,8 +129,8 @@ describe("getForwardablePorts", () => {
})

it("returns all ports for DaemonSet resources", () => {
const ports = getForwardablePorts(
[
const ports = getForwardablePorts({
resources: [
{
apiVersion: "apps/v1",
kind: "DaemonSet",
Expand All @@ -150,9 +150,9 @@ describe("getForwardablePorts", () => {
},
},
],
undefined,
"default"
)
parentAction: undefined,
mode: "default",
})

expect(ports).to.eql([
{
Expand All @@ -165,8 +165,8 @@ describe("getForwardablePorts", () => {
})

it("omits a Deployment port that is already pointed to by a Service resource", () => {
const ports = getForwardablePorts(
[
const ports = getForwardablePorts({
resources: [
{
apiVersion: "v1",
kind: "Service",
Expand Down Expand Up @@ -204,9 +204,9 @@ describe("getForwardablePorts", () => {
},
},
],
undefined,
"default"
)
parentAction: undefined,
mode: "default",
})

expect(ports).to.eql([
{
Expand Down

0 comments on commit c7c1256

Please sign in to comment.