Skip to content

Commit

Permalink
kubectl-raw: More explicitly refuse to do headers
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed Dec 25, 2020
1 parent 23aedd8 commit f0d1998
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion transports/via-kubectl-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,28 @@ export class KubectlRawRestClient implements RestClient {
let rawArgs = [command, ...(hasReqBody ? ['-f', '-'] : []), "--raw", path];

if (command === 'patch') {
// `kubectl patch` doesn't have --raw so we convert the HTTP request into a normalish `kubectl patch` command
if (path.includes('?')) throw new Error(
`TODO: KubectlRawRestClient doesn't know how to PATCH with a querystring yet`);

const patchMode = opts.contentType?.split('/')[1]?.split('-')[0] ?? 'none';
if (patchMode === 'apply') throw new Error(
`TODO: Server-Side Apply is not yet implemented (and also not enabled in vanilla Kubernetes yet)`);
if (!['json', 'merge', 'strategic'].includes(patchMode)) throw new Error(
`Unrecognized Content-Type ${opts.contentType} for PATCH, unable to translate to 'kubectl'`);
`Unrecognized Content-Type ${opts.contentType} for PATCH, unable to translate to 'kubectl patch'`);

const pathParts = path.slice(1).split('/');
const apiGroup = (pathParts.shift() == 'api') ? '' : pathParts.shift();
const [apiVersion, kindPlural, name] = pathParts;

rawArgs = [`patch`, `--type`, patchMode, `--patch-file`, `/dev/stdin`, `-o`, `json`, `--`, `${kindPlural}.${apiGroup}`, name];
console.log('kubectl', rawArgs.join(' '));

} else {
if (opts.accept) throw new Error(
`KubectlRawRestClient cannot include arbitrary Accept header '${opts.accept}'`);
if (opts.contentType) throw new Error(
`KubectlRawRestClient cannot include arbitrary Content-Type header '${opts.contentType}'`);
}

const [p, status] = await this.runKubectl(rawArgs, opts);
Expand Down

0 comments on commit f0d1998

Please sign in to comment.