Skip to content

Commit

Permalink
moved validating logic to webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Siryk authored and ribaraka committed Jan 11, 2024
1 parent 7d485b4 commit 2380abc
Show file tree
Hide file tree
Showing 16 changed files with 1,029 additions and 1,037 deletions.
25 changes: 0 additions & 25 deletions apis/clusterresources/v1beta1/azurevnetpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ limitations under the License.
package v1beta1

import (
"fmt"
"regexp"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -77,25 +74,3 @@ func (azure *AzureVNetPeering) NewPatch() client.Patch {
func init() {
SchemeBuilder.Register(&AzureVNetPeering{}, &AzureVNetPeeringList{})
}

func (azure *AzureVNetPeeringSpec) Validate() error {
dataCentreIDMatched, err := regexp.Match(models.UUIDStringRegExp, []byte(azure.DataCentreID))
if err != nil {
return err
}
if !dataCentreIDMatched {
return fmt.Errorf("data centre ID is a UUID formated string. It must fit the pattern: %s", models.UUIDStringRegExp)
}

for _, subnet := range azure.PeerSubnets {
peerSubnetMatched, err := regexp.Match(models.PeerSubnetsRegExp, []byte(subnet))
if err != nil {
return err
}
if !peerSubnetMatched {
return fmt.Errorf("the provided CIDR: %s must contain four dot separated parts and form the Private IP address. All bits in the host part of the CIDR must be 0. Suffix must be between 16-28", subnet)
}
}

return nil
}
23 changes: 23 additions & 0 deletions apis/clusterresources/v1beta1/azurevnetpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta1

import (
"fmt"
"regexp"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -105,3 +106,25 @@ func (r *AzureVNetPeering) ValidateDelete() error {
// TODO(user): fill in your validation logic upon object deletion.
return nil
}

func (azure *AzureVNetPeeringSpec) Validate() error {
dataCentreIDMatched, err := regexp.Match(models.UUIDStringRegExp, []byte(azure.DataCentreID))
if err != nil {
return err
}
if !dataCentreIDMatched {
return fmt.Errorf("data centre ID is a UUID formated string. It must fit the pattern: %s", models.UUIDStringRegExp)
}

for _, subnet := range azure.PeerSubnets {
peerSubnetMatched, err := regexp.Match(models.PeerSubnetsRegExp, []byte(subnet))
if err != nil {
return err
}
if !peerSubnetMatched {
return fmt.Errorf("the provided CIDR: %s must contain four dot separated parts and form the Private IP address. All bits in the host part of the CIDR must be 0. Suffix must be between 16-28", subnet)
}
}

return nil
}
14 changes: 0 additions & 14 deletions apis/clusterresources/v1beta1/exclusionwindow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,3 @@ func (r *ExclusionWindow) NewPatch() client.Patch {
old := r.DeepCopy()
return client.MergeFrom(old)
}

func (e *ExclusionWindowSpec) validateUpdate(old ExclusionWindowSpec) bool {
if e.DayOfWeek != old.DayOfWeek ||
e.ClusterID != old.ClusterID ||
e.DurationInHours != old.DurationInHours ||
e.StartHour != old.StartHour ||
(e.ClusterRef != nil && old.ClusterRef == nil) ||
(e.ClusterRef == nil && old.ClusterRef != nil) ||
(e.ClusterRef != nil && *e.ClusterRef != *old.ClusterRef) {
return false
}

return true
}
14 changes: 14 additions & 0 deletions apis/clusterresources/v1beta1/exclusionwindow_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ func (r *ExclusionWindow) ValidateDelete() error {
exclusionwindowlog.Info("validate delete", "name", r.Name)
return nil
}

func (e *ExclusionWindowSpec) validateUpdate(old ExclusionWindowSpec) bool {
if e.DayOfWeek != old.DayOfWeek ||
e.ClusterID != old.ClusterID ||
e.DurationInHours != old.DurationInHours ||
e.StartHour != old.StartHour ||
(e.ClusterRef != nil && old.ClusterRef == nil) ||
(e.ClusterRef == nil && old.ClusterRef != nil) ||
(e.ClusterRef != nil && *e.ClusterRef != *old.ClusterRef) {
return false
}

return true
}
25 changes: 0 additions & 25 deletions apis/clusterresources/v1beta1/gcpvpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ limitations under the License.
package v1beta1

import (
"fmt"
"regexp"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -75,25 +72,3 @@ func (gcp *GCPVPCPeering) NewPatch() client.Patch {
func init() {
SchemeBuilder.Register(&GCPVPCPeering{}, &GCPVPCPeeringList{})
}

func (gcp *GCPVPCPeeringSpec) Validate() error {
dataCentreIDMatched, err := regexp.Match(models.UUIDStringRegExp, []byte(gcp.DataCentreID))
if err != nil {
return err
}
if !dataCentreIDMatched {
return fmt.Errorf("data centre ID is a UUID formated string. It must fit the pattern: %s", models.UUIDStringRegExp)
}

for _, subnet := range gcp.PeerSubnets {
peerSubnetMatched, err := regexp.Match(models.PeerSubnetsRegExp, []byte(subnet))
if err != nil {
return err
}
if !peerSubnetMatched {
return fmt.Errorf("the provided CIDR: %s must contain four dot separated parts and form the Private IP address. All bits in the host part of the CIDR must be 0. Suffix must be between 16-28", subnet)
}
}

return nil
}
23 changes: 23 additions & 0 deletions apis/clusterresources/v1beta1/gcpvpcpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta1

import (
"fmt"
"regexp"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -101,3 +102,25 @@ func (r *GCPVPCPeering) ValidateDelete() error {
// TODO(user): fill in your validation logic upon object deletion.
return nil
}

func (gcp *GCPVPCPeeringSpec) Validate() error {
dataCentreIDMatched, err := regexp.Match(models.UUIDStringRegExp, []byte(gcp.DataCentreID))
if err != nil {
return err
}
if !dataCentreIDMatched {
return fmt.Errorf("data centre ID is a UUID formated string. It must fit the pattern: %s", models.UUIDStringRegExp)
}

for _, subnet := range gcp.PeerSubnets {
peerSubnetMatched, err := regexp.Match(models.PeerSubnetsRegExp, []byte(subnet))
if err != nil {
return err
}
if !peerSubnetMatched {
return fmt.Errorf("the provided CIDR: %s must contain four dot separated parts and form the Private IP address. All bits in the host part of the CIDR must be 0. Suffix must be between 16-28", subnet)
}
}

return nil
}
Loading

0 comments on commit 2380abc

Please sign in to comment.