Repository: banzaicloud/logging-operator
Pull request #220 (2019-11-08 16:39:36)

Kinesis Stream support
| Q               | A
| --------------- | ---
| Bug fix?        | no
| New feature?    | yes
| API breaks?     | no
| Deprecations?   | no
| Related tickets | 
| License         | Apache 2.0


### What's in this PR?

Kinesis Stream support.

### Checklist
<!-- Place an '[x]' (no spaces) in all applicable fields. Please remove unrelated fields. -->

- [x] Error handling code meets the [guideline](https://github.com/banzaicloud/pipeline/blob/master/docs/error-handling-guide.md)
- [x] Logging code meets the guideline (TODO)
- [x] User guide and development docs updated (if needed)
- [x] Related Helm chart(s) updated (if needed)

### To Do
<!-- (Please remove this section if you don't need it.) -->
- [ ] If the PR is not complete but you want to discuss the approach, list what remains to be done here



Patch:
diff --git a/docs/plugins/Readme.md b/docs/plugins/Readme.md
index a3692dee..0ab611de 100644
--- a/docs/plugins/Readme.md
+++ b/docs/plugins/Readme.md
@@ -23,6 +23,7 @@
 - [forward](./outputs/forward.md)
 - [gcs](./outputs/gcs.md)
 - [kafka](./outputs/kafka.md)
+- [kinesis_stream](./outputs/kinesis_stream.md)
 - [loki](./outputs/loki.md)
 - [oss](./outputs/oss.md)
 - [s3](./outputs/s3.md)
diff --git a/docs/plugins/outputs/kinesis_stream.md b/docs/plugins/outputs/kinesis_stream.md
new file mode 100644
index 00000000..b70edee2
--- /dev/null
+++ b/docs/plugins/outputs/kinesis_stream.md
@@ -0,0 +1,44 @@
+# Kinesis Stream output plugin for Fluentd
+## Overview
+  More info at https://github.com/awslabs/aws-fluent-plugin-kinesis#configuration-kinesis_streams
+
+ #### Example output configurations
+ ```
+ spec:
+   kinesisStream:
+     streamName: example-stream-name
+     region: us-east-1
+     format:
+       type: json
+ ```
+
+## Configuration
+### KinesisStream
+#### Send your logs to a Kinesis Stream
+
+| Variable Name | Type | Required | Default | Description |
+|---|---|---|---|---|
+| kinesisStream | string | Yes | - | Name of the stream to put data.<br> |
+| partition_key | string | No | - | A key to extract partition key from JSON object. Default nil, which means partition key will be generated randomly.<br> |
+| aws_key_id | *secret.Secret | No | - | AWS access key id. This parameter is required when your agent is not running on EC2 instance with an IAM Role.<br> |
+| aws_sec_key | *secret.Secret | No | - | AWS secret key. This parameter is required when your agent is not running on EC2 instance with an IAM Role.<br> |
+| aws_ses_token | *secret.Secret | No | - | AWS session token. This parameter is optional, but can be provided if using MFA or temporary credentials when your agent is not running on EC2 instance with an IAM Role.<br> |
+| aws_iam_retries | int | No | - | The number of attempts to make (with exponential backoff) when loading instance profile credentials from the EC2 metadata service using an IAM role. Defaults to 5 retries.<br> |
+| assume_role_credentials | *KinesisStreamAssumeRoleCredentials | No | - | Typically, you can use AssumeRole for cross-account access or federation.<br> |
+| region | string | No | - | AWS region of your stream. It should be in form like us-east-1, us-west-2. Default nil, which means try to find from environment variable AWS_REGION.<br> |
+| retries_on_batch_request | int | No | - | The plugin will put multiple records to Amazon Kinesis Data Streams in batches using PutRecords. A set of records in a batch may fail for reasons documented in the Kinesis Service API Reference for PutRecords. Failed records will be retried retries_on_batch_request times<br> |
+| reset_backoff_if_success | bool | No | - | Boolean, default true. If enabled, when after retrying, the next retrying checks the number of succeeded records on the former batch request and reset exponential backoff if there is any success. Because batch request could be composed by requests across shards, simple exponential backoff for the batch request wouldn't work some cases.<br> |
+| batch_request_max_count | int | No | - | Integer, default 500. The number of max count of making batch request from record chunk. It can't exceed the default value because it's API limit.<br> |
+| batch_request_max_size | int | No | - | Integer. The number of max size of making batch request from record chunk. It can't exceed the default value because it's API limit.<br> |
+| format | *Format | Yes | - | [Format](./format.md)<br> |
+| buffer | *Buffer | No | - | [Buffer](./buffer.md)<br> |
+### Assume Role Credentials
+#### assume_role_credentials
+
+| Variable Name | Type | Required | Default | Description |
+|---|---|---|---|---|
+| role_arn | string | Yes | - | The Amazon Resource Name (ARN) of the role to assume<br> |
+| role_session_name | string | Yes | - | An identifier for the assumed role session<br> |
+| policy | string | No | - | An IAM policy in JSON format<br> |
+| duration_seconds | string | No | - | The duration, in seconds, of the role session (900-3600)<br> |
+| external_id | string | No | - | A unique identifier that is used by third parties when assuming roles in their customers' accounts.<br> |
diff --git a/pkg/sdk/model/output/kinesis_stream.go b/pkg/sdk/model/output/kinesis_stream.go
new file mode 100644
index 00000000..3d8b55ef
--- /dev/null
+++ b/pkg/sdk/model/output/kinesis_stream.go
@@ -0,0 +1,130 @@
+// Copyright © 2019 Banzai Cloud
+//
+// 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 output
+
+import (
+	"github.com/banzaicloud/logging-operator/pkg/sdk/model/secret"
+	"github.com/banzaicloud/logging-operator/pkg/sdk/model/types"
+)
+
+// +docName:"Kinesis Stream output plugin for Fluentd"
+//  More info at https://github.com/awslabs/aws-fluent-plugin-kinesis#configuration-kinesis_streams
+//
+// #### Example output configurations
+// ```
+// spec:
+//   kinesisStream:
+//     streamName: example-stream-name
+//     region: us-east-1
+//     format:
+//       type: json
+// ```
+type _docKinesisStream interface{}
+
+// +kubebuilder:object:generate=true
+// +docName:"KinesisStream"
+// Send your logs to a Kinesis Stream
+type KinesisStreamOutputConfig struct {
+
+	// Name of the stream to put data.
+	KinesisStream string `json:"kinesisStream"`
+
+	// A key to extract partition key from JSON object. Default nil, which means partition key will be generated randomly.
+	PartitionKey string `json:"partition_key,omitempty"`
+
+	// AWS access key id. This parameter is required when your agent is not running on EC2 instance with an IAM Role.
+	AWSKeyId *secret.Secret `json:"aws_key_id,omitempty"`
+
+	// AWS secret key. This parameter is required when your agent is not running on EC2 instance with an IAM Role.
+	AWSSECKey *secret.Secret `json:"aws_sec_key,omitempty"`
+
+	// AWS session token. This parameter is optional, but can be provided if using MFA or temporary credentials when your agent is not running on EC2 instance with an IAM Role.
+	AWSSESToken *secret.Secret `json:"aws_ses_token,omitempty"`
+
+	// The number of attempts to make (with exponential backoff) when loading instance profile credentials from the EC2 metadata service using an IAM role. Defaults to 5 retries.
+	AWSIAMRetries int `json:"aws_iam_retries,omitempty"`
+
+	// Typically, you can use AssumeRole for cross-account access or federation.
+	AssumeRoleCredentials *KinesisStreamAssumeRoleCredentials `json:"assume_role_credentials,omitempty"`
+
+	// AWS region of your stream. It should be in form like us-east-1, us-west-2. Default nil, which means try to find from environment variable AWS_REGION.
+	Region string `json:"region,omitempty"`
+
+	// The plugin will put multiple records to Amazon Kinesis Data Streams in batches using PutRecords. A set of records in a batch may fail for reasons documented in the Kinesis Service API Reference for PutRecords. Failed records will be retried retries_on_batch_request times
+	RetriesOnBatchRequest int `json:"retries_on_batch_request,omitempty"`
+
+	// Boolean, default true. If enabled, when after retrying, the next retrying checks the number of succeeded records on the former batch request and reset exponential backoff if there is any success. Because batch request could be composed by requests across shards, simple exponential backoff for the batch request wouldn't work some cases.
+	ResetBackoffIfSuccess bool `json:"reset_backoff_if_success,omitempty"`
+
+	// Integer, default 500. The number of max count of making batch request from record chunk. It can't exceed the default value because it's API limit.
+	BatchRequestMaxCount int `json:"batch_request_max_count,omitempty"`
+
+	// Integer. The number of max size of making batch request from record chunk. It can't exceed the default value because it's API limit.
+	BatchRequestMaxSize int `json:"batch_request_max_size,omitempty"`
+
+	// +docLink:"Format,./format.md"
+	Format *Format `json:"format"`
+	// +docLink:"Buffer,./buffer.md"
+	Buffer *Buffer `json:"buffer,omitempty"`
+}
+
+// +kubebuilder:object:generate=true
+// +docName:"Assume Role Credentials"
+// assume_role_credentials
+type KinesisStreamAssumeRoleCredentials struct {
+	// The Amazon Resource Name (ARN) of the role to assume
+	RoleArn string `json:"role_arn"`
+	// An identifier for the assumed role session
+	RoleSessionName string `json:"role_session_name"`
+	// An IAM policy in JSON format
+	Policy string `json:"policy,omitempty"`
+	// The duration, in seconds, of the role session (900-3600)
+	DurationSeconds string `json:"duration_seconds,omitempty"`
+	// A unique identifier that is used by third parties when assuming roles in their customers' accounts.
+	ExternalId string `json:"external_id,omitempty"`
+}
+
+func (e *KinesisStreamOutputConfig) ToDirective(secretLoader secret.SecretLoader, id string) (types.Directive, error) {
+	pluginType := "kinesis_streams"
+	pluginID := id + "_" + pluginType
+	kinesis := &types.OutputPlugin{
+		PluginMeta: types.PluginMeta{
+			Type:      pluginType,
+			Directive: "match",
+			Tag:       "**",
+			Id:        pluginID,
+		},
+	}
+	if params, err := types.NewStructToStringMapper(secretLoader).StringsMap(e); err != nil {
+		return nil, err
+	} else {
+		kinesis.Params = params
+	}
+	if e.Buffer != nil {
+		if buffer, err := e.Buffer.ToDirective(secretLoader, pluginID); err != nil {
+			return nil, err
+		} else {
+			kinesis.SubDirectives = append(kinesis.SubDirectives, buffer)
+		}
+	}
+	if e.Format != nil {
+		if format, err := e.Format.ToDirective(secretLoader, ""); err != nil {
+			return nil, err
+		} else {
+			kinesis.SubDirectives = append(kinesis.SubDirectives, format)
+		}
+	}
+	return kinesis, nil
+}
diff --git a/pkg/sdk/model/output/zz_generated.deepcopy.go b/pkg/sdk/model/output/zz_generated.deepcopy.go
index 4a69892a..b9bd9c74 100644
--- a/pkg/sdk/model/output/zz_generated.deepcopy.go
+++ b/pkg/sdk/model/output/zz_generated.deepcopy.go
@@ -314,6 +314,66 @@ func (in *KafkaOutputConfig) DeepCopy() *KafkaOutputConfig {
 	return out
 }
 
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
+func (in *KinesisStreamAssumeRoleCredentials) DeepCopyInto(out *KinesisStreamAssumeRoleCredentials) {
+	*out = *in
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KinesisStreamAssumeRoleCredentials.
+func (in *KinesisStreamAssumeRoleCredentials) DeepCopy() *KinesisStreamAssumeRoleCredentials {
+	if in == nil {
+		return nil
+	}
+	out := new(KinesisStreamAssumeRoleCredentials)
+	in.DeepCopyInto(out)
+	return out
+}
+
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
+func (in *KinesisStreamOutputConfig) DeepCopyInto(out *KinesisStreamOutputConfig) {
+	*out = *in
+	if in.AWSKeyId != nil {
+		in, out := &in.AWSKeyId, &out.AWSKeyId
+		*out = new(secret.Secret)
+		(*in).DeepCopyInto(*out)
+	}
+	if in.AWSSECKey != nil {
+		in, out := &in.AWSSECKey, &out.AWSSECKey
+		*out = new(secret.Secret)
+		(*in).DeepCopyInto(*out)
+	}
+	if in.AWSSESToken != nil {
+		in, out := &in.AWSSESToken, &out.AWSSESToken
+		*out = new(secret.Secret)
+		(*in).DeepCopyInto(*out)
+	}
+	if in.AssumeRoleCredentials != nil {
+		in, out := &in.AssumeRoleCredentials, &out.AssumeRoleCredentials
+		*out = new(KinesisStreamAssumeRoleCredentials)
+		**out = **in
+	}
+	if in.Format != nil {
+		in, out := &in.Format, &out.Format
+		*out = new(Format)
+		**out = **in
+	}
+	if in.Buffer != nil {
+		in, out := &in.Buffer, &out.Buffer
+		*out = new(Buffer)
+		**out = **in
+	}
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KinesisStreamOutputConfig.
+func (in *KinesisStreamOutputConfig) DeepCopy() *KinesisStreamOutputConfig {
+	if in == nil {
+		return nil
+	}
+	out := new(KinesisStreamOutputConfig)
+	in.DeepCopyInto(out)
+	return out
+}
+
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *LokiOutput) DeepCopyInto(out *LokiOutput) {
 	*out = *in


Test patch:
diff --git a/pkg/sdk/model/output/kinesis_stream_test.go b/pkg/sdk/model/output/kinesis_stream_test.go
new file mode 100644
index 00000000..bef3e678
--- /dev/null
+++ b/pkg/sdk/model/output/kinesis_stream_test.go
@@ -0,0 +1,49 @@
+// Copyright © 2019 Banzai Cloud
+//
+// 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 output_test
+
+import (
+	"testing"
+
+	"github.com/banzaicloud/logging-operator/pkg/sdk/model/output"
+	"github.com/banzaicloud/logging-operator/pkg/sdk/model/render"
+	"github.com/ghodss/yaml"
+)
+
+func TestKinesisStream(t *testing.T) {
+	CONFIG := []byte(`
+streamName: test
+region: us-east-1
+format:
+  type: json
+buffer:
+  timekey: 1m
+  timekey_wait: 30s
+  timekey_use_utc: true
+`)
+	expected := `
+  <match **>
+    @type kinesis_streams
+    region us-east-1
+    <format>
+      @type json
+    </format>
+  </match>
+`
+	kinesis := &output.KinesisStreamOutputConfig{}
+	yaml.Unmarshal(CONFIG, kinesis)
+	test := render.NewOutputPluginTest(t, kinesis)
+	test.DiffResult(expected)
+}