diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9cf7c761..56ceab9d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,9 @@ jobs: with: go-version: 1.14 + - name: Lint + run: make lint + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v2 with: diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..e3bade34 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,25 @@ +linters: + auto-fix: false + enable: + - errcheck + - goimports + - golint + - gosec + - misspell + - scopelint + - unconvert + - unparam + - interfacer + - nakedret + - gocyclo + - dupl + - goconst + - lll +run: +linters-settings: + errcheck: + check-type-assertions: true + lll: + line-length: 250 + dupl: + threshold: 400 diff --git a/Makefile b/Makefile index f9234fef..b01f7076 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build +.PHONY: all build lint all: build build: @@ -6,3 +6,12 @@ build: install: build install bin/kubectl-operator $(shell go env GOPATH)/bin + +GOLANGCI_LINT_VER = "1.29.0" +lint: +# scripts/golangci-lint-check.sh +ifneq (${GOLANGCI_LINT_VER}, "$(shell ./bin/golangci-lint --version 2>/dev/null | cut -b 27-32)") + @echo "golangci-lint missing or not version '${GOLANGCI_LINT_VER}', downloading..." + curl -sSfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${GOLANGCI_LINT_VER}/install.sh" | sh -s -- -b ./bin "v${GOLANGCI_LINT_VER}" +endif + ./bin/golangci-lint --timeout 3m run \ No newline at end of file diff --git a/internal/cmd/catalog_list.go b/internal/cmd/catalog_list.go index e47c49b4..ca3de0d2 100644 --- a/internal/cmd/catalog_list.go +++ b/internal/cmd/catalog_list.go @@ -49,7 +49,7 @@ func newCatalogListCmd(cfg *action.Configuration) *cobra.Command { if allNamespaces { ns = "\t" + cs.Namespace } - age := time.Now().Sub(cs.CreationTimestamp.Time) + age := time.Since(cs.CreationTimestamp.Time) _, _ = fmt.Fprintf(tw, "%s%s\t%s\t%s\t%s\t%s\n", cs.Name, ns, cs.Spec.DisplayName, cs.Spec.SourceType, cs.Spec.Publisher, duration.HumanDuration(age)) } _ = tw.Flush() diff --git a/internal/cmd/operator_list.go b/internal/cmd/operator_list.go index cfc5bc67..65464752 100644 --- a/internal/cmd/operator_list.go +++ b/internal/cmd/operator_list.go @@ -55,7 +55,7 @@ func newOperatorListCmd(cfg *action.Configuration) *cobra.Command { if allNamespaces { ns = "\t" + sub.Namespace } - age := time.Now().Sub(sub.CreationTimestamp.Time) + age := time.Since(sub.CreationTimestamp.Time) _, _ = fmt.Fprintf(tw, "%s%s\t%s\t%s\t%s\t%s\t%s\n", sub.Spec.Package, ns, sub.Name, sub.Status.InstalledCSV, sub.Status.CurrentCSV, sub.Status.State, duration.HumanDuration(age)) } _ = tw.Flush() diff --git a/internal/cmd/operator_list_available.go b/internal/cmd/operator_list_available.go index 9e6de2d8..eda5ca4a 100644 --- a/internal/cmd/operator_list_available.go +++ b/internal/cmd/operator_list_available.go @@ -48,7 +48,7 @@ func newOperatorListAvailableCmd(cfg *action.Configuration) *cobra.Command { tw := tabwriter.NewWriter(os.Stdout, 3, 4, 2, ' ', 0) _, _ = fmt.Fprintf(tw, "NAME\tCATALOG\tCHANNEL\tLATEST CSV\tAGE\n") for _, op := range operators { - age := time.Now().Sub(op.CreationTimestamp.Time) + age := time.Since(op.CreationTimestamp.Time) for _, ch := range op.Status.Channels { _, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", op.Name, op.Status.CatalogSourceDisplayName, ch.Name, ch.CurrentCSV, duration.HumanDuration(age)) } diff --git a/internal/pkg/action/operator_install.go b/internal/pkg/action/operator_install.go index 30175464..e4902df8 100644 --- a/internal/pkg/action/operator_install.go +++ b/internal/pkg/action/operator_install.go @@ -51,7 +51,10 @@ func (i *OperatorInstall) BindFlags(fs *pflag.FlagSet) { fs.BoolVarP(&i.CreateOperatorGroup, "create-operator-group", "C", false, "create operator group if necessary") fs.VarP(&i.InstallMode, "install-mode", "i", "install mode") - fs.MarkHidden("install-mode") + err := fs.MarkHidden("install-mode") + if err != nil { + panic(`requested flag "install-mode" missing`) + } } func (i *OperatorInstall) Run(ctx context.Context) (*v1alpha1.ClusterServiceVersion, error) { @@ -135,6 +138,7 @@ func (i *OperatorInstall) getPackageChannel(pm *operatorsv1.PackageManifest) (*o } var packageChannel *operatorsv1.PackageChannel for _, ch := range pm.Status.Channels { + ch := ch if ch.Name == i.Channel { packageChannel = &ch } @@ -308,7 +312,7 @@ func guessStartingCSV(csvNameExample, desiredVersion string) string { const ( operatorNameRegexp = `[a-z0-9]([-a-z0-9]*[a-z0-9])?` - semverRegexp = `(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?` + semverRegexp = `(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?` //nolint:lll ) var csvNameRegexp = regexp.MustCompile(`^(` + operatorNameRegexp + `).(v?)(` + semverRegexp + `)$`) diff --git a/internal/pkg/action/operator_uninstall.go b/internal/pkg/action/operator_uninstall.go index 26134f2b..89c12970 100644 --- a/internal/pkg/action/operator_uninstall.go +++ b/internal/pkg/action/operator_uninstall.go @@ -49,6 +49,7 @@ func (u *OperatorUninstall) Run(ctx context.Context) error { var sub *v1alpha1.Subscription for _, s := range subs.Items { + s := s if u.Package == s.Spec.Package { sub = &s break @@ -85,6 +86,7 @@ func (u *OperatorUninstall) Run(ctx context.Context) error { return fmt.Errorf("list operatorgroups: %v", err) } for _, og := range ogs.Items { + og := og if err := u.config.Client.Delete(ctx, &og); err != nil { return fmt.Errorf("delete operatorgroup %q: %v", og.Name, err) } diff --git a/internal/pkg/action/operator_upgrade.go b/internal/pkg/action/operator_upgrade.go index 24cc8cd3..bd0835f0 100644 --- a/internal/pkg/action/operator_upgrade.go +++ b/internal/pkg/action/operator_upgrade.go @@ -39,6 +39,7 @@ func (u *OperatorUpgrade) Run(ctx context.Context) (*v1alpha1.ClusterServiceVers var sub *v1alpha1.Subscription for _, s := range subs.Items { + s := s if u.Package == s.Spec.Package { sub = &s break