sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/instancestate/queue.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package instancestate 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "strings" 23 24 "github.com/aws/aws-sdk-go/aws" 25 "github.com/aws/aws-sdk-go/aws/awserr" 26 "github.com/aws/aws-sdk-go/service/sqs" 27 "github.com/pkg/errors" 28 29 iamv1 "sigs.k8s.io/cluster-api-provider-aws/iam/api/v1beta1" 30 ) 31 32 func (s *Service) reconcileSQSQueue() error { 33 attrs := make(map[string]string) 34 attrs[sqs.QueueAttributeNameReceiveMessageWaitTimeSeconds] = "20" 35 36 _, err := s.SQSClient.CreateQueue(&sqs.CreateQueueInput{ 37 QueueName: aws.String(GenerateQueueName(s.scope.Name())), 38 Attributes: aws.StringMap(attrs), 39 }) 40 41 if err != nil { 42 if aerr, ok := err.(awserr.Error); ok { 43 if aerr.Code() == sqs.ErrCodeQueueNameExists { 44 return nil 45 } 46 } 47 } 48 return errors.Wrap(err, "unable to create new queue") 49 } 50 51 func (s *Service) deleteSQSQueue() error { 52 resp, err := s.SQSClient.GetQueueUrl(&sqs.GetQueueUrlInput{QueueName: aws.String(GenerateQueueName(s.scope.Name()))}) 53 if err != nil { 54 if queueNotFoundError(err) { 55 return nil 56 } 57 return errors.Wrap(err, "unable to get queue URL") 58 } 59 _, err = s.SQSClient.DeleteQueue(&sqs.DeleteQueueInput{QueueUrl: resp.QueueUrl}) 60 if err != nil && queueNotFoundError(err) { 61 return nil 62 } 63 64 return errors.Wrap(err, "unable to delete queue") 65 } 66 67 func (s *Service) createPolicyForRule(input *createPolicyForRuleInput) error { 68 attrs := make(map[string]string) 69 policy := iamv1.PolicyDocument{ 70 Version: iamv1.CurrentVersion, 71 ID: input.QueueArn, 72 Statement: iamv1.Statements{ 73 iamv1.StatementEntry{ 74 Sid: fmt.Sprintf("CAPAEvents_%s_%s", s.getEC2RuleName(), GenerateQueueName(s.scope.Name())), 75 Effect: iamv1.EffectAllow, 76 Principal: iamv1.Principals{iamv1.PrincipalService: iamv1.PrincipalID{"events.amazonaws.com"}}, 77 Action: iamv1.Actions{"sqs:SendMessage"}, 78 Resource: iamv1.Resources{input.QueueArn}, 79 Condition: iamv1.Conditions{ 80 "ArnEquals": map[string]string{"aws:SourceArn": input.RuleArn}, 81 }, 82 }, 83 }, 84 } 85 policyData, err := json.Marshal(policy) 86 if err != nil { 87 return errors.Wrap(err, "unable to JSON marshal policy") 88 } 89 attrs[sqs.QueueAttributeNamePolicy] = string(policyData) 90 91 _, err = s.SQSClient.SetQueueAttributes(&sqs.SetQueueAttributesInput{ 92 QueueUrl: aws.String(input.QueueURL), 93 Attributes: aws.StringMap(attrs), 94 }) 95 96 return errors.Wrap(err, "unable to update queue attributes") 97 } 98 99 // GenerateQueueName will generate a queue name. 100 func GenerateQueueName(clusterName string) string { 101 adjusted := strings.ReplaceAll(clusterName, ".", "-") 102 return fmt.Sprintf("%s-queue", adjusted) 103 } 104 105 func queueNotFoundError(err error) bool { 106 if aerr, ok := err.(awserr.Error); ok { 107 if aerr.Code() == sqs.ErrCodeQueueDoesNotExist { 108 return true 109 } 110 } 111 return false 112 } 113 114 type createPolicyForRuleInput struct { 115 QueueArn string 116 QueueURL string 117 RuleArn string 118 }