mirror of https://github.com/kubernetes/kops.git
189 lines
4.9 KiB
Go
189 lines
4.9 KiB
Go
/*
|
|
Copyright 2019 The Kubernetes 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 awstasks
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/service/eventbridge"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
"k8s.io/kops/upup/pkg/fi"
|
|
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
|
|
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
|
|
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
|
|
)
|
|
|
|
// +kops:fitask
|
|
type EventBridgeRule struct {
|
|
ID *string
|
|
Name *string
|
|
Lifecycle *fi.Lifecycle
|
|
|
|
EventPattern *string
|
|
TargetArn *string // required for cloudformation rendering
|
|
|
|
Tags map[string]string
|
|
}
|
|
|
|
var _ fi.CompareWithID = &EventBridgeRule{}
|
|
|
|
func (eb *EventBridgeRule) CompareWithID() *string {
|
|
return eb.Name
|
|
}
|
|
|
|
func (eb *EventBridgeRule) Find(c *fi.Context) (*EventBridgeRule, error) {
|
|
cloud := c.Cloud.(awsup.AWSCloud)
|
|
|
|
if eb.Name == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
request := &eventbridge.ListRulesInput{
|
|
NamePrefix: eb.Name,
|
|
}
|
|
response, err := cloud.EventBridge().ListRules(request)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error listing EventBridge rules: %v", err)
|
|
}
|
|
if response == nil || len(response.Rules) == 0 {
|
|
return nil, nil
|
|
}
|
|
if len(response.Rules) > 1 {
|
|
return nil, fmt.Errorf("found multiple EventBridge rules with the same name")
|
|
}
|
|
|
|
rule := response.Rules[0]
|
|
|
|
tagResponse, err := cloud.EventBridge().ListTagsForResource(&eventbridge.ListTagsForResourceInput{ResourceARN: rule.Arn})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error listing tags for EventBridge rule: %v", err)
|
|
}
|
|
|
|
actual := &EventBridgeRule{
|
|
ID: eb.ID,
|
|
Name: eb.Name,
|
|
Lifecycle: eb.Lifecycle,
|
|
EventPattern: rule.EventPattern,
|
|
TargetArn: eb.TargetArn,
|
|
Tags: mapEventBridgeTagsToMap(tagResponse.Tags),
|
|
}
|
|
return actual, nil
|
|
}
|
|
|
|
func (eb *EventBridgeRule) Run(c *fi.Context) error {
|
|
return fi.DefaultDeltaRunMethod(eb, c)
|
|
}
|
|
|
|
func (_ *EventBridgeRule) CheckChanges(a, e, changes *EventBridgeRule) error {
|
|
if a == nil {
|
|
if e.Name == nil {
|
|
return field.Required(field.NewPath("Name"), "")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (eb *EventBridgeRule) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *EventBridgeRule) error {
|
|
if a == nil {
|
|
var tags []*eventbridge.Tag
|
|
for k, v := range eb.Tags {
|
|
tags = append(tags, &eventbridge.Tag{
|
|
Key: aws.String(k),
|
|
Value: aws.String(v),
|
|
})
|
|
}
|
|
|
|
request := &eventbridge.PutRuleInput{
|
|
Name: eb.Name,
|
|
EventPattern: e.EventPattern,
|
|
Tags: tags,
|
|
}
|
|
|
|
_, err := t.Cloud.EventBridge().PutRule(request)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating EventBridge rule: %v", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type terraformEventBridgeRule struct {
|
|
Name *string `json:"name" cty:"name"`
|
|
EventPattern *terraform.Literal `json:"event_pattern" cty:"event_pattern"`
|
|
Tags map[string]string `json:"tags,omitempty" cty:"tags"`
|
|
}
|
|
|
|
func (_ *EventBridgeRule) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *EventBridgeRule) error {
|
|
m, err := t.AddFile("aws_cloudwatch_event_rule", *e.Name, "event_pattern", fi.NewStringResource(*e.EventPattern), false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tf := &terraformEventBridgeRule{
|
|
Name: e.Name,
|
|
EventPattern: m,
|
|
Tags: e.Tags,
|
|
}
|
|
|
|
return t.RenderResource("aws_cloudwatch_event_rule", *e.Name, tf)
|
|
}
|
|
|
|
func (eb *EventBridgeRule) TerraformLink() *terraform.Literal {
|
|
return terraform.LiteralProperty("aws_cloudwatch_event_rule", fi.StringValue(eb.Name), "id")
|
|
}
|
|
|
|
type cloudformationTarget struct {
|
|
Id *string
|
|
Arn *string
|
|
}
|
|
|
|
type cloudformationEventBridgeRule struct {
|
|
Name *string `json:"Name"`
|
|
EventPattern map[string]interface{} `json:"EventPattern"`
|
|
Targets []cloudformationTarget `json:"Targets"`
|
|
}
|
|
|
|
func (_ *EventBridgeRule) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *EventBridgeRule) error {
|
|
// convert event pattern string into a json struct
|
|
jsonString, err := fi.ResourceAsBytes(fi.NewStringResource(*e.EventPattern))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data := make(map[string]interface{})
|
|
err = json.Unmarshal(jsonString, &data)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing SQS PolicyDocument: %v", err)
|
|
}
|
|
|
|
target := &cloudformationTarget{
|
|
Id: s("1"),
|
|
Arn: e.TargetArn,
|
|
}
|
|
|
|
cf := &cloudformationEventBridgeRule{
|
|
Name: e.Name,
|
|
EventPattern: data,
|
|
Targets: []cloudformationTarget{*target},
|
|
}
|
|
|
|
return t.RenderResource("AWS::Events::Rule", *e.Name, cf)
|
|
}
|