@@ -18,31 +18,6 @@ import (
18
18
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
19
19
)
20
20
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
-
46
21
type NewALBIngressOptions struct {
47
22
Namespace string
48
23
Name string
@@ -53,26 +28,21 @@ type NewALBIngressOptions struct {
53
28
Reconciled bool
54
29
}
55
30
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
-
61
31
// NewALBIngress returns a minimal ALBIngress instance with a generated name that allows for lookup
62
32
// when new ALBIngress objects are created to determine if an instance of that ALBIngress already
63
33
// exists.
64
34
func NewALBIngress (o * NewALBIngressOptions ) * ALBIngress {
65
- ingressID := ID (o .Namespace , o .Name )
35
+ ingressID := GenerateID (o .Namespace , o .Name )
66
36
return & ALBIngress {
67
- ID : ingressID ,
37
+ id : ingressID ,
68
38
namespace : o .Namespace ,
69
39
clusterName : o .ClusterName ,
70
40
albNamePrefix : o .ALBNamePrefix ,
71
41
ingressName : o .Name ,
72
42
lock : new (sync.Mutex ),
73
43
logger : log .New (ingressID ),
74
44
recorder : o .Recorder ,
75
- Reconciled : o .Reconciled ,
45
+ reconciled : o .Reconciled ,
76
46
ingress : o .Ingress ,
77
47
}
78
48
}
@@ -115,15 +85,15 @@ func NewALBIngressFromIngress(o *NewALBIngressFromIngressOptions, annotationFact
115
85
newIngress .ingress = o .Ingress
116
86
// Ensure all desired state is removed from the copied ingress. The desired state of each
117
87
// component will be generated later in this function.
118
- newIngress .StripDesiredState ()
88
+ newIngress .stripDesiredState ()
119
89
newIngress .valid = false
120
90
}
121
91
122
92
// Load up the ingress with our current annotations.
123
93
newIngress .annotations , err = annotationFactory .ParseAnnotations (o .Ingress )
124
94
if err != nil {
125
95
msg := fmt .Sprintf ("Error parsing annotations: %s" , err .Error ())
126
- newIngress .Reconciled = false
96
+ newIngress .reconciled = false
127
97
newIngress .Eventf (api .EventTypeWarning , "ERROR" , msg )
128
98
newIngress .logger .Errorf (msg )
129
99
return newIngress
@@ -139,15 +109,15 @@ func NewALBIngressFromIngress(o *NewALBIngressFromIngressOptions, annotationFact
139
109
}
140
110
141
111
// Assemble the load balancer
142
- newIngress .LoadBalancer , err = lb .NewDesiredLoadBalancer (& lb.NewDesiredLoadBalancerOptions {
112
+ newIngress .loadBalancer , err = lb .NewDesiredLoadBalancer (& lb.NewDesiredLoadBalancerOptions {
143
113
ALBNamePrefix : o .ALBNamePrefix ,
144
114
Namespace : o .Ingress .GetNamespace (),
145
- ExistingLoadBalancer : newIngress .LoadBalancer ,
115
+ ExistingLoadBalancer : newIngress .loadBalancer ,
146
116
IngressName : o .Ingress .Name ,
147
117
IngressRules : o .Ingress .Spec .Rules ,
148
118
Logger : newIngress .logger ,
149
119
Annotations : newIngress .annotations ,
150
- Tags : newIngress .Tags (),
120
+ Tags : newIngress .Tags (o . ClusterName ),
151
121
GetServiceNodePort : o .GetServiceNodePort ,
152
122
GetNodes : o .GetNodes ,
153
123
})
@@ -156,7 +126,7 @@ func NewALBIngressFromIngress(o *NewALBIngressFromIngressOptions, annotationFact
156
126
msg := fmt .Sprintf ("Error instantiating load balancer: %s" , err .Error ())
157
127
newIngress .Eventf (api .EventTypeWarning , "ERROR" , msg )
158
128
newIngress .logger .Errorf (msg )
159
- newIngress .Reconciled = false
129
+ newIngress .reconciled = false
160
130
return newIngress
161
131
}
162
132
@@ -204,7 +174,7 @@ func NewALBIngressFromAWSLoadBalancer(o *NewALBIngressFromAWSLoadBalancerOptions
204
174
})
205
175
206
176
// Assemble load balancer
207
- ingress .LoadBalancer , err = lb .NewCurrentLoadBalancer (& lb.NewCurrentLoadBalancerOptions {
177
+ ingress .loadBalancer , err = lb .NewCurrentLoadBalancer (& lb.NewCurrentLoadBalancerOptions {
208
178
LoadBalancer : o .LoadBalancer ,
209
179
Tags : tags ,
210
180
ALBNamePrefix : o .ALBNamePrefix ,
@@ -238,20 +208,24 @@ func (a *ALBIngress) Eventf(eventtype, reason, messageFmt string, args ...interf
238
208
func (a * ALBIngress ) Hostnames () ([]api.LoadBalancerIngress , error ) {
239
209
var hostnames []api.LoadBalancerIngress
240
210
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 {
242
216
return hostnames , nil
243
217
}
244
218
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 ()})
247
221
}
248
222
249
223
if len (hostnames ) == 0 {
250
224
a .logger .Errorf ("No ALB hostnames for ingress" )
251
225
return nil , fmt .Errorf ("No ALB hostnames for ingress" )
252
226
}
253
227
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 ())
255
229
return hostnames , nil
256
230
}
257
231
@@ -264,25 +238,20 @@ func (a *ALBIngress) Reconcile(rOpts *ReconcileOptions) {
264
238
return
265
239
}
266
240
267
- errors := a .LoadBalancer .Reconcile (
241
+ errors := a .loadBalancer .Reconcile (
268
242
& lb.ReconcileOptions {
269
243
Eventf : rOpts .Eventf ,
270
244
})
271
245
if len (errors ) > 0 {
272
246
// marks reconciled state as false so UpdateIngressStatus won't operate
273
- a .Reconciled = false
247
+ a .reconciled = false
274
248
a .logger .Errorf ("Failed to reconcile state on this ingress" )
275
249
for _ , err := range errors {
276
250
a .logger .Errorf (" - %s" , err .Error ())
277
251
}
278
252
}
279
253
// 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
286
255
}
287
256
288
257
// Namespace returns the namespace of the ingress
@@ -291,16 +260,22 @@ func (a *ALBIngress) Namespace() string {
291
260
}
292
261
293
262
// 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 ()
297
266
}
298
267
}
299
268
300
269
// 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 {
302
271
tags := a .annotations .Tags
303
272
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
+
304
279
tags = append (tags , & elbv2.Tag {
305
280
Key : aws .String ("Namespace" ),
306
281
Value : aws .String (a .namespace ),
@@ -317,3 +292,12 @@ func (a *ALBIngress) Tags() []*elbv2.Tag {
317
292
type ReconcileOptions struct {
318
293
Eventf func (string , string , string , ... interface {})
319
294
}
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
+ }
0 commit comments