Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AliasPrefix controller #407

Merged
merged 1 commit into from
May 9, 2022
Merged
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
4 changes: 2 additions & 2 deletions apis/networking/aliasprefix_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type AliasPrefixSpec struct {
// NetworkInterfaceSelector defines the NetworkInterfaces
// for which this AliasPrefix should be applied
NetworkInterfaceSelector *metav1.LabelSelector
// Prefix is the provided Prefix or EphemeralPrefix which
// Prefix is the provided Prefix or Ephemeral which
// should be used by this AliasPrefix
Prefix PrefixSource
}
Expand All @@ -39,7 +39,7 @@ type PrefixSource struct {
// Value is a single IPPrefix value as defined in the AliasPrefix
Value *commonv1alpha1.IPPrefix
// EphemeralPrefix defines the Prefix which should be allocated by the AliasPrefix
EphemeralPrefix *EphemeralPrefixSource
Ephemeral *EphemeralPrefixSource
}

// AliasPrefixStatus defines the observed state of AliasPrefix
Expand Down
15 changes: 2 additions & 13 deletions apis/networking/aliasprefixrouting_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@ package networking

import (
commonv1alpha1 "github.com/onmetal/onmetal-api/apis/common/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// AliasPrefixSubset is one of the targets of a AliasPrefixRouting
type AliasPrefixSubset struct {
// MachinePoolRef is the machine pool hosting the targeted entities.
MachinePoolRef commonv1alpha1.LocalUIDReference
// Targets are the entities targeted by the alias prefix routing.
Targets []commonv1alpha1.LocalUIDReference
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand All @@ -38,10 +29,8 @@ type AliasPrefixRouting struct {
metav1.TypeMeta
metav1.ObjectMeta

// NetworkRef is the Network this AliasPrefixRouting should belong to
NetworkRef corev1.LocalObjectReference
// Subsets are the subsets that make up an AliasPrefixRouting
Subsets []AliasPrefixSubset
// Destinations are the destinations for an AliasPrefix.
Destinations []commonv1alpha1.LocalUIDReference
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
6 changes: 3 additions & 3 deletions apis/networking/v1alpha1/aliasprefix_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type AliasPrefixSpec struct {
// NetworkInterfaceSelector defines the NetworkInterfaces
// for which this AliasPrefix should be applied
NetworkInterfaceSelector *metav1.LabelSelector `json:"networkInterfaceSelector,omitempty"`
// Prefix is the provided Prefix or EphemeralPrefix which
// Prefix is the provided Prefix or Ephemeral which
// should be used by this AliasPrefix
Prefix PrefixSource `json:"prefix,omitempty"`
}
Expand All @@ -38,8 +38,8 @@ type AliasPrefixSpec struct {
type PrefixSource struct {
// Value is a single IPPrefix value as defined in the AliasPrefix
Value *commonv1alpha1.IPPrefix `json:"value,omitempty"`
// EphemeralPrefix defines the Prefix which should be allocated by the AliasPrefix
EphemeralPrefix *EphemeralPrefixSource `json:"ephemeralPrefix,omitempty"`
// Ephemeral defines the Prefix which should be allocated by the AliasPrefix
Ephemeral *EphemeralPrefixSource `json:"ephemeral,omitempty"`
}

// AliasPrefixStatus defines the observed state of AliasPrefix
Expand Down
15 changes: 2 additions & 13 deletions apis/networking/v1alpha1/aliasprefixrouting_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@ package v1alpha1

import (
commonv1alpha1 "github.com/onmetal/onmetal-api/apis/common/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// AliasPrefixRoutingSubset is one of the targets of a AliasPrefixRouting
type AliasPrefixRoutingSubset struct {
// MachinePoolRef is the machine pool hosting the targeted entities.
MachinePoolRef commonv1alpha1.LocalUIDReference `json:"machinePoolRef"`
// Targets are the entities targeted by the alias prefix routing.
Targets []commonv1alpha1.LocalUIDReference `json:"targets"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand All @@ -38,10 +29,8 @@ type AliasPrefixRouting struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// NetworkRef is the Network this AliasPrefixRouting should belong to
NetworkRef corev1.LocalObjectReference `json:"networkRef"`
// Subsets are the subsets that make up an AliasPrefixRouting
Subsets []AliasPrefixRoutingSubset `json:"subsets,omitempty"`
// Destinations are the destinations for an AliasPrefix.
Destinations []commonv1alpha1.LocalUIDReference `json:"destinations"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
10 changes: 4 additions & 6 deletions apis/networking/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 6 additions & 31 deletions apis/networking/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions apis/networking/v1alpha1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions apis/networking/validation/aliasprefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func validateAliasPrefixSpec(spec *networking.AliasPrefixSpec, fldPath *field.Pa
func validateAliasPrefixSources(prefixSource networking.PrefixSource, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

if prefixSource.EphemeralPrefix != nil && prefixSource.Value != nil {
allErrs = append(allErrs, field.Forbidden(fldPath, "only ephemeralPrefix or value should be provided"))
if prefixSource.Ephemeral != nil && prefixSource.Value != nil {
allErrs = append(allErrs, field.Forbidden(fldPath, "only ephemeral or value should be provided"))
}

if ephemeralPrefix := prefixSource.EphemeralPrefix; ephemeralPrefix != nil {
allErrs = append(allErrs, validateEphemeralAliasPrefixSource(prefixSource.EphemeralPrefix, fldPath.Child("ephemeralPrefix"))...)
if ephemeral := prefixSource.Ephemeral; ephemeral != nil {
allErrs = append(allErrs, validateEphemeralAliasPrefixSource(prefixSource.Ephemeral, fldPath.Child("ephemeral"))...)
}

allErrs = append(allErrs, validateValuePrefixSource(prefixSource.Value, fldPath.Child("prefixTemplate").Child("value"))...)
Expand Down Expand Up @@ -106,17 +106,17 @@ func ValidateAliasPrefixUpdate(newAliasPrefix, oldAliasPrefix *networking.AliasP
var allErrs field.ErrorList

allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessorUpdate(newAliasPrefix, oldAliasPrefix, field.NewPath("metadata"))...)
allErrs = append(allErrs, validateAliasPrefixUpdate(newAliasPrefix, oldAliasPrefix, field.NewPath("spec"))...)
allErrs = append(allErrs, validateAliasSpecPrefixUpdate(&newAliasPrefix.Spec, &oldAliasPrefix.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, ValidateAliasPrefix(newAliasPrefix)...)

return allErrs
}

// validateAliasPrefixUpdate validates the spec of a aliasPrefix object before an update.
func validateAliasPrefixUpdate(new, old *networking.AliasPrefix, fldPath *field.Path) field.ErrorList {
// validateAliasSpecPrefixUpdate validates the spec of a aliasPrefix object before an update.
func validateAliasSpecPrefixUpdate(newSpec, oldSpec *networking.AliasPrefixSpec, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

allErrs = append(allErrs, onmetalapivalidation.ValidateImmutableField(new, old, fldPath.Child("networkRef"))...)
allErrs = append(allErrs, onmetalapivalidation.ValidateImmutableField(newSpec.NetworkRef, oldSpec.NetworkRef, fldPath.Child("networkRef"))...)

return allErrs
}
8 changes: 4 additions & 4 deletions apis/networking/validation/aliasprefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ var _ = Describe("AliasPrefix", func() {
&networking.AliasPrefix{
Spec: networking.AliasPrefixSpec{
Prefix: networking.PrefixSource{
EphemeralPrefix: &networking.EphemeralPrefixSource{},
Ephemeral: &networking.EphemeralPrefixSource{},
},
},
},
ContainElement(RequiredField("spec.prefix.ephemeralPrefix.prefixTemplate")),
ContainElement(RequiredField("spec.prefix.ephemeral.prefixTemplate")),
),
Entry("ephemeral prefix and value are provided",
&networking.AliasPrefix{
Spec: networking.AliasPrefixSpec{
Prefix: networking.PrefixSource{
EphemeralPrefix: &networking.EphemeralPrefixSource{},
Value: v1alpha1.PtrToIPPrefix(v1alpha1.MustParseIPPrefix("10.0.0.0/24")),
Ephemeral: &networking.EphemeralPrefixSource{},
Value: v1alpha1.PtrToIPPrefix(v1alpha1.MustParseIPPrefix("10.0.0.0/24")),
},
},
},
Expand Down
74 changes: 9 additions & 65 deletions apis/networking/validation/aliasprefixrouting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package validation

import (
onmetalapivalidation "github.com/onmetal/onmetal-api/api/validation"
"github.com/onmetal/onmetal-api/apis/networking"
corev1 "k8s.io/api/core/v1"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand All @@ -31,75 +29,23 @@ func ValidateAliasPrefixRouting(aliasPrefixRouting *networking.AliasPrefixRoutin

allErrs = append(allErrs, apivalidation.ValidateObjectMetaAccessor(aliasPrefixRouting, true, apivalidation.NameIsDNSLabel, field.NewPath("metadata"))...)
allErrs = append(allErrs, validateAliasPrefixRouting(aliasPrefixRouting)...)
allErrs = append(allErrs, validateAliasPrefixRoutingSubsets(aliasPrefixRouting, field.NewPath("subsets"))...)

return allErrs
}

func validateAliasPrefixRouting(aliasPrefixRouting *networking.AliasPrefixRouting) field.ErrorList {
var allErrs field.ErrorList

if aliasPrefixRouting.NetworkRef == (corev1.LocalObjectReference{}) {
allErrs = append(allErrs, field.Required(field.NewPath("networkRef"), "must specify a network ref"))
} else {
for _, msg := range apivalidation.NameIsDNSLabel(aliasPrefixRouting.NetworkRef.Name, false) {
allErrs = append(allErrs, field.Invalid(field.NewPath("networkRef").Child("name"), aliasPrefixRouting.NetworkRef.Name, msg))
}
}

return allErrs
}

func validateAliasPrefixRoutingSubsets(aliasPrefixRouting *networking.AliasPrefixRouting, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

seenPoolNames := sets.NewString()
for idx := range aliasPrefixRouting.Subsets {
subset := &aliasPrefixRouting.Subsets[idx]

allErrs = append(allErrs, validateAliasPrefixRoutingSubset(subset, fldPath.Index(idx))...)

if seenPoolNames.Has(subset.MachinePoolRef.Name) {
allErrs = append(allErrs, field.Duplicate(fldPath.Index(idx), subset))
seenNames := sets.NewString()
for idx := range aliasPrefixRouting.Destinations {
destination := &aliasPrefixRouting.Destinations[idx]
destinationPath := field.NewPath("destinations").Index(idx)
if seenNames.Has(destination.Name) {
allErrs = append(allErrs, field.Duplicate(destinationPath, destination))
} else {
seenPoolNames.Insert(subset.MachinePoolRef.Name)
}
}

return allErrs
}

func validateAliasPrefixRoutingSubset(subset *networking.AliasPrefixSubset, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

if subset.MachinePoolRef.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("machinePoolRef", "name"), "must specify machine pool ref name"))
} else {
for _, msg := range apivalidation.NameIsDNSLabel(subset.MachinePoolRef.Name, false) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("machinePoolRef", "name"), subset.MachinePoolRef.Name, msg))
}
}

seenTargetNames := sets.NewString()
targetsPath := fldPath.Child("targets")
if len(subset.Targets) == 0 {
allErrs = append(allErrs, field.Required(targetsPath, "must specify at least 1 target"))
} else {
for i := range subset.Targets {
target := &subset.Targets[i]
targetPath := targetsPath.Index(i)
if target.Name == "" {
allErrs = append(allErrs, field.Required(targetPath.Child("name"), "must specify network interface ref name"))
} else {
for _, msg := range apivalidation.NameIsDNSLabel(target.Name, false) {
allErrs = append(allErrs, field.Invalid(targetPath.Child("name"), target.Name, msg))
}

if seenTargetNames.Has(target.Name) {
allErrs = append(allErrs, field.Duplicate(targetPath, target))
} else {
seenTargetNames.Insert(target.Name)
}
seenNames.Insert(destination.Name)
for _, msg := range apivalidation.NameIsDNSLabel(destination.Name, false) {
allErrs = append(allErrs, field.Invalid(destinationPath.Child("name"), destination.Name, msg))
}
}
}
Expand All @@ -122,7 +68,5 @@ func ValidateAliasPrefixRoutingUpdate(newAliasPrefixRouting, oldAliasPrefixRouti
func validateAliasPrefixRoutingUpdate(new, old *networking.AliasPrefixRouting) field.ErrorList {
var allErrs field.ErrorList

allErrs = append(allErrs, onmetalapivalidation.ValidateImmutableField(new, old, field.NewPath("networkRef"))...)

return allErrs
}
Loading