Skip to content

Commit 36754be

Browse files
committed
MINOR: make ReferenceGrantManager fields private
ToReferenceGrantFrom, ReferenceGrantsTo, and ToFrom are renamed to toReferenceGrantFrom, referenceGrantsTo, and toFrom. These maps are implementation details of the manager and should not be accessible outside the package. The unit tests access the private fields directly since they live in the same package.
1 parent f081cd6 commit 36754be

2 files changed

Lines changed: 39 additions & 39 deletions

File tree

k8s/gate/tree/reference_grant_manager.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ type ReferenceGrantNamespacedName types.NamespacedName
1919
// ReferenceGrantManager tracks which cross-namespace references are permitted
2020
// by ReferenceGrant resources. It maintains three maps:
2121
//
22-
// - ToReferenceGrantFrom: for each target (To), which ReferenceGrants cover it
23-
// and from which source types (From). Used to incrementally update ToFrom.
22+
// - toReferenceGrantFrom: for each target (To), which ReferenceGrants cover it
23+
// and from which source types (From). Used to incrementally update toFrom.
2424
//
25-
// - ReferenceGrantsTo: inverse index — for each ReferenceGrant, which targets
25+
// - referenceGrantsTo: inverse index — for each ReferenceGrant, which targets
2626
// (To) it covers. Required to efficiently remove all entries for a deleted grant.
2727
//
28-
// - ToFrom: the resolved flat map consumed by IsAccessGranted. Rebuilt from
29-
// ToReferenceGrantFrom by ComputeToFrom after each reconcile cycle.
28+
// - toFrom: the resolved flat map consumed by IsAccessGranted. Rebuilt from
29+
// toReferenceGrantFrom by ComputeToFrom after each reconcile cycle.
3030
type ReferenceGrantManager struct {
31-
ToReferenceGrantFrom map[To]map[ReferenceGrantNamespacedName]map[From]struct{}
32-
ReferenceGrantsTo map[ReferenceGrantNamespacedName]map[To]struct{}
33-
ToFrom map[To]map[From]struct{}
31+
toReferenceGrantFrom map[To]map[ReferenceGrantNamespacedName]map[From]struct{}
32+
referenceGrantsTo map[ReferenceGrantNamespacedName]map[To]struct{}
33+
toFrom map[To]map[From]struct{}
3434
}
3535

3636
// ConvertTo builds a To key from the target resource's namespace, API group,
@@ -58,16 +58,16 @@ func ConvertReferenceGrantNamespacedName(referenceGrant ReferenceGrant) Referenc
5858
// internal maps allocated.
5959
func NewReferenceGrantManager() *ReferenceGrantManager {
6060
return &ReferenceGrantManager{
61-
ToReferenceGrantFrom: map[To]map[ReferenceGrantNamespacedName]map[From]struct{}{},
62-
ReferenceGrantsTo: map[ReferenceGrantNamespacedName]map[To]struct{}{},
63-
ToFrom: map[To]map[From]struct{}{},
61+
toReferenceGrantFrom: map[To]map[ReferenceGrantNamespacedName]map[From]struct{}{},
62+
referenceGrantsTo: map[ReferenceGrantNamespacedName]map[To]struct{}{},
63+
toFrom: map[To]map[From]struct{}{},
6464
}
6565
}
6666

6767
// IsAccessGranted reports whether a resource of type (fromGroup, fromKind) in
6868
// fromNamespace may reference a resource of type (toGroup, toKind) named toName
6969
// in toNamespace. Same-namespace references are always permitted. Cross-namespace
70-
// access requires a matching entry in ToFrom, covering both named grants and
70+
// access requires a matching entry in toFrom, covering both named grants and
7171
// wildcard grants (empty name).
7272
func (mgr *ReferenceGrantManager) IsAccessGranted(fromGroup, fromKind, fromNamespace,
7373
toGroup, toKind, toNamespace, toName string,
@@ -79,8 +79,8 @@ func (mgr *ReferenceGrantManager) IsAccessGranted(fromGroup, fromKind, fromNames
7979
convertedTo := ConvertTo(toNamespace, toGroup, toKind, toName)
8080
convertedToWithoutName := ConvertTo(toNamespace, toGroup, toKind, "")
8181
convertedFrom := ConvertFrom(fromNamespace, fromGroup, fromKind)
82-
froms := mgr.ToFrom[convertedTo]
83-
fromsWithoutName := mgr.ToFrom[convertedToWithoutName]
82+
froms := mgr.toFrom[convertedTo]
83+
fromsWithoutName := mgr.toFrom[convertedToWithoutName]
8484
if froms == nil && fromsWithoutName == nil {
8585
// No grants for this 'To'
8686
return false
@@ -95,7 +95,7 @@ func (mgr *ReferenceGrantManager) IsAccessGranted(fromGroup, fromKind, fromNames
9595
// UpsertReferenceGrant registers or updates the access grants defined by the given
9696
// ReferenceGrant. It clears any previous entries for the grant before inserting the
9797
// current Spec, so that updates are handled correctly without diffing old vs new rules.
98-
// ToReferenceGrantFrom and ReferenceGrantsTo are updated; call ComputeToFrom afterwards
98+
// toReferenceGrantFrom and referenceGrantsTo are updated; call ComputeToFrom afterwards
9999
// to reflect the change in IsAccessGranted.
100100
func (mgr *ReferenceGrantManager) UpsertReferenceGrant(referenceGrant ReferenceGrant) {
101101
if referenceGrant.K8sResource == nil ||
@@ -113,8 +113,8 @@ func (mgr *ReferenceGrantManager) UpsertReferenceGrant(referenceGrant ReferenceG
113113
string(to.Kind),
114114
string(utils.PointerDefaultValueIfNil(to.Name)))
115115
// ___________________________
116-
// Update ToReferenceGrantFrom
117-
referenceGrantFrom := mgr.ToReferenceGrantFrom[convertedTo]
116+
// Update toReferenceGrantFrom
117+
referenceGrantFrom := mgr.toReferenceGrantFrom[convertedTo]
118118
for _, from := range referenceGrant.K8sResource.Spec.From {
119119
convertedFrom := ConvertFrom(string(from.Namespace),
120120
string(from.Group),
@@ -127,7 +127,7 @@ func (mgr *ReferenceGrantManager) UpsertReferenceGrant(referenceGrant ReferenceG
127127
convertedFrom: {},
128128
},
129129
}
130-
mgr.ToReferenceGrantFrom[convertedTo] = referenceGrantFrom
130+
mgr.toReferenceGrantFrom[convertedTo] = referenceGrantFrom
131131
} else {
132132
// Subsequent association so update the association
133133
froms := referenceGrantFrom[referenceGrantNamespacedName]
@@ -139,13 +139,13 @@ func (mgr *ReferenceGrantManager) UpsertReferenceGrant(referenceGrant ReferenceG
139139
}
140140
}
141141
// ________________________
142-
// Update ReferenceGrantsTo
143-
referenceGrantsTo := mgr.ReferenceGrantsTo[referenceGrantNamespacedName]
142+
// Update referenceGrantsTo
143+
referenceGrantsTo := mgr.referenceGrantsTo[referenceGrantNamespacedName]
144144
if referenceGrantsTo == nil {
145145
referenceGrantsTo = map[To]struct{}{
146146
convertedTo: {},
147147
}
148-
mgr.ReferenceGrantsTo[referenceGrantNamespacedName] = referenceGrantsTo
148+
mgr.referenceGrantsTo[referenceGrantNamespacedName] = referenceGrantsTo
149149
} else {
150150
referenceGrantsTo[convertedTo] = struct{}{}
151151
}
@@ -170,21 +170,21 @@ func (mgr *ReferenceGrantManager) RemoveReferenceGrantWithCheck(referenceGrant R
170170
}
171171
referenceGrantNamespacedName := ConvertReferenceGrantNamespacedName(referenceGrant)
172172
// Get all the To from ReferenceGrant with inverse index
173-
tos := mgr.ReferenceGrantsTo[referenceGrantNamespacedName]
173+
tos := mgr.referenceGrantsTo[referenceGrantNamespacedName]
174174
// For each To
175175
for to := range tos {
176-
referenceGrantFrom, exists := mgr.ToReferenceGrantFrom[to]
176+
referenceGrantFrom, exists := mgr.toReferenceGrantFrom[to]
177177
// If no association for this 'To'
178178
if referenceGrantFrom == nil && !exists {
179-
// Delete the 'To' key in inverse map ReferenceGrantsTo
179+
// Delete the 'To' key in inverse map referenceGrantsTo
180180
delete(tos, to)
181181
continue
182182
}
183183
// If empty association for this 'To'
184184
if exists && len(referenceGrantFrom) == 0 {
185-
// Delete the 'To' key in full map ToReferenceGrantFrom
186-
delete(mgr.ToReferenceGrantFrom, to)
187-
// Delete the 'To' key in inverse map ReferenceGrantsTo
185+
// Delete the 'To' key in full map toReferenceGrantFrom
186+
delete(mgr.toReferenceGrantFrom, to)
187+
// Delete the 'To' key in inverse map referenceGrantsTo
188188
delete(tos, to)
189189
// And continue
190190
continue
@@ -193,12 +193,12 @@ func (mgr *ReferenceGrantManager) RemoveReferenceGrantWithCheck(referenceGrant R
193193
delete(referenceGrantFrom, referenceGrantNamespacedName)
194194
// Cleanup if empty
195195
if len(referenceGrantFrom) == 0 {
196-
delete(mgr.ToReferenceGrantFrom, to)
196+
delete(mgr.toReferenceGrantFrom, to)
197197
}
198198
delete(tos, to)
199199
}
200200
if len(tos) == 0 {
201-
delete(mgr.ReferenceGrantsTo, referenceGrantNamespacedName)
201+
delete(mgr.referenceGrantsTo, referenceGrantNamespacedName)
202202
}
203203
}
204204

@@ -208,20 +208,20 @@ func (mgr *ReferenceGrantManager) RemoveReferenceGrant(referenceGrant ReferenceG
208208
mgr.RemoveReferenceGrantWithCheck(referenceGrant, true)
209209
}
210210

211-
// ComputeToFrom rebuilds the ToFrom map from ToReferenceGrantFrom. It must be called
211+
// ComputeToFrom rebuilds the toFrom map from toReferenceGrantFrom. It must be called
212212
// after all UpsertReferenceGrant and RemoveReferenceGrant calls for a reconcile cycle
213213
// have completed, and before any IsAccessGranted call consults the result.
214214
func (mgr *ReferenceGrantManager) ComputeToFrom() {
215-
mgr.ToFrom = map[To]map[From]struct{}{}
216-
for to, referenceGrantFrom := range mgr.ToReferenceGrantFrom {
215+
mgr.toFrom = map[To]map[From]struct{}{}
216+
for to, referenceGrantFrom := range mgr.toReferenceGrantFrom {
217217
for _, froms := range referenceGrantFrom {
218218
for from := range froms {
219-
existingroms := mgr.ToFrom[to]
220-
if existingroms == nil {
221-
existingroms = map[From]struct{}{}
222-
mgr.ToFrom[to] = existingroms
219+
existingFroms := mgr.toFrom[to]
220+
if existingFroms == nil {
221+
existingFroms = map[From]struct{}{}
222+
mgr.toFrom[to] = existingFroms
223223
}
224-
existingroms[from] = struct{}{}
224+
existingFroms[from] = struct{}{}
225225
}
226226
}
227227
}

k8s/gate/tree/reference_grant_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ func TestReferenceGrantManager_DeleteMultiToNoLeftovers(t *testing.T) {
184184
mgr.RemoveReferenceGrant(deleted(k8s))
185185
mgr.ComputeToFrom()
186186

187-
assert.Empty(t, mgr.ToReferenceGrantFrom, "ToReferenceGrantFrom must be empty after delete")
188-
assert.Empty(t, mgr.ReferenceGrantsTo, "ReferenceGrantsTo must be empty after delete")
187+
assert.Empty(t, mgr.toReferenceGrantFrom, "ToReferenceGrantFrom must be empty after delete")
188+
assert.Empty(t, mgr.referenceGrantsTo, "ReferenceGrantsTo must be empty after delete")
189189
assert.False(t, mgr.IsAccessGranted(gatewayv1.GroupName, "HTTPRoute", "route-ns", "", "Service", "backend-ns", "svc-a"))
190190
assert.False(t, mgr.IsAccessGranted(gatewayv1.GroupName, "HTTPRoute", "route-ns", "", "Service", "backend-ns", "svc-b"))
191191
}

0 commit comments

Comments
 (0)