-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set GWC accepted cond accordingly
The GWC accepted condition is now properly set to False with reason InvalidParameters in case the ParametersRef field of the GatewayClass spec is not a valid GatewayConfiguration. Signed-off-by: Mattia Lavacca <[email protected]>
- Loading branch information
Showing
12 changed files
with
339 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package gatewayclass | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
||
operatorv1beta1 "github.com/kong/gateway-operator/api/v1beta1" | ||
"github.com/kong/gateway-operator/pkg/consts" | ||
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes" | ||
) | ||
|
||
// getAcceptedCondition returns the accepted condition for the GatewayClass, with | ||
// the proper status, reason and message. | ||
func getAcceptedCondition(ctx context.Context, cl client.Client, gwc *gatewayv1.GatewayClass) (*metav1.Condition, error) { | ||
reason := string(gatewayv1.GatewayClassReasonAccepted) | ||
message := []string{} | ||
status := metav1.ConditionFalse | ||
|
||
if gwc.Spec.ParametersRef != nil { | ||
validRef := true | ||
if gwc.Spec.ParametersRef.Group != gatewayv1.Group(operatorv1beta1.SchemeGroupVersion.Group) || | ||
gwc.Spec.ParametersRef.Kind != "GatewayConfiguration" { | ||
reason = string(gatewayv1.GatewayClassReasonInvalidParameters) | ||
message = append(message, "ParametersRef must reference a gateway-operator.konghq.com/GatewayConfiguration") | ||
validRef = false | ||
} | ||
|
||
if gwc.Spec.ParametersRef.Namespace == nil { | ||
reason = string(gatewayv1.GatewayClassReasonInvalidParameters) | ||
message = append(message, "ParametersRef must reference a namespaced resource") | ||
validRef = false | ||
} | ||
|
||
if validRef { | ||
gatewayConfig := operatorv1beta1.GatewayConfiguration{} | ||
err := cl.Get(ctx, client.ObjectKey{Name: gwc.Spec.ParametersRef.Name, Namespace: string(*gwc.Spec.ParametersRef.Namespace)}, &gatewayConfig) | ||
if client.IgnoreNotFound(err) != nil { | ||
return nil, err | ||
} | ||
if k8serrors.IsNotFound(err) { | ||
reason = string(gatewayv1.GatewayClassReasonInvalidParameters) | ||
message = append(message, "The referenced GatewayConfiguration does not exist") | ||
} | ||
} | ||
} | ||
if reason == string(gatewayv1.GatewayClassReasonAccepted) { | ||
status = metav1.ConditionTrue | ||
message = []string{"GatewayClass is accepted"} | ||
} | ||
|
||
acceptedCondition := k8sutils.NewConditionWithGeneration( | ||
consts.ConditionType(gatewayv1.GatewayClassConditionStatusAccepted), | ||
status, | ||
consts.ConditionReason(reason), | ||
strings.Join(message, ". "), | ||
gwc.GetGeneration(), | ||
) | ||
|
||
return &acceptedCondition, nil | ||
} |
131 changes: 131 additions & 0 deletions
131
controller/gatewayclass/controller_reconciler_utils_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package gatewayclass | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
||
operatorv1beta1 "github.com/kong/gateway-operator/api/v1beta1" | ||
) | ||
|
||
func TestGetAcceptedCondition(t *testing.T) { | ||
scheme := runtime.NewScheme() | ||
assert.NoError(t, gatewayv1.Install(scheme)) | ||
assert.NoError(t, operatorv1beta1.AddToScheme(scheme)) | ||
|
||
tests := []struct { | ||
name string | ||
gwc *gatewayv1.GatewayClass | ||
existingObjs []runtime.Object | ||
expectedStatus metav1.ConditionStatus | ||
expectedReason string | ||
expectedMsg string | ||
}{ | ||
{ | ||
name: "ParametersRef is nil", | ||
gwc: &gatewayv1.GatewayClass{ | ||
Spec: gatewayv1.GatewayClassSpec{ | ||
ParametersRef: nil, | ||
}, | ||
}, | ||
expectedStatus: metav1.ConditionTrue, | ||
expectedReason: string(gatewayv1.GatewayClassReasonAccepted), | ||
expectedMsg: "GatewayClass is accepted", | ||
}, | ||
{ | ||
name: "Invalid ParametersRef Group and kind", | ||
gwc: &gatewayv1.GatewayClass{ | ||
Spec: gatewayv1.GatewayClassSpec{ | ||
|
||
ParametersRef: &gatewayv1.ParametersReference{ | ||
Group: "invalid.group", | ||
Kind: "InvalidKind", | ||
Namespace: lo.ToPtr(gatewayv1.Namespace("default")), | ||
Name: "invalid", | ||
}, | ||
}, | ||
}, | ||
expectedStatus: metav1.ConditionFalse, | ||
expectedReason: string(gatewayv1.GatewayClassReasonInvalidParameters), | ||
expectedMsg: "ParametersRef must reference a gateway-operator.konghq.com/GatewayConfiguration", | ||
}, | ||
{ | ||
name: "ParametersRef Namespace is nil", | ||
gwc: &gatewayv1.GatewayClass{ | ||
Spec: gatewayv1.GatewayClassSpec{ | ||
ParametersRef: &gatewayv1.ParametersReference{ | ||
Group: gatewayv1.Group(operatorv1beta1.SchemeGroupVersion.Group), | ||
Kind: "GatewayConfiguration", | ||
Name: "no-namespace", | ||
}, | ||
}, | ||
}, | ||
expectedStatus: metav1.ConditionFalse, | ||
expectedReason: string(gatewayv1.GatewayClassReasonInvalidParameters), | ||
expectedMsg: "ParametersRef must reference a namespaced resource", | ||
}, | ||
{ | ||
name: "GatewayConfiguration does not exist", | ||
gwc: &gatewayv1.GatewayClass{ | ||
Spec: gatewayv1.GatewayClassSpec{ | ||
ParametersRef: &gatewayv1.ParametersReference{ | ||
Group: gatewayv1.Group(operatorv1beta1.SchemeGroupVersion.Group), | ||
Kind: "GatewayConfiguration", | ||
Name: "nonexistent", | ||
Namespace: lo.ToPtr(gatewayv1.Namespace("default")), | ||
}, | ||
}, | ||
}, | ||
expectedStatus: metav1.ConditionFalse, | ||
expectedReason: string(gatewayv1.GatewayClassReasonInvalidParameters), | ||
expectedMsg: "The referenced GatewayConfiguration does not exist", | ||
}, | ||
{ | ||
name: "Valid ParametersRef", | ||
gwc: &gatewayv1.GatewayClass{ | ||
Spec: gatewayv1.GatewayClassSpec{ | ||
ParametersRef: &gatewayv1.ParametersReference{ | ||
Group: gatewayv1.Group(operatorv1beta1.SchemeGroupVersion.Group), | ||
Kind: "GatewayConfiguration", | ||
Name: "valid-config", | ||
Namespace: lo.ToPtr(gatewayv1.Namespace("default")), | ||
}, | ||
}, | ||
}, | ||
existingObjs: []runtime.Object{ | ||
&operatorv1beta1.GatewayConfiguration{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "valid-config", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
expectedStatus: metav1.ConditionTrue, | ||
expectedReason: string(gatewayv1.GatewayClassReasonAccepted), | ||
expectedMsg: "GatewayClass is accepted", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.TODO() | ||
cl := fake.NewClientBuilder(). | ||
WithScheme(scheme). | ||
WithRuntimeObjects(tt.existingObjs...). | ||
Build() | ||
|
||
condition, err := getAcceptedCondition(ctx, cl, tt.gwc) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, condition) | ||
assert.Equal(t, tt.expectedStatus, condition.Status) | ||
assert.Equal(t, tt.expectedReason, condition.Reason) | ||
assert.Equal(t, tt.expectedMsg, condition.Message) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.