diff --git a/examples/k8s-apply/template-manifest-object.yaml b/examples/k8s-apply/template-manifest-object.yaml new file mode 100644 index 0000000..f88963d --- /dev/null +++ b/examples/k8s-apply/template-manifest-object.yaml @@ -0,0 +1,41 @@ +apiVersion: scaffolder.backstage.io/v1beta3 +kind: Template +metadata: + name: deploy-resources-object + title: Deploy Resources using object + description: Deploy Resource to Kubernetes +spec: + owner: guest + type: service + # these are the steps which are rendered in the frontend with the form input + parameters: [] + steps: + - id: template + name: Generating component + action: fetch:template + input: + url: ./skeleton + - id: apply + name: apply-manifest + action: cnoe:kubernetes:apply + input: + namespaced: true + manifestObject: + apiVersion: v1 + kind: ConfigMap + metadata: + name: game-demo + data: + # property-like keys; each key maps to a simple value + player_initial_lives: "3" + ui_properties_file_name: "user-interface.properties" + + # file-like keys + game.properties: | + enemy.types=aliens,monsters + player.maximum-lives=5 + user-interface.properties: | + color.good=purple + color.bad=yellow + allow.textmode=true + clusterName: local diff --git a/examples/k8s-apply/template-manifest-string.yaml b/examples/k8s-apply/template-manifest-string.yaml new file mode 100644 index 0000000..312f557 --- /dev/null +++ b/examples/k8s-apply/template-manifest-string.yaml @@ -0,0 +1,41 @@ +apiVersion: scaffolder.backstage.io/v1beta3 +kind: Template +metadata: + name: deploy-resources-string + title: Deploy Resources using literal string + description: Deploy Resource to Kubernetes +spec: + owner: guest + type: service + # these are the steps which are rendered in the frontend with the form input + parameters: [] + steps: + - id: template + name: Generating component + action: fetch:template + input: + url: ./skeleton + - id: apply + name: apply-manifest + action: cnoe:kubernetes:apply + input: + namespaced: true + manifestString: | + apiVersion: v1 + kind: ConfigMap + metadata: + name: game-demo + data: + # property-like keys; each key maps to a simple value + player_initial_lives: "3" + ui_properties_file_name: "user-interface.properties" + + # file-like keys + game.properties: | + enemy.types=aliens,monsters + player.maximum-lives=5 + user-interface.properties: | + color.good=purple + color.bad=yellow + allow.textmode=true + clusterName: local diff --git a/packages/backend/src/plugins/k8s-apply.ts b/packages/backend/src/plugins/k8s-apply.ts index 0810cb1..bd3ea31 100644 --- a/packages/backend/src/plugins/k8s-apply.ts +++ b/packages/backend/src/plugins/k8s-apply.ts @@ -1,6 +1,6 @@ import { createTemplateAction, executeShellCommand} from '@backstage/plugin-scaffolder-node'; import { dumpYaml } from '@kubernetes/client-node'; -import YAML from 'yaml'; +import yaml from 'js-yaml'; import { Config } from '@backstage/config'; import { resolveSafeChildPath } from '@backstage/backend-common'; import fs from 'fs-extra'; @@ -57,17 +57,27 @@ export const createKubernetesApply = (config: Config) => { }, async handler(ctx) { let obj: any; + let manifestPath = resolveSafeChildPath(ctx.workspacePath, 'to-be-applied.yaml'); if (ctx.input.manifestString) { - obj = YAML.parse(ctx.input.manifestString); + obj = yaml.load(ctx.input.manifestString) + fs.writeFileSync(manifestPath, ctx.input.manifestString, { + encoding: 'utf8', + mode: '600', + }); } else if (ctx.input.manifestObject) { obj = ctx.input.manifestObject; + fs.writeFileSync(manifestPath, yaml.dump(ctx.input.manifestObject), { + encoding: 'utf8', + mode: '600', + }); } else { const filePath = resolveSafeChildPath( ctx.workspacePath, ctx.input.manifestPath!, ); const fileContent = fs.readFileSync(filePath, 'utf8'); - obj = YAML.parse(fileContent); + manifestPath = filePath + obj = yaml.load(fileContent); } if (ctx.input.clusterName) { @@ -122,10 +132,7 @@ export const createKubernetesApply = (config: Config) => { encoding: 'utf8', mode: '600', }); - const manifestPath = resolveSafeChildPath( - ctx.workspacePath, - ctx.input.manifestPath!, - ); + if (obj.metadata.generateName !== undefined) { await executeShellCommand({ command: 'kubectl', diff --git a/packages/backend/src/plugins/sanitize.ts b/packages/backend/src/plugins/sanitize.ts index 94d7037..03a87b1 100644 --- a/packages/backend/src/plugins/sanitize.ts +++ b/packages/backend/src/plugins/sanitize.ts @@ -25,6 +25,15 @@ export const createSanitizeResource = () => { }, }, }, + output: { + type: 'object', + properties: { + sanitized: { + type: 'string', + description: 'The sanitized yaml string' + } + } + } }, async handler(ctx) { const obj = yaml.load(ctx.input.document);