Skip to content
Closed
Show file tree
Hide file tree
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
20 changes: 17 additions & 3 deletions cloudmock/aws/mocksqs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package mocksqs
import (
"context"
"fmt"
"sort"
"strings"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -74,9 +76,12 @@ func (m *MockSQS) ListQueues(ctx context.Context, input *sqs.ListQueuesInput, op

response := &sqs.ListQueuesOutput{}

if queue, ok := m.Queues[*input.QueueNamePrefix]; ok {
response.QueueUrls = []string{aws.ToString(queue.url)}
for name, queue := range m.Queues {
if strings.HasPrefix(name, aws.ToString(input.QueueNamePrefix)) {
response.QueueUrls = append(response.QueueUrls, aws.ToString(queue.url))
}
}
sort.Strings(response.QueueUrls)
return response, nil
}

Expand Down Expand Up @@ -111,5 +116,14 @@ func (m *MockSQS) ListQueueTags(ctx context.Context, input *sqs.ListQueueTagsInp
}

func (m *MockSQS) DeleteQueue(ctx context.Context, input *sqs.DeleteQueueInput, optFns ...func(*sqs.Options)) (*sqs.DeleteQueueOutput, error) {
panic("Not implemented")
m.mutex.Lock()
defer m.mutex.Unlock()

for name, queue := range m.Queues {
if aws.ToString(queue.url) == aws.ToString(input.QueueUrl) {
delete(m.Queues, name)
return &sqs.DeleteQueueOutput{}, nil
}
}
return nil, fmt.Errorf("queue %q not found", aws.ToString(input.QueueUrl))
}
3 changes: 1 addition & 2 deletions pkg/model/awsmodel/nodeterminationhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/model"
"k8s.io/kops/pkg/model/iam"
"k8s.io/kops/pkg/util/stringorset"
"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -113,7 +112,7 @@ func (b *NodeTerminationHandlerBuilder) configureASG(c *fi.CloudupModelBuilderCo
}

func (b *NodeTerminationHandlerBuilder) build(c *fi.CloudupModelBuilderContext) error {
queueName := model.QueueNamePrefix(b.ClusterName()) + "-nth"
queueName := awsup.QueueNamePrefix(b.ClusterName()) + "-nth"

policy := iam.NewPolicy(b.ClusterName(), b.AWSPartition, b.Region)
arn := arn.ARN{
Expand Down
7 changes: 0 additions & 7 deletions pkg/model/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
Expand Down Expand Up @@ -236,9 +235,3 @@ func (b *KopsModelContext) LinkToPrivateRouteTableInZone(zoneName string) *awsta
func (b *KopsModelContext) InstanceName(ig *kops.InstanceGroup, suffix string) string {
return b.AutoscalingGroupName(ig) + suffix
}

func QueueNamePrefix(clusterName string) string {
// periods aren't allowed in queue name and queue name can't exceed 80 characters
safeClusterName := strings.ReplaceAll(clusterName, ".", "-")
return truncate.TruncateString(safeClusterName, truncate.TruncateStringOptions{MaxLength: 75, AlwaysAddHash: false})
}
28 changes: 28 additions & 0 deletions pkg/resources/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ import (
"context"
"reflect"
"sort"
"strings"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"k8s.io/kops/cloudmock/aws/mockec2"
"k8s.io/kops/cloudmock/aws/mockiam"
"k8s.io/kops/cloudmock/aws/mocksqs"
"k8s.io/kops/pkg/resources"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
)
Expand Down Expand Up @@ -344,6 +347,31 @@ func TestSharedVolume(t *testing.T) {
}
}

func TestListSQSQueues(t *testing.T) {
ctx := context.TODO()
cloud := awsup.BuildMockAWSCloud("us-east-1", "abc")
mockSQS := &mocksqs.MockSQS{}
cloud.MockSQS = mockSQS

// Long enough that the queue name is truncated and hashed
clusterName := "e2e-e2e-ci-kubernetes-kops-al2023-aws-conformance-aws-cni.tests-kops-aws.k8s.io"
queueName := awsup.QueueNamePrefix(clusterName) + "-nth"
if _, err := mockSQS.CreateQueue(ctx, &sqs.CreateQueueInput{QueueName: aws.String(queueName)}); err != nil {
t.Fatalf("error creating SQS queue: %v", err)
}

resourceTrackers, err := ListSQSQueues(cloud, "", clusterName)
if err != nil {
t.Fatalf("error listing SQS queues: %v", err)
}
if len(resourceTrackers) != 1 {
t.Fatalf("expected 1 SQS queue for cluster %q, got %d", clusterName, len(resourceTrackers))
}
if !strings.HasSuffix(resourceTrackers[0].ID, "/"+queueName) {
t.Errorf("expected queue URL ending in %q, got %q", queueName, resourceTrackers[0].ID)
}
}

func TestMatchesElbTags(t *testing.T) {
tc := []struct {
tags map[string]string
Expand Down
3 changes: 1 addition & 2 deletions pkg/resources/aws/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package aws
import (
"context"
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/service/sqs"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -63,7 +62,7 @@ func ListSQSQueues(cloud fi.Cloud, vpcID, clusterName string) ([]*resources.Reso
c := cloud.(awsup.AWSCloud)

klog.V(2).Infof("Listing SQS queues")
queuePrefix := strings.ReplaceAll(clusterName, ".", "-")
queuePrefix := awsup.QueueNamePrefix(clusterName)

request := &sqs.ListQueuesInput{
QueueNamePrefix: &queuePrefix,
Expand Down
9 changes: 9 additions & 0 deletions upup/pkg/fi/cloudup/awsup/aws_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ func GetClusterName40(cluster string) string {
})
}

// QueueNamePrefix returns the cluster-derived prefix of the SQS queue names.
// Periods are not allowed in queue names and queue names cannot exceed 80 characters,
// so leave room for a suffix such as "-nth".
// Creation and deletion must use the same prefix, or `kops delete cluster` cannot find the queue.
func QueueNamePrefix(clusterName string) string {
safeClusterName := strings.ReplaceAll(clusterName, ".", "-")
return truncate.TruncateString(safeClusterName, truncate.TruncateStringOptions{MaxLength: 75, AlwaysAddHash: false})
}

// GetResourceName32 will attempt to calculate a meaningful name for a resource given a prefix
// Will never return a string longer than 32 chars
func GetResourceName32(cluster string, prefix string) string {
Expand Down
3 changes: 1 addition & 2 deletions upup/pkg/fi/cloudup/template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import (
"k8s.io/kops/pkg/model/iam"
"k8s.io/kops/pkg/nodelabels"
"k8s.io/kops/pkg/resources/spotinst"
"k8s.io/kops/pkg/truncate"
"k8s.io/kops/pkg/wellknownports"
"k8s.io/kops/third_party/forked/text/template"
"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -444,7 +443,7 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS

if cluster.Spec.CloudProvider.AWS != nil && cluster.Spec.CloudProvider.AWS.NodeTerminationHandler != nil {
dest["DefaultQueueName"] = func() string {
s := truncate.TruncateString(strings.ReplaceAll(tf.ClusterName(), ".", "-"), truncate.TruncateStringOptions{MaxLength: 75, AlwaysAddHash: false})
s := awsup.QueueNamePrefix(tf.ClusterName())
domain := ".amazonaws.com/"
if strings.Contains(tf.Region, "cn-") {
domain = ".amazonaws.com.cn/"
Expand Down
Loading