Skip to content

Commit 38e0ffe

Browse files
committed
make controllerinformer interface private;
use named regexp groups
1 parent 228639b commit 38e0ffe

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

pkg/apiserver/apiserver.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/Sirupsen/logrus"
1515

1616
"github.com/zalando-incubator/postgres-operator/pkg/spec"
17+
"github.com/zalando-incubator/postgres-operator/pkg/util"
1718
"github.com/zalando-incubator/postgres-operator/pkg/util/config"
1819
)
1920

@@ -24,7 +25,7 @@ const (
2425
)
2526

2627
// ControllerInformer describes stats methods of a controller
27-
type ControllerInformer interface {
28+
type controllerInformer interface {
2829
GetConfig() *spec.ControllerConfig
2930
GetOperatorConfig() *config.Config
3031
GetStatus() *spec.ControllerStatus
@@ -40,7 +41,7 @@ type ControllerInformer interface {
4041
type Server struct {
4142
logger *logrus.Entry
4243
http http.Server
43-
controller ControllerInformer
44+
controller controllerInformer
4445
}
4546

4647
var (
@@ -54,7 +55,7 @@ var (
5455
)
5556

5657
// New creates new HTTP API server
57-
func New(controller ControllerInformer, port int, logger *logrus.Logger) *Server {
58+
func New(controller controllerInformer, port int, logger *logrus.Logger) *Server {
5859
s := &Server{
5960
logger: logger.WithField("pkg", "apiserver"),
6061
controller: controller,
@@ -140,24 +141,24 @@ func (s *Server) clusters(w http.ResponseWriter, req *http.Request) {
140141
err error
141142
)
142143

143-
if matches := clusterStatusURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil {
144-
resp, err = s.controller.ClusterStatus(matches[0][1], matches[0][2])
145-
} else if matches := teamURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil {
144+
if matches := util.FindNamedStringSubmatch(clusterStatusURL, req.URL.Path); matches != nil {
145+
resp, err = s.controller.ClusterStatus(matches["team"], matches["cluster"])
146+
} else if matches := util.FindNamedStringSubmatch(teamURL, req.URL.Path); matches != nil {
146147
teamClusters := s.controller.TeamClusterList()
147-
clusters, found := teamClusters[matches[0][1]]
148+
clusters, found := teamClusters[matches["team"]]
148149
if !found {
149150
s.respond(nil, fmt.Errorf("could not find clusters for the team"), w)
150151
}
151152

152153
clusterNames := make([]string, 0)
153154
for _, cluster := range clusters {
154-
clusterNames = append(clusterNames, cluster.Name[len(matches[0][1])+1:])
155+
clusterNames = append(clusterNames, cluster.Name[len(matches["team"])+1:])
155156
}
156157

157158
s.respond(clusterNames, nil, w)
158159
return
159-
} else if matches := clusterLogsURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil {
160-
resp, err = s.controller.ClusterLogs(matches[0][1], matches[0][2])
160+
} else if matches := util.FindNamedStringSubmatch(clusterLogsURL, req.URL.Path); matches != nil {
161+
resp, err = s.controller.ClusterLogs(matches["team"], matches["cluster"])
161162
} else if req.URL.Path == clustersURL {
162163
res := make(map[string][]string)
163164
for team, clusters := range s.controller.TeamClusterList() {
@@ -181,15 +182,16 @@ func (s *Server) workers(w http.ResponseWriter, req *http.Request) {
181182
resp interface{}
182183
err error
183184
)
185+
184186
if workerAllQueue.MatchString(req.URL.Path) {
185187
s.allQueues(w, req)
186188
return
187-
} else if matches := workerLogsURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil {
188-
workerID, _ := strconv.Atoi(matches[0][1])
189+
} else if matches := util.FindNamedStringSubmatch(workerLogsURL, req.URL.Path); matches != nil {
190+
workerID, _ := strconv.Atoi(matches["id"])
189191

190192
resp, err = s.controller.WorkerLogs(uint32(workerID))
191-
} else if matches := workerEventsQueueURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil {
192-
workerID, _ := strconv.Atoi(matches[0][1])
193+
} else if matches := util.FindNamedStringSubmatch(workerEventsQueueURL, req.URL.Path); matches != nil {
194+
workerID, _ := strconv.Atoi(matches["id"])
193195

194196
resp, err = s.controller.ListQueue(uint32(workerID))
195197
} else {

pkg/util/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/motomux/pretty"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212

13+
"regexp"
14+
1315
"github.com/zalando-incubator/postgres-operator/pkg/spec"
1416
)
1517

@@ -71,3 +73,29 @@ OUTER:
7173
}
7274
return result, len(result) == 0
7375
}
76+
77+
func FindNamedStringSubmatch(r *regexp.Regexp, s string) map[string]string {
78+
matches := r.FindStringSubmatch(s)
79+
grNames := r.SubexpNames()
80+
81+
if matches == nil {
82+
return nil
83+
}
84+
85+
groupMatches := 0
86+
res := make(map[string]string, len(grNames))
87+
for i, n := range grNames {
88+
if n == "" {
89+
continue
90+
}
91+
92+
res[n] = matches[i]
93+
groupMatches++
94+
}
95+
96+
if groupMatches == 0 {
97+
return nil
98+
}
99+
100+
return res
101+
}

pkg/util/util_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88

9+
"regexp"
10+
911
"github.com/zalando-incubator/postgres-operator/pkg/spec"
1012
)
1113

@@ -44,6 +46,17 @@ var substractTest = []struct {
4446
{[]string{"a", "b", "c", "d"}, []string{"a", "bb", "c", "d"}, []string{"b"}, false},
4547
}
4648

49+
var substringMatch = []struct {
50+
inRegex *regexp.Regexp
51+
inStr string
52+
out map[string]string
53+
}{
54+
{regexp.MustCompile(`aaaa (?P<num>\d+) bbbb`), "aaaa 123 bbbb", map[string]string{"num": "123"}},
55+
{regexp.MustCompile(`aaaa (?P<num>\d+) bbbb`), "a aa 123 bbbb", nil},
56+
{regexp.MustCompile(`aaaa \d+ bbbb`), "aaaa 123 bbbb", nil},
57+
{regexp.MustCompile(`aaaa (\d+) bbbb`), "aaaa 123 bbbb", nil},
58+
}
59+
4760
func TestRandomPassword(t *testing.T) {
4861
const pwdLength = 10
4962
pwd := RandomPassword(pwdLength)
@@ -100,3 +113,12 @@ func TestSubstractSlices(t *testing.T) {
100113
}
101114
}
102115
}
116+
117+
func TestFindNamedStringSubmatch(t *testing.T) {
118+
for _, tt := range substringMatch {
119+
actualRes := FindNamedStringSubmatch(tt.inRegex, tt.inStr)
120+
if !reflect.DeepEqual(actualRes, tt.out) {
121+
t.Errorf("FindNamedStringSubmatch expected: %#v, got: %#v", tt.out, actualRes)
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)