Extended plugin to support more k8s objects (#32)

Signed-off-by: Mani Marothu <manikumar.1215@gmail.com>
This commit is contained in:
Mani Marothu 2024-09-04 15:33:31 -07:00 committed by GitHub
parent 6329ea80b1
commit 2cde0a09ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -89,19 +89,16 @@ 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.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',
@ -111,10 +108,10 @@ export const createKubernetesApply = (config: Config) => {
ctx.workspacePath,
ctx.input.manifestPath!,
);
const fileContent = fs.readFileSync(filePath, 'utf8');
manifestPath = filePath;
obj = yaml.load(fileContent);
}
const fileContent = fs.readFileSync(manifestPath, 'utf8');
const objList: any[] = yaml.loadAll(fileContent);
if (ctx.input.clusterName) {
// Supports SA token authentication only
@ -189,19 +186,43 @@ export const createKubernetesApply = (config: Config) => {
args: [manifestPath],
logStream: ctx.logStream,
});
if (obj.metadata.generateName !== undefined) {
await executeShellCommand({
command: 'kubectl',
args: ['--kubeconfig', confFilePath, 'create', '-f', manifestPath],
logStream: ctx.logStream,
let counter = 1;
for (const obj of objList) {
let manifestFilePath = resolveSafeChildPath(
ctx.workspacePath,
'to-be-applied-' + counter.toString() + '.yaml',
);
fs.writeFileSync(manifestFilePath, yaml.dump(obj), {
encoding: 'utf8',
mode: '600',
});
return;
if (obj.metadata.generateName !== undefined) {
await executeShellCommand({
command: 'kubectl',
args: [
'--kubeconfig',
confFilePath,
'create',
'-f',
manifestFilePath,
],
logStream: ctx.logStream,
});
} else {
await executeShellCommand({
command: 'kubectl',
args: [
'--kubeconfig',
confFilePath,
'apply',
'-f',
manifestFilePath,
],
logStream: ctx.logStream,
});
}
counter += 1;
}
await executeShellCommand({
command: 'kubectl',
args: ['--kubeconfig', confFilePath, 'apply', '-f', manifestPath],
logStream: ctx.logStream,
});
return;
}
throw new Error('please specify a valid cluster name');