Skip to content

Commit 4e8d06e

Browse files
committed
refactor: remove unneccessary getters
1 parent 00a0343 commit 4e8d06e

5 files changed

+75
-64
lines changed

api/v1beta2/authpolicy_types.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ type AuthPolicyCommonSpec struct {
187187
AuthScheme *AuthSchemeSpec `json:"rules,omitempty"`
188188
}
189189

190+
// GetRouteSelectors returns the top-level route selectors of the auth scheme.
191+
// impl: RouteSelectorsGetter
192+
func (c AuthPolicyCommonSpec) GetRouteSelectors() []RouteSelector {
193+
return c.RouteSelectors
194+
}
195+
190196
type AuthPolicyStatus struct {
191197
// ObservedGeneration reflects the generation of the most recently observed spec.
192198
// +optional
@@ -279,7 +285,7 @@ func (ap *AuthPolicy) GetRulesHostnames() (ruleHosts []string) {
279285
}
280286
}
281287

282-
appendCommonSpecRuleHosts := func(c AuthPolicyCommonSpec) {
288+
appendCommonSpecRuleHosts := func(c *AuthPolicyCommonSpec) {
283289
if c.AuthScheme == nil {
284290
return
285291
}
@@ -306,8 +312,8 @@ func (ap *AuthPolicy) GetRulesHostnames() (ruleHosts []string) {
306312
}
307313
}
308314

309-
appendRuleHosts(ap)
310-
appendCommonSpecRuleHosts(ap.GetCommonSpec())
315+
appendRuleHosts(ap.Spec.CommonSpec())
316+
appendCommonSpecRuleHosts(ap.Spec.CommonSpec())
311317

312318
return
313319
}
@@ -324,30 +330,12 @@ func (ap *AuthPolicy) DirectReferenceAnnotationName() string {
324330
return AuthPolicyDirectReferenceAnnotationName
325331
}
326332

327-
func (ap *AuthPolicy) GetCommonSpec() AuthPolicyCommonSpec {
328-
if ap.Spec.Defaults != nil {
329-
return *ap.Spec.Defaults
333+
func (ap *AuthPolicySpec) CommonSpec() *AuthPolicyCommonSpec {
334+
if ap.Defaults != nil {
335+
return ap.Defaults
330336
}
331337

332-
return ap.Spec.AuthPolicyCommonSpec
333-
}
334-
335-
func (ap *AuthPolicy) GetNamedPatterns() map[string]authorinoapi.PatternExpressions {
336-
return ap.GetCommonSpec().NamedPatterns
337-
}
338-
339-
func (ap *AuthPolicy) GetConditions() []authorinoapi.PatternExpressionOrRef {
340-
return ap.GetCommonSpec().Conditions
341-
}
342-
343-
func (ap *AuthPolicy) GetAuthScheme() *AuthSchemeSpec {
344-
return ap.GetCommonSpec().AuthScheme
345-
}
346-
347-
// GetRouteSelectors returns the top-level route selectors of the auth scheme.
348-
// impl: RouteSelectorsGetter
349-
func (ap *AuthPolicy) GetRouteSelectors() []RouteSelector {
350-
return ap.GetCommonSpec().RouteSelectors
338+
return &ap.AuthPolicyCommonSpec
351339
}
352340

353341
//+kubebuilder:object:root=true

api/v1beta2/authpolicy_types_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func TestCommonAuthRuleSpecGetRouteSelectors(t *testing.T) {
3232
}
3333

3434
func TestAuthPolicySpecGetRouteSelectors(t *testing.T) {
35-
p := &AuthPolicy{}
36-
if p.GetRouteSelectors() != nil {
35+
spec := &AuthPolicySpec{}
36+
if spec.GetRouteSelectors() != nil {
3737
t.Errorf("Expected nil route selectors")
3838
}
3939
routeSelector := testBuildRouteSelector()
40-
p.Spec.RouteSelectors = []RouteSelector{routeSelector}
41-
result := p.GetRouteSelectors()
40+
spec.RouteSelectors = []RouteSelector{routeSelector}
41+
result := spec.GetRouteSelectors()
4242
if len(result) != 1 {
4343
t.Errorf("Expected 1 route selector, got %d", len(result))
4444
}

controllers/authpolicy_authconfig.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -111,40 +111,47 @@ func (r *AuthPolicyReconciler) desiredAuthConfig(ctx context.Context, ap *api.Au
111111
// hosts
112112
authConfig.Spec.Hosts = hosts
113113

114+
commonSpec := ap.Spec.CommonSpec()
115+
114116
// named patterns
115-
if namedPatterns := ap.GetNamedPatterns(); len(namedPatterns) > 0 {
117+
if namedPatterns := commonSpec.NamedPatterns; len(namedPatterns) > 0 {
116118
authConfig.Spec.NamedPatterns = namedPatterns
117119
}
118120

119121
// top-level conditions
120-
topLevelConditionsFromRouteSelectors, err := authorinoConditionsFromRouteSelectors(route, ap)
122+
topLevelConditionsFromRouteSelectors, err := authorinoConditionsFromRouteSelectors(route, commonSpec)
121123
if err != nil {
122124
return nil, err
123125
}
124126
if len(topLevelConditionsFromRouteSelectors) == 0 {
125127
topLevelConditionsFromRouteSelectors = authorinoConditionsFromHTTPRoute(route)
126128
}
127-
if len(topLevelConditionsFromRouteSelectors) > 0 || len(ap.GetConditions()) > 0 {
128-
authConfig.Spec.Conditions = append(ap.GetConditions(), topLevelConditionsFromRouteSelectors...)
129+
if len(topLevelConditionsFromRouteSelectors) > 0 || len(commonSpec.Conditions) > 0 {
130+
authConfig.Spec.Conditions = append(commonSpec.Conditions, topLevelConditionsFromRouteSelectors...)
131+
}
132+
133+
// return early if authScheme is nil
134+
if commonSpec.AuthScheme == nil {
135+
return authConfig, nil
129136
}
130137

131138
// authentication
132-
if authentication := ap.GetAuthScheme().Authentication; len(authentication) > 0 {
139+
if authentication := commonSpec.AuthScheme.Authentication; len(authentication) > 0 {
133140
authConfig.Spec.Authentication = authorinoSpecsFromConfigs(authentication, func(config api.AuthenticationSpec) authorinoapi.AuthenticationSpec { return config.AuthenticationSpec })
134141
}
135142

136143
// metadata
137-
if metadata := ap.GetAuthScheme().Metadata; len(metadata) > 0 {
144+
if metadata := commonSpec.AuthScheme.Metadata; len(metadata) > 0 {
138145
authConfig.Spec.Metadata = authorinoSpecsFromConfigs(metadata, func(config api.MetadataSpec) authorinoapi.MetadataSpec { return config.MetadataSpec })
139146
}
140147

141148
// authorization
142-
if authorization := ap.GetAuthScheme().Authorization; len(authorization) > 0 {
149+
if authorization := commonSpec.AuthScheme.Authorization; len(authorization) > 0 {
143150
authConfig.Spec.Authorization = authorinoSpecsFromConfigs(authorization, func(config api.AuthorizationSpec) authorinoapi.AuthorizationSpec { return config.AuthorizationSpec })
144151
}
145152

146153
// response
147-
if response := ap.GetAuthScheme().Response; response != nil {
154+
if response := commonSpec.AuthScheme.Response; response != nil {
148155
authConfig.Spec.Response = &authorinoapi.ResponseSpec{
149156
Unauthenticated: response.Unauthenticated,
150157
Unauthorized: response.Unauthorized,
@@ -160,7 +167,7 @@ func (r *AuthPolicyReconciler) desiredAuthConfig(ctx context.Context, ap *api.Au
160167
}
161168

162169
// callbacks
163-
if callbacks := ap.GetAuthScheme().Callbacks; len(callbacks) > 0 {
170+
if callbacks := commonSpec.AuthScheme.Callbacks; len(callbacks) > 0 {
164171
authConfig.Spec.Callbacks = authorinoSpecsFromConfigs(callbacks, func(config api.CallbackSpec) authorinoapi.CallbackSpec { return config.CallbackSpec })
165172
}
166173

@@ -187,8 +194,10 @@ func authorinoSpecsFromConfigs[T, U any](configs map[string]U, extractAuthorinoS
187194
}
188195

189196
func mergeConditionsFromRouteSelectorsIntoConfigs(ap *api.AuthPolicy, route *gatewayapiv1.HTTPRoute, authConfig *authorinoapi.AuthConfig) (*authorinoapi.AuthConfig, error) {
197+
commonSpec := ap.Spec.CommonSpec()
198+
190199
// authentication
191-
for name, config := range ap.GetAuthScheme().Authentication {
200+
for name, config := range commonSpec.AuthScheme.Authentication {
192201
conditions, err := authorinoConditionsFromRouteSelectors(route, config)
193202
if err != nil {
194203
return nil, err
@@ -202,7 +211,7 @@ func mergeConditionsFromRouteSelectorsIntoConfigs(ap *api.AuthPolicy, route *gat
202211
}
203212

204213
// metadata
205-
for name, config := range ap.GetAuthScheme().Metadata {
214+
for name, config := range commonSpec.AuthScheme.Metadata {
206215
conditions, err := authorinoConditionsFromRouteSelectors(route, config)
207216
if err != nil {
208217
return nil, err
@@ -216,7 +225,7 @@ func mergeConditionsFromRouteSelectorsIntoConfigs(ap *api.AuthPolicy, route *gat
216225
}
217226

218227
// authorization
219-
for name, config := range ap.GetAuthScheme().Authorization {
228+
for name, config := range commonSpec.AuthScheme.Authorization {
220229
conditions, err := authorinoConditionsFromRouteSelectors(route, config)
221230
if err != nil {
222231
return nil, err
@@ -230,7 +239,7 @@ func mergeConditionsFromRouteSelectorsIntoConfigs(ap *api.AuthPolicy, route *gat
230239
}
231240

232241
// response
233-
if response := ap.GetAuthScheme().Response; response != nil {
242+
if response := commonSpec.AuthScheme.Response; response != nil {
234243
// response success headers
235244
for name, config := range response.Success.Headers {
236245
conditions, err := authorinoConditionsFromRouteSelectors(route, config)
@@ -261,7 +270,7 @@ func mergeConditionsFromRouteSelectorsIntoConfigs(ap *api.AuthPolicy, route *gat
261270
}
262271

263272
// callbacks
264-
for name, config := range ap.GetAuthScheme().Callbacks {
273+
for name, config := range commonSpec.AuthScheme.Callbacks {
265274
conditions, err := authorinoConditionsFromRouteSelectors(route, config)
266275
if err != nil {
267276
return nil, err

0 commit comments

Comments
 (0)