-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Add Cloud Run Operator #27638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add Cloud Run Operator #27638
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """This module contains a Google Cloud Run Hook.""" | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Sequence | ||
|
|
||
| import google.auth.transport.requests | ||
|
|
||
| from airflow.hooks.base import BaseHook | ||
| from airflow.providers.google.common.hooks.base_google import GoogleBaseHook | ||
|
|
||
|
|
||
| class CloudRunHook(GoogleBaseHook): | ||
| """Hook for Google Cloud Run.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| gcp_conn_id: str = "google_cloud_default", | ||
| cloud_run_conn_id: str = "http_default", | ||
| delegate_to: str | None = None, | ||
| impersonation_chain: str | Sequence[str] | None = None, | ||
| ) -> None: | ||
| super().__init__( | ||
| gcp_conn_id=gcp_conn_id, delegate_to=delegate_to, impersonation_chain=impersonation_chain | ||
| ) | ||
| self.cloud_run_conn_id = cloud_run_conn_id | ||
|
|
||
| def get_conn(self) -> dict: | ||
| """ | ||
| Retrieves HTTP authentication header allowing | ||
| authenticated Google Cloud Run call. | ||
| :return: Authentication header | ||
| :rtype: dict | ||
| """ | ||
| http_connection = BaseHook.get_connection(self.cloud_run_conn_id) | ||
| credentials = self.get_id_token_credentials(target_audience=http_connection.host) | ||
| auth_req = google.auth.transport.requests.Request() | ||
| credentials.refresh(auth_req) | ||
|
|
||
| authentication_header = { | ||
| "Authorization": f"Bearer {credentials.token}", | ||
| "Content-Type": "application/json", | ||
| } | ||
| return authentication_header |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """This module contains a Google CloudRun operator.""" | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Sequence | ||
|
|
||
| from airflow.providers.google.cloud.hooks.cloud_run import CloudRunHook | ||
| from airflow.providers.http.operators.http import SimpleHttpOperator | ||
|
|
||
|
|
||
| class CloudRunOperator(SimpleHttpOperator): | ||
| """ | ||
| Performs an authenticated call against CloudRun | ||
|
|
||
| Under the hood, this operator use SimpleHttpOperator. | ||
| Check the documentation of SimpleHttpOperator for extra options. | ||
|
|
||
| This operator use the GCP connection to get a token and add it to http request Header. | ||
|
|
||
| .. seealso:: | ||
| For more information on how to use this operator, take a look at the guide: | ||
| :ref:`howto/operator:CloudRunOperator` | ||
|
|
||
| :param http_conn_id: The :ref:`CloudRun http connection<howto/connection:http>` to run | ||
| the operator against | ||
| :param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud. | ||
| """ | ||
|
|
||
| template_fields: Sequence[str] = ("gcp_conn_id",) | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| gcp_conn_id: str = "google_cloud_default", | ||
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(**kwargs) | ||
| self.gcp_conn_id = gcp_conn_id | ||
|
|
||
| def execute(self, context): | ||
| cloud_run = CloudRunHook( | ||
| gcp_conn_id=self.gcp_conn_id, | ||
| cloud_run_conn_id=self.http_conn_id, | ||
| ) | ||
|
|
||
| authentication_header = cloud_run.get_conn() | ||
| self.headers = {**authentication_header, **self.headers} | ||
| return super().execute(context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how much this code has to do with the this code:
https://github.com/apache/airflow/blob/0fd129f33792a95b06c4f4ad1f7ee83dc2f5cef5/airflow/providers/google/common/utils/id_token_credentials.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two seems indeed related. But
id_token_credentials.pyprovide the credentials from Google application default credentials.Whereas the
credential_provider.pyalready contains all the code to infer credentials from an Airflow Connection