-
Notifications
You must be signed in to change notification settings - Fork 26
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
Introduce Snowflake backend for Theia #112
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d064870
Introduce Snowflake backend for Theia
antoninbas 52d3390
Address review comments
antoninbas cdc7bf1
Add copyright notice to new Go source files
antoninbas 620c3b8
Address review comments
antoninbas 5d882fd
Update to SQL migrations
antoninbas 19c7020
Add transport ports to pods view
antoninbas 8a8d0e5
Remove timeInserted from pods view
antoninbas 5987af9
Fix SQL migrations
antoninbas cecea6d
Add missing copyright headers
antoninbas 9d84c7c
s/bucketName=flows/bucketPrefix=flows/ in Helm install
antoninbas 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 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 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 @@ | ||
creds |
This file contains 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,16 @@ | ||
GO ?= go | ||
BINDIR := $(CURDIR)/bin | ||
|
||
all: bin | ||
|
||
.PHONY: bin | ||
bin: | ||
$(GO) build -o $(BINDIR)/theia-sf antrea.io/theia/snowflake | ||
|
||
.PHONY: test | ||
test: | ||
$(GO) test -v ./... | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf bin |
This file contains 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,119 @@ | ||
# Theia with Snowflake | ||
|
||
We are introducing the ability to use Snowflake as the storage and computing | ||
platform for Theia. When using Snowflake, it is no longer necessary to run | ||
ClickHouse DB (flow records are stored in a Snowflake database) or Spark (flow | ||
processing is done by Snowflake virtual warehouses). Using Snowflake for Theia | ||
means that you have to bring your own AWS and Snowflake accounts (you will be | ||
charged for resource usage) and some features available with "standard" Theia | ||
are not available yet with Snowflake. | ||
|
||
## Getting started | ||
|
||
### Prerequisites | ||
|
||
Theia with Snowflake requires Antrea >= v1.9.0 and Theia >= v0.3.0. | ||
|
||
### Install the theia-sf CLI | ||
|
||
Because it is not yet distributed as a release asset, you will need to build the | ||
CLI yourself, which requires Git, Golang and Make. | ||
|
||
```bash | ||
git clone git@github.com:antrea-io/theia.git | ||
cd theia/snowflake | ||
make | ||
``` | ||
|
||
### Configure AWS credentials | ||
|
||
Follow the steps in the [AWS | ||
documentation](https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials) | ||
to specify credentials that can be used by the AWS Go SDK. Either configure the | ||
~/.aws/credentials file or set the required environment variables. | ||
|
||
You can also export the `AWS_REGION` environment variable, set to your preferred | ||
region. | ||
|
||
### Configure Snowflake credentials | ||
|
||
Export the following environment variables: `SNOWFLAKE_ACCOUNT`, | ||
`SNOWFLAKE_USER`, `SNOWFLAKE_PASSWORD`. | ||
|
||
### Create an S3 bucket to store infrastructure state | ||
|
||
You may skip this step if you already have a bucket that you want to use. | ||
|
||
```bash | ||
./bin/theia-sf create-bucket | ||
``` | ||
|
||
Retrieve the bucket name output by the command. | ||
|
||
### Create a KMS key to encrypt infrastructure state | ||
|
||
You may skip this step if you already have a KMS key that you want to use. If | ||
you choose *not* to use a KMS key, Snowflake credentials will be stored in your | ||
S3 bucket in clear text. | ||
|
||
```bash | ||
./bin/theia-sf create-kms-key | ||
``` | ||
|
||
Retrieve the key ID output by the command. | ||
|
||
### Provision all cloud resources | ||
|
||
```bash | ||
./bin/theia-sf onboard --bucket-name <BUCKET NAME> --key-id <KEY ID> | ||
``` | ||
|
||
The command output will include a table like this one, with important information: | ||
|
||
```text | ||
+----------------------------+------------------------------------------------------------------+ | ||
| Region | us-west-2 | | ||
| Bucket Name | antrea-flows-93e9ojn80fgn5vwt | | ||
| Bucket Flows Folder | flows | | ||
| Snowflake Database Name | ANTREA_93E9OJN80FGN5VWT | | ||
| Snowflake Schema Name | THEIA | | ||
| Snowflake Flows Table Name | FLOWS | | ||
| SNS Topic ARN | arn:aws:sns:us-west-2:867393676014:antrea-flows-93e9ojn80fgn5vwt | | ||
| SQS Queue ARN | arn:aws:sqs:us-west-2:867393676014:antrea-flows-93e9ojn80fgn5vwt | | ||
+----------------------------+------------------------------------------------------------------+ | ||
``` | ||
|
||
### Configure the Flow Aggregator in your cluster(s) | ||
|
||
```bash | ||
helm repo add antrea https://charts.antrea.io | ||
helm repo update | ||
helm install antrea antrea/antrea -n kube-system --set featureGates.FlowExporter=true | ||
helm install flow-aggregator antrea/flow-aggregator \ | ||
heanlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--set s3Uploader.enable=true \ | ||
--set s3Uploader.bucketName=<BUCKET NAME> \ | ||
--set s3Uploader.bucketPrefix=flows \ | ||
--set s3Uploader.awsCredentials.aws_access_key_id=<AWS ACCESS KEY ID> \ | ||
--set s3Uploader.awsCredentials.aws_secret_access_key=<AWS SECRET ACCESS KEY> \ | ||
-n flow-aggregator --create-namespace | ||
``` | ||
|
||
## Clean up | ||
|
||
Follow these steps if you want to delete all resources created by | ||
`theia-sf`. Just like for [onboarding](#getting-started), AWS credentials and | ||
Snowflake credentials are required. | ||
|
||
```bash | ||
# always call offboard with the same arguments as onboard! | ||
./bin/theia-sf offboard --bucket-name <BUCKET NAME> --key-id <KEY ID> | ||
# if you created a KMS key and you want to schedule it for deletion | ||
./bin/theia-sf delete-kms-key --key-id <KEY ID> | ||
# if you created an S3 bucket to store infra state and you want to delete it | ||
./bin/theia-sf delete-bucket --name <BUCKET NAME> | ||
``` | ||
|
||
## Running applications | ||
|
||
We are in the process of adding support for applications to Snowflake-powered | ||
Theia, starting with NetworkPolicy recommendation. |
This file contains 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,108 @@ | ||
// Copyright 2022 Antrea Authors. | ||
// | ||
// Licensed 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. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
awsconfig "github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types" | ||
"github.com/dustinkirkland/golang-petname" | ||
"github.com/spf13/cobra" | ||
|
||
s3client "antrea.io/theia/snowflake/pkg/aws/client/s3" | ||
) | ||
|
||
// createBucketCmd represents the create-bucket command | ||
var createBucketCmd = &cobra.Command{ | ||
Use: "create-bucket", | ||
Short: "Create an AWS S3 bucket", | ||
Long: `This command creates a new S3 bucket in your AWS account. This is | ||
useful if you don't already have a bucket available to store infrastructure | ||
state (such a bucket is required for the "onboard" command). | ||
|
||
Before calling this command, ensure that AWS credentials are available: | ||
https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials | ||
|
||
To create a bucket with a specific name: | ||
"theia-sf create-bucket --name this-is-the-name-i-want" | ||
|
||
To create a bucket with a random name in a specific non-default region: | ||
"theia-sf create-bucket --region us-east-2"`, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
region, _ := cmd.Flags().GetString("region") | ||
bucketName, _ := cmd.Flags().GetString("name") | ||
bucketPrefix, _ := cmd.Flags().GetString("prefix") | ||
if bucketName == "" { | ||
suffix := petname.Generate(4, "-") | ||
bucketName = fmt.Sprintf("%s-%s", bucketPrefix, suffix) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(region)) | ||
if err != nil { | ||
return fmt.Errorf("unable to load AWS SDK config: %w", err) | ||
|
||
} | ||
s3Client := s3client.GetClient(awsCfg) | ||
if err := createBucket(ctx, s3Client, bucketName, region); err != nil { | ||
return err | ||
} | ||
fmt.Printf("Bucket name: %s\n", bucketName) | ||
return nil | ||
}, | ||
} | ||
|
||
func createBucket(ctx context.Context, s3Client s3client.Interface, name string, region string) error { | ||
logger := logger.WithValues("bucket", name, "region", region) | ||
logger.Info("Checking if S3 bucket exists") | ||
_, err := s3Client.HeadBucket(ctx, &s3.HeadBucketInput{ | ||
Bucket: &name, | ||
}) | ||
var notFoundError *s3types.NotFound | ||
if err != nil && !errors.As(err, ¬FoundError) { | ||
return fmt.Errorf("error when looking for bucket '%s': %w", name, err) | ||
} | ||
if notFoundError == nil { | ||
logger.Info("S3 bucket already exists") | ||
return nil | ||
} | ||
logger.Info("Creating S3 bucket") | ||
if _, err := s3Client.CreateBucket(ctx, &s3.CreateBucketInput{ | ||
Bucket: &name, | ||
ACL: s3types.BucketCannedACLPrivate, | ||
CreateBucketConfiguration: &s3types.CreateBucketConfiguration{ | ||
LocationConstraint: s3types.BucketLocationConstraint(region), | ||
}, | ||
}); err != nil { | ||
return fmt.Errorf("error when creating bucket '%s': %w", name, err) | ||
} | ||
logger.Info("Created S3 bucket") | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(createBucketCmd) | ||
|
||
createBucketCmd.Flags().String("region", GetEnv("AWS_REGION", defaultRegion), "region where bucket should be created") | ||
createBucketCmd.Flags().String("name", "", "name of bucket to create") | ||
createBucketCmd.Flags().String("prefix", "antrea", "prefix to use for bucket name (with auto-generated suffix)") | ||
createBucketCmd.MarkFlagsMutuallyExclusive("name", "prefix") | ||
} |
This file contains 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,80 @@ | ||
// Copyright 2022 Antrea Authors. | ||
// | ||
// Licensed 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. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
awsconfig "github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/kms" | ||
"github.com/spf13/cobra" | ||
|
||
kmsclient "antrea.io/theia/snowflake/pkg/aws/client/kms" | ||
) | ||
|
||
// createKmsKeyCmd represents the create-kms-key command | ||
var createKmsKeyCmd = &cobra.Command{ | ||
Use: "create-kms-key", | ||
Short: "Create an AWS KMS key", | ||
Long: `This command creates a new KMS key in your AWS account. This key | ||
can be used to encrypt infrastructure state in the backend (S3 bucket). If you | ||
heanlan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
already have a KMS key that you want to use, you won't need this command. | ||
|
||
Before calling this command, ensure that AWS credentials are available: | ||
https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials | ||
|
||
To create a KMS key and obtain its ID: | ||
"theia-sf create-kms-key"`, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
region, _ := cmd.Flags().GetString("region") | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(region)) | ||
if err != nil { | ||
return fmt.Errorf("unable to load AWS SDK config: %w", err) | ||
|
||
} | ||
kmsClient := kmsclient.GetClient(awsCfg) | ||
keyID, err := createKey(ctx, kmsClient) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Key ID: %s\n", keyID) | ||
return nil | ||
}, | ||
} | ||
|
||
func createKey(ctx context.Context, kmsClient kmsclient.Interface) (string, error) { | ||
logger.Info("Creating key") | ||
description := "This key was created by theia-sf; it is used to encrypt infrastructure state" | ||
output, err := kmsClient.CreateKey(ctx, &kms.CreateKeyInput{ | ||
Description: &description, | ||
// we use default parameters for everything else | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("error when creating key: %w", err) | ||
} | ||
logger.Info("Created key") | ||
return *output.KeyMetadata.KeyId, nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(createKmsKeyCmd) | ||
|
||
createKmsKeyCmd.Flags().String("region", GetEnv("AWS_REGION", defaultRegion), "region where key should be created") | ||
} |
This file contains 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,19 @@ | ||
// Copyright 2022 Antrea Authors. | ||
// | ||
// Licensed 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. | ||
|
||
package cmd | ||
|
||
const ( | ||
defaultRegion = "us-west-2" | ||
) |
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.
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.
this is fine I think, "set" is also the past participle of the "set" verb