Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Commit e58dd7c

Browse files
authored
remove containsLabel function, integrate with matchLabels to reduce complexity (#21)
* remove containsLabel function, integrate with matchLabels to reduce time complexity * fix comment * mod tidy * update deps * fix make docker-dev
1 parent b89da35 commit e58dd7c

File tree

12 files changed

+87
-52
lines changed

12 files changed

+87
-52
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# Changelog
22

3+
## 0.2.3
4+
5+
Optimize rule checking logic for better performance
6+
37
## 0.2.2
4-
8+
59
Update dependencies
610

711
## 0.2.1
8-
12+
913
Update dependencies
1014

1115
## 0.2.0

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GOCLEAN=$(GOCMD) clean
44
GORUN=$(GOCMD) run
55
PROJECT=circa10a/k8s-label-rules-webhook
66
BINARY=webhook
7-
VERSION=0.2.2
7+
VERSION=0.2.3
88
GOBUILDFLAGS=-ldflags="-s -w -X main.Version=$(VERSION)"
99

1010
# First target for travis ci
@@ -46,7 +46,7 @@ docker-build:
4646
docker-run:
4747
docker run --rm -v $(shell pwd)/rules.yaml:/rules.yaml \
4848
-p 8080:8080 \
49-
$(PROJECT) --file rules.yaml --metrics
49+
$(PROJECT):$(VERSION) --file rules.yaml --metrics
5050

5151
docker-dev: docker-build docker-run
5252

api/docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ type swaggerInfo struct {
234234

235235
// SwaggerInfo holds exported Swagger Info so clients can modify it
236236
var SwaggerInfo = swaggerInfo{
237-
Version: "0.1.0",
237+
Version: "0.2.3",
238238
Host: "",
239239
BasePath: "",
240240
Schemes: []string{},

api/swagger.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"name": "MIT",
1212
"url": "https://github.com/circa10a/k8s-label-rules-webhook/blob/master/LICENSE"
1313
},
14-
"version": "0.1.0"
14+
"version": "0.2.3"
1515
},
1616
"paths": {
1717
"/": {

api/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ info:
8181
name: MIT
8282
url: https://github.com/circa10a/k8s-label-rules-webhook/blob/master/LICENSE
8383
title: k8s-label-rules-webhook
84-
version: 0.1.0
84+
version: 0.2.3
8585
paths:
8686
//:
8787
post:

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ require (
99
github.com/fatih/structs v1.1.0 // indirect
1010
github.com/gavv/httpexpect v2.0.0+incompatible
1111
github.com/gin-gonic/gin v1.6.3
12+
github.com/go-openapi/spec v0.19.7 // indirect
13+
github.com/go-openapi/swag v0.19.9 // indirect
14+
github.com/golang/protobuf v1.4.1 // indirect
1215
github.com/google/go-querystring v1.0.0 // indirect
1316
github.com/gorilla/websocket v1.4.1 // indirect
1417
github.com/imkira/go-interpol v1.1.0 // indirect
1518
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
19+
github.com/mailru/easyjson v0.7.1 // indirect
1620
github.com/mattn/go-colorable v0.1.6 // indirect
1721
github.com/moul/http2curl v1.0.0 // indirect
1822
github.com/onsi/ginkgo v1.12.0 // indirect
1923
github.com/onsi/gomega v1.9.0 // indirect
20-
github.com/prometheus/client_golang v1.2.1 // indirect
24+
github.com/prometheus/client_golang v1.6.0 // indirect
2125
github.com/sergi/go-diff v1.0.0 // indirect
2226
github.com/sirupsen/logrus v1.6.0
2327
github.com/smartystreets/goconvey v1.6.4 // indirect
@@ -32,5 +36,8 @@ require (
3236
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
3337
github.com/yudai/pp v2.0.1+incompatible // indirect
3438
github.com/zsais/go-gin-prometheus v0.1.0
39+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect
40+
golang.org/x/sys v0.0.0-20200508214444-3aab700007d7 // indirect
41+
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5 // indirect
3542
gopkg.in/yaml.v2 v2.2.8
3643
)

go.sum

Lines changed: 56 additions & 20 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func flags() {
6969
}
7070

7171
// @title k8s-label-rules-webhook
72-
// @version 0.2.0
72+
// @version 0.2.3
7373
// @description A kubernetes webhook to standardize labels on resources
7474

7575
// @contact.name GitHub

routes.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,8 @@ func labelValidationHandler() gin.HandlerFunc {
3939
if c.ShouldBindJSON(&k8sData) == nil {
4040
labels := k8sData.Request.Object.Metadata.Labels
4141
uid := k8sData.Request.Object.Metadata.UID
42-
// Ensure labels provided contain keys identified in the ruleset
43-
containLabelErr := R.ensureLabelsContainRules(labels)
44-
// Reject request if not
45-
if containLabelErr != nil {
46-
sendResponse(c, k8sData, uid, false, http.StatusForbidden, containLabelErr.Error())
47-
return
48-
}
49-
// Ensure labels provided match regex of keys identified in the ruleset
42+
// Ensure labels contain keys identified in the ruleset
43+
// Ensure labels are present and user provided regex match values identified in the ruleset
5044
matchLabelErr := R.ensureLabelsMatchRules(labels)
5145
// Reject request if not
5246
if matchLabelErr != nil {

rules.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import (
99
"gopkg.in/yaml.v2"
1010
)
1111

12-
// Rules array from yaml
12+
// Rules is a slice of rules that are loaded from a yaml array
1313
type rules struct {
1414
Rules []rule `yaml:"rules" json:"rules"`
1515
CompiledRegexs map[string]*regexp.Regexp
1616
}
1717

18-
// Individual rule within rules array
18+
// Rule is a struct that represents a rule within rules array
1919
type rule struct {
2020
Name string `yaml:"name" json:"name"`
2121
Key string `yaml:"key" json:"key"`
2222
Value value `yaml:"value" json:"value"`
2323
}
2424

25-
// Value struct within each rule
25+
// Value is struct within each rule which only supports regex, but can be expanded
2626
type value struct {
2727
Regex string `yaml:"regex" json:"regex"`
2828
}
@@ -81,20 +81,14 @@ func (r *rules) validateAllRulesRegex() []ruleError {
8181
return r.compileRegex(false)
8282
}
8383

84-
func (r *rules) ensureLabelsContainRules(labels map[string]interface{}) error {
84+
func (r *rules) ensureLabelsMatchRules(labels map[string]interface{}) error {
8585
for _, rule := range r.Rules {
8686
// Ensure labels contains rule
8787
if _, ok := labels[rule.Key]; !ok {
8888
// If rule is not found, reject
8989
errStr := fmt.Sprintf("%v not in labels", rule.Key)
9090
return errors.New(errStr)
9191
}
92-
}
93-
return nil
94-
}
95-
96-
func (r *rules) ensureLabelsMatchRules(labels map[string]interface{}) error {
97-
for _, rule := range r.Rules {
9892
// Force all values to strings to prevent panic from interface conversion
9993
labelVal := fmt.Sprintf("%v", labels[rule.Key])
10094
regex, _ := r.CompiledRegexs[rule.Name]

0 commit comments

Comments
 (0)