Skip to content

Commit

Permalink
Add option to override default contentType header
Browse files Browse the repository at this point in the history
  • Loading branch information
rfun committed May 6, 2024
1 parent ab4f06c commit 4606b4c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,25 @@ service:

## Inputs

| parameter | description | required | default |
| ------------ | ----------------------------------------------------- | -------- | ------------------------ |
| action | Action to perform (upsert, delete) | `true` | upsert |
| annotations | Key/Value pair of annotations for the application | `false` | {} |
| clientId | ArgoCD Client Id / Username | `false` | |
| clientSecret | ArgoCD Client Secret / Password | `false` | |
| clusterName | Cluster name to deploy to | `true` | |
| baseUrl | ArgoCD base url to use | `true` | |
| gitRef | Revision to deploy to ArgoCD | `false` | ${{ github.ref }} |
| gitRepo | GitHub repository to use | `false` | ${{ github.repository }} |
| info | Key/Value pair of argo info values | `false` | {} |
| labels | Key/Value pair of labels to apply to argo application | `false` | {} |
| name | Name of the application to create | `true` | |
| namespace | Namespace to deploy application to | `true` | |
| path | Path to helm chart | `false` | |
| project | Argo project to create application in | `true` | |
| tokens | Key/Value list of tokens to replace in helm chart | `false` | {} |
| valuesFile | Values file to pass to ArgoCD | `true` | |
| parameter | description | required | default |
| ------------ | ----------------------------------------------------- | -------- | ------------------------------- |
| action | Action to perform (upsert, delete) | `true` | upsert |
| annotations | Key/Value pair of annotations for the application | `false` | {} |
| clientId | ArgoCD Client Id / Username | `false` | |
| clientSecret | ArgoCD Client Secret / Password | `false` | |
| clusterName | Cluster name to deploy to | `true` | |
| contentType | Override default content-type header | `false` | application/json; charset=utf-8 |
| baseUrl | ArgoCD base url to use | `true` | |
| gitRef | Revision to deploy to ArgoCD | `false` | ${{ github.ref }} |
| gitRepo | GitHub repository to use | `false` | ${{ github.repository }} |
| info | Key/Value pair of argo info values | `false` | {} |
| labels | Key/Value pair of labels to apply to argo application | `false` | {} |
| name | Name of the application to create | `true` | |
| namespace | Namespace to deploy application to | `true` | |
| path | Path to helm chart | `false` | |
| project | Argo project to create application in | `true` | |
| tokens | Key/Value list of tokens to replace in helm chart | `false` | {} |
| valuesFile | Values file to pass to ArgoCD | `true` | |

<!-- action-docs-inputs -->

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ inputs:
clusterName:
description: 'Cluster name to deploy to'
required: true
contentType:
description: 'Change default content-type header from `application/json; charset=utf-8`'
required: false
dryRun:
description: 'Dry run mode (do not create or delete anything)'
required: false
Expand Down
37 changes: 31 additions & 6 deletions src/argocd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ export class ArgoCDApi {

if (this.dryRun) return app;

// Add content header if needed
const additionalHeaders = {};
if (args.contentType && args.contentType !== '') {
additionalHeaders['Content-Type'] = args.contentType;
}

// does the application exist?
const appExists = await (
await this._getClient()
).get(`applications/${releaseName}?project=${args.project}`);
).get(`applications/${releaseName}?project=${args.project}`, {
additionalHeaders,
});

let res: IRestResponse<unknown>;

Expand All @@ -66,12 +74,16 @@ export class ArgoCDApi {
info(`Updating application: ${releaseName}`);
res = await (
await this._getClient()
).replace(`applications/${releaseName}`, app.toManifest());
).replace(`applications/${releaseName}`, app.toManifest(), {
additionalHeaders,
});
} else {
info(`Creating application: ${releaseName}`);
res = await (
await this._getClient()
).create(`applications`, app.toManifest());
).create(`applications`, app.toManifest(), {
additionalHeaders,
});
}

if (res.statusCode === 200) {
Expand All @@ -84,12 +96,23 @@ export class ArgoCDApi {
/**
* Removes an argo application from kubernetes
*/
async destroyApplication(releaseName: string): Promise<void> {
async destroyApplication(
releaseName: string,
contentType: string | undefined,
): Promise<void> {
if (this.dryRun) return;

// Add content header if needed
const additionalHeaders = {};
if (contentType && contentType !== '') {
additionalHeaders['Content-Type'] = contentType;
}

const appRes = await (
await this._getClient()
).get<IArgoCDApplication>(`applications/${releaseName}`);
).get<IArgoCDApplication>(`applications/${releaseName}`, {
additionalHeaders,
});

// if the deployment can't be found, return successfully since it's technically removed
if (appRes.statusCode === 404) {
Expand All @@ -99,7 +122,9 @@ export class ArgoCDApi {
// now remove the application
const deleteRes = await (
await this._getClient()
).del(`applications/${releaseName}`);
).del(`applications/${releaseName}`, {
additionalHeaders,
});

if (deleteRes.statusCode === 200) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/argocd/interfaces/create-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface ICreateApplication {
* Kubernetes cluster name to deploy this application to
*/
clusterName: string;
/**
* Custom Content type for ArgoCD requests. Replaces the default `application/json; charset=utf-8`
*/
contentType: string;
/**
* Git revision to point at (branch, commit, tag, etc)
*/
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum INPUTS {
CLIENT_ID = 'clientId',
CLIENT_SECRET = 'clientSecret',
CLUSTER_NAME = 'clusterName',
CONTENT_TYPE = 'contentType',
DRY_RUN = 'dryRun',
INFO = 'info',
LABELS = 'labels',
Expand Down Expand Up @@ -60,6 +61,10 @@ interface ActionArgs {
* Cluster name to deploy to
*/
clusterName: string;
/**
* Custom Content type for ArgoCD requests. Replaces the default `application/json; charset=utf-8`
*/
contentType: string;
/**
* Dry run mode
*/
Expand Down Expand Up @@ -134,6 +139,7 @@ async function upsertApplication(args: ActionArgs) {

const app = await client.createApplication(args.name, {
clusterName: args.clusterName,
contentType: args.contentType,
gitRef: args.prNumber ? `pull/${args.prNumber}/head` : args.repoRevision,
gitRepo: args.repoUrl,
helmValues: await promisify(readFile)(args.valuesFile),
Expand Down Expand Up @@ -194,7 +200,7 @@ ${dump(app.toManifest())}
async function deleteApplication(args: ActionArgs) {
const client = getClient(args);

await client.destroyApplication(args.name);
await client.destroyApplication(args.name, args.contentType);

core.summary.addRaw(`# ${
args.dryRun
Expand Down Expand Up @@ -236,6 +242,7 @@ function getInputs(): ActionArgs {
clientId: core.getInput(INPUTS.CLIENT_ID, { required: false }),
clientSecret: core.getInput(INPUTS.CLIENT_SECRET, { required: false }),
clusterName: core.getInput(INPUTS.CLUSTER_NAME, { required: true }),
contentType: core.getInput(INPUTS.CONTENT_TYPE, { required: false }),
dryRun: core.getBooleanInput(INPUTS.DRY_RUN, { required: false }),
name: core.getInput(INPUTS.NAME, { required: true }),
namespace: core.getInput(INPUTS.NAMESPACE, { required: true }),
Expand Down

0 comments on commit 4606b4c

Please sign in to comment.