forked from vmware-archive/kubeless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
134 lines (121 loc) · 4.59 KB
/
publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright (c) 2016-2017 Bitnami
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 kinesis
import (
"net/url"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/kubeless/kubeless/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var publishCmd = &cobra.Command{
Use: "publish FLAG",
Short: "publish message to a Kinesis stream",
Long: `publish message to a Kinesis stream`,
Run: func(cmd *cobra.Command, args []string) {
records, err := cmd.Flags().GetStringArray("records")
if err != nil {
logrus.Fatal(err)
}
streamName, err := cmd.Flags().GetString("stream")
if err != nil {
logrus.Fatal(err)
}
region, err := cmd.Flags().GetString("aws-region")
if err != nil {
logrus.Fatal(err)
}
key, err := cmd.Flags().GetString("partition-key")
if err != nil {
logrus.Fatal(err)
}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
logrus.Fatal(err)
}
if ns == "" {
ns = utils.GetDefaultNamespace()
}
secretName, err := cmd.Flags().GetString("secret")
if err != nil {
logrus.Fatal(err)
}
client := utils.GetClientOutOfCluster()
secret, err := client.Core().Secrets(ns).Get(secretName, metav1.GetOptions{})
if err != nil {
logrus.Errorf("Error getting secret: %s necessary to connect to AWS services. Erro: %v", secretName, err)
}
if _, ok := secret.Data["aws_access_key_id"]; !ok {
logrus.Fatalf("Error getting aws_access_key_id from the secret: %s necessary to connect to AWS Kinesis service. Error: %v", secretName, err)
}
if _, ok := secret.Data["aws_secret_access_key"]; !ok {
logrus.Fatalf("Error getting aws_aaws_secret_access_keyccess_key_id from the secret: %s necessary to connect to AWS Kinesis service. Error: %v", secretName, err)
}
awsAccessKey := string(secret.Data["aws_access_key_id"][:])
awsSecretAccessKey := string(secret.Data["aws_secret_access_key"][:])
endpointURL, err := cmd.Flags().GetString("endpoint")
if err != nil {
logrus.Fatal(err)
}
if len(endpointURL) > 0 {
_, err = url.ParseRequestURI(endpointURL)
if err != nil {
panic(err)
}
}
customCreds := credentials.NewStaticCredentials(awsAccessKey, awsSecretAccessKey, "")
var s *session.Session
if len(endpointURL) > 0 {
s = session.New(&aws.Config{Region: aws.String(region), Endpoint: aws.String(endpointURL), Credentials: customCreds})
} else {
s = session.New(&aws.Config{Region: aws.String(region), Credentials: customCreds})
}
kc := kinesis.New(s)
entries := make([]*kinesis.PutRecordsRequestEntry, len(records))
for i, record := range records {
entries[i] = &kinesis.PutRecordsRequestEntry{
Data: []byte(record),
PartitionKey: aws.String(key),
}
}
_, err = kc.PutRecords(&kinesis.PutRecordsInput{
Records: entries,
StreamName: aws.String(streamName),
})
if err != nil {
panic("Failed to put to record in the stream. Error: " + err.Error())
}
},
}
func init() {
var records []string
publishCmd.Flags().StringP("stream", "", "", "Name of the AWS Kinesis stream")
publishCmd.Flags().StringP("aws-region", "", "", "AWS region in which stream is available")
publishCmd.Flags().StringP("partition-key", "", "", "partiion key to use put message in AWS kinesis stream")
publishCmd.Flags().StringArray("records", records, "Specify list of records to be published to the stream")
publishCmd.Flags().StringP("endpoint", "", "", "Override AWS's default service URL with the given URL")
publishCmd.Flags().StringP("secret", "", "", "Kubernetes secret that has AWS access key and secret key")
publishCmd.Flags().StringP("namespace", "", "", "Specify namespace for the Kinesis trigger")
publishCmd.MarkFlagRequired("stream")
publishCmd.MarkFlagRequired("aws-region")
publishCmd.MarkFlagRequired("partition-key")
publishCmd.MarkFlagRequired("message")
publishCmd.MarkFlagRequired("aws_access_key_id")
publishCmd.MarkFlagRequired("aws_secret_access_key")
publishCmd.MarkFlagRequired("records")
publishCmd.MarkFlagRequired("secret")
}