Skip to content

Commit

Permalink
Adding validate phase to the CI
Browse files Browse the repository at this point in the history
Adds a validate phase to the CI which runs a linter. Also fixes
linter issues discovered during the initial run
  • Loading branch information
MbolotSuse committed Oct 14, 2022
1 parent 7e4a51b commit 9480a3e
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 37 deletions.
16 changes: 16 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ steps:
- pull_request
---
kind: pipeline
name: validate

steps:
- name: validate
image: registry.suse.com/bci/bci-base:15.4
commands:
- zypper in -y go=1.19 git tar gzip make
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.49.0
- mv ./bin/golangci-lint /usr/local/bin/golangci-lint
- make validate
when:
event:
- push
- pull_request
---
kind: pipeline
name: test

steps:
Expand Down
67 changes: 67 additions & 0 deletions .golangci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"linters": {
"disable-all": true,
"enable": [
"govet",
"revive",
"goimports",
"misspell",
"ineffassign",
"gofmt"
]
},
"linters-settings": {
"govet": {
"check-shadowing": false
},
"gofmt": {
"simplify": false
}
},
"run": {
"skip-dirs": [
"vendor",
"tests",
"pkg/client",
"pkg/generated"
],
"tests": false,
"timeout": "10m"
},
"issues": {
"exclude-rules": [
{
"linters": "govet",
"text": "^(nilness|structtag)"
},
{
"path":"pkg/apis/management.cattle.io/v3/globaldns_types.go",
"text":".*lobalDns.*"
},
{
"path": "pkg/apis/management.cattle.io/v3/zz_generated_register.go",
"text":".*lobalDns.*"
},
{
"path":"pkg/apis/management.cattle.io/v3/zz_generated_list_types.go",
"text":".*lobalDns.*"
},
{
"linters": "revive",
"text": "should have comment"
},
{
"linters": "revive",
"text": "should be of the form"
},
{
"linters": "revive",
"text": "by other packages, and that stutters"
},
{
"linters": "typecheck",
"text": "imported but not used as apierrors"
}
]
}
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ run-host: build
docker run $(DOCKER_ARGS) --net=host --uts=host --rm -it -v ${HOME}/.kube:/root/.kube steve --kubeconfig /root/.kube/config --http-listen-port 8989 --https-listen-port 0

test:
bash scripts/test.sh
bash scripts/test.sh

validate:
bash scripts/validate.sh
2 changes: 1 addition & 1 deletion pkg/auth/cli/webhookcli.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cli

import (
"k8s.io/client-go/tools/clientcmd"
"os"
"time"

"github.com/rancher/steve/pkg/auth"
"github.com/urfave/cli"
"k8s.io/client-go/tools/clientcmd"
)

type WebhookConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package auth

import (
"io/ioutil"
"k8s.io/client-go/rest"
"net/http"
"strings"
"time"
Expand All @@ -13,6 +12,7 @@ import (
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/plugin/pkg/authenticator/token/webhook"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/transport"
Expand Down
8 changes: 4 additions & 4 deletions pkg/podimpersonation/podimpersonation.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ type PodOptions struct {
// CreatePod will create a pod with a service account that impersonates as user. Corresponding
// ClusterRoles, ClusterRoleBindings, and ServiceAccounts will be create.
// IMPORTANT NOTES:
// 1. To ensure this is used securely the namespace assigned to the pod must be a dedicated
// namespace used only for the purpose of running impersonated pods. This is to ensure
// proper protection for the service accounts created.
// 2. The pod must KUBECONFIG env var set to where you expect the kubeconfig to reside
// 1. To ensure this is used securely the namespace assigned to the pod must be a dedicated
// namespace used only for the purpose of running impersonated pods. This is to ensure
// proper protection for the service accounts created.
// 2. The pod must KUBECONFIG env var set to where you expect the kubeconfig to reside
func (s *PodImpersonation) CreatePod(ctx context.Context, user user.Info, pod *v1.Pod, podOptions *PodOptions) (*v1.Pod, error) {
if podOptions == nil {
podOptions = &PodOptions{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/counts/counts.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func removeSummary(counts Summary, summary summary.Summary) Summary {
if counts.States == nil {
counts.States = map[string]int{}
}
counts.States[simpleState(summary)] -= 1
counts.States[simpleState(summary)]--
}
return counts
}
Expand All @@ -297,7 +297,7 @@ func addSummary(counts Summary, summary summary.Summary) Summary {
if counts.States == nil {
counts.States = map[string]int{}
}
counts.States[simpleState(summary)] += 1
counts.States[simpleState(summary)]++
}
return counts
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/converter/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/rancher/steve/pkg/schema/table"
apiextv1 "github.com/rancher/wrangler/pkg/generated/controllers/apiextensions.k8s.io/v1"
"github.com/rancher/wrangler/pkg/schemas"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/converter/k8stonorman.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/wrangler/pkg/generated/controllers/apiextensions.k8s.io/v1"
v1 "github.com/rancher/wrangler/pkg/generated/controllers/apiextensions.k8s.io/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/schema/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (c *Collection) Schemas(user user.Info) (*types.APISchemas, error) {
func (c *Collection) removeOldRecords(access *accesscontrol.AccessSet, user user.Info) {
current, ok := c.userCache.Get(user.GetName())
if ok {
currentId, cOk := current.(string)
if cOk && currentId != access.ID {
currentID, cOk := current.(string)
if cOk && currentID != access.ID {
// we only want to keep around one record per user. If our current access record is invalid, purge the
//record of it from the cache, so we don't keep duplicates
c.purgeUserRecords(currentId)
c.purgeUserRecords(currentID)
c.userCache.Remove(user.GetName())
}
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/server/handler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import (

func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) {
vars := mux.Vars(apiOp.Request)
group := vars["group"]
if group == "core" {
group = ""
}

apiOp.Name = vars["name"]
apiOp.Type = vars["type"]

Expand Down
2 changes: 1 addition & 1 deletion pkg/stores/metrics/metrics_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.
apiEvent, err := s.Store.Watch(apiOp, schema, w)
m.RecordProxyStoreResponseTime(err, float64(time.Since(storeStart).Milliseconds()))
return apiEvent, err
}
}
19 changes: 9 additions & 10 deletions pkg/stores/partition/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,16 @@ func (p *ParallelPartitionLister) feeder(ctx context.Context, state listState, l
}
capacity = 0
return nil
} else {
result <- list.Objects
capacity -= len(list.Objects)
if list.Continue == "" {
return nil
}
// loop again and get more data
state.Continue = list.Continue
state.PartitionName = partition.Name()
state.Offset = 0
}
result <- list.Objects
capacity -= len(list.Objects)
if list.Continue == "" {
return nil
}
// loop again and get more data
state.Continue = list.Continue
state.PartitionName = partition.Name()
state.Offset = 0
}
})
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/stores/proxy/rbac_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,13 @@ func isPassthroughUnconstrained(apiOp *types.APIRequest, schema *types.APISchema
if apiOp.Namespace != "" {
if resources[apiOp.Namespace].All {
return nil, true
} else {
return []partition.Partition{
Partition{
Namespace: apiOp.Namespace,
Names: resources[apiOp.Namespace].Names,
},
}, false
}
return []partition.Partition{
Partition{
Namespace: apiOp.Namespace,
Names: resources[apiOp.Namespace].Names,
},
}, false
}

var result []partition.Partition
Expand Down
12 changes: 12 additions & 0 deletions scripts/validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -e
golangci-lint run
go mod tidy
go mod verify
unclean=$(g status --porcelain --untracked-files=no)
if [ -n "$unclean" ]; then
echo "Encountered dirty repo!";
echo "$unclean";
exit 1;
fi

0 comments on commit 9480a3e

Please sign in to comment.