fix k8s-apply

This commit is contained in:
Manabu McCloskey 2024-03-12 18:05:52 -07:00
parent 5b2fb49803
commit d627de14bc
4 changed files with 105 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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',

View file

@ -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);