Skip to content
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

Refactor queue creation and attribute handling #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strconv"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -17,7 +18,16 @@ import (

const (
longPollingWaitTimeSeconds = 20
queuePolicy = `
)

var queuePolicy = loadQueuePolicy()

func loadQueuePolicy() string {
filePath := "/etc/lifecycled/queue_policy.json"
policy, err := os.ReadFile(filePath)
if err != nil {
// Handle the error, e.g., by providing a default policy
return `
{
"Version":"2012-10-17",
"Statement":[
Expand All @@ -35,7 +45,9 @@ const (
]
}
`
)
}
return string(policy)
}

// SQSClient for testing purposes
//go:generate mockgen -destination=mocks/mock_sqs_client.go -package=mocks github.com/buildkite/lifecycled SQSClient
Expand Down Expand Up @@ -69,16 +81,24 @@ func NewQueue(queueName, topicArn string, sqsClient SQSClient, snsClient SNSClie

// Create the SQS queue.
func (q *Queue) Create() error {
attributes := map[string]*string{
"Policy": aws.String(fmt.Sprintf(queuePolicy, q.topicArn)),
"ReceiveMessageWaitTimeSeconds": aws.String(strconv.Itoa(longPollingWaitTimeSeconds)),
}

kmsMasterKeyID := os.Getenv("KMS_MASTER_KEY_ID")
if kmsMasterKeyID != "" {
attributes["KMSMasterKeyId"] = aws.String(kmsMasterKeyID)
}

out, err := q.sqsClient.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(q.name),
Attributes: map[string]*string{
"Policy": aws.String(fmt.Sprintf(queuePolicy, q.topicArn)),
"ReceiveMessageWaitTimeSeconds": aws.String(strconv.Itoa(longPollingWaitTimeSeconds)),
},
QueueName: aws.String(q.name),
Attributes: attributes,
})
if err != nil {
return err
}

q.url = aws.StringValue(out.QueueUrl)
return nil
}
Expand Down