From 7217046ef57f65173dd80657101b03f301ccb6bb Mon Sep 17 00:00:00 2001 From: Sergey Dudoladov Date: Wed, 2 Oct 2019 13:36:54 +0200 Subject: [PATCH] update linting --- Makefile | 14 +++++----- kubectl-pg/README.md | 4 +-- kubectl-pg/cmd/addDb.go | 5 ++-- kubectl-pg/cmd/create.go | 8 ++++-- kubectl-pg/cmd/extVolume.go | 9 ++++--- kubectl-pg/cmd/scale.go | 8 ++++-- kubectl-pg/cmd/update.go | 8 ++++-- pkg/apis/acid.zalan.do/v1/crds.go | 26 +++++++++---------- pkg/apis/acid.zalan.do/v1/marshal.go | 2 +- .../v1/operator_configuration_type.go | 2 +- pkg/cluster/cluster.go | 4 +-- pkg/cluster/k8sres.go | 5 ++-- pkg/controller/controller.go | 6 ++--- pkg/util/teams/teams_test.go | 1 + 14 files changed, 60 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index 89ecf853d..19a33242f 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ GITSTATUS = $(shell git status --porcelain || echo "no changes") SOURCES = cmd/main.go VERSION ?= $(shell git describe --tags --always --dirty) DIRS := cmd pkg -PKG := `go list ./... | grep -v /vendor/` +PKG := `go list ./... | grep --invert-match /vendor/ | grep --invert-match /kubectl-pg` ifeq ($(DEBUG),1) DOCKERFILE = DebugDockerfile @@ -46,7 +46,7 @@ default: local clean: rm -rf build scm-source.json -local: ${SOURCES} +local: test ${SOURCES} hack/verify-codegen.sh CGO_ENABLED=${CGO_ENABLED} go build -o build/${BINARY} $(LOCAL_BUILD_FLAGS) -ldflags "$(LDFLAGS)" $^ @@ -60,7 +60,7 @@ docker-context: scm-source.json linux mkdir -p docker/build/ cp build/linux/${BINARY} scm-source.json docker/build/ -docker: ${DOCKERDIR}/${DOCKERFILE} docker-context +docker: test ${DOCKERDIR}/${DOCKERFILE} docker-context echo `(env)` echo "Tag ${TAG}" echo "Version ${VERSION}" @@ -78,22 +78,24 @@ scm-source.json: .git echo '{\n "url": "git:$(GITURL)",\n "revision": "$(GITHEAD)",\n "author": "$(USER)",\n "status": "$(GITSTATUS)"\n}' > scm-source.json tools: + @go get -u github.com/gordonklaus/ineffassign @go get -u honnef.co/go/tools/cmd/staticcheck @go get -u github.com/Masterminds/glide fmt: @gofmt -l -w -s $(DIRS) -vet: +vet: fmt @go vet $(PKG) + @ineffassign kubectl-pg pkg @staticcheck $(PKG) deps: @glide install --strip-vendor -test: +test: vet hack/verify-codegen.sh - @go test ./... + @go test $(PKG) e2e: docker # build operator image to be tested cd e2e; make tools test clean diff --git a/kubectl-pg/README.md b/kubectl-pg/README.md index c6d4c38ee..e7b2d1700 100644 --- a/kubectl-pg/README.md +++ b/kubectl-pg/README.md @@ -33,7 +33,7 @@ $ $GOPATH/src/github.com/zalando/postgres-operator/kubectl-pg go install # This will place the kubectl-pg binary in your $GOPATH/bin ``` -### Before using the kubectl pg plugin make sure to set KUBECONFIG env varibale +### Before using the kubectl pg plugin make sure to set KUBECONFIG env variable Ideally KUBECONFIG is found in $HOME/.kube/config else specify the KUBECONFIG path here. @@ -117,7 +117,7 @@ Note: -p represents psql prompt ```kubectl pg logs -c CLUSTER``` #Fetches the logs of a random pod. Not master ```kubectl pg logs -c CLUSTER -m``` #Fetches the logs of master -```kubectl pg logs -c CLUSTER -r 2``` #Fecthes the logs of specified replica +```kubectl pg logs -c CLUSTER -r 2``` #Fetches the logs of specified replica ## Development diff --git a/kubectl-pg/cmd/addDb.go b/kubectl-pg/cmd/addDb.go index 1da426703..e67f23438 100644 --- a/kubectl-pg/cmd/addDb.go +++ b/kubectl-pg/cmd/addDb.go @@ -25,11 +25,12 @@ package cmd import ( "encoding/json" "fmt" + "log" + "github.com/spf13/cobra" PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "log" ) // addDbCmd represents the addDb command @@ -69,7 +70,7 @@ func addDb(dbName string, dbOwner string, clusterName string) { var dbOwnerExists bool dbUsers := postgresql.Spec.Users - for key, _ := range dbUsers { + for key := range dbUsers { if key == dbOwner { dbOwnerExists = true } diff --git a/kubectl-pg/cmd/create.go b/kubectl-pg/cmd/create.go index 2ed7b50c0..dd7060892 100644 --- a/kubectl-pg/cmd/create.go +++ b/kubectl-pg/cmd/create.go @@ -24,12 +24,13 @@ package cmd import ( "fmt" + "io/ioutil" + "log" + "github.com/spf13/cobra" v1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1" - "io/ioutil" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme" - "log" ) // createCmd kubectl pg create. @@ -50,6 +51,9 @@ kubectl pg create -f cluster-manifest.yaml func create(fileName string) { config := getConfig() postgresConfig, err := PostgresqlLister.NewForConfig(config) + if err != nil { + log.Fatal(err) + } ymlFile, err := ioutil.ReadFile(fileName) if err != nil { log.Fatal(err) diff --git a/kubectl-pg/cmd/extVolume.go b/kubectl-pg/cmd/extVolume.go index 351876c68..b9e857548 100644 --- a/kubectl-pg/cmd/extVolume.go +++ b/kubectl-pg/cmd/extVolume.go @@ -25,20 +25,21 @@ package cmd import ( "encoding/json" "fmt" + "log" + "strconv" + "github.com/spf13/cobra" PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "log" - "strconv" ) // extVolumeCmd represents the extVolume command var extVolumeCmd = &cobra.Command{ Use: "ext-volume", Short: "Increases the volume size of a given Postgres cluster", - Long: `Extends the volume of the postgres cluster. But volume cannot be shrinked.`, + Long: `Extends the volume of the postgres cluster. But volume cannot be shrunk.`, Run: func(cmd *cobra.Command, args []string) { clusterName, _ := cmd.Flags().GetString("cluster") if len(args) > 0 { @@ -98,7 +99,7 @@ func extVolume(increasedVolumeSize string, clusterName string) { } else if newSize.Value() == oldSize.Value() { fmt.Println("volume already has the desired size.") } else { - fmt.Printf("volume %s size cannot be shrinked.\n", postgresql.Spec.Volume.Size) + fmt.Printf("volume %s size cannot be shrunk.\n", postgresql.Spec.Volume.Size) } } diff --git a/kubectl-pg/cmd/scale.go b/kubectl-pg/cmd/scale.go index d273bbd6f..7122d09f5 100644 --- a/kubectl-pg/cmd/scale.go +++ b/kubectl-pg/cmd/scale.go @@ -25,6 +25,9 @@ package cmd import ( "encoding/json" "fmt" + "log" + "strconv" + "github.com/spf13/cobra" PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1" v1 "k8s.io/api/apps/v1" @@ -32,8 +35,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" - "log" - "strconv" ) // scaleCmd represents the scale command @@ -44,6 +45,9 @@ var scaleCmd = &cobra.Command{ Scaling to 0 leads to down time.`, Run: func(cmd *cobra.Command, args []string) { clusterName, err := cmd.Flags().GetString("cluster") + if err != nil { + log.Fatal(err) + } namespace, err := cmd.Flags().GetString("namespace") if err != nil { log.Fatal(err) diff --git a/kubectl-pg/cmd/update.go b/kubectl-pg/cmd/update.go index 6efdb8b8c..d812bbff0 100644 --- a/kubectl-pg/cmd/update.go +++ b/kubectl-pg/cmd/update.go @@ -24,13 +24,14 @@ package cmd import ( "fmt" + "io/ioutil" + "log" + "github.com/spf13/cobra" v1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" PostgresqlLister "github.com/zalando/postgres-operator/pkg/generated/clientset/versioned/typed/acid.zalan.do/v1" - "io/ioutil" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "log" ) // updateCmd represents kubectl pg update @@ -55,6 +56,9 @@ kubectl pg update -f cluster-manifest.yaml func updatePgResources(fileName string) { config := getConfig() postgresConfig, err := PostgresqlLister.NewForConfig(config) + if err != nil { + log.Fatal(err) + } ymlFile, err := ioutil.ReadFile(fileName) if err != nil { log.Fatal(err) diff --git a/pkg/apis/acid.zalan.do/v1/crds.go b/pkg/apis/acid.zalan.do/v1/crds.go index 50833db26..fe02866f8 100644 --- a/pkg/apis/acid.zalan.do/v1/crds.go +++ b/pkg/apis/acid.zalan.do/v1/crds.go @@ -21,48 +21,48 @@ const ( // PostgresCRDResourceColumns definition of AdditionalPrinterColumns for postgresql CRD var PostgresCRDResourceColumns = []apiextv1beta1.CustomResourceColumnDefinition{ - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Team", Type: "string", Description: "Team responsible for Postgres cluster", JSONPath: ".spec.teamId", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Version", Type: "string", Description: "PostgreSQL version", JSONPath: ".spec.postgresql.version", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Pods", Type: "integer", Description: "Number of Pods per Postgres cluster", JSONPath: ".spec.numberOfInstances", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Volume", Type: "string", Description: "Size of the bound volume", JSONPath: ".spec.volume.size", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "CPU-Request", Type: "string", Description: "Requested CPU for Postgres containers", JSONPath: ".spec.resources.requests.cpu", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Memory-Request", Type: "string", Description: "Requested memory for Postgres containers", JSONPath: ".spec.resources.requests.memory", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Age", Type: "date", JSONPath: ".metadata.creationTimestamp", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Status", Type: "string", Description: "Current sync status of postgresql resource", @@ -72,31 +72,31 @@ var PostgresCRDResourceColumns = []apiextv1beta1.CustomResourceColumnDefinition{ // OperatorConfigCRDResourceColumns definition of AdditionalPrinterColumns for OperatorConfiguration CRD var OperatorConfigCRDResourceColumns = []apiextv1beta1.CustomResourceColumnDefinition{ - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Image", Type: "string", Description: "Spilo image to be used for Pods", JSONPath: ".configuration.docker_image", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Cluster-Label", Type: "string", Description: "Label for K8s resources created by operator", JSONPath: ".configuration.kubernetes.cluster_name_label", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Service-Account", Type: "string", Description: "Name of service account to be used", JSONPath: ".configuration.kubernetes.pod_service_account_name", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Min-Instances", Type: "integer", Description: "Minimum number of instances per Postgres cluster", JSONPath: ".configuration.min_instances", }, - apiextv1beta1.CustomResourceColumnDefinition{ + { Name: "Age", Type: "date", JSONPath: ".metadata.creationTimestamp", diff --git a/pkg/apis/acid.zalan.do/v1/marshal.go b/pkg/apis/acid.zalan.do/v1/marshal.go index d180f784c..bf23c05ea 100644 --- a/pkg/apis/acid.zalan.do/v1/marshal.go +++ b/pkg/apis/acid.zalan.do/v1/marshal.go @@ -81,7 +81,7 @@ func (ps *PostgresStatus) UnmarshalJSON(data []byte) error { if err != nil { metaErr := json.Unmarshal(data, &status) if metaErr != nil { - return fmt.Errorf("Could not parse status: %v; err %v", string(data), metaErr) + return fmt.Errorf("could not parse status: %v; err %v", string(data), metaErr) } tmp.PostgresClusterStatus = status } diff --git a/pkg/apis/acid.zalan.do/v1/operator_configuration_type.go b/pkg/apis/acid.zalan.do/v1/operator_configuration_type.go index 6f1c8d59c..02648b83b 100644 --- a/pkg/apis/acid.zalan.do/v1/operator_configuration_type.go +++ b/pkg/apis/acid.zalan.do/v1/operator_configuration_type.go @@ -45,7 +45,7 @@ type KubernetesMetaConfiguration struct { PodServiceAccountDefinition string `json:"pod_service_account_definition,omitempty"` PodServiceAccountRoleBindingDefinition string `json:"pod_service_account_role_binding_definition,omitempty"` PodTerminateGracePeriod Duration `json:"pod_terminate_grace_period,omitempty"` - SpiloPrivileged bool `json:"spilo_privileged,omitemty"` + SpiloPrivileged bool `json:"spilo_privileged,omitempty"` SpiloFSGroup *int64 `json:"spilo_fsgroup,omitempty"` WatchedNamespace string `json:"watched_namespace,omitempty"` PDBNameFormat config.StringTemplate `json:"pdb_name_format,omitempty"` diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 1f8fe203f..c771a74e1 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -842,7 +842,7 @@ func (c *Cluster) initHumanUsers() error { for _, postgresSuperuserTeam := range c.OpConfig.PostgresSuperuserTeams { err := c.initTeamMembers(postgresSuperuserTeam, true) if err != nil { - return fmt.Errorf("Cannot create a team %q of Postgres superusers: %v", postgresSuperuserTeam, err) + return fmt.Errorf("cannot create a team %q of Postgres superusers: %v", postgresSuperuserTeam, err) } if postgresSuperuserTeam == c.Spec.TeamID { clusterIsOwnedBySuperuserTeam = true @@ -856,7 +856,7 @@ func (c *Cluster) initHumanUsers() error { err := c.initTeamMembers(c.Spec.TeamID, false) if err != nil { - return fmt.Errorf("Cannot create a team %q of admins owning the PG cluster: %v", c.Spec.TeamID, err) + return fmt.Errorf("cannot create a team %q of admins owning the PG cluster: %v", c.Spec.TeamID, err) } return nil diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index 78d128387..dda0336d8 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -174,6 +174,7 @@ PatroniInitDBParams: for _, k := range initdbOptionNames { v := patroni.InitDB[k] for i, defaultParam := range config.Bootstrap.Initdb { + //lint:ignore S1034 looks like false positive: defaultParam.(type) cannot be used outside of type switch switch defaultParam.(type) { case map[string]string: { @@ -1308,11 +1309,11 @@ func (c *Cluster) generateCloneEnvironment(description *acidv1.CloneDescription) c.logger.Info(msg, description.S3WalPath) envs := []v1.EnvVar{ - v1.EnvVar{ + { Name: "CLONE_WAL_S3_BUCKET", Value: c.OpConfig.WALES3Bucket, }, - v1.EnvVar{ + { Name: "CLONE_WAL_BUCKET_SCOPE_SUFFIX", Value: getBucketScopeSuffix(description.UID), }, diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index a492a85e2..593b43aaa 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -6,7 +6,7 @@ import ( "sync" "github.com/sirupsen/logrus" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" rbacv1beta1 "k8s.io/api/rbac/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -176,7 +176,7 @@ func (c *Controller) initPodServiceAccount() { switch { case err != nil: - panic(fmt.Errorf("Unable to parse pod service account definition from the operator config map: %v", err)) + panic(fmt.Errorf("unable to parse pod service account definition from the operator config map: %v", err)) case groupVersionKind.Kind != "ServiceAccount": panic(fmt.Errorf("pod service account definition in the operator config map defines another type of resource: %v", groupVersionKind.Kind)) default: @@ -224,7 +224,7 @@ func (c *Controller) initRoleBinding() { switch { case err != nil: - panic(fmt.Errorf("Unable to parse the definition of the role binding for the pod service account definition from the operator config map: %v", err)) + panic(fmt.Errorf("unable to parse the definition of the role binding for the pod service account definition from the operator config map: %v", err)) case groupVersionKind.Kind != "RoleBinding": panic(fmt.Errorf("role binding definition in the operator config map defines another type of resource: %v", groupVersionKind.Kind)) default: diff --git a/pkg/util/teams/teams_test.go b/pkg/util/teams/teams_test.go index 637c4e16c..401a4068c 100644 --- a/pkg/util/teams/teams_test.go +++ b/pkg/util/teams/teams_test.go @@ -133,6 +133,7 @@ var requestsURLtc = []struct { }{ { "coffee://localhost/", + //lint:ignore ST1005 formatting matches the value returned by Teams API fmt.Errorf(`Get coffee://localhost/teams/acid: unsupported protocol scheme "coffee"`), }, {