Skip to content

Commit

Permalink
chore: replace gatewayv1alpha2.HTTPRoute with gatewayv1beta1.HTTPRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Sep 6, 2022
1 parent d295d65 commit ed7317c
Show file tree
Hide file tree
Showing 41 changed files with 1,025 additions and 676 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ linters-settings:
alias: gateway${1}
issues:
fix: true
max-same-issues: 0
exclude-rules:
- linters:
- ineffassign
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
- Added support for Gateway API's v1beta1 versions of: GatewayClass, Gateway
and HTTPRoute.
[#2889](https://github.com/Kong/kubernetes-ingress-controller/issues/2889)
[#2894](https://github.com/Kong/kubernetes-ingress-controller/issues/2894)

#### Fixed

Expand Down
3 changes: 2 additions & 1 deletion internal/admission/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

configuration "github.com/kong/kubernetes-ingress-controller/v2/pkg/apis/configuration/v1"
)
Expand Down Expand Up @@ -304,7 +305,7 @@ func (a RequestHandler) handleValidation(ctx context.Context, request admissionv
return nil, err
}
case httprouteGVResource:
httproute := gatewayv1alpha2.HTTPRoute{}
httproute := gatewayv1beta1.HTTPRoute{}
deserializer := codecs.UniversalDeserializer()
_, _, err = deserializer.Decode(request.Object.Raw, nil, &httproute)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/admission/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

configuration "github.com/kong/kubernetes-ingress-controller/v2/pkg/apis/configuration/v1"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func (v KongFakeValidator) ValidateGateway(ctx context.Context, gateway gatewayv
return v.Result, v.Message, v.Error
}

func (v KongFakeValidator) ValidateHTTPRoute(ctx context.Context, gateway gatewayv1alpha2.HTTPRoute) (bool, string, error) {
func (v KongFakeValidator) ValidateHTTPRoute(ctx context.Context, gateway gatewayv1beta1.HTTPRoute) (bool, string, error) {
return v.Result, v.Message, v.Error
}

Expand Down
4 changes: 2 additions & 2 deletions internal/admission/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type KongValidator interface {
ValidateClusterPlugin(ctx context.Context, plugin kongv1.KongClusterPlugin) (bool, string, error)
ValidateCredential(ctx context.Context, secret corev1.Secret) (bool, string, error)
ValidateGateway(ctx context.Context, gateway gatewayv1alpha2.Gateway) (bool, string, error)
ValidateHTTPRoute(ctx context.Context, httproute gatewayv1alpha2.HTTPRoute) (bool, string, error)
ValidateHTTPRoute(ctx context.Context, httproute gatewayv1beta1.HTTPRoute) (bool, string, error)
}

// KongHTTPValidator implements KongValidator interface to validate Kong
Expand Down Expand Up @@ -324,7 +324,7 @@ func (validator KongHTTPValidator) ValidateGateway(
}

func (validator KongHTTPValidator) ValidateHTTPRoute(
ctx context.Context, httproute gatewayv1alpha2.HTTPRoute,
ctx context.Context, httproute gatewayv1beta1.HTTPRoute,
) (bool, string, error) {
// in order to be sure whether or not an HTTPRoute resource is managed by this
// controller we disallow references to Gateway resources that do not exist.
Expand Down
6 changes: 3 additions & 3 deletions internal/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (r *GatewayReconciler) gatewayHasMatchingGatewayClass(obj client.Object) bo
r.Log.Error(err, "could not retrieve gatewayclass", "gatewayclass", gateway.Spec.GatewayClassName)
return false
}
return gatewayClass.Spec.ControllerName == gatewayv1beta1.GatewayController(ControllerName)
return gatewayClass.Spec.ControllerName == ControllerName
}

// gatewayClassMatchesController is a watch predicate which filters out events for gatewayclasses which
Expand All @@ -158,7 +158,7 @@ func (r *GatewayReconciler) gatewayClassMatchesController(obj client.Object) boo
r.Log.Error(fmt.Errorf("unexpected object type in gatewayclass watch predicates"), "expected", "*gatewayv1beta1.GatewayClass", "found", reflect.TypeOf(obj))
return false
}
return gatewayClass.Spec.ControllerName == gatewayv1beta1.GatewayController(ControllerName)
return gatewayClass.Spec.ControllerName == ControllerName
}

// listGatewaysForGatewayClass is a watch predicate which finds all the gateway objects reference
Expand Down Expand Up @@ -291,7 +291,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
debug(log, gateway, "ensured object was removed from the data-plane (if ever present)")
return ctrl.Result{}, r.DataplaneClient.DeleteObject(gateway)
}
if gwc.Spec.ControllerName != gatewayv1beta1.GatewayController(ControllerName) {
if gwc.Spec.ControllerName != ControllerName {
debug(log, gateway, "unsupported gatewayclass controllername, ignoring", "gatewayclass", gwc.Name, "controllername", gwc.Spec.ControllerName)
if err := r.DataplaneClient.DeleteObject(gateway); err != nil {
debug(log, gateway, "failed to delete object from data-plane, requeuing")
Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/gateway/gateway_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func Test_reconcileGatewaysIfClassMatches(t *testing.T) {
Name: "us",
},
Spec: gatewayv1beta1.GatewayClassSpec{
ControllerName: gatewayv1beta1.GatewayController(ControllerName),
ControllerName: ControllerName,
},
}

Expand Down Expand Up @@ -331,7 +331,7 @@ func Test_isGatewayControlledAndUnmanagedMode(t *testing.T) {
Name: "us",
},
Spec: gatewayv1beta1.GatewayClassSpec{
ControllerName: gatewayv1beta1.GatewayController(ControllerName),
ControllerName: ControllerName,
},
}

Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/gateway/gateway_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func isGatewayReady(gateway *gatewayv1alpha2.Gateway) bool {
// is controlled by this controller and the gateway is configured for unmanaged mode.
func isGatewayInClassAndUnmanaged(gatewayClass *gatewayv1beta1.GatewayClass, gateway gatewayv1alpha2.Gateway) bool {
_, ok := annotations.ExtractUnmanagedGatewayMode(gateway.Annotations)
return ok && gatewayClass.Spec.ControllerName == gatewayv1beta1.GatewayController(ControllerName)
return ok && gatewayClass.Spec.ControllerName == ControllerName
}

// getRefFromPublishService splits a publish service string in the format namespace/name into a types.NamespacedName
Expand Down Expand Up @@ -553,7 +553,7 @@ func isGatewayClassEventInClass(log logr.Logger, watchEvent interface{}) bool {
log.Error(fmt.Errorf("invalid type"), "received invalid object type in event handlers", "expected", "GatewayClass", "found", reflect.TypeOf(obj))
continue
}
if gwc.Spec.ControllerName == gatewayv1beta1.GatewayController(ControllerName) {
if gwc.Spec.ControllerName == ControllerName {
return true
}
}
Expand Down
5 changes: 2 additions & 3 deletions internal/controllers/gateway/gatewayclass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/kong/kubernetes-ingress-controller/v2/internal/util"
Expand All @@ -27,7 +26,7 @@ const (
// ControllerName is the unique identifier for this controller and is used
// within GatewayClass resources to indicate that this controller should
// support connected Gateway resources.
ControllerName gatewayv1alpha2.GatewayController = "konghq.com/kic-gateway-controller"
ControllerName gatewayv1beta1.GatewayController = "konghq.com/kic-gateway-controller"
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -80,7 +79,7 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
}
log.V(util.DebugLevel).Info("processing gatewayclass", "name", req.Name)

if gwc.Spec.ControllerName == gatewayv1beta1.GatewayController(ControllerName) {
if gwc.Spec.ControllerName == ControllerName {
alreadyAccepted := false
for _, cond := range gwc.Status.Conditions {
if cond.Reason == string(gatewayv1beta1.GatewayClassConditionStatusAccepted) {
Expand Down
58 changes: 31 additions & 27 deletions internal/controllers/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
// data-plane config for an HTTPRoute if it somehow becomes disconnected from
// a supported Gateway and GatewayClass.
return c.Watch(
&source.Kind{Type: &gatewayv1alpha2.HTTPRoute{}},
&source.Kind{Type: &gatewayv1beta1.HTTPRoute{}},
&handler.EnqueueRequestForObject{},
)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForGatewayClass(obj client.Object) [
}

// map all HTTPRoute objects
httprouteList := gatewayv1alpha2.HTTPRouteList{}
httprouteList := gatewayv1beta1.HTTPRouteList{}
if err := r.Client.List(context.Background(), &httprouteList); err != nil {
r.Log.Error(err, "failed to list httproute objects from the cached client")
return nil
Expand Down Expand Up @@ -199,7 +199,7 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForGateway(obj client.Object) []reco
}

// map all HTTPRoute objects
httprouteList := gatewayv1alpha2.HTTPRouteList{}
httprouteList := gatewayv1beta1.HTTPRouteList{}
if err := r.Client.List(context.Background(), &httprouteList); err != nil {
r.Log.Error(err, "failed to list httproute objects from the cached client")
return nil
Expand Down Expand Up @@ -239,7 +239,7 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForGateway(obj client.Object) []reco
func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("NetV1Alpha2HTTPRoute", req.NamespacedName)

httproute := new(gatewayv1alpha2.HTTPRoute)
httproute := new(gatewayv1beta1.HTTPRoute)
if err := r.Get(ctx, req.NamespacedName, httproute); err != nil {
// if the queued object is no longer present in the proxy cache we need
// to ensure that if it was ever added to the cache, it gets removed.
Expand Down Expand Up @@ -366,9 +366,9 @@ var httprouteParentKind = "Gateway"
// ensureGatewayReferenceStatus takes any number of Gateways that should be
// considered "attached" to a given HTTPRoute and ensures that the status
// for the HTTPRoute is updated appropriately.
func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Context, httproute *gatewayv1alpha2.HTTPRoute, gateways ...supportedGatewayWithCondition) (bool, error) {
func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Context, httproute *gatewayv1beta1.HTTPRoute, gateways ...supportedGatewayWithCondition) (bool, error) {
// map the existing parentStatues to avoid duplications
parentStatuses := make(map[string]*gatewayv1alpha2.RouteParentStatus)
parentStatuses := make(map[string]*gatewayv1beta1.RouteParentStatus)
for _, existingParent := range httproute.Status.Parents {
namespace := httproute.Namespace
if existingParent.ParentRef.Namespace != nil {
Expand All @@ -386,12 +386,12 @@ func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Cont
statusChangesWereMade := false
for _, gateway := range gateways {
// build a new status for the parent Gateway
gatewayParentStatus := &gatewayv1alpha2.RouteParentStatus{
ParentRef: gatewayv1alpha2.ParentReference{
Group: (*gatewayv1alpha2.Group)(&gatewayv1alpha2.GroupVersion.Group),
Kind: util.StringToGatewayAPIKindPtr(httprouteParentKind),
Namespace: (*gatewayv1alpha2.Namespace)(&gateway.gateway.Namespace),
Name: gatewayv1alpha2.ObjectName(gateway.gateway.Name),
gatewayParentStatus := &gatewayv1beta1.RouteParentStatus{
ParentRef: gatewayv1beta1.ParentReference{
Group: (*gatewayv1beta1.Group)(&gatewayv1beta1.GroupVersion.Group),
Kind: util.StringToGatewayAPIKindV1Beta1Ptr(httprouteParentKind),
Namespace: (*gatewayv1beta1.Namespace)(&gateway.gateway.Namespace),
Name: gatewayv1beta1.ObjectName(gateway.gateway.Name),
},
ControllerName: ControllerName,
Conditions: []metav1.Condition{{
Expand All @@ -403,7 +403,7 @@ func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Cont
}},
}
if gateway.listenerName != "" {
gatewayParentStatus.ParentRef.SectionName = (*gatewayv1alpha2.SectionName)(pointer.StringPtr(gateway.listenerName))
gatewayParentStatus.ParentRef.SectionName = (*gatewayv1beta1.SectionName)(pointer.StringPtr(gateway.listenerName))
}

key := fmt.Sprintf("%s/%s/%s", gateway.gateway.Namespace, gateway.gateway.Name, gateway.listenerName)
Expand Down Expand Up @@ -438,7 +438,7 @@ func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Cont
}

// update the httproute status with the new status references
httproute.Status.Parents = make([]gatewayv1alpha2.RouteParentStatus, 0, len(parentStatuses))
httproute.Status.Parents = make([]gatewayv1beta1.RouteParentStatus, 0, len(parentStatuses))
for _, parent := range parentStatuses {
httproute.Status.Parents = append(httproute.Status.Parents, *parent)
}
Expand All @@ -455,9 +455,9 @@ func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusAdded(ctx context.Cont
// ensureGatewayReferenceStatusRemoved uses the ControllerName provided by the Gateway
// implementation to prune status references to Gateways supported by this controller
// in the provided HTTPRoute object.
func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusRemoved(ctx context.Context, httproute *gatewayv1alpha2.HTTPRoute) (bool, error) {
func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusRemoved(ctx context.Context, httproute *gatewayv1beta1.HTTPRoute) (bool, error) {
// drop all status references to supported Gateway objects
newStatuses := make([]gatewayv1alpha2.RouteParentStatus, 0)
newStatuses := make([]gatewayv1beta1.RouteParentStatus, 0)
for _, status := range httproute.Status.Parents {
if status.ControllerName != ControllerName {
newStatuses = append(newStatuses, status)
Expand All @@ -481,14 +481,18 @@ func (r *HTTPRouteReconciler) ensureGatewayReferenceStatusRemoved(ctx context.Co
}

// setRouteConditionResolvedRefsCondition sets a condition of type ResolvedRefs on the route status.
func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(ctx context.Context, httpRoute *gatewayv1alpha2.HTTPRoute, parentStatuses map[string]*gatewayv1alpha2.RouteParentStatus) (map[string]*gatewayv1alpha2.RouteParentStatus, bool, error) {
func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(
ctx context.Context,
httpRoute *gatewayv1beta1.HTTPRoute,
parentStatuses map[string]*gatewayv1beta1.RouteParentStatus,
) (map[string]*gatewayv1beta1.RouteParentStatus, bool, error) {
var changed bool
resolvedRefsStatus := metav1.ConditionFalse
reason, err := r.getHTTPRouteRuleReason(ctx, *httpRoute)
if err != nil {
return nil, false, err
}
if reason == gatewayv1alpha2.RouteReasonResolvedRefs {
if reason == gatewayv1beta1.RouteReasonResolvedRefs {
resolvedRefsStatus = metav1.ConditionTrue
}

Expand All @@ -497,7 +501,7 @@ func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(ctx context
for _, parentStatus := range parentStatuses {
var conditionFound bool
for _, cond := range parentStatus.Conditions {
if cond.Type == string(gatewayv1alpha2.RouteConditionResolvedRefs) &&
if cond.Type == string(gatewayv1beta1.RouteConditionResolvedRefs) &&
cond.Status == resolvedRefsStatus &&
cond.Reason == string(reason) {
conditionFound = true
Expand All @@ -506,7 +510,7 @@ func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(ctx context
}
if !conditionFound {
parentStatus.Conditions = append(parentStatus.Conditions, metav1.Condition{
Type: string(gatewayv1alpha2.RouteConditionResolvedRefs),
Type: string(gatewayv1beta1.RouteConditionResolvedRefs),
Status: resolvedRefsStatus,
ObservedGeneration: httpRoute.Generation,
LastTransitionTime: metav1.Now(),
Expand All @@ -519,7 +523,7 @@ func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(ctx context
return parentStatuses, changed, nil
}

func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRoute gatewayv1alpha2.HTTPRoute) (gatewayv1alpha2.RouteConditionReason, error) {
func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRoute gatewayv1beta1.HTTPRoute) (gatewayv1beta1.RouteConditionReason, error) {
for _, rule := range httpRoute.Spec.Rules {
for _, backendRef := range rule.BackendRefs {
backendNamespace := httpRoute.Namespace
Expand All @@ -529,7 +533,7 @@ func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRo

// Check if the BackendRef GroupKind is supported
if !util.IsBackendRefGroupKindSupported(backendRef.Group, backendRef.Kind) {
return gatewayv1alpha2.RouteReasonInvalidKind, nil
return gatewayv1beta1.RouteReasonInvalidKind, nil
}

// Check if all the objects referenced actually exist
Expand All @@ -540,22 +544,22 @@ func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRo
if !k8serrors.IsNotFound(err) {
return "", err
}
return gatewayv1alpha2.RouteReasonBackendNotFound, nil
return gatewayv1beta1.RouteReasonBackendNotFound, nil
}

// Check if the object referenced is in another namespace,
// and if there is grant for that reference
if httpRoute.Namespace != backendNamespace {
if !r.EnableReferenceGrant {
return gatewayv1alpha2.RouteReasonRefNotPermitted, nil
return gatewayv1beta1.RouteReasonRefNotPermitted, nil
}

referenceGrantList := &gatewayv1alpha2.ReferenceGrantList{}
if err := r.Client.List(ctx, referenceGrantList, client.InNamespace(backendNamespace)); err != nil {
return "", err
}
if len(referenceGrantList.Items) == 0 {
return gatewayv1alpha2.RouteReasonRefNotPermitted, nil
return gatewayv1beta1.RouteReasonRefNotPermitted, nil
}
var isGranted bool
for _, grant := range referenceGrantList.Items {
Expand All @@ -565,10 +569,10 @@ func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRo
}
}
if !isGranted {
return gatewayv1alpha2.RouteReasonRefNotPermitted, nil
return gatewayv1beta1.RouteReasonRefNotPermitted, nil
}
}
}
}
return gatewayv1alpha2.RouteReasonResolvedRefs, nil
return gatewayv1beta1.RouteReasonResolvedRefs, nil
}
Loading

0 comments on commit ed7317c

Please sign in to comment.