Skip to content

Commit cb99d97

Browse files
committed
Refactoring: make client package for client related annotations.
1 parent 0edf16f commit cb99d97

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

docs/user-guide/nginx-configuration/annotations-risk.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
| CertificateAuth | auth-tls-secret | Medium | location |
2323
| CertificateAuth | auth-tls-verify-client | Medium | location |
2424
| CertificateAuth | auth-tls-verify-depth | Low | location |
25-
| ClientBodyBufferSize | client-body-buffer-size | Low | location |
25+
| Client | client-body-buffer-size | Low | location |
2626
| ConfigurationSnippet | configuration-snippet | Critical | location |
2727
| Connection | connection-proxy-header | Low | location |
2828
| CorsConfig | cors-allow-credentials | Low | ingress |

internal/ingress/annotations/annotations.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"k8s.io/ingress-nginx/internal/ingress/annotations/authtls"
3232
"k8s.io/ingress-nginx/internal/ingress/annotations/backendprotocol"
3333
"k8s.io/ingress-nginx/internal/ingress/annotations/canary"
34-
"k8s.io/ingress-nginx/internal/ingress/annotations/clientbodybuffersize"
34+
"k8s.io/ingress-nginx/internal/ingress/annotations/client"
3535
"k8s.io/ingress-nginx/internal/ingress/annotations/connection"
3636
"k8s.io/ingress-nginx/internal/ingress/annotations/cors"
3737
"k8s.io/ingress-nginx/internal/ingress/annotations/customheaders"
@@ -80,7 +80,7 @@ type Ingress struct {
8080
BasicDigestAuth auth.Config
8181
Canary canary.Config
8282
CertificateAuth authtls.Config
83-
ClientBodyBufferSize string
83+
Client client.Config
8484
CustomHeaders customheaders.Config
8585
ConfigurationSnippet string
8686
Connection connection.Config
@@ -129,7 +129,7 @@ func NewAnnotationFactory(cfg resolver.Resolver) map[string]parser.IngressAnnota
129129
"BasicDigestAuth": auth.NewParser(auth.AuthDirectory, cfg),
130130
"Canary": canary.NewParser(cfg),
131131
"CertificateAuth": authtls.NewParser(cfg),
132-
"ClientBodyBufferSize": clientbodybuffersize.NewParser(cfg),
132+
"Client": client.NewParser(cfg),
133133
"CustomHeaders": customheaders.NewParser(cfg),
134134
"ConfigurationSnippet": snippet.NewParser(cfg),
135135
"Connection": connection.NewParser(cfg),

internal/ingress/annotations/clientbodybuffersize/main.go internal/ingress/annotations/client/main.go

+38-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package clientbodybuffersize
17+
package client
1818

1919
import (
2020
networking "k8s.io/api/networking/v1"
@@ -27,7 +27,7 @@ const (
2727
clientBodyBufferSizeAnnotation = "client-body-buffer-size"
2828
)
2929

30-
var clientBodyBufferSizeConfig = parser.Annotation{
30+
var clientAnnotations = parser.Annotation{
3131
Group: "backend",
3232
Annotations: parser.AnnotationFields{
3333
clientBodyBufferSizeAnnotation: {
@@ -42,30 +42,54 @@ var clientBodyBufferSizeConfig = parser.Annotation{
4242
},
4343
}
4444

45-
type clientBodyBufferSize struct {
45+
type Config struct {
46+
BodyBufferSize string `json:"bodyBufferSize"`
47+
}
48+
49+
// Equal tests for equality between two Configuration types
50+
func (l1 *Config) Equal(l2 *Config) bool {
51+
if l1 == l2 {
52+
return true
53+
}
54+
if l1 == nil || l2 == nil {
55+
return false
56+
}
57+
if l1.BodyBufferSize != l2.BodyBufferSize {
58+
return false
59+
}
60+
61+
return true
62+
}
63+
64+
type client struct {
4665
r resolver.Resolver
4766
annotationConfig parser.Annotation
4867
}
4968

50-
// NewParser creates a new clientBodyBufferSize annotation parser
69+
// NewParser creates a new client annotation parser
5170
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
52-
return clientBodyBufferSize{
71+
return client{
5372
r: r,
54-
annotationConfig: clientBodyBufferSizeConfig,
73+
annotationConfig: clientAnnotations,
5574
}
5675
}
5776

58-
func (cbbs clientBodyBufferSize) GetDocumentation() parser.AnnotationFields {
59-
return cbbs.annotationConfig.Annotations
77+
func (c client) GetDocumentation() parser.AnnotationFields {
78+
return c.annotationConfig.Annotations
6079
}
6180

6281
// Parse parses the annotations contained in the ingress rule
63-
// used to add an client-body-buffer-size to the provided locations
64-
func (cbbs clientBodyBufferSize) Parse(ing *networking.Ingress) (interface{}, error) {
65-
return parser.GetStringAnnotation(clientBodyBufferSizeAnnotation, ing, cbbs.annotationConfig.Annotations)
82+
// used to add an client related configuration to the provided locations.
83+
func (c client) Parse(ing *networking.Ingress) (interface{}, error) {
84+
config := &Config{}
85+
86+
var err error
87+
config.BodyBufferSize, err = parser.GetStringAnnotation(clientBodyBufferSizeAnnotation, ing, c.annotationConfig.Annotations)
88+
89+
return config, err
6690
}
6791

68-
func (cbbs clientBodyBufferSize) Validate(anns map[string]string) error {
69-
maxrisk := parser.StringRiskToRisk(cbbs.r.GetSecurityConfiguration().AnnotationsRiskLevel)
70-
return parser.CheckAnnotationRisk(anns, maxrisk, clientBodyBufferSizeConfig.Annotations)
92+
func (c client) Validate(annotations map[string]string) error {
93+
maxRisk := parser.StringRiskToRisk(c.r.GetSecurityConfiguration().AnnotationsRiskLevel)
94+
return parser.CheckAnnotationRisk(annotations, maxRisk, clientAnnotations.Annotations)
7195
}

internal/ingress/annotations/clientbodybuffersize/main_test.go internal/ingress/annotations/client/main_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package clientbodybuffersize
17+
package client
1818

1919
import (
2020
"testing"
2121

2222
api "k8s.io/api/core/v1"
2323
networking "k8s.io/api/networking/v1"
24-
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
2526
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
2627
"k8s.io/ingress-nginx/internal/ingress/resolver"
2728
)
@@ -48,7 +49,7 @@ func TestParse(t *testing.T) {
4849
}
4950

5051
ing := &networking.Ingress{
51-
ObjectMeta: meta_v1.ObjectMeta{
52+
ObjectMeta: metav1.ObjectMeta{
5253
Name: "foo",
5354
Namespace: api.NamespaceDefault,
5455
},
@@ -58,9 +59,13 @@ func TestParse(t *testing.T) {
5859
for _, testCase := range testCases {
5960
ing.SetAnnotations(testCase.annotations)
6061
//nolint:errcheck // Ignore the error since invalid cases will be checked with expected results
61-
result, _ := ap.Parse(ing)
62-
if result != testCase.expected {
63-
t.Errorf("expected %v but returned %v, annotations: %s", testCase.expected, result, testCase.annotations)
62+
res, _ := ap.Parse(ing)
63+
c, ok := res.(*Config)
64+
if !ok {
65+
t.Fatal("expected a client.Config type")
66+
}
67+
if c.BodyBufferSize != testCase.expected {
68+
t.Errorf("expected %v but returned %v, annotations: %s", testCase.expected, res, testCase.annotations)
6469
}
6570
}
6671
}

internal/ingress/controller/controller.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"k8s.io/apimachinery/pkg/util/sets"
3333
"k8s.io/apimachinery/pkg/util/wait"
3434
clientset "k8s.io/client-go/kubernetes"
35+
"k8s.io/klog/v2"
36+
3537
"k8s.io/ingress-nginx/internal/ingress/annotations"
3638
"k8s.io/ingress-nginx/internal/ingress/annotations/canary"
3739
"k8s.io/ingress-nginx/internal/ingress/annotations/log"
@@ -47,7 +49,6 @@ import (
4749
"k8s.io/ingress-nginx/internal/nginx"
4850
"k8s.io/ingress-nginx/pkg/apis/ingress"
4951
utilingress "k8s.io/ingress-nginx/pkg/util/ingress"
50-
"k8s.io/klog/v2"
5152
)
5253

5354
const (
@@ -1502,7 +1503,7 @@ func (n *NGINXController) createServers(data []*ingress.Ingress,
15021503

15031504
func locationApplyAnnotations(loc *ingress.Location, anns *annotations.Ingress) {
15041505
loc.BasicDigestAuth = anns.BasicDigestAuth
1505-
loc.ClientBodyBufferSize = anns.ClientBodyBufferSize
1506+
loc.ClientBodyBufferSize = anns.Client.BodyBufferSize
15061507
loc.CustomHeaders = anns.CustomHeaders
15071508
loc.ConfigurationSnippet = anns.ConfigurationSnippet
15081509
loc.CorsConfig = anns.CorsConfig

0 commit comments

Comments
 (0)