Skip to content

Commit

Permalink
Issue quota & billing requests against the provided project
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Dec 15, 2022
1 parent ef292bf commit aa153c2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ jobs:
for generating the Connect Gateway endpoint. This only applies if
"use_connect_gateway" is true. Defaults to auto discovery if empty.

- `quota_project_id` - (Optional) Project ID from which to pull quota. The
caller must have `serviceusage.services.use` permission on the project. If
unspecified, this defaults to the project of the authenticated principle.
This is an advanced setting, most users should leave this blank.

## Outputs

- Exports env var `KUBECONFIG` which is set to the generated `kubeconfig` file path.
Expand Down
8 changes: 8 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ inputs:
Defaults to auto discovery if empty.
required: false

quota_project_id:
description: |-
Project ID from which to pull quota. The caller must have
serviceusage.services.use permission on the project. If unspecified, this
defaults to the project of the authenticated principle. This is an
advanced setting, most users should leave this blank.
required: false

branding:
icon: 'lock'
color: 'blue'
Expand Down
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

31 changes: 19 additions & 12 deletions src/gkeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { presence } from '@google-github-actions/actions-utils';
import { GoogleAuth } from 'google-auth-library';
import { Headers } from 'gaxios';
import YAML from 'yaml';

// Do not listen to the linter - this can NOT be rewritten as an ES6 import statement.
Expand Down Expand Up @@ -43,6 +44,7 @@ const membershipResourceNamePattern = new RegExp(
*/
type ClientOptions = {
projectID?: string;
quotaProjectID?: string;
location?: string;
};

Expand Down Expand Up @@ -130,6 +132,7 @@ export class ClusterClient {
* resource name.
*/
readonly #projectID?: string;
readonly #quotaProjectID?: string;
readonly #location?: string;

readonly defaultEndpoint = 'https://container.googleapis.com/v1';
Expand All @@ -148,6 +151,7 @@ export class ClusterClient {
});

this.#projectID = opts?.projectID;
this.#quotaProjectID = opts?.quotaProjectID;
this.#location = opts?.location;
}

Expand Down Expand Up @@ -209,9 +213,7 @@ export class ClusterClient {
const url = `${this.cloudResourceManagerEndpoint}/projects/${projectID}`;
const resp = (await this.auth.request({
url: url,
headers: {
'User-Agent': userAgent,
},
headers: this.#defaultHeaders(),
})) as { data: { name: string } };

// projectRef of form projects/<project-num>"
Expand All @@ -236,9 +238,7 @@ export class ClusterClient {
const membershipURL = `${this.hubEndpoint}/${name}`;
const resp = (await this.auth.request({
url: membershipURL,
headers: {
'User-Agent': userAgent,
},
headers: this.#defaultHeaders(),
})) as HubMembershipResponse;

const membership = resp.data;
Expand Down Expand Up @@ -281,9 +281,7 @@ export class ClusterClient {
const url = `${this.hubEndpoint}/projects/${projectID}/locations/global/memberships?filter=endpoint.gkeCluster.resourceLink="${clusterResourceLink}"`;
const resp = (await this.auth.request({
url: url,
headers: {
'User-Agent': userAgent,
},
headers: this.#defaultHeaders(),
})) as HubMembershipsResponse;

const memberships = resp.data.resources;
Expand Down Expand Up @@ -315,9 +313,7 @@ export class ClusterClient {
const url = `${this.defaultEndpoint}/${this.getResource(clusterName)}`;
const resp = (await this.auth.request({
url: url,
headers: {
'User-Agent': userAgent,
},
headers: this.#defaultHeaders(),
})) as ClusterResponse;
return resp;
}
Expand Down Expand Up @@ -372,6 +368,17 @@ export class ClusterClient {
};
return YAML.stringify(kubeConfig);
}

#defaultHeaders(): Headers {
const h: Headers = {
'User-Agent': userAgent,
};

if (this.#quotaProjectID) {
h['X-Goog-User-Project'] = this.#quotaProjectID;
}
return h;
}
}

type cluster = {
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function run(): Promise<void> {
try {
// Get inputs
let projectID = getInput('project_id');
const quotaProjectID = getInput('quota_project_id');
let location = getInput('location');
const clusterName = ClusterClient.parseResourceName(
getInput('cluster_name', { required: true }),
Expand Down Expand Up @@ -95,6 +96,7 @@ async function run(): Promise<void> {
// Create Container Cluster client
const client = new ClusterClient({
projectID: projectID,
quotaProjectID: quotaProjectID,
location: location,
});

Expand Down

0 comments on commit aa153c2

Please sign in to comment.