Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: '1.25.x'
go-version: '1.26.x'
- name: checkout
uses: actions/checkout@v6
- name: lint
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: '1.25.x'
go-version: '1.26.x'
- name: checkout
uses: actions/checkout@v6
- name: check manifests
Expand All @@ -59,7 +59,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: '1.25.x'
go-version: '1.26.x'
- name: checkout
uses: actions/checkout@v6
- name: check go modules
Expand All @@ -73,7 +73,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: '1.25.x'
go-version: '1.26.x'
- name: checkout
uses: actions/checkout@v6
- name: check release-build
Expand All @@ -87,7 +87,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v6
with:
go-version: '1.25.x'
go-version: '1.26.x'
- name: checkout
uses: actions/checkout@v6
- name: test
Expand Down
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ HACKTMPDIR=$(CURDIR)/hack/tmp
BUILDDIR=$(CURDIR)/build/_output
GOFILES=$(shell find . -name "*.go" | grep -vE "(\/vendor\/)|(_test.go)")
TESTPKGS=./...
ENVTEST_K8S_VERSION=1.28
ENVTEST_K8S_VERSION=1.36
ARCH ?= $(shell go env GOARCH)
OS ?= $(shell go env GOOS)
GOPROXY ?=
Expand Down Expand Up @@ -123,19 +123,20 @@ $(BUILDDIR)/$(BINARY_NAME): $(GOFILES) | $(BUILDDIR)

# Tools
GO = go
GO_MOD_VERSION := $(shell grep '^go ' go.mod | awk '{print $$2}')

# golangci-lint is used to lint go code.
GOLANGCI_LINT_PKG=github.com/golangci/golangci-lint/v2/cmd/golangci-lint
GOLANGCI_LINT_BIN= golangci-lint
GOLANGCI_LINT_VER = v2.11.4
GOLANGCI_LINT_VER = v2.12.2
GOLANGCI_LINT = $(TOOLSDIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)
$(GOLANGCI_LINT):
$(call go-install-tool,$(GOLANGCI_LINT_PKG),$(GOLANGCI_LINT_BIN),$(GOLANGCI_LINT_VER))

# controller gen is used to generate manifests and code for Kubernetes controllers.
CONTROLLER_GEN_PKG = sigs.k8s.io/controller-tools/cmd/controller-gen
CONTROLLER_GEN_BIN = controller-gen
CONTROLLER_GEN_VER = v0.19.0
CONTROLLER_GEN_VER = v0.21.0
CONTROLLER_GEN = $(TOOLSDIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
$(CONTROLLER_GEN):
$(call go-install-tool,$(CONTROLLER_GEN_PKG),$(CONTROLLER_GEN_BIN),$(CONTROLLER_GEN_VER))
Expand All @@ -151,7 +152,7 @@ $(KUSTOMIZE):
# setup-envtest is used to install test Kubernetes control plane components for envtest-based tests.
SETUP_ENVTEST_PKG := sigs.k8s.io/controller-runtime/tools/setup-envtest
SETUP_ENVTEST_BIN := setup-envtest
SETUP_ENVTEST_VER := release-0.22
SETUP_ENVTEST_VER := release-0.24
SETUP_ENVTEST := $(abspath $(TOOLSDIR)/$(SETUP_ENVTEST_BIN)-$(SETUP_ENVTEST_VER))
$(SETUP_ENVTEST):
$(call go-install-tool,$(SETUP_ENVTEST_PKG),$(SETUP_ENVTEST_BIN),$(SETUP_ENVTEST_VER))
Expand Down Expand Up @@ -461,8 +462,10 @@ dev-skaffold: $(SKAFFOLD) $(CERT_MANAGER_YAML) manifests generate dev-minikube #
$(SKAFFOLD) debug --default-repo=$(SKAFFOLD_REGISTRY) --detect-minikube=false

# go-install-tool will 'go install' a go module $1 with version $3 and install it with the name $2-$3 to $TOOLSDIR.
# GOTOOLCHAIN is set explicitly so tools are always built with the Go version declared in go.mod,
# rather than the toolchain pinned in the tool's own go.mod.
define go-install-tool
$Q echo "Installing $(2)-$(3) to $(TOOLSDIR)"
$Q GOBIN=$(TOOLSDIR) go install $(1)@$(3)
$Q GOBIN=$(TOOLSDIR) GOTOOLCHAIN=go$(GO_MOD_VERSION) go install $(1)@$(3)
$Q mv $(TOOLSDIR)/$(2) $(TOOLSDIR)/$(2)-$(3)
endef
17 changes: 15 additions & 2 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,30 @@ limitations under the License.
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "mellanox.com", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

func addKnownTypes(s *runtime.Scheme) error {
s.AddKnownTypes(GroupVersion,
&HostDeviceNetwork{}, &HostDeviceNetworkList{},
&IPoIBNetwork{}, &IPoIBNetworkList{},
&MacvlanNetwork{}, &MacvlanNetworkList{},
&NicClusterPolicy{}, &NicClusterPolicyList{},
&NicNodePolicy{}, &NicNodePolicyList{},
)
metav1.AddToGroupVersion(s, GroupVersion)
return nil
}
4 changes: 0 additions & 4 deletions api/v1alpha1/hostdevicenetwork_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,3 @@ type HostDeviceNetworkList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []HostDeviceNetwork `json:"items"`
}

func init() {
SchemeBuilder.Register(&HostDeviceNetwork{}, &HostDeviceNetworkList{})
}
4 changes: 0 additions & 4 deletions api/v1alpha1/ipoibnetwork_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,3 @@ type IPoIBNetworkList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []IPoIBNetwork `json:"items"`
}

func init() {
SchemeBuilder.Register(&IPoIBNetwork{}, &IPoIBNetworkList{})
}
4 changes: 0 additions & 4 deletions api/v1alpha1/macvlannetwork_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,3 @@ type MacvlanNetworkList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []MacvlanNetwork `json:"items"`
}

func init() {
SchemeBuilder.Register(&MacvlanNetwork{}, &MacvlanNetworkList{})
}
4 changes: 0 additions & 4 deletions api/v1alpha1/nicclusterpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,3 @@ func (n *NicClusterPolicy) GetConditions() []metav1.Condition {
func (n *NicClusterPolicy) SetConditions(conditions []metav1.Condition) {
n.Status.Conditions = conditions
}

func init() {
SchemeBuilder.Register(&NicClusterPolicy{}, &NicClusterPolicyList{})
}
4 changes: 0 additions & 4 deletions api/v1alpha1/nicnodepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,3 @@ func (n *NicNodePolicy) GetConditions() []metav1.Condition {
func (n *NicNodePolicy) SetConditions(conditions []metav1.Condition) {
n.Status.Conditions = conditions
}

func init() {
SchemeBuilder.Register(&NicNodePolicy{}, &NicNodePolicyList{})
}
37 changes: 8 additions & 29 deletions api/v1alpha1/validator/hostdevicenetwork_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ package validator

import (
"context"
"errors"
"regexp"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/Mellanox/network-operator/api/v1alpha1"
Expand All @@ -39,65 +36,47 @@ var hostDeviceNetworkLog = logf.Log.WithName("hostdevicenetwork-resource")

type hostDeviceNetworkValidator struct{}

var _ webhook.CustomValidator = &hostDeviceNetworkValidator{}
var _ admission.Validator[*v1alpha1.HostDeviceNetwork] = &hostDeviceNetworkValidator{}

// SetupHostDeviceNetworkWebhookWithManager sets up webhook for HostDeviceNetwork.
func SetupHostDeviceNetworkWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&v1alpha1.HostDeviceNetwork{}).
return ctrl.NewWebhookManagedBy(mgr, &v1alpha1.HostDeviceNetwork{}).
WithValidator(&hostDeviceNetworkValidator{}).
Complete()
}

//nolint:lll
//+kubebuilder:webhook:path=/validate-mellanox-com-v1alpha1-hostdevicenetwork,mutating=false,failurePolicy=fail,sideEffects=None,groups=mellanox.com,resources=hostdevicenetworks,verbs=create;update,versions=v1alpha1,name=vhostdevicenetwork.kb.io,admissionReviewVersions=v1

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (w *hostDeviceNetworkValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
// ValidateCreate implements admission.Validator so a webhook will be registered for the type
func (w *hostDeviceNetworkValidator) ValidateCreate(_ context.Context, hostDeviceNetwork *v1alpha1.HostDeviceNetwork) (admission.Warnings, error) {
if skipValidations {
nicClusterPolicyLog.Info("skipping CR validation")
return nil, nil
}
hostDeviceNetwork, ok := obj.(*v1alpha1.HostDeviceNetwork)
if !ok {
return nil, errors.New("failed to unmarshal HostDeviceNetwork object to validate")
}
hostDeviceNetworkLog.Info("validate create", "name", hostDeviceNetwork.Name)
return nil, w.validateHostDeviceNetwork(hostDeviceNetwork)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
// ValidateUpdate implements admission.Validator so a webhook will be registered for the type
func (w *hostDeviceNetworkValidator) ValidateUpdate(
_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
_ context.Context, _, hostDeviceNetwork *v1alpha1.HostDeviceNetwork) (admission.Warnings, error) {
if skipValidations {
nicClusterPolicyLog.Info("skipping CR validation")
return nil, nil
}

hostDeviceNetwork, ok := newObj.(*v1alpha1.HostDeviceNetwork)
if !ok {
return nil, errors.New("failed to unmarshal HostDeviceNetwork object to validate")
}
hostDeviceNetworkLog.Info("validate update", "name", hostDeviceNetwork.Name)

return nil, w.validateHostDeviceNetwork(hostDeviceNetwork)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
// ValidateDelete implements admission.Validator so a webhook will be registered for the type
func (w *hostDeviceNetworkValidator) ValidateDelete(
_ context.Context, obj runtime.Object) (admission.Warnings, error) {
_ context.Context, hostDeviceNetwork *v1alpha1.HostDeviceNetwork) (admission.Warnings, error) {
if skipValidations {
nicClusterPolicyLog.Info("skipping CR validation")
return nil, nil
}

hostDeviceNetwork, ok := obj.(*v1alpha1.HostDeviceNetwork)
if !ok {
return nil, errors.New("failed to unmarshal HostDeviceNetwork object to validate")
}

hostDeviceNetworkLog.Info("validate delete", "name", hostDeviceNetwork.Name)

// Validation for delete call is not required
return nil, nil
}
Expand Down
Loading
Loading