Skip to content

Commit

Permalink
controller runtime upgraded to the 0.15.0 (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
LanDeleih authored May 24, 2023
1 parent 687e76f commit 86b01c2
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 458 deletions.
50 changes: 33 additions & 17 deletions api/v1alpha1/ip_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package v1alpha1

import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -48,70 +50,84 @@ func (in *IP) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &IP{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *IP) ValidateCreate() error {
func (in *IP) ValidateCreate() (admission.Warnings, error) {
iplog.Info("validate create", "name", in.Name)

var allErrs field.ErrorList
var warnings admission.Warnings

if in.Spec.Consumer != nil {
if _, err := schema.ParseGroupVersion(in.Spec.Consumer.APIVersion); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer.apiVersion"), in.Spec.Consumer.APIVersion, err.Error()))
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec.consumer.apiVersion"), in.Spec.Consumer.APIVersion, err.Error()))
}
}

if in.Spec.Subnet.Name == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.subnet.name"), in.Spec.IP, "Parent subnet should be defined"))
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec.subnet.name"), in.Spec.IP, "Parent subnet should be defined"))
}

if len(allErrs) > 0 {
return apierrors.NewInvalid(in.GroupVersionKind().GroupKind(), in.Name, allErrs)
return warnings, apierrors.NewInvalid(in.GroupVersionKind().GroupKind(), in.Name, allErrs)
}

return nil
return warnings, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *IP) ValidateUpdate(old runtime.Object) error {
func (in *IP) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
iplog.Info("validate update", "name", in.Name)

var warnings admission.Warnings

oldIP, ok := old.(*IP)
if !ok {
return apierrors.NewInternalError(errors.New("cannot cast previous object version to IP CR type"))
return warnings, apierrors.NewInternalError(
errors.New("cannot cast previous object version to IP CR type"))
}

var allErrs field.ErrorList

if !(oldIP.Spec.IP == nil && in.Spec.IP == nil) {
if oldIP.Spec.IP == nil || in.Spec.IP == nil ||
!oldIP.Spec.IP.Equal(in.Spec.IP) {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.ip"), in.Spec.IP, "IP change is disallowed"))
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec.ip"), in.Spec.IP, "IP change is disallowed"))
}
}

if oldIP.Spec.Subnet.Name != in.Spec.Subnet.Name {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.subnet.name"), in.Spec.Subnet.Name, "Subnet change is disallowed"))
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec.subnet.name"), in.Spec.Subnet.Name, "Subnet change is disallowed"))
}

if len(allErrs) > 0 {
return apierrors.NewInvalid(in.GroupVersionKind().GroupKind(), in.Name, allErrs)
return warnings, apierrors.NewInvalid(in.GroupVersionKind().GroupKind(), in.Name, allErrs)
}

return nil
return warnings, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *IP) ValidateDelete() error {
func (in *IP) ValidateDelete() (admission.Warnings, error) {
iplog.Info("validate delete", "name", in.Name)

var warnings admission.Warnings

if in.Spec.Consumer == nil {
return nil
return warnings, nil
}

unstruct := &unstructured.Unstructured{}
gv, err := schema.ParseGroupVersion(in.Spec.Consumer.APIVersion)
if err != nil {
iplog.Error(err, "unable to parse APIVerson of consumer resource, therefore allowing to delete IP", "name", in.Name, "api version", in.Spec.Consumer.APIVersion)
return nil
message := fmt.Sprintf("unable to parse APIVerson of consumer resource, therefore allowing to delete IP."+
"name: %s, api version: %s",
in.Name, in.Spec.Consumer.APIVersion)
iplog.Error(err, message)
return append(warnings, message), nil
}

gvk := gv.WithKind(in.Spec.Consumer.Kind)
Expand All @@ -125,8 +141,8 @@ func (in *IP) ValidateDelete() error {
if err := ipWebhookClient.Get(ctx, namespacedName, unstruct); !apierrors.IsNotFound(err) {
var allErrs field.ErrorList
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, "Consumer is not deleted"))
return apierrors.NewInvalid(gvk.GroupKind(), in.Name, allErrs)
return warnings, apierrors.NewInvalid(gvk.GroupKind(), in.Name, allErrs)
}

return nil
return warnings, nil
}
43 changes: 28 additions & 15 deletions api/v1alpha1/network_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1alpha1
import (
"fmt"
"math"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -42,13 +43,15 @@ func (in *Network) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &Network{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *Network) ValidateCreate() error {
func (in *Network) ValidateCreate() (admission.Warnings, error) {
networklog.Info("validate create", "name", in.Name)

var allErrs field.ErrorList
var warnings admission.Warnings

if in.Spec.Type == "" && in.Spec.ID != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.id"), in.Spec.ID, "setting network ID without type is disallowed"))
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec.id"), in.Spec.ID, "setting network ID without type is disallowed"))
}

if err := in.validateID(); err != nil {
Expand All @@ -61,30 +64,37 @@ func (in *Network) ValidateCreate() error {
Group: gvk.Group,
Kind: gvk.Kind,
}
return apierrors.NewInvalid(gk, in.Name, allErrs)
return warnings, apierrors.NewInvalid(gk, in.Name, allErrs)
}

return nil
return warnings, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *Network) ValidateUpdate(old runtime.Object) error {
func (in *Network) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
networklog.Info("validate update", "name", in.Name)

var warnings admission.Warnings

oldNetwork, ok := old.(*Network)
if !ok {
return apierrors.NewInternalError(errors.New("cannot cast previous object version to Network CR type"))
message := errors.New("cannot cast previous object version to Network CR type")
return append(warnings, message.Error()), apierrors.NewInternalError(message)
}

var allErrs field.ErrorList

if oldNetwork.Spec.Type != "" &&
oldNetwork.Spec.Type != in.Spec.Type {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.type"), in.Spec.Type, "network type change is disallowed; resource should be released (deleted) first"))
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec.type"), in.Spec.Type, "network type change is disallowed; resource should be released (deleted) first"))
}

if (oldNetwork.Spec.ID != nil && oldNetwork.Spec.ID.Cmp(&in.Spec.ID.Int) != 0) ||
(oldNetwork.Spec.ID == nil && oldNetwork.Spec.Type != "" && in.Spec.ID != nil) {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.id"), in.Spec.ID, "network ID change after assignment is disallowed; resource should be released (deleted) first"))
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec.id"), in.Spec.ID,
"network ID change after assignment is disallowed; resource should be released (deleted) first"))
}

if err := in.validateID(); err != nil {
Expand All @@ -97,35 +107,38 @@ func (in *Network) ValidateUpdate(old runtime.Object) error {
Group: gvk.Group,
Kind: gvk.Kind,
}
return apierrors.NewInvalid(gk, in.Name, allErrs)
return warnings, apierrors.NewInvalid(gk, in.Name, allErrs)
}

return nil
return warnings, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *Network) ValidateDelete() error {
func (in *Network) ValidateDelete() (admission.Warnings, error) {
networklog.Info("validate delete", "name", in.Name)

var allErrs field.ErrorList
var warnings admission.Warnings

if len(in.Status.IPv4Ranges) > 0 {
allErrs = append(allErrs, field.InternalError(field.NewPath("metadata.name"), errors.New("Network has active IPv4 subnets")))
allErrs = append(allErrs, field.InternalError(
field.NewPath("metadata.name"), errors.New("Network has active IPv4 subnets")))
}

if len(in.Status.IPv6Ranges) > 0 {
allErrs = append(allErrs, field.InternalError(field.NewPath("metadata.name"), errors.New("Network has active IPv6 subnets")))
allErrs = append(allErrs, field.InternalError(
field.NewPath("metadata.name"), errors.New("Network has active IPv6 subnets")))
}

if len(allErrs) > 0 {
return apierrors.NewInvalid(
return warnings, apierrors.NewInvalid(
schema.GroupKind{
Group: GroupVersion.Group,
Kind: "Network",
}, in.Name, allErrs)
}

return nil
return warnings, nil
}

func (in *Network) validateID() *field.Error {
Expand Down
24 changes: 14 additions & 10 deletions api/v1alpha1/networkcounter_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
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"
)

// log is for logging in this package.
Expand All @@ -39,26 +40,29 @@ func (in *NetworkCounter) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &NetworkCounter{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *NetworkCounter) ValidateCreate() error {
func (in *NetworkCounter) ValidateCreate() (admission.Warnings, error) {
networkcounterlog.Info("validate create", "name", in.Name)
return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *NetworkCounter) ValidateUpdate(_ runtime.Object) error {
func (in *NetworkCounter) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
networkcounterlog.Info("validate update", "name", in.Name)
return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *NetworkCounter) ValidateDelete() error {
func (in *NetworkCounter) ValidateDelete() (admission.Warnings, error) {
networkcounterlog.Info("validate delete", "name", in.Name)

var allErrs field.ErrorList
var warnings admission.Warnings

if len(in.Spec.Vacant) == 0 {
allErrs = append(allErrs, field.InternalError(field.NewPath("metadata.name"), errors.New("Network Counter is still in use by networks")))
return apierrors.NewInvalid(
allErrs = append(allErrs, field.InternalError(
field.NewPath("metadata.name"),
errors.New("Network Counter is still in use by networks")))
return warnings, apierrors.NewInvalid(
schema.GroupKind{
Group: GroupVersion.Group,
Kind: "NetworkCounter",
Expand All @@ -69,15 +73,15 @@ func (in *NetworkCounter) ValidateDelete() error {
end := in.Spec.Vacant[0].End

if end == nil && begin.Eq(CMPLSFirstAvailableID) {
return nil
return warnings, nil
}

if begin.Eq(CVXLANFirstAvaliableID) && end.Eq(CVXLANMaxID) {
return nil
return warnings, nil
}

allErrs = append(allErrs, field.InternalError(field.NewPath("metadata.name"), errors.New("Network Counter is still in use by networks")))
return apierrors.NewInvalid(
return warnings, apierrors.NewInvalid(
schema.GroupKind{
Group: GroupVersion.Group,
Kind: "NetworkCounter",
Expand Down
Loading

0 comments on commit 86b01c2

Please sign in to comment.