Skip to content
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

Add Google Cloud Platform support #13598

Merged
merged 35 commits into from
Jan 14, 2020
Merged

Add Google Cloud Platform support #13598

merged 35 commits into from
Jan 14, 2020

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Sep 11, 2019

This PR introduces the support for Google Cloud Platform to Functionbeat. This branch is located in the elastic/beats repository, so anyone on our team has access to it.

Manager

Authentication

To use the API to deploy, remove and update functions, users need to set the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable should point to a JSON file which contains all the relevant information for Google to authenticate.

(About authentication for GCP libs: https://cloud.google.com/docs/authentication/getting-started)

Required roles

  • Cloud Functions Developer
  • Cloud Functions Service Agent
  • Service Account User
  • Storage Admin
  • Storage Object Admin

Note: Cloud Functions Developer role is in beta. We should not make GCP support GA, until it becomes stable.

Configuration

# Configure functions to run on Google Cloud Platform, currently, we assume that the credentials
# are present in the environment to correctly create the function when using the CLI.
#
# Configure which region your project is located in.
functionbeat.provider.gcp.location_id: "europe-west1"
# Configure which Google Cloud project to deploy your functions.
functionbeat.provider.gcp.project_id: "my-project-123456"
# Configure the Google Cloud Storage we should upload the function artifact.
functionbeat.provider.gcp.storage_name: "functionbeat-deploy"

functionbeat.provider.gcp.functions:

Export

Function templates can be exported into YAML. With this YAML configuration, users can deploy the function using the Google Cloud Deployment Manager.

New functions

Google Pub/Sub

A function under the folder pkg/pubsub is available to get events from Google Pub/Sub.

Configuration
  # Define the list of function availables, each function required to have a unique name.
  # Create a function that accepts events coming from Google Pub/Sub.
  - name: pubsub
    enabled: false
    type: pubsub

    # Description of the method to help identify them when you run multiples functions.
    description: "Google Cloud Function for Pub/Sub"

    # The maximum memory allocated for this function, the configured size must be a factor of 64.
    # Default is 256MiB.
    #memory_size: 256MiB

    # Execution timeout in seconds. If the function does not finish in time,
    # it is considered failed and terminated. Default is 60s.
    #timeout: 60s

    # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
    #service_account_email: {projectid}@appspot.gserviceaccount.com

    # Labels of the function.
    #labels:
    # mylabel: label

    # VPC Connector this function can connect to.
    # Format: projects/*/locations/*/connectors/* or fully-qualified URI
    #vpc_connector: ""

    # Number of maximum instances running at the same time. Default is unlimited.
    #maximum_instances: 0

    trigger:
      event_type: "providers/cloud.pubsub/eventTypes/topic.publish"
      resource: "projects/_/pubsub/myPubSub"
      #service: "pubsub.googleapis.com"

    # Optional fields that you can specify to add additional information to the
    # output. Fields can be scalar values, arrays, dictionaries, or any nested
    # combination of these.
    #fields:
    #  env: staging

    # Define custom processors for this function.
    #processors:
    #  - dissect:
    #      tokenizer: "%{key1} %{key2}"

Google Cloud Storage

A function under the folder pkg/storage is available to get events from Google Cloud Storage.

Configuration
 # Create a function that accepts events coming from Google Cloud Storage.
 - name: storage
   enabled: false
   type: storage

   # Description of the method to help identify them when you run multiples functions.
   description: "Google Cloud Function for Cloud Storage"

   # The maximum memory allocated for this function, the configured size must be a factor of 64.
   # Default is 256MiB.
   #memory_size: 256MiB

   # Execution timeout in seconds. If the function does not finish in time,
   # it is considered failed and terminated. Default is 60s.
   #timeout: 60s

   # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
   #service_account_email: {projectid}@appspot.gserviceaccount.com

   # Labels of the function.
   #labels:
   # mylabel: label

   # VPC Connector this function can connect to.
   # Format: projects/*/locations/*/connectors/* or fully-qualified URI
   #vpc_connector: ""

   # Number of maximum instances running at the same time. Default is unlimited.
   #maximum_instances: 0

   # Optional fields that you can specify to add additional information to the
   # output. Fields can be scalar values, arrays, dictionaries, or any nested
   # combination of these.
   #fields:
   #  env: staging

   # Define custom processors for this function.
   #processors:
   #  - dissect:
   #      tokenizer: "%{key1} %{key2}"

Vendor

  • cloud.google.com/go/functions/metadata
  • cloud.google.com/go/storage

How to test

Manager

Make sure you have a GCP account with the required roles listed in the PR.

Package

Functionbeat is able to zip the functions which can be deployed to cloud providers.

./functionbeat package

Make sure all three zip files are generated and the --output flag is applied correctly.

Expected packages:

  1. package-aws.zip
  2. package-gcp-pubsub.zip
  3. package-gcp-storage.zip

Deployment

Download a credentials file for your user. Pass the path to Functionbeat as an environment variable:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/file.json
General

Adjust the following general settings for GCP.

  • location_id: Where your function is going to be deployed. (Available locations: https://cloud.google.com/about/locations/)
  • project_id: The ID of your project
    image
  • storage_name: Name of the storage bucket you want to upload the function. If it does not exists, it will be created, given you have the appropriate role.
functionbeat.provider.gcp.location_id: "europe-west2"
functionbeat.provider.gcp.project_id: "my-project-123456"
functionbeat.provider.gcp.storage_name: "functionbeat-deploy"
Pub/Sub function

With the following minimal configuration, you can deploy a function which is triggered by new Pub/Sub events.

Set the option trigger.resource to the ID of your Pub/Sub:

image

functionbeat.provider.gcp.functions:
  - name: pubsub
    enabled: true
    type: pubsub

    trigger:
      resource: "projects/_/pubsub/myPubSub"

Besides deploying a function with a minimal config, try to experiment with the other available options to see if all of them are working.

Run the following command to deploy the function:

./functionbeat deploy pubsub

The deployed function shows up in the functions list, if everything went correctly.

To trigger the function, go to the configured Pub/Sub topic and publish a message. The logs of the function invocation can be found under Stackdriver/Logging/Logs Viewer.

image

Storage

Configure a trigger for the storage function under trigger.resource, just like in case of Pub/Sub trigger.
Furthermore, you can configure multiple trigger types for the function as event_type:

  • google.storage.object.finalize
  • google.storage.object.delete
  • google.storage.object.archive
  • google.storage.object.metadataUpdate
trigger:
  resource: "projects/my-project/buckets/my-storage"
  event_type: "google.storage.object.finalize"

The function can be triggered by creating/deleting/archiving things from the bucket or any metadata change. Logs are located under the same log viewer as in case of Pub/Sub function.

@kvch kvch added in progress Pull request is currently in progress. review Functionbeat labels Sep 11, 2019
x-pack/functionbeat/provider/gcp/storage/storage.go Outdated Show resolved Hide resolved
x-pack/functionbeat/provider/gcp/gcp/storage.go Outdated Show resolved Hide resolved
x-pack/functionbeat/provider/gcp/gcp/storage.go Outdated Show resolved Hide resolved
x-pack/functionbeat/provider/gcp/gcp/storage.go Outdated Show resolved Hide resolved
x-pack/functionbeat/provider/gcp/gcp/pubsub.go Outdated Show resolved Hide resolved
x-pack/functionbeat/provider/gcp/gcp/pubsub.go Outdated Show resolved Hide resolved
@kvch
Copy link
Contributor Author

kvch commented Dec 12, 2019

Until go modules is fully supported in beats, Functionbeat on GCP are using vendoring. I have applied the required patches, it should not change the code a lot. Packaging still needs some adjustments to include vendor in the folder pkg.

@kvch kvch added [zube]: In Review and removed [zube]: Blocked in progress Pull request is currently in progress. labels Jan 6, 2020
@kvch kvch marked this pull request as ready for review January 6, 2020 15:51
@zube zube bot added the in progress Pull request is currently in progress. label Jan 6, 2020
@kvch kvch added needs_backport PR is waiting to be backported to other branches. and removed in progress Pull request is currently in progress. labels Jan 6, 2020
@urso
Copy link

urso commented Jan 6, 2020

To use the API to deploy, remove and update functions, users need to set the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable should point to a JSON file which contains all the relevant information for Google to authenticate.

I think one can also use option.WithCredentialsFile.

Only compile with TLS1.3 support for go1.13 or newer is used. If an
older go version is used we stick with TLS1.2 max.

The change also introduces TLSVersionMin/Max and
TLSVersionDefaultMin/Max constants, so to keep the tests intact.
import "crypto/tls"

const (
TLSVersionSSL30 TLSVersion = tls.VersionSSL30

Choose a reason for hiding this comment

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

exported const TLSVersionSSL30 should have comment (or a comment on this block) or be unexported

import "crypto/tls"

const (
TLSVersionSSL30 TLSVersion = tls.VersionSSL30

Choose a reason for hiding this comment

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

exported const TLSVersionSSL30 should have comment (or a comment on this block) or be unexported

import "crypto/tls"

const (
TLSVersionSSL30 TLSVersion = tls.VersionSSL30

Choose a reason for hiding this comment

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

exported const TLSVersionSSL30 should have comment (or a comment on this block) or be unexported

@kvch
Copy link
Contributor Author

kvch commented Jan 14, 2020

Failing tests are unrelated.

@kvch kvch merged commit e8e18d0 into master Jan 14, 2020
kvch added a commit to kvch/beats that referenced this pull request Jan 14, 2020
This PR introduces the support for Google Cloud Platform to Functionbeat. This branch is located in the `elastic/beats` repository, so anyone on our team has access to it.

To use the API to deploy, remove and update functions, users need to set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`. This variable should point to a JSON file which contains all the relevant information for Google to authenticate.

(About authentication for GCP libs: https://cloud.google.com/docs/authentication/getting-started)

* Cloud Functions Developer
* Cloud Functions Service Agent
* Service Account User
* Storage Admin
* Storage Object Admin

Note: Cloud Functions Developer role is in beta. We should not make GCP support GA, until it becomes stable.

```yaml
functionbeat.provider.gcp.location_id: "europe-west1"
functionbeat.provider.gcp.project_id: "my-project-123456"
functionbeat.provider.gcp.storage_name: "functionbeat-deploy"

functionbeat.provider.gcp.functions:
```

Function templates can be exported into YAML. With this YAML configuration, users can deploy the function using the [Google Cloud Deployment Manager](https://cloud.google.com/deployment-manager/).

A function under the folder `pkg/pubsub` is available to get events from Google Pub/Sub.

```yaml
  # Define the list of function availables, each function required to have a unique name.
  # Create a function that accepts events coming from Google Pub/Sub.
  - name: pubsub
    enabled: false
    type: pubsub

    # Description of the method to help identify them when you run multiples functions.
    description: "Google Cloud Function for Pub/Sub"

    # The maximum memory allocated for this function, the configured size must be a factor of 64.
    # Default is 256MiB.
    #memory_size: 256MiB

    # Execution timeout in seconds. If the function does not finish in time,
    # it is considered failed and terminated. Default is 60s.
    #timeout: 60s

    # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
    #service_account_email: {projectid}@appspot.gserviceaccount.com

    # Labels of the function.
    #labels:
    # mylabel: label

    # VPC Connector this function can connect to.
    # Format: projects/*/locations/*/connectors/* or fully-qualified URI
    #vpc_connector: ""

    # Number of maximum instances running at the same time. Default is unlimited.
    #maximum_instances: 0

    trigger:
      event_type: "providers/cloud.pubsub/eventTypes/topic.publish"
      resource: "projects/_/pubsub/myPubSub"
      #service: "pubsub.googleapis.com"

    # Optional fields that you can specify to add additional information to the
    # output. Fields can be scalar values, arrays, dictionaries, or any nested
    # combination of these.
    #fields:
    #  env: staging

    # Define custom processors for this function.
    #processors:
    #  - dissect:
    #      tokenizer: "%{key1} %{key2}"
```

A function under the folder pkg/storage is available to get events from Google Cloud Storage.

```yaml
 # Create a function that accepts events coming from Google Cloud Storage.
 - name: storage
   enabled: false
   type: storage

   # Description of the method to help identify them when you run multiples functions.
   description: "Google Cloud Function for Cloud Storage"

   # The maximum memory allocated for this function, the configured size must be a factor of 64.
   # Default is 256MiB.
   #memory_size: 256MiB

   # Execution timeout in seconds. If the function does not finish in time,
   # it is considered failed and terminated. Default is 60s.
   #timeout: 60s

   # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
   #service_account_email: {projectid}@appspot.gserviceaccount.com

   # Labels of the function.
   #labels:
   # mylabel: label

   # VPC Connector this function can connect to.
   # Format: projects/*/locations/*/connectors/* or fully-qualified URI
   #vpc_connector: ""

   # Number of maximum instances running at the same time. Default is unlimited.
   #maximum_instances: 0

   # Optional fields that you can specify to add additional information to the
   # output. Fields can be scalar values, arrays, dictionaries, or any nested
   # combination of these.
   #fields:
   #  env: staging

   # Define custom processors for this function.
   #processors:
   #  - dissect:
   #      tokenizer: "%{key1} %{key2}"
```

* `cloud.google.com/go/functions/metadata`
*  `cloud.google.com/go/storage`
(cherry picked from commit e8e18d0)
@kvch kvch added v7.6.0 and removed needs_backport PR is waiting to be backported to other branches. labels Jan 14, 2020
kvch added a commit that referenced this pull request Jan 14, 2020
* Add Google Cloud Platform support (#13598)

This PR introduces the support for Google Cloud Platform to Functionbeat. This branch is located in the `elastic/beats` repository, so anyone on our team has access to it.

To use the API to deploy, remove and update functions, users need to set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`. This variable should point to a JSON file which contains all the relevant information for Google to authenticate.

(About authentication for GCP libs: https://cloud.google.com/docs/authentication/getting-started)

* Cloud Functions Developer
* Cloud Functions Service Agent
* Service Account User
* Storage Admin
* Storage Object Admin

Note: Cloud Functions Developer role is in beta. We should not make GCP support GA, until it becomes stable.

```yaml
functionbeat.provider.gcp.location_id: "europe-west1"
functionbeat.provider.gcp.project_id: "my-project-123456"
functionbeat.provider.gcp.storage_name: "functionbeat-deploy"

functionbeat.provider.gcp.functions:
```

Function templates can be exported into YAML. With this YAML configuration, users can deploy the function using the [Google Cloud Deployment Manager](https://cloud.google.com/deployment-manager/).

A function under the folder `pkg/pubsub` is available to get events from Google Pub/Sub.

```yaml
  # Define the list of function availables, each function required to have a unique name.
  # Create a function that accepts events coming from Google Pub/Sub.
  - name: pubsub
    enabled: false
    type: pubsub

    # Description of the method to help identify them when you run multiples functions.
    description: "Google Cloud Function for Pub/Sub"

    # The maximum memory allocated for this function, the configured size must be a factor of 64.
    # Default is 256MiB.
    #memory_size: 256MiB

    # Execution timeout in seconds. If the function does not finish in time,
    # it is considered failed and terminated. Default is 60s.
    #timeout: 60s

    # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
    #service_account_email: {projectid}@appspot.gserviceaccount.com

    # Labels of the function.
    #labels:
    # mylabel: label

    # VPC Connector this function can connect to.
    # Format: projects/*/locations/*/connectors/* or fully-qualified URI
    #vpc_connector: ""

    # Number of maximum instances running at the same time. Default is unlimited.
    #maximum_instances: 0

    trigger:
      event_type: "providers/cloud.pubsub/eventTypes/topic.publish"
      resource: "projects/_/pubsub/myPubSub"
      #service: "pubsub.googleapis.com"

    # Optional fields that you can specify to add additional information to the
    # output. Fields can be scalar values, arrays, dictionaries, or any nested
    # combination of these.
    #fields:
    #  env: staging

    # Define custom processors for this function.
    #processors:
    #  - dissect:
    #      tokenizer: "%{key1} %{key2}"
```

A function under the folder pkg/storage is available to get events from Google Cloud Storage.

```yaml
 # Create a function that accepts events coming from Google Cloud Storage.
 - name: storage
   enabled: false
   type: storage

   # Description of the method to help identify them when you run multiples functions.
   description: "Google Cloud Function for Cloud Storage"

   # The maximum memory allocated for this function, the configured size must be a factor of 64.
   # Default is 256MiB.
   #memory_size: 256MiB

   # Execution timeout in seconds. If the function does not finish in time,
   # it is considered failed and terminated. Default is 60s.
   #timeout: 60s

   # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
   #service_account_email: {projectid}@appspot.gserviceaccount.com

   # Labels of the function.
   #labels:
   # mylabel: label

   # VPC Connector this function can connect to.
   # Format: projects/*/locations/*/connectors/* or fully-qualified URI
   #vpc_connector: ""

   # Number of maximum instances running at the same time. Default is unlimited.
   #maximum_instances: 0

   # Optional fields that you can specify to add additional information to the
   # output. Fields can be scalar values, arrays, dictionaries, or any nested
   # combination of these.
   #fields:
   #  env: staging

   # Define custom processors for this function.
   #processors:
   #  - dissect:
   #      tokenizer: "%{key1} %{key2}"
```

* `cloud.google.com/go/functions/metadata`
*  `cloud.google.com/go/storage`
(cherry picked from commit e8e18d0)

* fix vendor

* update notice

* add missing vendor

* update notice
sayden pushed a commit to sayden/beats that referenced this pull request Jan 15, 2020
This PR introduces the support for Google Cloud Platform to Functionbeat. This branch is located in the `elastic/beats` repository, so anyone on our team has access to it.

### Manager

#### Authentication

To use the API to deploy, remove and update functions, users need to set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`. This variable should point to a JSON file which contains all the relevant information for Google to authenticate.

(About authentication for GCP libs: https://cloud.google.com/docs/authentication/getting-started)

#### Required roles

* Cloud Functions Developer
* Cloud Functions Service Agent
* Service Account User
* Storage Admin
* Storage Object Admin

Note: Cloud Functions Developer role is in beta. We should not make GCP support GA, until it becomes stable.

#### Configuration

```yaml
# Configure functions to run on Google Cloud Platform, currently, we assume that the credentials
# are present in the environment to correctly create the function when using the CLI.
#
# Configure which region your project is located in.
functionbeat.provider.gcp.location_id: "europe-west1"
# Configure which Google Cloud project to deploy your functions.
functionbeat.provider.gcp.project_id: "my-project-123456"
# Configure the Google Cloud Storage we should upload the function artifact.
functionbeat.provider.gcp.storage_name: "functionbeat-deploy"

functionbeat.provider.gcp.functions:
```

#### Export

Function templates can be exported into YAML. With this YAML configuration, users can deploy the function using the [Google Cloud Deployment Manager](https://cloud.google.com/deployment-manager/).

### New functions

#### Google Pub/Sub

A function under the folder `pkg/pubsub` is available to get events from Google Pub/Sub.

##### Configuration

```yaml
  # Define the list of function availables, each function required to have a unique name.
  # Create a function that accepts events coming from Google Pub/Sub.
  - name: pubsub
    enabled: false
    type: pubsub

    # Description of the method to help identify them when you run multiples functions.
    description: "Google Cloud Function for Pub/Sub"

    # The maximum memory allocated for this function, the configured size must be a factor of 64.
    # Default is 256MiB.
    #memory_size: 256MiB

    # Execution timeout in seconds. If the function does not finish in time,
    # it is considered failed and terminated. Default is 60s.
    #timeout: 60s

    # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
    #service_account_email: {projectid}@appspot.gserviceaccount.com

    # Labels of the function.
    #labels:
    # mylabel: label

    # VPC Connector this function can connect to.
    # Format: projects/*/locations/*/connectors/* or fully-qualified URI
    #vpc_connector: ""

    # Number of maximum instances running at the same time. Default is unlimited.
    #maximum_instances: 0

    trigger:
      event_type: "providers/cloud.pubsub/eventTypes/topic.publish"
      resource: "projects/_/pubsub/myPubSub"
      #service: "pubsub.googleapis.com"

    # Optional fields that you can specify to add additional information to the
    # output. Fields can be scalar values, arrays, dictionaries, or any nested
    # combination of these.
    #fields:
    #  env: staging

    # Define custom processors for this function.
    #processors:
    #  - dissect:
    #      tokenizer: "%{key1} %{key2}"
```

#### Google Cloud Storage

A function under the folder pkg/storage is available to get events from Google Cloud Storage.

##### Configuration
```yaml
 # Create a function that accepts events coming from Google Cloud Storage.
 - name: storage
   enabled: false
   type: storage

   # Description of the method to help identify them when you run multiples functions.
   description: "Google Cloud Function for Cloud Storage"

   # The maximum memory allocated for this function, the configured size must be a factor of 64.
   # Default is 256MiB.
   #memory_size: 256MiB

   # Execution timeout in seconds. If the function does not finish in time,
   # it is considered failed and terminated. Default is 60s.
   #timeout: 60s

   # Email of the service account of the function. Defaults to {projectid}@appspot.gserviceaccount.com
   #service_account_email: {projectid}@appspot.gserviceaccount.com

   # Labels of the function.
   #labels:
   # mylabel: label

   # VPC Connector this function can connect to.
   # Format: projects/*/locations/*/connectors/* or fully-qualified URI
   #vpc_connector: ""

   # Number of maximum instances running at the same time. Default is unlimited.
   #maximum_instances: 0

   # Optional fields that you can specify to add additional information to the
   # output. Fields can be scalar values, arrays, dictionaries, or any nested
   # combination of these.
   #fields:
   #  env: staging

   # Define custom processors for this function.
   #processors:
   #  - dissect:
   #      tokenizer: "%{key1} %{key2}"
```

### Vendor
* `cloud.google.com/go/functions/metadata`
*  `cloud.google.com/go/storage`

(cherry picked from commit e8e18d0)

# Conflicts:
#	vendor/vendor.json
@kvch kvch mentioned this pull request Jan 17, 2020
14 tasks
@andresrc andresrc requested review from sayden and removed request for sayden January 20, 2020 20:20
@kvch
Copy link
Contributor Author

kvch commented Jan 31, 2020

Opened a PR to fix the indexing issue for storage: #16000

@sayden sayden added the test-plan-regression Manually testing this PR found a regression label Feb 3, 2020
@andresrc andresrc added the Team:Integrations Label for the Integrations team label Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Functionbeat release-highlight review Team:Integrations Label for the Integrations team test-plan Add this PR to be manual test plan test-plan-regression Manually testing this PR found a regression v7.6.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants