|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "github.com/aws/aws-sdk-go/aws" |
| 9 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 10 | + "github.com/pkg/errors" |
| 11 | + "regexp" |
| 12 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/annotations" |
| 13 | + elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2" |
| 14 | + "strconv" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + LBAttrsAccessLogsS3Enabled = "access_logs.s3.enabled" |
| 19 | + LBAttrsAccessLogsS3Bucket = "access_logs.s3.bucket" |
| 20 | + LBAttrsAccessLogsS3Prefix = "access_logs.s3.prefix" |
| 21 | + LBAttrsLoadBalancingCrossZoneEnabled = "load_balancing.cross_zone.enabled" |
| 22 | + |
| 23 | + resourceIDLoadBalancer = "LoadBalancer" |
| 24 | +) |
| 25 | + |
| 26 | +func (t *defaultModelBuildTask) buildLoadBalancer(ctx context.Context, scheme elbv2model.LoadBalancerScheme) error { |
| 27 | + spec, err := t.buildLoadBalancerSpec(ctx, scheme) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + t.loadBalancer = elbv2model.NewLoadBalancer(t.stack, resourceIDLoadBalancer, spec) |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, scheme elbv2model.LoadBalancerScheme) (elbv2model.LoadBalancerSpec, error) { |
| 36 | + ipAddressType := elbv2model.IPAddressTypeIPV4 |
| 37 | + lbAttributes, err := t.buildLoadBalancerAttributes(ctx) |
| 38 | + if err != nil { |
| 39 | + return elbv2model.LoadBalancerSpec{}, err |
| 40 | + } |
| 41 | + tags, err := t.buildLoadBalancerTags(ctx) |
| 42 | + if err != nil { |
| 43 | + return elbv2model.LoadBalancerSpec{}, err |
| 44 | + } |
| 45 | + subnetMappings, err := t.buildSubnetMappings(ctx, t.ec2Subnets) |
| 46 | + if err != nil { |
| 47 | + return elbv2model.LoadBalancerSpec{}, err |
| 48 | + } |
| 49 | + name := t.buildLoadBalancerName(ctx, scheme) |
| 50 | + spec := elbv2model.LoadBalancerSpec{ |
| 51 | + Name: name, |
| 52 | + Type: elbv2model.LoadBalancerTypeNetwork, |
| 53 | + Scheme: &scheme, |
| 54 | + IPAddressType: &ipAddressType, |
| 55 | + SubnetMappings: subnetMappings, |
| 56 | + LoadBalancerAttributes: lbAttributes, |
| 57 | + Tags: tags, |
| 58 | + } |
| 59 | + return spec, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (t *defaultModelBuildTask) buildLoadBalancerScheme(_ context.Context) (elbv2model.LoadBalancerScheme, error) { |
| 63 | + scheme := elbv2model.LoadBalancerSchemeInternetFacing |
| 64 | + internal := false |
| 65 | + if _, err := t.annotationParser.ParseBoolAnnotation(annotations.SvcLBSuffixInternal, &internal, t.service.Annotations); err != nil { |
| 66 | + return "", err |
| 67 | + } |
| 68 | + if internal { |
| 69 | + scheme = elbv2model.LoadBalancerSchemeInternal |
| 70 | + } |
| 71 | + return scheme, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (t *defaultModelBuildTask) buildAdditionalResourceTags(_ context.Context) (map[string]string, error) { |
| 75 | + tags := make(map[string]string) |
| 76 | + _, err := t.annotationParser.ParseStringMapAnnotation(annotations.SvcLBSuffixAdditionalTags, &tags, t.service.Annotations) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + return tags, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (t *defaultModelBuildTask) buildLoadBalancerTags(ctx context.Context) (map[string]string, error) { |
| 84 | + return t.buildAdditionalResourceTags(ctx) |
| 85 | +} |
| 86 | + |
| 87 | +func (t *defaultModelBuildTask) buildSubnetMappings(_ context.Context, ec2Subnets []*ec2.Subnet) ([]elbv2model.SubnetMapping, error) { |
| 88 | + var eipAllocation []string |
| 89 | + eipConfigured := t.annotationParser.ParseStringSliceAnnotation(annotations.SvcLBSuffixEIPAllocations, &eipAllocation, t.service.Annotations) |
| 90 | + if eipConfigured && len(eipAllocation) != len(ec2Subnets) { |
| 91 | + return []elbv2model.SubnetMapping{}, errors.Errorf("number of EIP allocations (%d) and subnets (%d) must match", len(eipAllocation), len(ec2Subnets)) |
| 92 | + } |
| 93 | + subnetMappings := make([]elbv2model.SubnetMapping, 0, len(ec2Subnets)) |
| 94 | + for idx, subnet := range ec2Subnets { |
| 95 | + mapping := elbv2model.SubnetMapping{ |
| 96 | + SubnetID: aws.StringValue(subnet.SubnetId), |
| 97 | + } |
| 98 | + if idx < len(eipAllocation) { |
| 99 | + mapping.AllocationID = aws.String(eipAllocation[idx]) |
| 100 | + } |
| 101 | + subnetMappings = append(subnetMappings, mapping) |
| 102 | + } |
| 103 | + return subnetMappings, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (t *defaultModelBuildTask) buildLoadBalancerAttributes(_ context.Context) ([]elbv2model.LoadBalancerAttribute, error) { |
| 107 | + var attrs []elbv2model.LoadBalancerAttribute |
| 108 | + accessLogEnabled := t.defaultAccessLogS3Enabled |
| 109 | + bucketName := t.defaultAccessLogsS3Bucket |
| 110 | + bucketPrefix := t.defaultAccessLogsS3Prefix |
| 111 | + if _, err := t.annotationParser.ParseBoolAnnotation(annotations.SvcLBSuffixAccessLogEnabled, &accessLogEnabled, t.service.Annotations); err != nil { |
| 112 | + return []elbv2model.LoadBalancerAttribute{}, err |
| 113 | + } |
| 114 | + if accessLogEnabled { |
| 115 | + t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixAccessLogS3BucketName, &bucketName, t.service.Annotations) |
| 116 | + t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixAccessLogS3BucketPrefix, &bucketPrefix, t.service.Annotations) |
| 117 | + } |
| 118 | + crossZoneEnabled := t.defaultLoadBalancingCrossZoneEnabled |
| 119 | + if _, err := t.annotationParser.ParseBoolAnnotation(annotations.SvcLBSuffixCrossZoneLoadBalancingEnabled, &crossZoneEnabled, t.service.Annotations); err != nil { |
| 120 | + return []elbv2model.LoadBalancerAttribute{}, err |
| 121 | + } |
| 122 | + |
| 123 | + attrs = []elbv2model.LoadBalancerAttribute{ |
| 124 | + { |
| 125 | + Key: LBAttrsAccessLogsS3Enabled, |
| 126 | + Value: strconv.FormatBool(accessLogEnabled), |
| 127 | + }, |
| 128 | + { |
| 129 | + Key: LBAttrsAccessLogsS3Bucket, |
| 130 | + Value: bucketName, |
| 131 | + }, |
| 132 | + { |
| 133 | + Key: LBAttrsAccessLogsS3Prefix, |
| 134 | + Value: bucketPrefix, |
| 135 | + }, |
| 136 | + { |
| 137 | + Key: LBAttrsLoadBalancingCrossZoneEnabled, |
| 138 | + Value: strconv.FormatBool(crossZoneEnabled), |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + return attrs, nil |
| 143 | +} |
| 144 | + |
| 145 | +var invalidLoadBalancerNamePattern = regexp.MustCompile("[[:^alnum:]]") |
| 146 | + |
| 147 | +func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme elbv2model.LoadBalancerScheme) string { |
| 148 | + uuidHash := sha256.New() |
| 149 | + _, _ = uuidHash.Write([]byte(t.clusterName)) |
| 150 | + _, _ = uuidHash.Write([]byte(t.service.UID)) |
| 151 | + _, _ = uuidHash.Write([]byte(scheme)) |
| 152 | + uuid := hex.EncodeToString(uuidHash.Sum(nil)) |
| 153 | + |
| 154 | + sanitizedNamespace := invalidLoadBalancerNamePattern.ReplaceAllString(t.service.Namespace, "") |
| 155 | + sanitizedName := invalidLoadBalancerNamePattern.ReplaceAllString(t.service.Name, "") |
| 156 | + return fmt.Sprintf("k8s-%.8s-%.8s-%.10s", sanitizedNamespace, sanitizedName, uuid) |
| 157 | +} |
0 commit comments