Skip to content

Commit

Permalink
fix golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
guangbochen committed Feb 3, 2023
1 parent 455ff6b commit 610e12f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 49 deletions.
18 changes: 8 additions & 10 deletions .golangci.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
"linters": {
"disable-all": true,
"enable": [
"govet",
"golint",
"goimports",
"misspell",
"ineffassign",
"gofmt",
"staticcheck",
"bodyclose",
"gosimple",
"prealloc",
"deadcode",
"errcheck"
"misspell",
"revive",
"prealloc"
]
},
"run": {
Expand All @@ -28,5 +21,10 @@
"build-tags": [
"test"
]
},
"linters-settings": {
"goimports": {
"local-prefixes": "github.com/harvester/harvester-network-controller"
}
}
}
1 change: 1 addition & 0 deletions pkg/controller/agent/linkmonitor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linkmonitor
import (
"context"
"fmt"

ctlcorev1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/vishvananda/netlink"
"k8s.io/apimachinery/pkg/labels"
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/agent/nad/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func (h Handler) addLocalArea(nad *nadv1.NetworkAttachmentDefinition) error {
return v.AddLocalArea(localArea)
}

func (h Handler) existDuplicateNad(vlanIdStr, cn string) (bool, error) {
func (h Handler) existDuplicateNad(vlanIDStr, cn string) (bool, error) {
nads, err := h.nadCache.List("", labels.Set(map[string]string{
utils.KeyVlanLabel: vlanIdStr,
utils.KeyVlanLabel: vlanIDStr,
utils.KeyClusterNetworkLabel: cn,
}).AsSelector())
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/manager/node/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package node
import (
"context"
"fmt"
"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io"
ctlcorev1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"

"github.com/deckarep/golang-set/v2"
mapset "github.com/deckarep/golang-set/v2"
ctlcorev1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"

"github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io"
networkv1 "github.com/harvester/harvester-network-controller/pkg/apis/network.harvesterhci.io/v1beta1"
"github.com/harvester/harvester-network-controller/pkg/config"
ctlnetworkv1 "github.com/harvester/harvester-network-controller/pkg/generated/controllers/network.harvesterhci.io/v1beta1"
Expand Down
13 changes: 7 additions & 6 deletions pkg/webhook/clusternetwork/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ const (
deleteErr = "could not delete cluster network %s because %w"
)

type cnValidator struct {
type CnValidator struct {
types.DefaultValidator
vcCache ctlnetworkv1.VlanConfigCache
}

var _ types.Validator = &cnValidator{}
var _ types.Validator = &CnValidator{}

func NewCnValidator(vcCache ctlnetworkv1.VlanConfigCache) *cnValidator {
return &cnValidator{
func NewCnValidator(vcCache ctlnetworkv1.VlanConfigCache) *CnValidator {
validator := &CnValidator{
vcCache: vcCache,
}
return validator
}

func (c *cnValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
func (c *CnValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
cn := oldObj.(*networkv1.ClusterNetwork)

if cn.Name == utils.ManagementClusterNetworkName {
Expand All @@ -56,7 +57,7 @@ func (c *cnValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
return nil
}

func (c *cnValidator) Resource() types.Resource {
func (c *CnValidator) Resource() types.Resource {
return types.Resource{
Names: []string{"clusternetworks"},
Scope: admissionregv1.ClusterScope,
Expand Down
23 changes: 12 additions & 11 deletions pkg/webhook/nad/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ const (
deleteErr = "could not delete nad %s/%s because %w"
)

type nadValidator struct {
type Validator struct {
types.DefaultValidator
vmCache ctlkubevirtv1.VirtualMachineCache
}

var _ types.Validator = &nadValidator{}
var _ types.Validator = &Validator{}

func NewNadValidator(vmCache ctlkubevirtv1.VirtualMachineCache) *nadValidator {
return &nadValidator{
func NewNadValidator(vmCache ctlkubevirtv1.VirtualMachineCache) *Validator {
return &Validator{
vmCache: vmCache,
}
}

func (n *nadValidator) Create(_ *types.Request, newObj runtime.Object) error {
func (n *Validator) Create(_ *types.Request, newObj runtime.Object) error {
netAttachDef := newObj.(*cniv1.NetworkAttachmentDefinition)

config := netAttachDef.Spec.Config
Expand Down Expand Up @@ -65,7 +65,7 @@ func (n *nadValidator) Create(_ *types.Request, newObj runtime.Object) error {
return nil
}

func (n *nadValidator) Update(_ *types.Request, oldObj, newObj runtime.Object) error {
func (n *Validator) Update(_ *types.Request, oldObj, newObj runtime.Object) error {
oldNad, newNad := oldObj.(*cniv1.NetworkAttachmentDefinition), newObj.(*cniv1.NetworkAttachmentDefinition)

if oldNad.Spec.Config != newNad.Spec.Config {
Expand All @@ -75,7 +75,7 @@ func (n *nadValidator) Update(_ *types.Request, oldObj, newObj runtime.Object) e
return nil
}

func (n *nadValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
func (n *Validator) Delete(_ *types.Request, oldObj runtime.Object) error {
netAttachDef := oldObj.(*cniv1.NetworkAttachmentDefinition)

// multus network name can be <networkName> or <namespace>/<networkName>
Expand All @@ -85,12 +85,13 @@ func (n *nadValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
if err != nil {
return fmt.Errorf(deleteErr, netAttachDef.Namespace, networkName, err)
}
if vmsTmp, err := n.vmCache.GetByIndex(indexeres.VMByNetworkIndex, netAttachDef.Name); err != nil {
vmsTmp, err := n.vmCache.GetByIndex(indexeres.VMByNetworkIndex, netAttachDef.Name)
if err != nil {
return fmt.Errorf(deleteErr, netAttachDef.Namespace, networkName, err)
} else {
vms = append(vms, vmsTmp...)
}

vms = append(vms, vmsTmp...)

if len(vms) > 0 {
vmNameList := make([]string, 0, len(vms))
for _, vm := range vms {
Expand All @@ -102,7 +103,7 @@ func (n *nadValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
return nil
}

func (n *nadValidator) Resource() types.Resource {
func (n *Validator) Resource() types.Resource {
return types.Resource{
Names: []string{"network-attachment-definitions"},
Scope: admissionregv1.NamespacedScope,
Expand Down
16 changes: 8 additions & 8 deletions pkg/webhook/vlanconfig/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
"github.com/harvester/harvester-network-controller/pkg/utils"
)

type vlanConfigMutator struct {
type Mutator struct {
types.DefaultMutator

nodeCache ctlcorev1.NodeCache
}

var _ types.Mutator = &vlanConfigMutator{}
var _ types.Mutator = &Mutator{}

func NewNadMutator(nodeCache ctlcorev1.NodeCache) *vlanConfigMutator {
return &vlanConfigMutator{
func NewNadMutator(nodeCache ctlcorev1.NodeCache) *Mutator {
return &Mutator{
nodeCache: nodeCache,
}
}

func (v *vlanConfigMutator) Create(_ *types.Request, newObj runtime.Object) (types.Patch, error) {
func (v *Mutator) Create(_ *types.Request, newObj runtime.Object) (types.Patch, error) {
vlanConfig := newObj.(*networkv1.VlanConfig)

annotationPatch, err := v.matchNodes(vlanConfig)
Expand All @@ -39,7 +39,7 @@ func (v *vlanConfigMutator) Create(_ *types.Request, newObj runtime.Object) (typ
return append(getCnLabelPatch(vlanConfig), annotationPatch...), nil
}

func (v *vlanConfigMutator) Update(_ *types.Request, oldObj, newObj runtime.Object) (types.Patch, error) {
func (v *Mutator) Update(_ *types.Request, oldObj, newObj runtime.Object) (types.Patch, error) {
newVc := newObj.(*networkv1.VlanConfig)
oldVc := newObj.(*networkv1.VlanConfig)

Expand Down Expand Up @@ -75,7 +75,7 @@ func getCnLabelPatch(v *networkv1.VlanConfig) types.Patch {
}}
}

func (v *vlanConfigMutator) matchNodes(vc *networkv1.VlanConfig) (types.Patch, error) {
func (v *Mutator) matchNodes(vc *networkv1.VlanConfig) (types.Patch, error) {
nodes, err := v.nodeCache.List(labels.Set(vc.Spec.NodeSelector).AsSelector())
if err != nil {
return nil, err
Expand Down Expand Up @@ -113,7 +113,7 @@ func matchedNodesToPatch(vc *networkv1.VlanConfig, matchedNodes []string) (types
}, nil
}

func (v *vlanConfigMutator) Resource() types.Resource {
func (v *Mutator) Resource() types.Resource {
return types.Resource{
Names: []string{"vlanconfigs"},
Scope: admissionregv1.ClusterScope,
Expand Down
18 changes: 9 additions & 9 deletions pkg/webhook/vlanconfig/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ const (
deleteErr = "could not delete vlanConfig %s because %w"
)

type vlanConfigValidator struct {
type Validator struct {
types.DefaultValidator
nadCache ctlcniv1.NetworkAttachmentDefinitionCache
vsCache ctlnetworkv1.VlanStatusCache
}

func NewVlanConfigValidator(nadCache ctlcniv1.NetworkAttachmentDefinitionCache,
vsCache ctlnetworkv1.VlanStatusCache) *vlanConfigValidator {
return &vlanConfigValidator{
vsCache ctlnetworkv1.VlanStatusCache) *Validator {
return &Validator{
nadCache: nadCache,
vsCache: vsCache,
}
}

var _ types.Validator = &vlanConfigValidator{}
var _ types.Validator = &Validator{}

func (v *vlanConfigValidator) Create(_ *types.Request, newObj runtime.Object) error {
func (v *Validator) Create(_ *types.Request, newObj runtime.Object) error {
vc := newObj.(*networkv1.VlanConfig)

if vc.Spec.ClusterNetwork == utils.ManagementClusterNetworkName {
Expand All @@ -62,7 +62,7 @@ func (v *vlanConfigValidator) Create(_ *types.Request, newObj runtime.Object) er
return nil
}

func (v *vlanConfigValidator) Update(_ *types.Request, oldObj, newObj runtime.Object) error {
func (v *Validator) Update(_ *types.Request, oldObj, newObj runtime.Object) error {
vc := newObj.(*networkv1.VlanConfig)

if vc.Spec.ClusterNetwork == utils.ManagementClusterNetworkName {
Expand All @@ -77,7 +77,7 @@ func (v *vlanConfigValidator) Update(_ *types.Request, oldObj, newObj runtime.Ob
return nil
}

func (v *vlanConfigValidator) Delete(_ *types.Request, oldObj runtime.Object) error {
func (v *Validator) Delete(_ *types.Request, oldObj runtime.Object) error {
vc := oldObj.(*networkv1.VlanConfig)
// The vlanconfig is not allowed to be deleted if it has applied to some nodes and its clusternetwork is attached by
// some nads.
Expand Down Expand Up @@ -107,7 +107,7 @@ func (v *vlanConfigValidator) Delete(_ *types.Request, oldObj runtime.Object) er
return nil
}

func (v *vlanConfigValidator) Resource() types.Resource {
func (v *Validator) Resource() types.Resource {
return types.Resource{
Names: []string{"vlanconfigs"},
Scope: admissionregv1.ClusterScope,
Expand All @@ -122,7 +122,7 @@ func (v *vlanConfigValidator) Resource() types.Resource {
}
}

func (v *vlanConfigValidator) checkOverlaps(vc *networkv1.VlanConfig) error {
func (v *Validator) checkOverlaps(vc *networkv1.VlanConfig) error {
var matchedNodes []string
if vc.Annotations == nil || vc.Annotations[utils.KeyMatchedNodes] == "" {
return nil
Expand Down

0 comments on commit 610e12f

Please sign in to comment.