Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(k8s): correct 0.12 => 0.13 service resource conversion #5272

Merged
merged 1 commit into from
Oct 19, 2023
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
37 changes: 31 additions & 6 deletions core/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { basename, dirname, join, resolve } from "path"
import { pathExists, readFile } from "fs-extra"
import { flatten, keyBy, set } from "lodash"
import { flatten, isEmpty, keyBy, set } from "lodash"

import { KubernetesModule } from "./module-config"
import { KubernetesResource } from "../types"
Expand Down Expand Up @@ -496,12 +496,37 @@ export function convertServiceResource(
return null
}

return {
kind: s.kind,
name: s.name || module.name,
podSelector: s.podSelector,
containerName: s.containerName,
return convertServiceResourceSpec(s, module.name)
}

/**
* Converts 0.12 {@link ServiceResourceSpec} to 0.13 {@link KubernetesTargetResourceSpec}.
*
* NOTE! In 0.13 {@link KubernetesTargetResourceSpec} has stricter schema validation rules
* than {@link ServiceResourceSpec} in 0.12.
*
* The resulting {@link KubernetesTargetResourceSpec} can contain either a pair of {@code (kind, name)}
* or a {@code podSelector}.
* The {@code podSelector} takes precedence over the {@code (kind, name)}, see {@link getTargetResource}.
*
* @param module the current module
* @param serviceResourceSpec the input service resource spec in 0.12 format
* @return the 0.13 compatible kubernetes service resource spec
* @see targetResourceSpecSchema
* @see getTargetResource
*/
export function convertServiceResourceSpec(s: ServiceResourceSpec, moduleName: string): KubernetesTargetResourceSpec {
// Set only the necessary fields to satisfy the schema restrictions defined for KubernetesTargetResourceSpec.
const result: KubernetesTargetResourceSpec =
s.podSelector && !isEmpty(s.podSelector)
? { podSelector: s.podSelector }
: { kind: s.kind, name: s.name || moduleName }

if (s.containerName) {
result.containerName = s.containerName
}

return result
}

export async function runOrTestWithPod(
Expand Down
4 changes: 0 additions & 4 deletions core/test/integ/src/plugins/kubernetes/sync-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ describe("sync mode deployments and sync behavior", () => {
target: {
kind: "Deployment",
name: "some-deployment",
containerName: undefined,
podSelector: undefined,
},
mode: "two-way",
sourcePath: join(module.path, "src"),
Expand Down Expand Up @@ -323,8 +321,6 @@ describe("sync mode deployments and sync behavior", () => {
target: {
kind: "Deployment",
name: "some-deployment",
containerName: undefined,
podSelector: undefined,
},
mode: "two-way",
exclude: ["bad/things"],
Expand Down
62 changes: 62 additions & 0 deletions core/test/unit/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { KubernetesTargetResourceSpec, ServiceResourceSpec } from "../../../../../../src/plugins/kubernetes/config"
import { convertServiceResourceSpec } from "../../../../../../src/plugins/kubernetes/kubernetes-type/common"
import { expect } from "chai"

describe("convertServiceResource", () => {
const moduleName = "module-a"

it("picks kind and name if podSelector is not defined", async () => {
const serviceResourceSpec: ServiceResourceSpec = {
kind: "Deployment",
name: "service-a",
}

const kubernetesResourceSpec = convertServiceResourceSpec(serviceResourceSpec, moduleName)
const expectedKubernetesResourceSpec: KubernetesTargetResourceSpec = {
kind: "Deployment",
name: "service-a",
}
expect(kubernetesResourceSpec).to.eql(expectedKubernetesResourceSpec)
})

it("picks kind and name if podSelector is empty", async () => {
const serviceResourceSpec: ServiceResourceSpec = {
kind: "Deployment",
name: "service-a",
podSelector: {},
}

const kubernetesResourceSpec = convertServiceResourceSpec(serviceResourceSpec, moduleName)
const expectedKubernetesResourceSpec: KubernetesTargetResourceSpec = {
kind: "Deployment",
name: "service-a",
}
expect(kubernetesResourceSpec).to.eql(expectedKubernetesResourceSpec)
})

it("picks podSelector instead of kind and name if podSelector is not empty", async () => {
const serviceResourceSpec: ServiceResourceSpec = {
kind: "Deployment",
name: "service-a",
podSelector: {
app: "app-service-a",
},
}

const kubernetesResourceSpec = convertServiceResourceSpec(serviceResourceSpec, moduleName)
const expectedKubernetesResourceSpec: KubernetesTargetResourceSpec = {
podSelector: {
app: "app-service-a",
},
}
expect(kubernetesResourceSpec).to.eql(expectedKubernetesResourceSpec)
})
})