Skip to content

Commit

Permalink
Adding linters and aligning code (#169)
Browse files Browse the repository at this point in the history
* Adding linters and aligning code

* Aligning ingressHostnames to AllowedListSpec
  • Loading branch information
prometherion authored Jan 13, 2021
1 parent 89c66de commit d270055
Show file tree
Hide file tree
Showing 56 changed files with 428 additions and 562 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
with:
version: latest
only-new-issues: false
args: --timeout 2m
args: --timeout 2m --config .golangci.yml
diff:
name: diff
runs-on: ubuntu-18.04
Expand Down
59 changes: 59 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
maligned:
suggest-new: true
goimports:
local-prefixes: github.com/clastix/capsule
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- goconst
- gocritic
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- maligned

issues:
exclude:
- Using the variable on range scope .* in function literal

service:
golangci-lint-version: 1.33.x

run:
skip-files:
- "zz_.*\\.go$"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ goimports:
# Linting code as PR is expecting
.PHONY: golint
golint:
golangci-lint run
golangci-lint run -c .golangci.yml

# Running e2e tests in a KinD instance
.PHONY: e2e
Expand Down
31 changes: 17 additions & 14 deletions api/v1alpha1/storage_class_list.go → api/v1alpha1/allowed_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ limitations under the License.
package v1alpha1

import (
"regexp"
"sort"
"strings"
)

type StorageClassList []string

func (n StorageClassList) Len() int {
return len(n)
}

func (n StorageClassList) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
type AllowedListSpec struct {
Exact []string `json:"allowed,omitempty"`
Regex string `json:"allowedRegex,omitempty"`
}

func (n StorageClassList) Less(i, j int) bool {
return strings.ToLower(n[i]) < strings.ToLower(n[j])
func (in *AllowedListSpec) ExactMatch(value string) (ok bool) {
if len(in.Exact) > 0 {
sort.SliceStable(in.Exact, func(i, j int) bool {
return strings.ToLower(in.Exact[i]) < strings.ToLower(in.Exact[j])
})
i := sort.SearchStrings(in.Exact, value)
ok = i < len(in.Exact) && in.Exact[i] == value
}
return
}

func (n StorageClassList) IsStringInList(value string) (ok bool) {
sort.Sort(n)
i := sort.SearchStrings(n, value)
ok = i < n.Len() && n[i] == value
func (in AllowedListSpec) RegexMatch(value string) (ok bool) {
if len(in.Regex) > 0 {
ok = regexp.MustCompile(in.Regex).MatchString(value)
}
return
}
80 changes: 80 additions & 0 deletions api/v1alpha1/allowed_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2020 Clastix Labs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAllowedListSpec_ExactMatch(t *testing.T) {
type tc struct {
In []string
True []string
False []string
}
for _, tc := range []tc{
{
[]string{"foo", "bar", "bizz", "buzz"},
[]string{"foo", "bar", "bizz", "buzz"},
[]string{"bing", "bong"},
},
{
[]string{"one", "two", "three"},
[]string{"one", "two", "three"},
[]string{"a", "b", "c"},
},
{
nil,
nil,
[]string{"any", "value"},
},
} {
a := AllowedListSpec{
Exact: tc.In,
}
for _, ok := range tc.True {
assert.True(t, a.ExactMatch(ok))
}
for _, ko := range tc.False {
assert.False(t, a.ExactMatch(ko))
}
}
}

func TestAllowedListSpec_RegexMatch(t *testing.T) {
type tc struct {
Regex string
True []string
False []string
}
for _, tc := range []tc{
{`first-\w+-pattern`, []string{"first-date-pattern", "first-year-pattern"}, []string{"broken", "first-year", "second-date-pattern"}},
{``, nil, []string{"any", "value"}},
} {
a := AllowedListSpec{
Regex: tc.Regex,
}
for _, ok := range tc.True {
assert.True(t, a.RegexMatch(ok))
}
for _, ko := range tc.False {
assert.False(t, a.RegexMatch(ko))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

package domain

type SearchIn interface {
IsStringInList(value string) bool
type AllowedList interface {
ExactMatch(value string) bool
RegexMatch(value string) bool
}
12 changes: 7 additions & 5 deletions api/v1alpha1/domain/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"regexp"
)

const defaultRegistryName = "docker.io"

type registry map[string]string

func (r registry) Registry() string {
Expand All @@ -28,7 +30,7 @@ func (r registry) Registry() string {
return ""
}
if len(res) == 0 {
return "docker.io"
return defaultRegistryName
}
return res
}
Expand All @@ -38,7 +40,7 @@ func (r registry) Repository() string {
if !ok {
return ""
}
if res == "docker.io" {
if res == defaultRegistryName {
return ""
}
return res
Expand All @@ -64,15 +66,15 @@ func (r registry) Tag() string {
}

func NewRegistry(value string) Registry {
registry := make(registry)
reg := make(registry)
r := regexp.MustCompile(`(((?P<registry>[a-zA-Z0-9-.]+)\/)?((?P<repository>[a-zA-Z0-9-.]+)\/))?(?P<image>[a-zA-Z0-9-.]+)(:(?P<tag>[a-zA-Z0-9-.]+))?`)
match := r.FindStringSubmatch(value)
for i, name := range r.SubexpNames() {
if i > 0 && i <= len(match) {
registry[name] = match[i]
reg[name] = match[i]
}
}
return registry
return reg
}

type Registry interface {
Expand Down
43 changes: 0 additions & 43 deletions api/v1alpha1/ingress_class_list.go

This file was deleted.

43 changes: 0 additions & 43 deletions api/v1alpha1/namespace_list.go

This file was deleted.

43 changes: 0 additions & 43 deletions api/v1alpha1/registry_class_list.go

This file was deleted.

Loading

0 comments on commit d270055

Please sign in to comment.