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 NKG-specific field validation for Gateways #407

Merged
merged 9 commits into from
Feb 9, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement NKG-specific field validation for Gateway resource
Fixes #406
pleshakov committed Feb 2, 2023
commit 17fce8819868c87245135fdc02aa5e46d2c3c5d7
52 changes: 32 additions & 20 deletions internal/state/change_processor_test.go
Original file line number Diff line number Diff line change
@@ -286,12 +286,18 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: false,
AttachedRoutes: 1,
Conditions: append(
conditions.NewDefaultListenerConditions(),
conditions.NewTODO("GatewayClass is invalid or doesn't exist"),
),
},
"listener-443-1": {
Valid: false,
AttachedRoutes: 1,
Conditions: append(
conditions.NewDefaultListenerConditions(),
conditions.NewTODO("GatewayClass is invalid or doesn't exist"),
),
},
},
},
@@ -391,12 +397,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -500,12 +506,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -610,12 +616,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1Updated.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -719,12 +725,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1Updated.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -827,12 +833,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1Updated.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -928,12 +934,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw1Updated.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -1049,12 +1055,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw2.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 1,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -1113,12 +1119,12 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw2.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: true,
AttachedRoutes: 0,
Conditions: conditions.NewDefaultListenerConditions(),
},
"listener-443-1": {
Valid: true,
AttachedRoutes: 0,
Conditions: conditions.NewDefaultListenerConditions(),
},
},
},
@@ -1146,12 +1152,18 @@ var _ = Describe("ChangeProcessor", func() {
ObservedGeneration: gw2.Generation,
ListenerStatuses: map[string]state.ListenerStatus{
"listener-80-1": {
Valid: false,
AttachedRoutes: 0,
Conditions: append(
conditions.NewDefaultListenerConditions(),
conditions.NewTODO("GatewayClass is invalid or doesn't exist"),
),
},
"listener-443-1": {
Valid: false,
AttachedRoutes: 0,
Conditions: append(
conditions.NewDefaultListenerConditions(),
conditions.NewTODO("GatewayClass is invalid or doesn't exist"),
),
},
},
},
140 changes: 130 additions & 10 deletions internal/state/conditions/conditions.go
Original file line number Diff line number Diff line change
@@ -7,8 +7,13 @@ import (
"sigs.k8s.io/gateway-api/apis/v1beta1"
)

// RouteReasonInvalidListener is used with the "Accepted" condition when the route references an invalid listener.
const RouteReasonInvalidListener v1beta1.RouteConditionReason = "InvalidListener"
const (
// RouteReasonInvalidListener is used with the "Accepted" condition when the route references an invalid listener.
RouteReasonInvalidListener v1beta1.RouteConditionReason = "InvalidListener"
// ListenerReasonUnsupportedValue is used with the "Accepted" condition when a value of a field in a Listener
// is invalid or not supported.
ListenerReasonUnsupportedValue v1beta1.ListenerConditionReason = "UnsupportedValue"
)

// Condition defines a condition to be reported in the status of resources.
type Condition struct {
@@ -19,18 +24,32 @@ type Condition struct {
}

// DeduplicateConditions removes duplicate conditions based on the condition type.
// The last condition wins.
// The last condition wins. The order of conditions is preserved.
func DeduplicateConditions(conds []Condition) []Condition {
uniqueConds := make(map[string]Condition)
type elem struct {
cond Condition
reverseIdx int
}

uniqueElems := make(map[string]elem)

for _, cond := range conds {
uniqueConds[cond.Type] = cond
idx := 0
for i := len(conds) - 1; i >= 0; i-- {
if _, exist := uniqueElems[conds[i].Type]; exist {
continue
}

uniqueElems[conds[i].Type] = elem{
cond: conds[i],
reverseIdx: idx,
}
idx++
}

result := make([]Condition, 0, len(uniqueConds))
result := make([]Condition, len(uniqueElems))

for _, cond := range uniqueConds {
result = append(result, cond)
for _, el := range uniqueElems {
result[len(result)-el.reverseIdx-1] = el.cond
}

return result
@@ -81,6 +100,107 @@ func NewRouteInvalidListener() Condition {
Type: string(v1beta1.RouteConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(RouteReasonInvalidListener),
Message: "The listener is invalid for this parent ref",
Message: "Listener is invalid for this parent ref",
}
}

// NewListenerPortUnavailable returns a Condition that indicates a port is unavailable in a Listener.
func NewListenerPortUnavailable(msg string) Condition {
return Condition{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonPortUnavailable),
Message: msg,
}
}

// NewDefaultListenerConditions returns the default Conditions that must be present in the status of a Listener.
func NewDefaultListenerConditions() []Condition {
return []Condition{
{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionTrue,
Reason: string(v1beta1.ListenerReasonAccepted),
Message: "Listener is accepted",
},
{
Type: string(v1beta1.ListenerReasonResolvedRefs),
Status: metav1.ConditionTrue,
Reason: string(v1beta1.ListenerReasonResolvedRefs),
Message: "All references are resolved",
},
{
Type: string(v1beta1.ListenerConditionConflicted),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonNoConflicts),
Message: "No conflicts",
},
}
}

// NewListenerUnsupportedValue returns a Condition that indicates that a field of a Listener has an unsupported value.
// Unsupported means that the value is not supported by the implementation or invalid.
func NewListenerUnsupportedValue(msg string) Condition {
return Condition{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(ListenerReasonUnsupportedValue),
Message: msg,
}
}

// NewListenerInvalidCertificateRef returns Conditions that indicate that a CertificateRef of a Listener is invalid.
func NewListenerInvalidCertificateRef(msg string) []Condition {
return []Condition{
{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonInvalidCertificateRef),
Message: msg,
},
{
Type: string(v1beta1.ListenerReasonResolvedRefs),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonInvalidCertificateRef),
Message: msg,
},
}
}

// NewListenerConflictedHostname returns Conditions that indicate that a hostname of a Listener is conflicted.
func NewListenerConflictedHostname(msg string) []Condition {
return []Condition{
{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonHostnameConflict),
Message: msg,
},
{
Type: string(v1beta1.ListenerConditionConflicted),
Status: metav1.ConditionTrue,
Reason: string(v1beta1.ListenerReasonHostnameConflict),
Message: msg,
},
}
}

// NewListenerUnsupportedAddress returns a Condition that indicates that the address of a Listener is unsupported.
func NewListenerUnsupportedAddress(msg string) Condition {
return Condition{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonUnsupportedAddress),
Message: msg,
}
}

// NewListenerUnsupportedProtocol returns a Condition that indicates that the protocol of a Listener is unsupported.
func NewListenerUnsupportedProtocol(msg string) Condition {
return Condition{
Type: string(v1beta1.ListenerConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(v1beta1.ListenerReasonUnsupportedProtocol),
Message: msg,
}
}
42 changes: 25 additions & 17 deletions internal/state/conditions/conditions_test.go
Original file line number Diff line number Diff line change
@@ -12,42 +12,50 @@ func TestDeduplicateDeduplicateRouteConditions(t *testing.T) {

conds := []Condition{
{
Type: "Type1",
Status: metav1.ConditionTrue,
Type: "Type1",
Status: metav1.ConditionTrue,
Message: "0",
},
{
Type: "Type1",
Status: metav1.ConditionFalse,
Type: "Type1",
Status: metav1.ConditionFalse,
Message: "1",
},
{
Type: "Type2",
Status: metav1.ConditionFalse,
Type: "Type2",
Status: metav1.ConditionFalse,
Message: "2",
},
{
Type: "Type2",
Status: metav1.ConditionTrue,
Type: "Type2",
Status: metav1.ConditionTrue,
Message: "3",
},
{
Type: "Type3",
Status: metav1.ConditionTrue,
Type: "Type3",
Status: metav1.ConditionTrue,
Message: "4",
},
}

expected := []Condition{
{
Type: "Type1",
Status: metav1.ConditionFalse,
Type: "Type1",
Status: metav1.ConditionFalse,
Message: "1",
},
{
Type: "Type2",
Status: metav1.ConditionTrue,
Type: "Type2",
Status: metav1.ConditionTrue,
Message: "3",
},
{
Type: "Type3",
Status: metav1.ConditionTrue,
Type: "Type3",
Status: metav1.ConditionTrue,
Message: "4",
},
}

result := DeduplicateConditions(conds)
g.Expect(result).Should(ConsistOf(expected))
g.Expect(result).Should(Equal(expected))
}
Loading