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

chore: use github-action for golangci-lint workflow #538

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Lint checks
on:
push:
branches:
- 'main'
- '[0-9]+.[1-9][0-9]*.x'
pull_request:
branches:
- 'main'
- '[0-9]+.[1-9][0-9]*.x'
paths:
- "**.go"
- "**/go.mod"
- "**/go.sum"
- ".golangi.yml"
- ".github/workflows/golangci-lint.yml"
- "!docs/**"
env:
# renovate: datasource=github-releases depName=golangci/golangci-lint
GOLANGCI_LINT_VERSION: "v1.55.2"
GO_VERSION: "~1.20"
jobs:
golangci-lint:
name: golangci-lint
runs-on: ubuntu-22.04
strategy:
fail-fast: false
steps:
- name: Check out code
uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
working-directory: ./
version: ${{ env.GOLANGCI_LINT_VERSION }}
args: --config ./.golangci.yml -v
29 changes: 3 additions & 26 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,9 @@ permissions:
contents: read

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.DEFAULT_GO_VERSION }}
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Environment
run: |
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Module cache
uses: actions/cache@v3
env:
cache-name: go-mod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/go.sum') }}
- name: Run linter
run: make lint

unit-test:
name: Unit Tests
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Install Go
uses: actions/setup-go@v4
Expand Down Expand Up @@ -78,7 +55,7 @@ jobs:
docker-local:
permissions:
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -117,7 +94,7 @@ jobs:
path: ${{ github.workspace }}/open-feature-operator-local.tar

e2e-test:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
needs: docker-local
strategy:
matrix:
Expand Down
34 changes: 34 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
run:
timeout: 5m
go: '1.20'
linters:
enable:
- gofmt # Gofmt checks whether code was gofmt-ed. By default, this tool runs with -s option to check for code simplification
- gci # Gci controls golang package import order and makes it always deterministic.
- errorlint # errorlint can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
- containedctx # containedctx is a linter that detects struct contained context.Context field
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- nilnil # Checks that there is no simultaneous return of nil error and an invalid value.
- noctx # noctx finds sending http request without context.Context
- gocyclo # measure cyclomatic complexity
- gocognit # measure cognitive complexity
- funlen # limit function length
- dupl # Detect code duplication

issues:
exclude-rules:
- linters:
- containedctx
- gocyclo
- gocognit
- funlen
path: _test\.go

linters-settings:
gocyclo:
min-complexity: 10
gocognit:
min-complexity: 20
funlen:
lines: 120
statements: 120
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ e2e-test-validate-local:
.PHONY: lint
lint:
go install -v github.com/golangci/golangci-lint/cmd/golangci-lint@latest
${GOPATH}/bin/golangci-lint run --deadline=3m --timeout=3m ./... # Run linters
${GOPATH}/bin/golangci-lint run --deadline=3m --timeout=3m --config=./.golangci.yml -v ./... # Run linters

.PHONY: generate-crdocs
generate-crdocs: kustomize crdocs
Expand Down
2 changes: 2 additions & 0 deletions apis/core/v1alpha1/flagsourceconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type FlagSourceConfigurationList struct {
Items []FlagSourceConfiguration `json:"items"`
}

//nolint:gocyclo
func NewFlagSourceConfigurationSpec() (*FlagSourceConfigurationSpec, error) {
fsc := &FlagSourceConfigurationSpec{
MetricsPort: DefaultMetricPort,
Expand Down Expand Up @@ -278,6 +279,7 @@ func NewFlagSourceConfigurationSpec() (*FlagSourceConfigurationSpec, error) {
return fsc, nil
}

//nolint:gocyclo
func (fc *FlagSourceConfigurationSpec) Merge(new *FlagSourceConfigurationSpec) {
if new == nil {
return
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1beta1 contains API Schema definitions for the core v1beta1 API group
//+kubebuilder:object:generate=true
//+groupName=core.openfeature.dev
// +kubebuilder:object:generate=true
// +groupName=core.openfeature.dev
package v1beta1

import (
Expand Down
4 changes: 2 additions & 2 deletions controllers/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package common
import (
"context"
"fmt"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"

"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
appsV1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down
5 changes: 4 additions & 1 deletion controllers/common/flagd-injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-logr/logr"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
"github.com/open-feature/open-feature-operator/controllers/common/constant"
Expand All @@ -16,7 +18,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"
)

const (
Expand Down Expand Up @@ -45,6 +46,7 @@ type FlagdContainerInjector struct {
FlagDResourceRequirements corev1.ResourceRequirements
}

//nolint:gocyclo
func (fi *FlagdContainerInjector) InjectFlagd(
ctx context.Context,
objectMeta *metav1.ObjectMeta,
Expand Down Expand Up @@ -179,6 +181,7 @@ func (fi *FlagdContainerInjector) handleSidecarSources(ctx context.Context, obje
return nil
}

//nolint:gocyclo
func (fi *FlagdContainerInjector) buildSources(ctx context.Context, objectMeta *metav1.ObjectMeta, flagSourceConfig *v1alpha1.FlagSourceConfigurationSpec, podSpec *corev1.PodSpec, sidecar *corev1.Container) ([]types.SourceConfig, error) {
var sourceCfgCollection []types.SourceConfig

Expand Down
7 changes: 5 additions & 2 deletions controllers/common/flagd-injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package common
import (
"context"
"errors"
"reflect"
"testing"

"github.com/go-logr/logr/testr"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
"github.com/open-feature/open-feature-operator/controllers/common/constant"
Expand All @@ -17,10 +20,8 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
)

func TestFlagdContainerInjector_InjectDefaultSyncProvider(t *testing.T) {
Expand Down Expand Up @@ -999,6 +1000,7 @@ func Test_getSecurityContext(t *testing.T) {
}
}

//nolint:dupl
func TestFlagdContainerInjector_EnableClusterRoleBinding_AddDefaultServiceAccountName(t *testing.T) {

namespace, fakeClient := initEnableClusterroleBindingTestEnv()
Expand Down Expand Up @@ -1042,6 +1044,7 @@ func TestFlagdContainerInjector_EnableClusterRoleBinding_AddDefaultServiceAccoun
require.Equal(t, namespace, updatedCrb.Subjects[0].Namespace)
}

//nolint:dupl
func TestFlagdContainerInjector_EnableClusterRoleBinding_ServiceAccountName(t *testing.T) {

namespace, fakeClient := initEnableClusterroleBindingTestEnv()
Expand Down
3 changes: 2 additions & 1 deletion controllers/common/flagd-proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package common

import (
"context"
"testing"

"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/apps/v1"
v12 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
)

func TestNewFlagdProxyConfiguration(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion controllers/common/mock/flagd-injector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions controllers/core/featureflagconfiguration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ import (
"context"

"github.com/go-logr/logr"
corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
"github.com/open-feature/open-feature-operator/controllers/common"
"github.com/open-feature/open-feature-operator/pkg/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"

corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// FeatureFlagConfigurationReconciler reconciles a FeatureFlagConfiguration object
Expand Down Expand Up @@ -61,6 +59,7 @@ type FeatureFlagConfigurationReconciler struct {

const CrdName = "FeatureFlagConfiguration"

//nolint:gocognit,gocyclo
func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Log.Info("Reconciling" + CrdName)

Expand Down
7 changes: 4 additions & 3 deletions controllers/core/flagsourceconfiguration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"
"time"

"github.com/go-logr/logr"
corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
"github.com/open-feature/open-feature-operator/controllers/common"
appsV1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -30,9 +32,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/go-logr/logr"
corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
)

// FlagSourceConfigurationReconciler reconciles a FlagSourceConfiguration object
Expand All @@ -55,6 +54,8 @@ type FlagSourceConfigurationReconciler struct {

// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile
//
//nolint:gocyclo
func (r *FlagSourceConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Log.Info("Searching for FlagSourceConfiguration")

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook"
//+kubebuilder:scaffold:imports
)

const (
Expand Down Expand Up @@ -87,6 +86,7 @@ func init() {
//+kubebuilder:scaffold:scheme
}

//nolint:funlen,gocognit,gocyclo
func main() {
flag.StringVar(&metricsAddr, metricsBindAddressFlagName, ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, healthProbeBindAddressFlagName, ":8081", "The address the probe endpoint binds to.")
Expand Down
5 changes: 2 additions & 3 deletions webhooks/featureflagconfiguration_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"net/http"
"strings"

schemas "github.com/open-feature/schemas/json"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/go-logr/logr"
corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
schemas "github.com/open-feature/schemas/json"
"github.com/xeipuuv/gojsonschema"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
Expand Down
9 changes: 5 additions & 4 deletions webhooks/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package webhooks
import (
"context"
"encoding/json"
goErr "errors"
"fmt"
controllercommon "github.com/open-feature/open-feature-operator/controllers/common"
"github.com/open-feature/open-feature-operator/controllers/common/constant"
"net/http"
"reflect"
"strings"
"time"

goErr "errors"

"github.com/go-logr/logr"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
controllercommon "github.com/open-feature/open-feature-operator/controllers/common"
"github.com/open-feature/open-feature-operator/controllers/common/constant"
"github.com/open-feature/open-feature-operator/pkg/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -57,6 +56,8 @@ type PodMutator struct {
}

// Handle injects the flagd sidecar (if the prerequisites are all met)
//
//nolint:gocyclo
func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
defer func() {
if err := recover(); err != nil {
Expand Down
Loading
Loading