Skip to content

Commit addcf02

Browse files
committed
Fixes an issue when changing between different target-type annotations
1 parent 9b64724 commit addcf02

File tree

9 files changed

+25
-9
lines changed

9 files changed

+25
-9
lines changed

docs/ingress-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Optional annotations are:
9797
9898
- **listen-ports**: Defines the ports the ALB will expose. It defaults to `[{"HTTP": 80}]` unless a certificate ARN is defined, then it is `[{"HTTPS": 443}]`. Uses a format as follows '[{"HTTP":8080,"HTTPS": 443}]'.
9999
100-
- **target-type**: Defines if the EC2 instance ID or the pod IP is added to the Target Groups. Defaults to `instance`. Valid options are `instance` and `pod`. With `instance` the Target Group targets are `<ec2 instance id>:<node port>`, for `pod` the targets are `<pod ip>:<pod port>`. When using the pod IP, it will route from all availabilty zones. `pod` is to be used when the pod network is routable and can be reached by the ALB.
100+
- **target-type**: Defines if the EC2 instance ID or the pod IP are used in the managed Target Groups. Defaults to `instance`. Valid options are `instance` and `pod`. With `instance` the Target Group targets are `<ec2 instance id>:<node port>`, for `pod` the targets are `<pod ip>:<pod port>`. When using the pod IP, it will route from all availabilty zones. `pod` is to be used when the pod network is routable and can be reached by the ALB.
101101
102102
- **security-groups**: [Security groups](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html) that should be applied to the ALB instance. These can be referenced by security group IDs or the name tag associated with each security group. Example ID values are `sg-723a380a,sg-a6181ede,sg-a5181edd`. Example tag values are `appSG, webSG`. When the annotation is not present, the controller will create a security group with appropriate ports allowing access to `0.0.0.0/0` and attached to the ALB. It will also create a security group for instances that allows all TCP traffic when the source is the security group created for the ALB.
103103

pkg/alb/rs/rule.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ func (r *Rule) Reconcile(rOpts *ReconcileOptions) error {
111111
log.Prettify(r.rs.current.Priority),
112112
log.Prettify(r.rs.current.Conditions))
113113

114-
// case *r.rs.desired.IsDefault: // rule is default (attached to listener), do nothing
115-
// r.logger.Debugf("Found desired rule that is a default and is already created with its respective listener. Rule: %s", log.Prettify(r.rs.desired))
116-
// r.rs.current = r.rs.desired
114+
case *r.rs.desired.IsDefault:
115+
// rule is default (attached to listener), do nothing
116+
// Seems to happen automatically, if we try to change it we get an error:
117+
// OperationNotPermitted: Default rule '<arn>' cannot be modified
118+
r.rs.current = r.rs.desired
117119

118120
case r.rs.current == nil: // rule doesn't exist and should be created
119121
r.logger.Infof("Start Rule creation.")
@@ -220,6 +222,9 @@ func (r *Rule) needsModification() bool {
220222
case crs == nil:
221223
r.logger.Debugf("Current is nil")
222224
return true
225+
case !util.DeepEqual(crs.Actions, drs.Actions):
226+
r.logger.Debugf("Actions needs to be changed (%v != %v)", log.Prettify(crs.Actions), log.Prettify(drs.Actions))
227+
return true
223228
case !conditionsEqual(crs.Conditions, drs.Conditions):
224229
r.logger.Debugf("Conditions needs to be changed (%v != %v)", log.Prettify(crs.Conditions), log.Prettify(drs.Conditions))
225230
return true

pkg/alb/rs/rule_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ func genTG(arn, svcname string) *tg.TargetGroup {
382382
Protocol: aws.String("HTTP"),
383383
},
384384
})
385+
tg.CopyCurrentToDesired(t)
385386
return t
386387
}
387388
func TestTargetGroupArn(t *testing.T) {

pkg/alb/rs/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewCurrentRules(o *NewCurrentRulesOptions) (Rules, error) {
3333
// TODO LOOKUP svcName based on TG
3434
i, tg := o.TargetGroups.FindCurrentByARN(*r.Actions[0].TargetGroupArn)
3535
if i < 0 {
36-
return nil, fmt.Errorf("failed to find a target group associated with a rule. This should not be possible. Rule: %s", awsutil.Prettify(r.RuleArn))
36+
return nil, fmt.Errorf("failed to find a target group associated with a rule. This should not be possible. Rule: %s, ARN: %s", awsutil.Prettify(r.RuleArn), *r.Actions[0].TargetGroupArn)
3737
}
3838

3939
newRule := NewCurrentRule(&NewCurrentRuleOptions{

pkg/alb/tg/targetgroup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ type NewDesiredTargetGroupOptions struct {
3838
func NewDesiredTargetGroup(o *NewDesiredTargetGroupOptions) *TargetGroup {
3939
hasher := md5.New()
4040
hasher.Write([]byte(o.LoadBalancerID))
41-
name := hex.EncodeToString(hasher.Sum(nil))
4241

4342
targetType := aws.String("instance")
4443
if *o.Annotations.TargetType == "pod" {
4544
targetType = aws.String("ip")
45+
hasher.Write([]byte(*targetType))
4646
}
4747

48+
name := hex.EncodeToString(hasher.Sum(nil))
4849
id := fmt.Sprintf("%.12s-%.5d-%.5s-%.7s", o.ALBNamePrefix, o.Port, *o.Annotations.BackendProtocol, name)
4950

5051
// TODO: Quick fix as we can't have the loadbalancer and target groups share pointers to the same

pkg/alb/tg/targetgroups.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (t TargetGroups) LookupBySvc(svc string, port int32) int {
2020
if v == nil {
2121
continue
2222
}
23-
if v.SvcName == svc && (v.SvcPort == port || v.SvcPort == 0) {
23+
if v.SvcName == svc && (v.SvcPort == port || v.SvcPort == 0) && v.tg.desired != nil {
2424
return p
2525
}
2626
}

pkg/alb/tg/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,12 @@ const (
6060
tagsModified
6161
attributesModified
6262
)
63+
64+
// CopyCurrentToDesired is used for testing other packages against tg
65+
func CopyCurrentToDesired(a *TargetGroup) {
66+
if a != nil {
67+
a.tg.desired = a.tg.current
68+
a.tags.desired = a.tags.current
69+
a.targets.desired = a.targets.current
70+
}
71+
}

pkg/annotations/annotations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func (a *Annotations) setTargetType(annotations map[string]string) error {
402402
a.TargetType = aws.String("instance")
403403
return nil
404404
case annotations[targetTypeKey] != "instance" && annotations[targetTypeKey] != "pod":
405-
return fmt.Errorf("ALB Routing Type [%v] must be either `instance` or `pod`", annotations[targetTypeKey])
405+
return fmt.Errorf("ALB Target Type [%v] must be either `instance` or `pod`", annotations[targetTypeKey])
406406
}
407407
a.TargetType = aws.String(annotations[targetTypeKey])
408408
return nil

pkg/aws/albelbv2/elbv2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (e *ELBV2) RemoveTargetGroup(arn *string) error {
249249
for i := 0; i < deleteTargetGroupReattemptMax; i++ {
250250
_, err := e.DeleteTargetGroup(in)
251251
if err == nil {
252-
break
252+
return nil
253253
}
254254

255255
if aerr, ok := err.(awserr.Error); ok {

0 commit comments

Comments
 (0)