Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #162 from creativice/fix/upgrade-cloud-run-keys-ap…
Browse files Browse the repository at this point in the history
…p-engine-error-handling

Upgrade Cloud Run keys, handle App Engine project not found error - Fix #151, #158
  • Loading branch information
austinkelleher authored May 19, 2021
2 parents bcaf2ff + 7344c94 commit 35d2858
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
47 changes: 31 additions & 16 deletions src/steps/app-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,44 @@ export async function fetchAppEngineServices(
const {
jobState,
instance: { config },
logger,
} = context;

const client = new AppEngineClient({ config });

await client.iterateAppEngineServices(async (service) => {
const serviceEntity = createAppEngineServiceEntity(service);
await jobState.addEntity(serviceEntity);
try {
await client.iterateAppEngineServices(async (service) => {
const serviceEntity = createAppEngineServiceEntity(service);
await jobState.addEntity(serviceEntity);

// There's 1:1 mapping of GCloud project to AppEngine application
// So we can safely map these to the only one existing app engine application
const applicationEntity = await jobState.getData<Entity>(
ENTITY_TYPE_APP_ENGINE_APPLICATION,
);
if (applicationEntity) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.HAS,
from: applicationEntity,
to: serviceEntity,
}),
// There's 1:1 mapping of GCloud project to AppEngine application
// So we can safely map these to the only one existing app engine application
const applicationEntity = await jobState.getData<Entity>(
ENTITY_TYPE_APP_ENGINE_APPLICATION,
);
if (applicationEntity) {
await jobState.addRelationship(
createDirectRelationship({
_class: RelationshipClass.HAS,
from: applicationEntity,
to: serviceEntity,
}),
);
}
});
} catch (err) {
// client.iterateAppEngineServices()'s this.iterateApi() already called withErrorHandling(), this is one way of getting the original error code
if (err._cause.code === 404) {
logger.trace(
{ err },
`Could not fetch app engine services for project ${client.projectId}`,
);

return;
}
});

throw err;
}
}

export async function fetchAppEngineServiceVersions(
Expand Down
6 changes: 3 additions & 3 deletions src/steps/cloud-run/__snapshots__/converters.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Object {
"_class": Array [
"Configuration",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/configurations/example-cloud-run-service",
"_key": "cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -112,7 +112,7 @@ Object {
"_class": Array [
"Configuration",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/routes/example-cloud-run-service",
"_key": "cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -197,7 +197,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_rawData": Array [
Object {
"name": "default",
Expand Down
22 changes: 11 additions & 11 deletions src/steps/cloud-run/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -124,7 +124,7 @@ Object {
"_class": Array [
"Configuration",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/configurations/example-cloud-run-service",
"_key": "cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -228,9 +228,9 @@ Object {
"collectedRelationships": Array [
Object {
"_class": "MANAGES",
"_fromEntityKey": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service",
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service|manages|/apis/serving.knative.dev/v1/namespaces/711888229551/configurations/example-cloud-run-service",
"_toEntityKey": "/apis/serving.knative.dev/v1/namespaces/711888229551/configurations/example-cloud-run-service",
"_fromEntityKey": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd|manages|cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_toEntityKey": "cloudrun_configuration:f190d809-4de1-4eb3-80bb-ad0ac9954f85",
"_type": "google_cloud_run_service_manages_configuration",
"displayName": "MANAGES",
},
Expand All @@ -252,7 +252,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -369,7 +369,7 @@ Object {
"_class": Array [
"Configuration",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/routes/example-cloud-run-service",
"_key": "cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_rawData": Array [
Object {
"name": "default",
Expand Down Expand Up @@ -451,9 +451,9 @@ Object {
"collectedRelationships": Array [
Object {
"_class": "MANAGES",
"_fromEntityKey": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service",
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service|manages|/apis/serving.knative.dev/v1/namespaces/711888229551/routes/example-cloud-run-service",
"_toEntityKey": "/apis/serving.knative.dev/v1/namespaces/711888229551/routes/example-cloud-run-service",
"_fromEntityKey": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd|manages|cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_toEntityKey": "cloudrun_route:f538487c-6e93-4038-9f6a-b7936cc4dd04",
"_type": "google_cloud_run_service_manages_route",
"displayName": "MANAGES",
},
Expand All @@ -475,7 +475,7 @@ Object {
"_class": Array [
"Service",
],
"_key": "/apis/serving.knative.dev/v1/namespaces/711888229551/services/example-cloud-run-service",
"_key": "cloudrun_service:fce2d165-9ccb-4b6c-9204-67362d41aadd",
"_rawData": Array [
Object {
"name": "default",
Expand Down
18 changes: 15 additions & 3 deletions src/steps/cloud-run/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export interface MetadataComputedPropertyData {
duplicateProperties: string[];
}

export function getCloudRunServiceKey(uid: string) {
return `cloudrun_service:${uid}`;
}

export function getCloudRunRouteKey(uid: string) {
return `cloudrun_route:${uid}`;
}

export function getCloudRunConfigurationKey(uid: string) {
return `cloudrun_configuration:${uid}`;
}

export function createCloudRunServiceEntity(
data: run_v1.Schema$Service,
projectId: string,
Expand All @@ -42,7 +54,7 @@ export function createCloudRunServiceEntity(
assign: {
_class: ENTITY_CLASS_CLOUD_RUN_SERVICE,
_type: ENTITY_TYPE_CLOUD_RUN_SERVICE,
_key: data.metadata?.selfLink as string,
_key: getCloudRunServiceKey(data.metadata?.uid as string),
name: data.metadata?.name,
displayName: data.metadata?.name as string,
category: ['infrastructure'],
Expand All @@ -60,7 +72,7 @@ export function createCloudRunRouteEntity(data: run_v1.Schema$Route) {
assign: {
_class: ENTITY_CLASS_CLOUD_RUN_ROUTE,
_type: ENTITY_TYPE_CLOUD_RUN_ROUTE,
_key: data.metadata?.selfLink as string,
_key: getCloudRunRouteKey(data.metadata?.uid as string),
name: data.metadata?.name,
displayName: data.metadata?.name as string,
url: data.status?.url,
Expand Down Expand Up @@ -158,7 +170,7 @@ export function createCloudRunConfigurationEntity(
assign: {
_class: ENTITY_CLASS_CLOUD_RUN_CONFIGURATION,
_type: ENTITY_TYPE_CLOUD_RUN_CONFIGURATION,
_key: data.metadata?.selfLink as string,
_key: getCloudRunConfigurationKey(data.metadata?.uid as string),
name: data.metadata?.name,
displayName: data.metadata?.name as string,
apiVersion: data.apiVersion,
Expand Down
5 changes: 3 additions & 2 deletions src/steps/cloud-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
createCloudRunConfigurationEntity,
createCloudRunRouteEntity,
createCloudRunServiceEntity,
getCloudRunServiceKey,
MetadataComputedPropertyData,
} from './converters';

Expand Down Expand Up @@ -63,7 +64,7 @@ export async function fetchCloudRunRoutes(
);
if (ownerService) {
const serviceEntity = await jobState.findEntity(
`/apis/serving.knative.dev/v1/namespaces/${route.metadata?.namespace}/services/${ownerService.name}`,
getCloudRunServiceKey(ownerService.uid as string),
);
if (serviceEntity) {
await jobState.addRelationship(
Expand Down Expand Up @@ -115,7 +116,7 @@ export async function fetchCloudRunConfigurations(
);
if (ownerService) {
const serviceEntity = await jobState.findEntity(
`/apis/serving.knative.dev/v1/namespaces/${configuration.metadata?.namespace}/services/${ownerService.name}`,
getCloudRunServiceKey(ownerService.uid as string),
);
if (serviceEntity) {
await jobState.addRelationship(
Expand Down

0 comments on commit 35d2858

Please sign in to comment.