@@ -48,26 +48,27 @@ const (
48
48
Delete = "delete"
49
49
)
50
50
51
- // Action that should take place on the resource
51
+ // Action that should take place on the resource.
52
52
type Action interface {
53
53
Type () ActionType
54
54
Name () string
55
55
}
56
56
57
- // BackendSetAction denotes the action that should be taken on the given backend set.
57
+ // BackendSetAction denotes the action that should be taken on the given
58
+ // BackendSet.
58
59
type BackendSetAction struct {
59
60
Action
60
61
61
62
actionType ActionType
62
63
BackendSet baremetal.BackendSet
63
64
}
64
65
65
- // Type of the Action
66
+ // Type of the Action.
66
67
func (b * BackendSetAction ) Type () ActionType {
67
68
return b .actionType
68
69
}
69
70
70
- // Name of the action's object
71
+ // Name of the action's object.
71
72
func (b * BackendSetAction ) Name () string {
72
73
return b .BackendSet .Name
73
74
}
@@ -76,20 +77,20 @@ func (b *BackendSetAction) String() string {
76
77
return fmt .Sprintf ("BackendSetAction:{Name: %s, Type: %v }" , b .Name (), b .actionType )
77
78
}
78
79
79
- // ListenerAction denotes the action that should be taken on the given listener .
80
+ // ListenerAction denotes the action that should be taken on the given Listener .
80
81
type ListenerAction struct {
81
82
Action
82
83
83
84
actionType ActionType
84
85
Listener baremetal.Listener
85
86
}
86
87
87
- // Type of the Action
88
+ // Type of the Action.
88
89
func (l * ListenerAction ) Type () ActionType {
89
90
return l .actionType
90
91
}
91
92
92
- // Name of the action's object
93
+ // Name of the action's object.
93
94
func (l * ListenerAction ) Name () string {
94
95
return l .Listener .Name
95
96
}
@@ -98,7 +99,8 @@ func (l *ListenerAction) String() string {
98
99
return fmt .Sprintf ("ListenerAction:{Name: %s, Type: %v }" , l .Name (), l .actionType )
99
100
}
100
101
101
- // TODO(horwitz): this doesn't check weight which we may want in the future to evenly distribute Local traffic policy load.
102
+ // TODO(horwitz): this doesn't check weight which we may want in the future to
103
+ // evenly distribute Local traffic policy load.
102
104
func hasBackendSetChanged (actual , desired baremetal.BackendSet ) bool {
103
105
if ! reflect .DeepEqual (actual .HealthChecker , desired .HealthChecker ) {
104
106
return true
@@ -114,8 +116,8 @@ func hasBackendSetChanged(actual, desired baremetal.BackendSet) bool {
114
116
115
117
nameFormat := "%s:%d"
116
118
117
- // Since the lengths are equal that means the membership must be the same else
118
- // there has been change.
119
+ // Since the lengths are equal that means the membership must be the same
120
+ // else there has been change.
119
121
desiredSet := sets .NewString ()
120
122
for _ , backend := range desired .Backends {
121
123
name := fmt .Sprintf (nameFormat , backend .IPAddress , backend .Port )
@@ -138,7 +140,7 @@ func getBackendSetChanges(actual, desired map[string]baremetal.BackendSet) []Act
138
140
for name , actualBackendSet := range actual {
139
141
desiredBackendSet , ok := desired [name ]
140
142
if ! ok {
141
- // no longer exists
143
+ // No longer exists.
142
144
backendSetActions = append (backendSetActions , & BackendSetAction {
143
145
BackendSet : actualBackendSet ,
144
146
actionType : Delete ,
@@ -157,7 +159,7 @@ func getBackendSetChanges(actual, desired map[string]baremetal.BackendSet) []Act
157
159
// Now check if any need to be created.
158
160
for name , desiredBackendSet := range desired {
159
161
if _ , ok := actual [name ]; ! ok {
160
- // doesn 't exist so lets create it
162
+ // Doesn 't exist so lets create it.
161
163
backendSetActions = append (backendSetActions , & BackendSetAction {
162
164
BackendSet : desiredBackendSet ,
163
165
actionType : Create ,
@@ -277,30 +279,34 @@ func parseSecretString(secretString string) (string, string) {
277
279
return "" , secretString
278
280
}
279
281
282
+ // sortAndCombineActions combines two slices of Actions and then sorts them to
283
+ // ensure that BackendSets are created prior to their associated Listeners but
284
+ // deleted after their associated Listeners.
280
285
func sortAndCombineActions (backendSetActions []Action , listenerActions []Action ) []Action {
281
286
actions := append (backendSetActions , listenerActions ... )
282
287
sort .Slice (actions , func (i , j int ) bool {
283
- // One action will be backendset and one will be the listener
284
288
a1 := actions [i ]
285
289
a2 := actions [j ]
290
+
291
+ // Sort by the name until we get to the point a1 and a2 are Actions upon
292
+ // an associated Listener and BackendSet (which share the same name).
286
293
if a1 .Name () != a2 .Name () {
287
- // Since the actions aren't for the same listener/backendset then just
288
- // sort by the name until we get to the point we are
289
294
return a1 .Name () < a2 .Name ()
290
295
}
291
296
292
- // For create and delete (which is what we really care about) the ActionType
293
- // will always be the same so we can get away with just checking the first action.
297
+ // For Create and Delete (which is what we really care about) the
298
+ // ActionType will always be the same so we can get away with just
299
+ // checking the type of the first action.
294
300
switch a1 .Type () {
295
301
case Create :
296
- // Create the BackendSet then Listener
302
+ // Create the BackendSet then Listener.
297
303
_ , ok := a1 .(* BackendSetAction )
298
304
return ok
299
305
case Update :
300
- // Doesn't matter
306
+ // Doesn't matter.
301
307
return true
302
308
case Delete :
303
- // Delete the Listener then BackendSet
309
+ // Delete the Listener then BackendSet.
304
310
_ , ok := a2 .(* BackendSetAction )
305
311
return ok
306
312
default :
@@ -313,6 +319,7 @@ func sortAndCombineActions(backendSetActions []Action, listenerActions []Action)
313
319
}
314
320
315
321
func getBackendPort (backends []baremetal.Backend ) uint64 {
316
- // TODO: what happens if this is 0? e.g. we scale the pods to 0 for a deployment
322
+ // TODO: what happens if this is 0? e.g. we scale the pods to 0 for a
323
+ // deployment.
317
324
return uint64 (backends [0 ].Port )
318
325
}
0 commit comments