Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fieldalignment linter, then mostly ignore it #2795

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ output:
linters:
enable:
- bodyclose
- deadcode
zmerlynn marked this conversation as resolved.
Show resolved Hide resolved
- dupl
- exportloopref
- goconst
Expand All @@ -61,11 +60,16 @@ linters:
- nakedret
- revive
- staticcheck
- structcheck
- unconvert
- unparam

linters-settings:
govet:
disable-all: false
enable-all: true
disable:
# extremely noisy, also against common Go style in most cases
- shadow
gocritic:
enabled-tags:
- performance
Expand All @@ -90,3 +94,11 @@ issues:
exclude-use-default: false
exclude:
- Using the variable on range scope .* in function literal
exclude-rules:
# Skip fieldalignment checks on:
# - tests: memory footprint doesn't matter
# - cmd/: we assume these are one-off config entries
# - pkg/apis: Keep strong convention on TypeMeta/ObjectMeta/Spec/Status
- path: '(.+)_test\.go|^test/|^cmd/.*|^pkg/apis/.*'
# fieldalignment is in the `govet` linter, so exclude based on text instead of all of govet
text: '^fieldalignment: .*'
2 changes: 2 additions & 0 deletions pkg/cloudproduct/gke/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var logger = runtime.NewLoggerWithSource("gke")
type gkeAutopilot struct{}

// hostPortAssignment is the JSON structure of the `host-port-assignment` annotation
//
//nolint:govet // API-like, keep consistent
type hostPortAssignment struct {
Min int32 `json:"min,omitempty"`
Max int32 `json:"max,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/fleetautoscalers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ import (
)

// fasThread is used for tracking each Fleet's autoscaling jobs
//
//nolint:govet // ignore fieldalignment, one per fleet autoscaler
type fasThread struct {
generation int64
cancel context.CancelFunc
}

// Controller is the FleetAutoscaler controller
//
//nolint:govet // ignore fieldalignment, singleton
type Controller struct {
baseLogger *logrus.Entry
clock clock.Clock
Expand Down
2 changes: 1 addition & 1 deletion pkg/gameserverallocations/allocation_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func readyOrAllocatedGameServerMatcher(gs *agonesv1.GameServer) bool {
// AllocationCache maintains a cache of GameServers that could potentially be allocated.
type AllocationCache struct {
baseLogger *logrus.Entry
cache gameServerCacheEntry
cache gameServerCache
gameServerLister listerv1.GameServerLister
gameServerSynced cache.InformerSynced
workerqueue *workerqueue.WorkerQueue
Expand Down
14 changes: 8 additions & 6 deletions pkg/gameserverallocations/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
)

// gameserver cache to keep the Ready state gameserver.
type gameServerCacheEntry struct {
//
//nolint:govet // ignore fieldalignment, singleton embedded in AllocationCache
type gameServerCache struct {
mu sync.RWMutex
cache map[string]*agonesv1.GameServer
}

// Store saves the data in the cache.
func (e *gameServerCacheEntry) Store(key string, gs *agonesv1.GameServer) {
func (e *gameServerCache) Store(key string, gs *agonesv1.GameServer) {
e.mu.Lock()
defer e.mu.Unlock()
if e.cache == nil {
Expand All @@ -37,7 +39,7 @@ func (e *gameServerCacheEntry) Store(key string, gs *agonesv1.GameServer) {
}

// Delete deletes the data. If it exists returns true.
func (e *gameServerCacheEntry) Delete(key string) bool {
func (e *gameServerCache) Delete(key string) bool {
e.mu.Lock()
defer e.mu.Unlock()
ret := false
Expand All @@ -52,7 +54,7 @@ func (e *gameServerCacheEntry) Delete(key string) bool {
}

// Load returns the data from cache. It return true if the value exists in the cache
func (e *gameServerCacheEntry) Load(key string) (*agonesv1.GameServer, bool) {
func (e *gameServerCache) Load(key string) (*agonesv1.GameServer, bool) {
e.mu.RLock()
defer e.mu.RUnlock()
val, ok := e.cache[key]
Expand All @@ -61,7 +63,7 @@ func (e *gameServerCacheEntry) Load(key string) (*agonesv1.GameServer, bool) {
}

// Range extracts data from the cache based on provided function f.
func (e *gameServerCacheEntry) Range(f func(key string, gs *agonesv1.GameServer) bool) {
func (e *gameServerCache) Range(f func(key string, gs *agonesv1.GameServer) bool) {
e.mu.RLock()
defer e.mu.RUnlock()
for k, v := range e.cache {
Expand All @@ -72,7 +74,7 @@ func (e *gameServerCacheEntry) Range(f func(key string, gs *agonesv1.GameServer)
}

// Len returns the current length of the cache
func (e *gameServerCacheEntry) Len() int {
func (e *gameServerCache) Len() int {
e.mu.RLock()
defer e.mu.RUnlock()
return len(e.cache)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gameserverallocations/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGameServerCacheEntry(t *testing.T) {
gs2 := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs2"}}
gs3 := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs3"}}

cache := gameServerCacheEntry{}
cache := gameServerCache{}

gs, ok := cache.Load("gs1")
assert.Nil(t, gs)
Expand Down
2 changes: 2 additions & 0 deletions pkg/gameservers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const (
)

// Controller is a the main GameServer crd controller
//
//nolint:govet // ignore fieldalignment, singleton
type Controller struct {
baseLogger *logrus.Entry
sidecarImage string
Expand Down
2 changes: 2 additions & 0 deletions pkg/gameservers/pernodecounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
// Ready GameServers currently exist on each node.
// This is useful for scheduling allocations, fleet management
// mostly under a Packed strategy
//
//nolint:govet // ignore fieldalignment, singleton
type PerNodeCounter struct {
logger *logrus.Entry
gameServerSynced cache.InformerSynced
Expand Down
2 changes: 2 additions & 0 deletions pkg/gameservers/portallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type portAllocation map[int32]bool
// appropriate locking is taken.
// The PortAllocator does not currently support mixing static portAllocations (or any pods with defined HostPort)
// within the dynamic port range other than the ones it coordinates.
//
//nolint:govet // ignore fieldalignment, singleton
type PortAllocator struct {
logger *logrus.Entry
mutex sync.RWMutex
Expand Down
2 changes: 1 addition & 1 deletion pkg/gameserversets/gameserver_state_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

// gameServerSetCacheEntry manages a list of items created and deleted locally for a single game server set.
type gameServerSetCacheEntry struct {
mu sync.Mutex
pendingCreation map[string]*agonesv1.GameServer
pendingDeletion map[string]*agonesv1.GameServer
mu sync.Mutex
}

func (e *gameServerSetCacheEntry) created(gs *agonesv1.GameServer) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/metrics/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func init() {
}

// Controller is a metrics controller collecting Agones state metrics
//
//nolint:govet // ignore fieldalignment, singleton
type Controller struct {
logger *logrus.Entry
gameServerLister listerv1.GameServerLister
Expand Down
2 changes: 2 additions & 0 deletions pkg/sdkserver/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func defaultGs() *sdk.GameServer {
// LocalSDKServer type is the SDKServer implementation for when the sidecar
// is being run for local development, and doesn't connect to the
// Kubernetes cluster
//
//nolint:govet // ignore fieldalignment, singleton
type LocalSDKServer struct {
gsMutex sync.RWMutex
gs *sdk.GameServer
Expand Down
1 change: 0 additions & 1 deletion pkg/sdkserver/localsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ func TestLocalSDKServerPlayerConnectAndDisconnect(t *testing.T) {

e := &alpha.Empty{}

// nolint: maligned
fixtures := map[string]struct {
testMode bool
gs *agonesv1.GameServer
Expand Down
3 changes: 2 additions & 1 deletion pkg/sdkserver/sdkserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ var (

// SDKServer is a gRPC server, that is meant to be a sidecar
// for a GameServer that will update the game server status on SDK requests
// nolint: maligned
//
//nolint:govet // ignore fieldalignment, singleton
type SDKServer struct {
logger *logrus.Entry
gameServerName string
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/workerqueue/workerqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type Handler func(context.Context, string) error
// WorkerQueue is an opinionated queue + worker for use
// with controllers and related and processing Kubernetes watched
// events and synchronising resources
//
//nolint:govet // ignore fieldalignment, singleton
type WorkerQueue struct {
logger *logrus.Entry
keyName string
Expand Down