Skip to content

Commit e09d131

Browse files
committed
Refactored albingresses into albingress
1 parent 3a5b776 commit e09d131

File tree

5 files changed

+101
-97
lines changed

5 files changed

+101
-97
lines changed

pkg/albingress/albingress.go

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,6 @@ import (
1818
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
1919
)
2020

21-
var logger *log.Logger
22-
23-
func init() {
24-
logger = log.New("ingress")
25-
}
26-
27-
// ALBIngress contains all information above the cluster, ingress resource, and AWS resources
28-
// needed to assemble an ALB, TargetGroup, Listener and Rules.
29-
type ALBIngress struct {
30-
ID string
31-
namespace string
32-
ingressName string
33-
clusterName string
34-
albNamePrefix string
35-
recorder record.EventRecorder
36-
ingress *extensions.Ingress
37-
lock *sync.Mutex
38-
annotations *annotations.Annotations
39-
ManagedSecurityGroups util.AWSStringSlice // sgs managed by this controller rather than annotation
40-
LoadBalancer *lb.LoadBalancer
41-
valid bool
42-
logger *log.Logger
43-
Reconciled bool
44-
}
45-
4621
type NewALBIngressOptions struct {
4722
Namespace string
4823
Name string
@@ -53,26 +28,21 @@ type NewALBIngressOptions struct {
5328
Reconciled bool
5429
}
5530

56-
// ID returns an ingress id based off of a namespace and name
57-
func ID(namespace, name string) string {
58-
return fmt.Sprintf("%s/%s", namespace, name)
59-
}
60-
6131
// NewALBIngress returns a minimal ALBIngress instance with a generated name that allows for lookup
6232
// when new ALBIngress objects are created to determine if an instance of that ALBIngress already
6333
// exists.
6434
func NewALBIngress(o *NewALBIngressOptions) *ALBIngress {
65-
ingressID := ID(o.Namespace, o.Name)
35+
ingressID := GenerateID(o.Namespace, o.Name)
6636
return &ALBIngress{
67-
ID: ingressID,
37+
id: ingressID,
6838
namespace: o.Namespace,
6939
clusterName: o.ClusterName,
7040
albNamePrefix: o.ALBNamePrefix,
7141
ingressName: o.Name,
7242
lock: new(sync.Mutex),
7343
logger: log.New(ingressID),
7444
recorder: o.Recorder,
75-
Reconciled: o.Reconciled,
45+
reconciled: o.Reconciled,
7646
ingress: o.Ingress,
7747
}
7848
}
@@ -115,15 +85,15 @@ func NewALBIngressFromIngress(o *NewALBIngressFromIngressOptions, annotationFact
11585
newIngress.ingress = o.Ingress
11686
// Ensure all desired state is removed from the copied ingress. The desired state of each
11787
// component will be generated later in this function.
118-
newIngress.StripDesiredState()
88+
newIngress.stripDesiredState()
11989
newIngress.valid = false
12090
}
12191

12292
// Load up the ingress with our current annotations.
12393
newIngress.annotations, err = annotationFactory.ParseAnnotations(o.Ingress)
12494
if err != nil {
12595
msg := fmt.Sprintf("Error parsing annotations: %s", err.Error())
126-
newIngress.Reconciled = false
96+
newIngress.reconciled = false
12797
newIngress.Eventf(api.EventTypeWarning, "ERROR", msg)
12898
newIngress.logger.Errorf(msg)
12999
return newIngress
@@ -139,15 +109,15 @@ func NewALBIngressFromIngress(o *NewALBIngressFromIngressOptions, annotationFact
139109
}
140110

141111
// Assemble the load balancer
142-
newIngress.LoadBalancer, err = lb.NewDesiredLoadBalancer(&lb.NewDesiredLoadBalancerOptions{
112+
newIngress.loadBalancer, err = lb.NewDesiredLoadBalancer(&lb.NewDesiredLoadBalancerOptions{
143113
ALBNamePrefix: o.ALBNamePrefix,
144114
Namespace: o.Ingress.GetNamespace(),
145-
ExistingLoadBalancer: newIngress.LoadBalancer,
115+
ExistingLoadBalancer: newIngress.loadBalancer,
146116
IngressName: o.Ingress.Name,
147117
IngressRules: o.Ingress.Spec.Rules,
148118
Logger: newIngress.logger,
149119
Annotations: newIngress.annotations,
150-
Tags: newIngress.Tags(),
120+
Tags: newIngress.Tags(o.ClusterName),
151121
GetServiceNodePort: o.GetServiceNodePort,
152122
GetNodes: o.GetNodes,
153123
})
@@ -156,7 +126,7 @@ func NewALBIngressFromIngress(o *NewALBIngressFromIngressOptions, annotationFact
156126
msg := fmt.Sprintf("Error instantiating load balancer: %s", err.Error())
157127
newIngress.Eventf(api.EventTypeWarning, "ERROR", msg)
158128
newIngress.logger.Errorf(msg)
159-
newIngress.Reconciled = false
129+
newIngress.reconciled = false
160130
return newIngress
161131
}
162132

@@ -204,7 +174,7 @@ func NewALBIngressFromAWSLoadBalancer(o *NewALBIngressFromAWSLoadBalancerOptions
204174
})
205175

206176
// Assemble load balancer
207-
ingress.LoadBalancer, err = lb.NewCurrentLoadBalancer(&lb.NewCurrentLoadBalancerOptions{
177+
ingress.loadBalancer, err = lb.NewCurrentLoadBalancer(&lb.NewCurrentLoadBalancerOptions{
208178
LoadBalancer: o.LoadBalancer,
209179
Tags: tags,
210180
ALBNamePrefix: o.ALBNamePrefix,
@@ -238,20 +208,24 @@ func (a *ALBIngress) Eventf(eventtype, reason, messageFmt string, args ...interf
238208
func (a *ALBIngress) Hostnames() ([]api.LoadBalancerIngress, error) {
239209
var hostnames []api.LoadBalancerIngress
240210

241-
if a.LoadBalancer == nil {
211+
if a.reconciled != true {
212+
return nil, fmt.Errorf("ingress is not in sync, hostname invalid")
213+
}
214+
215+
if a.loadBalancer == nil {
242216
return hostnames, nil
243217
}
244218

245-
if a.LoadBalancer.Hostname() != nil {
246-
hostnames = append(hostnames, api.LoadBalancerIngress{Hostname: *a.LoadBalancer.Hostname()})
219+
if a.loadBalancer.Hostname() != nil {
220+
hostnames = append(hostnames, api.LoadBalancerIngress{Hostname: *a.loadBalancer.Hostname()})
247221
}
248222

249223
if len(hostnames) == 0 {
250224
a.logger.Errorf("No ALB hostnames for ingress")
251225
return nil, fmt.Errorf("No ALB hostnames for ingress")
252226
}
253227

254-
a.logger.Debugf("Ingress library requested hostname list and we returned %s", *a.LoadBalancer.Hostname())
228+
a.logger.Debugf("Ingress library requested hostname list and we returned %s", *a.loadBalancer.Hostname())
255229
return hostnames, nil
256230
}
257231

@@ -264,25 +238,20 @@ func (a *ALBIngress) Reconcile(rOpts *ReconcileOptions) {
264238
return
265239
}
266240

267-
errors := a.LoadBalancer.Reconcile(
241+
errors := a.loadBalancer.Reconcile(
268242
&lb.ReconcileOptions{
269243
Eventf: rOpts.Eventf,
270244
})
271245
if len(errors) > 0 {
272246
// marks reconciled state as false so UpdateIngressStatus won't operate
273-
a.Reconciled = false
247+
a.reconciled = false
274248
a.logger.Errorf("Failed to reconcile state on this ingress")
275249
for _, err := range errors {
276250
a.logger.Errorf(" - %s", err.Error())
277251
}
278252
}
279253
// marks reconciled state as true so that UpdateIngressStatus will operate
280-
a.Reconciled = true
281-
}
282-
283-
// Name returns the name of the ingress
284-
func (a *ALBIngress) Name() string {
285-
return fmt.Sprintf("%s-%s", a.namespace, a.ingressName)
254+
a.reconciled = true
286255
}
287256

288257
// Namespace returns the namespace of the ingress
@@ -291,16 +260,22 @@ func (a *ALBIngress) Namespace() string {
291260
}
292261

293262
// StripDesiredState strips all desired objects from an ALBIngress
294-
func (a *ALBIngress) StripDesiredState() {
295-
if a.LoadBalancer != nil {
296-
a.LoadBalancer.StripDesiredState()
263+
func (a *ALBIngress) stripDesiredState() {
264+
if a.loadBalancer != nil {
265+
a.loadBalancer.StripDesiredState()
297266
}
298267
}
299268

300269
// Tags returns an elbv2.Tag slice of standard tags for the ingress AWS resources
301-
func (a *ALBIngress) Tags() []*elbv2.Tag {
270+
func (a *ALBIngress) Tags(clusterName string) []*elbv2.Tag {
302271
tags := a.annotations.Tags
303272

273+
// https://github.com/kubernetes/kubernetes/blob/master/pkg/cloudprovider/providers/aws/tags.go
274+
tags = append(tags, &elbv2.Tag{
275+
Key: aws.String("kubernetes.io/cluster/" + clusterName),
276+
Value: aws.String("owned"),
277+
})
278+
304279
tags = append(tags, &elbv2.Tag{
305280
Key: aws.String("Namespace"),
306281
Value: aws.String(a.namespace),
@@ -317,3 +292,12 @@ func (a *ALBIngress) Tags() []*elbv2.Tag {
317292
type ReconcileOptions struct {
318293
Eventf func(string, string, string, ...interface{})
319294
}
295+
296+
// id returns an ingress id based off of a namespace and name
297+
func GenerateID(namespace, name string) string {
298+
return fmt.Sprintf("%s/%s", namespace, name)
299+
}
300+
301+
func (a *ALBIngress) GetLoadBalancer() *lb.LoadBalancer {
302+
return a.loadBalancer
303+
}

pkg/albingresses/albingresses.go renamed to pkg/albingress/albingresses.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package albingresses
1+
package albingress
22

33
import (
44
"strconv"
@@ -12,25 +12,13 @@ import (
1212
"k8s.io/client-go/tools/record"
1313
"k8s.io/ingress/core/pkg/ingress/annotations/class"
1414

15-
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/albingress"
1615
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/annotations"
1716
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/aws/ec2"
1817
albelbv2 "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/aws/elbv2"
1918
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/aws/waf"
20-
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/log"
2119
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
2220
)
2321

24-
// ALBIngresses is a list of ALBIngress. It is held by the ALBController instance and evaluated
25-
// against to determine what should be created, deleted, and modified.
26-
type ALBIngresses []*albingress.ALBIngress
27-
28-
var logger *log.Logger
29-
30-
func init() {
31-
logger = log.New("ingresses")
32-
}
33-
3422
// NewALBIngressesFromIngressesOptions are the options to NewALBIngressesFromIngresses
3523
type NewALBIngressesFromIngressesOptions struct {
3624
Recorder record.EventRecorder
@@ -58,12 +46,12 @@ func NewALBIngressesFromIngresses(o *NewALBIngressesFromIngressesOptions, annota
5846
}
5947

6048
// Find the existing ingress for this Kubernetes ingress (if it existed).
61-
id := albingress.ID(ingResource.GetNamespace(), ingResource.Name)
49+
id := GenerateID(ingResource.GetNamespace(), ingResource.Name)
6250
_, existingIngress := o.ALBIngresses.FindByID(id)
6351

6452
// Produce a new ALBIngress instance for every ingress found. If ALBIngress returns nil, there
6553
// was an issue with the ingress (e.g. bad annotations) and should not be added to the list.
66-
ALBIngress := albingress.NewALBIngressFromIngress(&albingress.NewALBIngressFromIngressOptions{
54+
ALBIngress := NewALBIngressFromIngress(&NewALBIngressFromIngressOptions{
6755
Ingress: ingResource,
6856
ExistingIngress: existingIngress,
6957
ClusterName: o.ClusterName,
@@ -173,7 +161,7 @@ func AssembleIngressesFromAWS(o *AssembleIngressesFromAWSOptions) ALBIngresses {
173161
wafACLID = wafResult.WebACLId
174162
}
175163

176-
albIngress, err := albingress.NewALBIngressFromAWSLoadBalancer(&albingress.NewALBIngressFromAWSLoadBalancerOptions{
164+
albIngress, err := NewALBIngressFromAWSLoadBalancer(&NewALBIngressFromAWSLoadBalancerOptions{
177165
LoadBalancer: loadBalancer,
178166
ALBNamePrefix: o.ALBNamePrefix,
179167
Recorder: o.Recorder,
@@ -199,9 +187,9 @@ func AssembleIngressesFromAWS(o *AssembleIngressesFromAWSOptions) ALBIngresses {
199187
}
200188

201189
// FindByID locates the ingress by the id parameter and returns its position
202-
func (a ALBIngresses) FindByID(id string) (int, *albingress.ALBIngress) {
190+
func (a ALBIngresses) FindByID(id string) (int, *ALBIngress) {
203191
for p, v := range a {
204-
if v.ID == id {
192+
if v.id == id {
205193
return p, v
206194
}
207195
}
@@ -216,13 +204,13 @@ func (a ALBIngresses) RemovedIngresses(newList ALBIngresses) ALBIngresses {
216204
// Loop through every ingress in current (old) ingress list known to ALBController
217205
for _, ingress := range a {
218206
// Ingress objects not found in newList might qualify for deletion.
219-
if i, _ := newList.FindByID(ingress.ID); i < 0 {
207+
if i, _ := newList.FindByID(ingress.id); i < 0 {
220208
// If the ALBIngress still contains a LoadBalancer, it still needs to be deleted.
221209
// In this case, strip all desired state and add it to the deleteableIngress list.
222210
// If the ALBIngress contains no LoadBalancer, it was previously deleted and is
223211
// no longer relevant to the ALBController.
224-
if ingress.LoadBalancer != nil {
225-
ingress.StripDesiredState()
212+
if ingress.loadBalancer != nil {
213+
ingress.stripDesiredState()
226214
deleteableIngress = append(deleteableIngress, ingress)
227215
}
228216
}

pkg/albingress/types.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package albingress
2+
3+
import (
4+
"sync"
5+
6+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/alb/lb"
7+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/annotations"
8+
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/log"
9+
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
10+
extensions "k8s.io/api/extensions/v1beta1"
11+
"k8s.io/client-go/tools/record"
12+
)
13+
14+
var logger *log.Logger
15+
16+
func init() {
17+
logger = log.New("ingress")
18+
}
19+
20+
// ALBIngresses is a list of ALBIngress. It is held by the ALBController instance and evaluated
21+
// against to determine what should be created, deleted, and modified.
22+
type ALBIngresses []*ALBIngress
23+
24+
// ALBIngress contains all information above the cluster, ingress resource, and AWS resources
25+
// needed to assemble an ALB, TargetGroup, Listener and Rules.
26+
type ALBIngress struct {
27+
id string
28+
namespace string
29+
ingressName string
30+
clusterName string
31+
albNamePrefix string
32+
recorder record.EventRecorder
33+
ingress *extensions.Ingress
34+
lock *sync.Mutex
35+
annotations *annotations.Annotations
36+
managedSecurityGroups util.AWSStringSlice // sgs managed by this controller rather than annotation
37+
loadBalancer *lb.LoadBalancer
38+
valid bool
39+
logger *log.Logger
40+
reconciled bool
41+
}

0 commit comments

Comments
 (0)