From defb53b6ade0caa3391c36fd19c03f2346d20983 Mon Sep 17 00:00:00 2001 From: Leonardo Cecchi Date: Tue, 1 Oct 2024 11:44:23 +0200 Subject: [PATCH] chore: makes linter happy Signed-off-by: Leonardo Cecchi --- .golangci.yml | 146 ++++++++++++++++++++------ cmd/instance/main.go | 4 +- cmd/operator/main.go | 1 + internal/cnpgi/instance/backup.go | 4 + internal/cnpgi/instance/constants.go | 2 + internal/cnpgi/instance/doc.go | 2 + internal/cnpgi/instance/identity.go | 4 + internal/cnpgi/instance/start.go | 2 + internal/cnpgi/instance/wal.go | 35 ++++-- internal/cnpgi/instance/wal_import.go | 14 +-- internal/cnpgi/operator/constants.go | 1 + internal/cnpgi/operator/doc.go | 2 + internal/cnpgi/operator/identity.go | 5 + internal/cnpgi/operator/reconciler.go | 4 + internal/cnpgi/operator/start.go | 2 + internal/controller/doc.go | 3 + 16 files changed, 184 insertions(+), 47 deletions(-) create mode 100644 internal/cnpgi/instance/doc.go create mode 100644 internal/cnpgi/operator/doc.go create mode 100644 internal/controller/doc.go diff --git a/.golangci.yml b/.golangci.yml index 3c4e64c..67f4b07 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,43 +8,127 @@ linters-settings: - prefix(github.com/cloudnative-pg/plugin-barman-cloud) - blank - dot - nlreturn: - # Size of the block (including return statement that is still "OK") - # so no return split required. - # Default: 1 - block-size: 3 + gosec: + excludes: + - G101 # remove this exclude when https://github.com/securego/gosec/issues/1001 is fixed + +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + enable: + - asciicheck + - bodyclose + - dogsled + - dupl + - durationcheck + - errcheck + - copyloopvar + - gci + - gocognit + - goconst + - gocritic + - gocyclo + - gofmt + - gofumpt + - goheader + - goimports + - gomoddirectives + - gomodguard + - goprintffuncname + - gosec + - gosimple + - govet + - ginkgolinter + - importas + - ineffassign + - lll + - makezero + - misspell + - nakedret + - nestif + - prealloc + - predeclared + - revive + - rowserrcheck + - sqlclosecheck + - staticcheck + - stylecheck + - thelper + - tparallel + - typecheck + - unconvert + - unparam + - unused + - wastedassign + - whitespace + + # to be checked: + # - errorlint + # - forbidigo + # - forcetypeassert + # - goerr113 + # - ifshort + # - nilerr + # - nlreturn + # - noctx + # - nolintlint + # - paralleltest + # - promlinter + # - tagliatelle + # - wrapcheck + + # don't enable: + # - cyclop + # - depguard + # - exhaustive + # - exhaustivestruct + # - funlen + # - gochecknoglobals + # - gochecknoinits + # - godot + # - godox + # - gomnd + # - testpackage + # - wsl + + # deprecated: + # - deadcode + # - golint + # - interfacer + # - maligned + # - scopelint + # - structcheck + # - varcheck + +run: + timeout: 5m issues: exclude-rules: # Allow dot imports for ginkgo and gomega - source: ginkgo|gomega linters: - - revive + - revive text: "should not use dot imports" + # Exclude some linters from running on tests files. + - path: _test\.go + linters: + - goconst + # Exclude lll issues for lines with long annotations - linters: - - stylecheck - text: "should not use dot imports" + - lll + source: "//\\s*\\+" + # We have no control of this in zz_generated files and it looks like that excluding those files is not enough + # so we disable "ST1016: methods on the same type should have the same receiver name" in api directory + - linters: + - stylecheck + text: "ST1016:" + path: api/ + exclude-use-default: false exclude-dirs: - - vendor - -linters: - enable-all: true - disable: - - depguard - - err113 - - exhaustruct - - execinquery - - exportloopref - - funlen - - gochecknoglobals - - gochecknoinits - - godox - - gomnd - - paralleltest - - testpackage - - varnamelen - - wsl - -run: - timeout: 10m - allow-parallel-runners: true + # This tests are generated by kubebuilder + - test + exclude-files: + - zz_generated.* + - internal/controller/suite_test.go diff --git a/cmd/instance/main.go b/cmd/instance/main.go index 50073e2..4eed69c 100644 --- a/cmd/instance/main.go +++ b/cmd/instance/main.go @@ -1,3 +1,4 @@ +// Package main is the implementation of the CNPG-i PostgreSQL sidecar package main import ( @@ -9,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" - controllerruntime "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" @@ -37,7 +37,7 @@ func main() { clusterName := mustGetEnv("CLUSTER_NAME") instanceName := mustGetEnv("INSTANCE_NAME") - mgr, err := controllerruntime.NewManager(controllerruntime.GetConfigOrDie(), controllerruntime.Options{ + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, Cache: cache.Options{ ByObject: map[client.Object]cache.ByObject{ diff --git a/cmd/operator/main.go b/cmd/operator/main.go index a8cb483..202cd16 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package main contains the implementation of the CNPG-i operator plugin package main import ( diff --git a/internal/cnpgi/instance/backup.go b/internal/cnpgi/instance/backup.go index 749addc..3521697 100644 --- a/internal/cnpgi/instance/backup.go +++ b/internal/cnpgi/instance/backup.go @@ -6,10 +6,13 @@ import ( "github.com/cloudnative-pg/cnpg-i/pkg/backup" ) +// BackupServiceImplementation is the implementation +// of the Backup CNPG capability type BackupServiceImplementation struct { backup.UnimplementedBackupServer } +// GetCapabilities implements the BackupService interface func (b BackupServiceImplementation) GetCapabilities( _ context.Context, _ *backup.BackupCapabilitiesRequest, ) (*backup.BackupCapabilitiesResult, error) { @@ -26,6 +29,7 @@ func (b BackupServiceImplementation) GetCapabilities( }, nil } +// Backup implements the Backup interface func (b BackupServiceImplementation) Backup(_ context.Context, _ *backup.BackupRequest) (*backup.BackupResult, error) { // TODO implement me panic("implement me") diff --git a/internal/cnpgi/instance/constants.go b/internal/cnpgi/instance/constants.go index 9ab4db9..811a698 100644 --- a/internal/cnpgi/instance/constants.go +++ b/internal/cnpgi/instance/constants.go @@ -2,6 +2,8 @@ package instance import "github.com/cloudnative-pg/cnpg-i/pkg/identity" +// PluginName is the name of the plugin from the instance manager +// Point-of-view const PluginName = "instance.barman-cloud.cloudnative-pg.io" // Data is the metadata of this plugin. diff --git a/internal/cnpgi/instance/doc.go b/internal/cnpgi/instance/doc.go new file mode 100644 index 0000000..7eee0d7 --- /dev/null +++ b/internal/cnpgi/instance/doc.go @@ -0,0 +1,2 @@ +// Package instance implements the capabilities used by the operator sidecar +package instance diff --git a/internal/cnpgi/instance/identity.go b/internal/cnpgi/instance/identity.go index 85e2f0a..69ede5e 100644 --- a/internal/cnpgi/instance/identity.go +++ b/internal/cnpgi/instance/identity.go @@ -10,12 +10,14 @@ import ( barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" ) +// IdentityImplementation implements IdentityServer type IdentityImplementation struct { identity.UnimplementedIdentityServer BarmanObjectKey client.ObjectKey Client client.Client } +// GetPluginMetadata implements IdentityServer func (i IdentityImplementation) GetPluginMetadata( _ context.Context, _ *identity.GetPluginMetadataRequest, @@ -23,6 +25,7 @@ func (i IdentityImplementation) GetPluginMetadata( return &Data, nil } +// GetPluginCapabilities implements IdentityServer func (i IdentityImplementation) GetPluginCapabilities( _ context.Context, _ *identity.GetPluginCapabilitiesRequest, @@ -47,6 +50,7 @@ func (i IdentityImplementation) GetPluginCapabilities( }, nil } +// Probe implements IdentityServer func (i IdentityImplementation) Probe( ctx context.Context, _ *identity.ProbeRequest, diff --git a/internal/cnpgi/instance/start.go b/internal/cnpgi/instance/start.go index 1501ee2..f2f549f 100644 --- a/internal/cnpgi/instance/start.go +++ b/internal/cnpgi/instance/start.go @@ -10,6 +10,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +// CNPGI is the implementation of the PostgreSQL sidecar type CNPGI struct { Client client.Client BarmanObjectKey client.ObjectKey @@ -27,6 +28,7 @@ type CNPGI struct { InstanceName string } +// Start starts the GRPC service func (c *CNPGI) Start(ctx context.Context) error { enrich := func(server *grpc.Server) error { wal.RegisterWALServer(server, WALServiceImplementation{ diff --git a/internal/cnpgi/instance/wal.go b/internal/cnpgi/instance/wal.go index 764d370..fec6dbd 100644 --- a/internal/cnpgi/instance/wal.go +++ b/internal/cnpgi/instance/wal.go @@ -22,6 +22,7 @@ import ( barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" ) +// WALServiceImplementation is the implementation of the WAL Service type WALServiceImplementation struct { BarmanObjectKey client.ObjectKey ClusterObjectKey client.ObjectKey @@ -33,7 +34,11 @@ type WALServiceImplementation struct { wal.UnimplementedWALServer } -func (w WALServiceImplementation) GetCapabilities(ctx context.Context, request *wal.WALCapabilitiesRequest) (*wal.WALCapabilitiesResult, error) { +// GetCapabilities implements the WALService interface +func (w WALServiceImplementation) GetCapabilities( + _ context.Context, + _ *wal.WALCapabilitiesRequest, +) (*wal.WALCapabilitiesResult, error) { return &wal.WALCapabilitiesResult{ Capabilities: []*wal.WALCapability{ { @@ -54,7 +59,11 @@ func (w WALServiceImplementation) GetCapabilities(ctx context.Context, request * }, nil } -func (w WALServiceImplementation) Archive(ctx context.Context, request *wal.WALArchiveRequest) (*wal.WALArchiveResult, error) { +// Archive implements the WALService interface +func (w WALServiceImplementation) Archive( + ctx context.Context, + request *wal.WALArchiveRequest, +) (*wal.WALArchiveResult, error) { var objectStore barmancloudv1.ObjectStore if err := w.Client.Get(ctx, w.BarmanObjectKey, &objectStore); err != nil { return nil, err @@ -90,7 +99,11 @@ func (w WALServiceImplementation) Archive(ctx context.Context, request *wal.WALA return &wal.WALArchiveResult{}, nil } -func (w WALServiceImplementation) Restore(ctx context.Context, request *wal.WALRestoreRequest) (*wal.WALRestoreResult, error) { +// Restore implements the WALService interface +func (w WALServiceImplementation) Restore( + ctx context.Context, + request *wal.WALRestoreRequest, +) (*wal.WALRestoreResult, error) { contextLogger := log.FromContext(ctx) startTime := time.Now() @@ -181,7 +194,7 @@ func (w WALServiceImplementation) Restore(ctx context.Context, request *wal.WALR return nil, walStatus[0].Err } - //We skip this step if streaming connection is not available + // We skip this step if streaming connection is not available endOfWALStream := isEndOfWALStream(walStatus) if isStreamingAvailable(cluster, w.InstanceName) && endOfWALStream { contextLogger.Info( @@ -213,12 +226,20 @@ func (w WALServiceImplementation) Restore(ctx context.Context, request *wal.WALR return &wal.WALRestoreResult{}, nil } -func (w WALServiceImplementation) Status(ctx context.Context, request *wal.WALStatusRequest) (*wal.WALStatusResult, error) { +// Status implements the WALService interface +func (w WALServiceImplementation) Status( + _ context.Context, + _ *wal.WALStatusRequest, +) (*wal.WALStatusResult, error) { // TODO implement me panic("implement me") } -func (w WALServiceImplementation) SetFirstRequired(ctx context.Context, request *wal.SetFirstRequiredRequest) (*wal.SetFirstRequiredResult, error) { +// SetFirstRequired implements the WALService interface +func (w WALServiceImplementation) SetFirstRequired( + _ context.Context, + _ *wal.SetFirstRequiredRequest, +) (*wal.SetFirstRequiredResult, error) { // TODO implement me panic("implement me") } @@ -325,7 +346,7 @@ func gatherWALFilesToRestore(walName string, parallel int) (walList []string, er return walList, err } -// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive +// ErrEndOfWALStreamReached is returned when end of WAL is detected in the cloud archive. var ErrEndOfWALStreamReached = errors.New("end of WAL reached") // checkEndOfWALStreamFlag returns ErrEndOfWALStreamReached if the flag is set in the restorer. diff --git a/internal/cnpgi/instance/wal_import.go b/internal/cnpgi/instance/wal_import.go index c10ad4e..a2005dd 100644 --- a/internal/cnpgi/instance/wal_import.go +++ b/internal/cnpgi/instance/wal_import.go @@ -75,8 +75,8 @@ var ( WALSegmentNameRe + `$`) - // ErrorBadWALSegmentName is raised when parsing an invalid segment name. - ErrorBadWALSegmentName = errors.New("invalid WAL segment name") + // ErrBadWALSegmentName is raised when parsing an invalid segment name. + ErrBadWALSegmentName = errors.New("invalid WAL segment name") ) // Segment contains the information inside a WAL segment name. @@ -109,23 +109,23 @@ func SegmentFromName(name string) (Segment, error) { // We could have used WALSegmentRe directly, but we wanted to adhere to barman code subMatches := WALRe.FindStringSubmatch(baseName) if len(subMatches) != 4 { - return Segment{}, ErrorBadWALSegmentName + return Segment{}, ErrBadWALSegmentName } if len(subMatches[0]) != 24 { - return Segment{}, ErrorBadWALSegmentName + return Segment{}, ErrBadWALSegmentName } if tli, err = strconv.ParseInt(subMatches[1], 16, 32); err != nil { - return Segment{}, ErrorBadWALSegmentName + return Segment{}, ErrBadWALSegmentName } if log, err = strconv.ParseInt(subMatches[2], 16, 32); err != nil { - return Segment{}, ErrorBadWALSegmentName + return Segment{}, ErrBadWALSegmentName } if seg, err = strconv.ParseInt(subMatches[3], 16, 32); err != nil { - return Segment{}, ErrorBadWALSegmentName + return Segment{}, ErrBadWALSegmentName } return Segment{ diff --git a/internal/cnpgi/operator/constants.go b/internal/cnpgi/operator/constants.go index f162ec5..08f1e0f 100644 --- a/internal/cnpgi/operator/constants.go +++ b/internal/cnpgi/operator/constants.go @@ -2,6 +2,7 @@ package operator import "github.com/cloudnative-pg/cnpg-i/pkg/identity" +// PluginName is the name of this plugin const PluginName = "operator.barman-cloud.cloudnative-pg.io" // Data is the metadata of this plugin. diff --git a/internal/cnpgi/operator/doc.go b/internal/cnpgi/operator/doc.go new file mode 100644 index 0000000..21a4635 --- /dev/null +++ b/internal/cnpgi/operator/doc.go @@ -0,0 +1,2 @@ +// Package operator implements the capabilities used by CNPG +package operator diff --git a/internal/cnpgi/operator/identity.go b/internal/cnpgi/operator/identity.go index 7ef5ea7..040a2ee 100644 --- a/internal/cnpgi/operator/identity.go +++ b/internal/cnpgi/operator/identity.go @@ -6,10 +6,13 @@ import ( "github.com/cloudnative-pg/cnpg-i/pkg/identity" ) +// IdentityImplementation is the implementation of the CNPG-i +// Identity entrypoint type IdentityImplementation struct { identity.UnimplementedIdentityServer } +// GetPluginMetadata implements Identity func (i IdentityImplementation) GetPluginMetadata( _ context.Context, _ *identity.GetPluginMetadataRequest, @@ -17,6 +20,7 @@ func (i IdentityImplementation) GetPluginMetadata( return &Data, nil } +// GetPluginCapabilities implements identity func (i IdentityImplementation) GetPluginCapabilities( _ context.Context, _ *identity.GetPluginCapabilitiesRequest, @@ -41,6 +45,7 @@ func (i IdentityImplementation) GetPluginCapabilities( }, nil } +// Probe implements Identity func (i IdentityImplementation) Probe( _ context.Context, _ *identity.ProbeRequest, diff --git a/internal/cnpgi/operator/reconciler.go b/internal/cnpgi/operator/reconciler.go index 2092984..b0e3063 100644 --- a/internal/cnpgi/operator/reconciler.go +++ b/internal/cnpgi/operator/reconciler.go @@ -6,10 +6,12 @@ import ( "github.com/cloudnative-pg/cnpg-i/pkg/reconciler" ) +// ReconcilerImplementation implements the Reconciler capability type ReconcilerImplementation struct { reconciler.UnimplementedReconcilerHooksServer } +// GetCapabilities implements the Reconciler interface func (r ReconcilerImplementation) GetCapabilities( _ context.Context, _ *reconciler.ReconcilerHooksCapabilitiesRequest, @@ -26,6 +28,7 @@ func (r ReconcilerImplementation) GetCapabilities( }, nil } +// Pre implements the reconciler interface func (r ReconcilerImplementation) Pre( _ context.Context, _ *reconciler.ReconcilerHooksRequest, @@ -35,6 +38,7 @@ func (r ReconcilerImplementation) Pre( }, nil } +// Post implements the reconciler interface func (r ReconcilerImplementation) Post( _ context.Context, _ *reconciler.ReconcilerHooksRequest, diff --git a/internal/cnpgi/operator/start.go b/internal/cnpgi/operator/start.go index 7cb00b1..af745f9 100644 --- a/internal/cnpgi/operator/start.go +++ b/internal/cnpgi/operator/start.go @@ -9,8 +9,10 @@ import ( "google.golang.org/grpc" ) +// CNPGI is the implementation of the Operator plugin type CNPGI struct{} +// Start starts the GRPC server func (c *CNPGI) Start(ctx context.Context) error { cmd := http.CreateMainCmd(IdentityImplementation{}, func(server *grpc.Server) error { // Register the declared implementations diff --git a/internal/controller/doc.go b/internal/controller/doc.go new file mode 100644 index 0000000..5f85d6a --- /dev/null +++ b/internal/controller/doc.go @@ -0,0 +1,3 @@ +// Package controller implements a controller for the CRDs as defined +// by this operator +package controller