Skip to content

Commit

Permalink
Merge pull request konpyutaika#245 from r65535/fixing-staticcheck-issues
Browse files Browse the repository at this point in the history
Adding support for staticcheck and govuln
  • Loading branch information
mh013370 authored Mar 29, 2023
2 parents d97e389 + 80ffc61 commit 4403b66
Show file tree
Hide file tree
Showing 40 changed files with 122 additions and 202 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fixed Bugs

- [PR #243](https://github.com/konpyutaika/nifikop/pull/243) - **[Operator]** Re-Fixed bug where an incorrect condition was used to determine whether or not to substitute a custom authorizers template.
- [PR #245](https://github.com/konpyutaika/nifikop/pull/245) - **[Operator]** Added staticcheck linting and go vuln scanning to Makefile. Fixed all linting issues with operator

### Deprecated

Expand Down
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,26 @@ fmt:
vet:
go vet ./...

# Run https://staticcheck.io against code
.PHONY: staticcheck
staticcheck:
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...

# RUN https://go.dev/blog/vuln against code for known CVEs
.PHONY: govuln
govuln:
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...

# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
.PHONY: test
test: manifests generate fmt vet envtest
test: manifests generate fmt vet staticcheck govuln envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out

.PHONY: test-with-vendor
test-with-vendor: manifests generate fmt vet envtest
test-with-vendor: manifests generate fmt vet staticcheck govuln envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -mod=vendor ./... -coverprofile cover.out

# Run against the configured Kubernetes cluster in ~/.kube/config
Expand Down
15 changes: 3 additions & 12 deletions api/v1/nifidataflow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,15 @@ func (d *NifiDataflowSpec) GetSyncMode() DataflowSyncMode {
}

func (d *NifiDataflowSpec) SyncOnce() bool {
if d.GetSyncMode() == SyncOnce {
return true
}
return false
return d.GetSyncMode() == SyncOnce
}

func (d *NifiDataflowSpec) SyncAlways() bool {
if d.GetSyncMode() == SyncAlways {
return true
}
return false
return d.GetSyncMode() == SyncAlways
}

func (d *NifiDataflowSpec) SyncNever() bool {
if d.GetSyncMode() == SyncNever {
return true
}
return false
return d.GetSyncMode() == SyncNever
}

func (d *NifiDataflowSpec) GetParentProcessGroupID(rootProcessGroupId string) string {
Expand Down
12 changes: 6 additions & 6 deletions api/v1alpha1/nificluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func convertNifiClusterSpec(src *NifiClusterSpec, dst *v1.NifiCluster) error {
dst.Spec.OneNifiNodePerNode = src.OneNifiNodePerNode
dst.Spec.PropagateLabels = src.PropagateLabels
if src.NodeUserIdentityTemplate != nil {
dst.Spec.NodeUserIdentityTemplate = &(*src.NodeUserIdentityTemplate)
dst.Spec.NodeUserIdentityTemplate = src.NodeUserIdentityTemplate
}
dst.Spec.SidecarConfigs = src.SidecarConfigs
dst.Spec.TopologySpreadConstraints = src.TopologySpreadConstraints
if src.NifiControllerTemplate != nil {
dst.Spec.NifiControllerTemplate = &(*src.NifiControllerTemplate)
dst.Spec.NifiControllerTemplate = src.NifiControllerTemplate
}
if src.ControllerUserIdentity != nil {
dst.Spec.ControllerUserIdentity = &(*src.ControllerUserIdentity)
dst.Spec.ControllerUserIdentity = src.ControllerUserIdentity
}

convertNifiClusterSecretRef(src.SecretRef, dst)
Expand Down Expand Up @@ -507,15 +507,15 @@ func convertNifiClusterFromSpec(src *v1.NifiClusterSpec, dst *NifiCluster) error
dst.Spec.OneNifiNodePerNode = src.OneNifiNodePerNode
dst.Spec.PropagateLabels = src.PropagateLabels
if src.NodeUserIdentityTemplate != nil {
dst.Spec.NodeUserIdentityTemplate = &(*src.NodeUserIdentityTemplate)
dst.Spec.NodeUserIdentityTemplate = src.NodeUserIdentityTemplate
}
dst.Spec.SidecarConfigs = src.SidecarConfigs
dst.Spec.TopologySpreadConstraints = src.TopologySpreadConstraints
if src.NifiControllerTemplate != nil {
dst.Spec.NifiControllerTemplate = &(*src.NifiControllerTemplate)
dst.Spec.NifiControllerTemplate = src.NifiControllerTemplate
}
if src.ControllerUserIdentity != nil {
dst.Spec.ControllerUserIdentity = &(*src.ControllerUserIdentity)
dst.Spec.ControllerUserIdentity = src.ControllerUserIdentity
}

convertNifiClusterFromSecretRef(src.SecretRef, dst)
Expand Down
4 changes: 0 additions & 4 deletions api/v1alpha1/nificluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var nificlusterlog = logf.Log.WithName("nificluster-resource")

func (r *NifiCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down
15 changes: 3 additions & 12 deletions api/v1alpha1/nifidataflow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,15 @@ func (d *NifiDataflowSpec) GetSyncMode() DataflowSyncMode {
}

func (d *NifiDataflowSpec) SyncOnce() bool {
if d.GetSyncMode() == SyncOnce {
return true
}
return false
return d.GetSyncMode() == SyncOnce
}

func (d *NifiDataflowSpec) SyncAlways() bool {
if d.GetSyncMode() == SyncAlways {
return true
}
return false
return d.GetSyncMode() == SyncAlways
}

func (d *NifiDataflowSpec) SyncNever() bool {
if d.GetSyncMode() == SyncNever {
return true
}
return false
return d.GetSyncMode() == SyncNever
}

func (d *NifiDataflowSpec) GetParentProcessGroupID(rootProcessGroupId string) string {
Expand Down
4 changes: 0 additions & 4 deletions api/v1alpha1/nifidataflow_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var nifidataflowlog = logf.Log.WithName("nifidataflow-resource")

func (r *NifiDataflow) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down
4 changes: 0 additions & 4 deletions api/v1alpha1/nifiparametercontext_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var nifiparametercontextlog = logf.Log.WithName("nifiparametercontext-resource")

func (r *NifiParameterContext) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down
4 changes: 0 additions & 4 deletions api/v1alpha1/nifiregistryclient_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var nifiregistryclientlog = logf.Log.WithName("nifiregistryclient-resource")

func (r *NifiRegistryClient) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down
4 changes: 0 additions & 4 deletions api/v1alpha1/nifiuser_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var nifiuserlog = logf.Log.WithName("nifiuser-resource")

func (r *NifiUser) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down
4 changes: 0 additions & 4 deletions api/v1alpha1/nifiusergroup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var nifiusergrouplog = logf.Log.WithName("nifiusergroup-resource")

func (r *NifiUserGroup) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down
5 changes: 3 additions & 2 deletions controllers/nificlustertask_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package controllers
import (
"context"
"fmt"
"github.com/konpyutaika/nifikop/api/v1"
"reflect"
"time"

v1 "github.com/konpyutaika/nifikop/api/v1"

"emperror.dev/errors"
"github.com/konpyutaika/nifikop/pkg/clientwrappers/scale"
"github.com/konpyutaika/nifikop/pkg/errorfactory"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (r *NifiClusterTaskReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

if len(nodesWithDownscaleSucceeded) > 0 {
err = r.handleNodeRemoveStatus(instance, nodesWithDownscaleSucceeded)
_ = r.handleNodeRemoveStatus(instance, nodesWithDownscaleSucceeded)
}

return Reconciled()
Expand Down
4 changes: 2 additions & 2 deletions controllers/nifidataflow_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (r *NifiDataflowReconciler) Reconcile(ctx context.Context, req ctrl.Request
}

// Get the last configuration viewed by the operator.
o, err := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
// Create it if not exist.
if o == nil {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(instance); err != nil {
Expand All @@ -94,7 +94,7 @@ func (r *NifiDataflowReconciler) Reconcile(ctx context.Context, req ctrl.Request
if err := r.Client.Update(ctx, instance); err != nil {
return RequeueWithError(r.Log, "failed to update NifiDataflow "+instance.Name, err)
}
o, err = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
}

// Check if the cluster reference changed.
Expand Down
4 changes: 2 additions & 2 deletions controllers/nifiparametercontext_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl
}

// Get the last configuration viewed by the operator.
o, err := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
// Create it if not exist.
if o == nil {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(instance); err != nil {
Expand All @@ -93,7 +93,7 @@ func (r *NifiParameterContextReconciler) Reconcile(ctx context.Context, req ctrl
if err := r.Client.Update(ctx, instance); err != nil {
return RequeueWithError(r.Log, "failed to update NifiParameterContext "+instance.Name, err)
}
o, err = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
}

// Check if the cluster reference changed.
Expand Down
6 changes: 3 additions & 3 deletions controllers/nifiregistryclient_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/konpyutaika/nifikop/api/v1"
"reflect"

"github.com/banzaicloud/k8s-objectmatcher/patch"
v1 "github.com/konpyutaika/nifikop/api/v1"
"github.com/konpyutaika/nifikop/pkg/clientwrappers/registryclient"
"github.com/konpyutaika/nifikop/pkg/k8sutil"
"github.com/konpyutaika/nifikop/pkg/nificlient/config"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (r *NifiRegistryClientReconciler) Reconcile(ctx context.Context, req ctrl.R
}

// Get the last configuration viewed by the operator.
o, err := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
// Create it if not exist.
if o == nil {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(instance); err != nil {
Expand All @@ -90,7 +90,7 @@ func (r *NifiRegistryClientReconciler) Reconcile(ctx context.Context, req ctrl.R
if err := r.Client.Update(ctx, instance); err != nil {
return RequeueWithError(r.Log, "failed to update NifiRegistryClient "+instance.Name, err)
}
o, err = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
}

// Check if the cluster reference changed.
Expand Down
7 changes: 4 additions & 3 deletions controllers/nifiuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/konpyutaika/nifikop/api/v1"
"reflect"

v1 "github.com/konpyutaika/nifikop/api/v1"

"emperror.dev/errors"
"github.com/banzaicloud/k8s-objectmatcher/patch"
certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (r *NifiUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}

// Get the last configuration viewed by the operator.
o, err := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
// Create it if not exist.
if o == nil {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(instance); err != nil {
Expand All @@ -96,7 +97,7 @@ func (r *NifiUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err := r.Client.Update(ctx, instance); err != nil {
return RequeueWithError(r.Log, "failed to update NifiUser "+instance.Name, err)
}
o, err = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
}

// Check if the cluster reference changed.
Expand Down
7 changes: 4 additions & 3 deletions controllers/nifiusergroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/konpyutaika/nifikop/api/v1"
"reflect"

v1 "github.com/konpyutaika/nifikop/api/v1"

"emperror.dev/errors"
"github.com/banzaicloud/k8s-objectmatcher/patch"
"github.com/konpyutaika/nifikop/pkg/clientwrappers/usergroup"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (r *NifiUserGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

// Get the last configuration viewed by the operator.
o, err := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ := patch.DefaultAnnotator.GetOriginalConfiguration(instance)
// Create it if not exist.
if o == nil {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(instance); err != nil {
Expand All @@ -91,7 +92,7 @@ func (r *NifiUserGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if err := r.Client.Update(ctx, instance); err != nil {
return RequeueWithError(r.Log, "failed to update NifiUserGroup "+instance.Name, err)
}
o, err = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
o, _ = patch.DefaultAnnotator.GetOriginalConfiguration(instance)
}

// Check if the cluster reference changed.
Expand Down
2 changes: 0 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
Expand All @@ -38,7 +37,6 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

Expand Down
6 changes: 3 additions & 3 deletions pkg/clientwrappers/accesspolicies/policies.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package accesspolicies

import (
"github.com/konpyutaika/nifikop/api/v1"
v1 "github.com/konpyutaika/nifikop/api/v1"
"github.com/konpyutaika/nifikop/pkg/clientwrappers"
"github.com/konpyutaika/nifikop/pkg/common"
"github.com/konpyutaika/nifikop/pkg/nificlient"
Expand Down Expand Up @@ -84,7 +84,7 @@ func UpdateAccessPolicy(
}

updateAccessPolicyEntity(accessPolicy, addUsers, removeUsers, addUserGroups, removeUserGroups, config, entity)
entity, err = nClient.UpdateAccessPolicy(*entity)
_, _ = nClient.UpdateAccessPolicy(*entity)
return clientwrappers.ErrorUpdateOperation(log, err, "Update user")
}

Expand All @@ -109,7 +109,7 @@ func UpdateAccessPolicyEntity(
addRemoveUsersFromAccessPolicyEntity(addUsers, removeUsers, entity)
addRemoveUserGroupsFromAccessPolicyEntity(addUserGroups, removeUserGroups, entity)

entity, err = nClient.UpdateAccessPolicy(*entity)
_, _ = nClient.UpdateAccessPolicy(*entity)
return clientwrappers.ErrorUpdateOperation(log, err, "Update user")
}

Expand Down
Loading

0 comments on commit 4403b66

Please sign in to comment.