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): ensure patchResources can patch namespace #5334

Merged
merged 1 commit into from
Nov 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 10 additions & 12 deletions core/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,7 @@ export async function getManifests({
}

const declaredManifests = await readManifests(ctx, action, log)

if (action.kind === "Deploy") {
// Add metadata ConfigMap to aid quick status check
const metadataManifest = getMetadataManifest(action, defaultNamespace, declaredManifests)
const declaredMetadataManifest: DeclaredManifest = {
declaration: { type: "inline", index: declaredManifests.length },
manifest: metadataManifest,
}
declaredManifests.push(declaredMetadataManifest)
}

const patchedManifests = await Promise.all(declaredManifests.map(patchManifest))

const unmatchedPatches = (actionSpec.patchResources || []).filter((p) => {
const manifest = declaredManifests.find((m) => m.manifest.kind === p.kind && m.manifest.metadata.name === p.name)
if (manifest) {
Expand All @@ -177,6 +165,16 @@ export async function getManifests({
)
}

if (action.kind === "Deploy") {
// Add metadata ConfigMap to aid quick status check
const metadataManifest = getMetadataManifest(action, defaultNamespace, patchedManifests)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix here is that now we're getting the metadata for the patched manifests.

const declaredMetadataManifest: DeclaredManifest = {
declaration: { type: "inline", index: patchedManifests.length },
manifest: metadataManifest,
}
patchedManifests.push(declaredMetadataManifest)
}

const postProcessedManifests = await Promise.all(patchedManifests.map(postProcessManifest))

validateDeclaredManifests(postProcessedManifests)
Expand Down
48 changes: 48 additions & 0 deletions core/test/integ/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,54 @@ describe("getManifests", () => {
expect(manifests[0].spec.replicas).to.eql(3)
expect(manifests[1].data.hello).to.eql("patched-world")
})
it("should store patched version in metadata ConfigMap", async () => {
const action = cloneDeep(graph.getDeploy("deploy-action"))
action["_config"]["spec"]["patchResources"] = [
{
name: "busybox-deployment",
kind: "Deployment",
patch: {
metadata: {
namespace: "patched-namespace-deployment",
},
},
},
{
name: "test-configmap",
kind: "ConfigMap",
patch: {
metadata: {
namespace: "patched-namespace-configmap",
},
},
},
]
const resolvedAction = await garden.resolveAction<KubernetesDeployAction>({
action,
log: garden.log,
graph,
})

const manifests = await getManifests({ ctx, api, action: resolvedAction, log: garden.log, defaultNamespace })

const metadataConfigMap = manifests.filter((m) => m.metadata.name === "garden-meta-deploy-deploy-action")
expect(JSON.parse(metadataConfigMap[0].data.manifestMetadata)).to.eql({
"Deployment/busybox-deployment": {
apiVersion: "apps/v1",
key: "Deployment/busybox-deployment",
kind: "Deployment",
name: "busybox-deployment",
namespace: "patched-namespace-deployment", // <--- The patched namespace should be used here
},
"ConfigMap/test-configmap": {
apiVersion: "v1",
key: "ConfigMap/test-configmap",
kind: "ConfigMap",
name: "test-configmap",
namespace: "patched-namespace-configmap", // <--- The patched namespace should be used here
},
})
})
it("should apply patches to file and inline manifests", async () => {
const action = cloneDeep(graph.getDeploy("deploy-action"))
action["_config"]["spec"]["manifests"] = [
Expand Down