Skip to content

Cloud Run changes to support callable functions#10168

Open
brittanycho wants to merge 2 commits intomainfrom
run-callable-functions
Open

Cloud Run changes to support callable functions#10168
brittanycho wants to merge 2 commits intomainfrom
run-callable-functions

Conversation

@brittanycho
Copy link
Copy Markdown
Contributor

Cloud Run changes to support callable functions

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the deployment process to fully support callable functions on Cloud Run. It establishes a critical mechanism to reserve unique URLs for these functions by creating a temporary placeholder Google Cloud Functions v2 function. This ensures that the necessary endpoint infrastructure is in place before the primary Cloud Run service is deployed, streamlining the overall deployment workflow for callable functions.

Highlights

  • Cloud Run Callable Functions: Introduced support for deploying callable functions on Cloud Run, enabling a new type of function deployment.
  • URL Reservation Mechanism: Implemented a new reserveCallableUrl method to pre-create a placeholder Google Cloud Functions v2 function, ensuring a unique URL is reserved for callable Cloud Run endpoints.
  • Comprehensive Testing: Added extensive unit tests for Cloud Run function creation and the new callable URL reservation logic, covering various scenarios including error handling.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the functionality to reserve callable URLs for Cloud Run functions. This is achieved by adding a new reserveCallableUrl method in fabricator.ts which creates a placeholder GCFv2 function using a 'hello world' zip from a GCS bucket. The implementation includes logic to ignore ALREADY_EXISTS errors during this process. Corresponding unit tests have been added in fabricator.spec.ts to cover the new createEndpoint behavior for Cloud Run and the reserveCallableUrl method's functionality, including error handling. Review feedback suggests improving the robustness of GCS URL parsing, adhering to the style guide by avoiding any for error types and type assertions, and updating test cases to use FirebaseError for better type safety and consistency.

Comment on lines +213 to +218
apiFunction.buildConfig.source = {
storageSource: {
bucket: HELLO_WORLD_GCS_URL.split("/")[2],
object: HELLO_WORLD_GCS_URL.split("/").slice(3).join("/"),
},
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Parsing the GCS URL by splitting the string is a bit fragile. If the URL format changes (e.g., more path segments), this could break.

Using a regular expression would be more robust for extracting the bucket and object path.

    const gcsUrlMatch = HELLO_WORLD_GCS_URL.match(/gs:\/\/([^\/]+)\/(.*)/);
    if (!gcsUrlMatch) {
      // This should not happen if the constant is well-formed.
      throw new FirebaseError(`Invalid GCS URL for hello world source: ${HELLO_WORLD_GCS_URL}`);
    }
    const [, bucket, object] = gcsUrlMatch;
    apiFunction.buildConfig.source = {
      storageSource: {
        bucket,
        object,
      },
    };

Comment on lines +233 to +239
} catch (err: any) {
if (err.status === 409) {
// Already exists, which is fine.
return;
}
throw err;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The repository style guide states: 'Never use any or unknown as an escape hatch. Define proper interfaces/types or use type guards.' Using err: any violates this rule.

You should use err: unknown and then check its type. Since gcfV2.createFunction is expected to throw a FirebaseError, you can check for that specifically.

Suggested change
} catch (err: any) {
if (err.status === 409) {
// Already exists, which is fine.
return;
}
throw err;
}
} catch (err: unknown) {
if (err instanceof FirebaseError && err.status === 409) {
// Already exists, which is fine.
return;
}
throw err;
}

throw err;
}
})
.catch(rethrowAs(endpoint, "reserve URL" as any));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using as any here bypasses a type-check because 'reserve URL' is not a valid OperationType.

To fix this properly, you should add 'reserve URL' to the OperationType union in src/deploy/functions/release/reporter.ts. This will improve type safety and remove the need for as any.

After updating OperationType, you can remove as any from this line.

Comment on lines +1873 to +1875
const err = new Error("Already exists");
(err as any).status = 409;
gcfv2.createFunction.rejects(err);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To align with the suggested improvement in fabricator.ts to catch FirebaseError specifically, this test should be updated to reject with a FirebaseError instance. This makes the test more realistic and ensures it remains valid after the production code is updated.

Also, using (err as any) is a violation of the style guide. Creating a FirebaseError is cleaner.

You'll need to import FirebaseError from ../../../error.

Suggested change
const err = new Error("Already exists");
(err as any).status = 409;
gcfv2.createFunction.rejects(err);
const err = new FirebaseError("Already exists", { status: 409 });
gcfv2.createFunction.rejects(err);

Comment on lines +1884 to +1886
const err = new Error("Server Error");
(err as any).status = 500;
gcfv2.createFunction.rejects(err);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the other test case, this should be updated to reject with a FirebaseError instance to align with the suggested improvements in fabricator.ts and adhere to the style guide.

Suggested change
const err = new Error("Server Error");
(err as any).status = 500;
gcfv2.createFunction.rejects(err);
const err = new FirebaseError("Server Error", { status: 500 });
gcfv2.createFunction.rejects(err);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants