Skip to content

Commit

Permalink
Rebase update
Browse files Browse the repository at this point in the history
  • Loading branch information
ReToCode committed Mar 15, 2024
1 parent a8a050e commit cb21626
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 100 deletions.
4 changes: 2 additions & 2 deletions docs/test-version.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ The following Gateway API version and Ingress were tested as part of the release

| Ingress | Tested version | Unavailable features |
| ------- | ----------------------- | ------------------------------ |
| Istio | v1.20.3 | retry,httpoption,update |
| Contour | v1.28.1 | httpoption,update |
| Istio | v1.20.3 | retry,update |
| Contour | v1.28.1 | update |
2 changes: 1 addition & 1 deletion pkg/reconciler/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress
}

// For now, we only generate the redirected HTTPRoute for external visibility,
// because currently we do not support HTTPs on cluster-local domains.
// because currently we do not (yet) support HTTPs on cluster-local domains.
var redirectHTTPRoute *gatewayapi.HTTPRoute
if ing.Spec.HTTPOption == v1alpha1.HTTPOptionRedirected && rule.Visibility == v1alpha1.IngressVisibilityExternalIP {
redirectHTTPRoute, err = c.reconcileRedirectHTTPRoute(ctx, ing, &rule)
Expand Down
17 changes: 8 additions & 9 deletions pkg/reconciler/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,20 @@ func TestReconcileTLS(t *testing.T) {
Name: "TLS ingress with httpOption redirected",
Key: "ns/name",
Objects: append([]runtime.Object{
ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS(secretName)),
ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS()),
secret(secretName, nsName),
gw(defaultListener),
}, servicesAndEndpoints...),
WantCreates: []runtime.Object{
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS(secretName)), withSectionName("kni-")),
httpRedirectRoute(t, ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS(secretName)), withSectionName("http")),
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS()), withSectionName("kni-")),
httpRedirectRoute(t, ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS()), withSectionName("http")),
rp(secret(secretName, nsName)),
},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: gw(defaultListener, tlsListener("secure.example.com", nsName, secretName)),
Object: gw(defaultListener, tlsListener("example.com", nsName, secretName)),
}},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS(secretName), func(i *v1alpha1.Ingress) {
Object: ing(withBasicSpec, withGatewayAPIClass, withHTTPOptionRedirected, withTLS(), func(i *v1alpha1.Ingress) {
// These are the things we expect to change in status.
i.Status.InitializeConditions()
i.Status.MarkLoadBalancerReady(
Expand Down Expand Up @@ -540,8 +540,7 @@ func makeItReady(i *v1alpha1.Ingress) {
func httpRoute(t *testing.T, i *v1alpha1.Ingress, opts ...HTTPRouteOption) runtime.Object {
t.Helper()
ingress.InsertProbe(i)
ctx := (&testConfigStore{config: defaultConfig}).ToContext(context.Background())
httpRoute, _ := resources.MakeHTTPRoute(ctx, i, &i.Spec.Rules[0], gatewayRef)
httpRoute, _ := resources.MakeHTTPRoute(i, &i.Spec.Rules[0], gatewayRef)
for _, opt := range opts {
opt(httpRoute)
}
Expand Down Expand Up @@ -708,8 +707,8 @@ func rp(to *corev1.Secret) *gatewayapi.ReferenceGrant {
Namespace: gatewayapi.Namespace(testNamespace),
}},
To: []gatewayapi.ReferenceGrantTo{{
Group: gatewayapi.Group(""),
Kind: gatewayapi.Kind("Secret"),
Group: "",
Kind: "Secret",
Name: (*gatewayapi.ObjectName)(&to.Name),
}},
},
Expand Down
21 changes: 10 additions & 11 deletions pkg/reconciler/ingress/reconcile_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

Check failure on line 28 in pkg/reconciler/ingress/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

SA1019: "k8s.io/utils/pointer" is deprecated: Use functions in k8s.io/utils/ptr instead: ptr.To to obtain a pointer, ptr.Deref to dereference a pointer, ptr.Equal to compare dereferenced pointers. (staticcheck)
"k8s.io/utils/ptr"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapi "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand All @@ -42,7 +43,6 @@ func (c *Reconciler) reconcileWorkloadRoute(
ctx context.Context, ing *netv1alpha1.Ingress,
rule *netv1alpha1.IngressRule,
) (*gatewayapi.HTTPRoute, error) {

gatewayConfig := config.FromContext(ctx).Gateway.Gateways[rule.Visibility]
gatewayRef := gatewayapi.ParentReference{
Group: (*gatewayapi.Group)(&gatewayapi.GroupVersion.Group),
Expand Down Expand Up @@ -102,26 +102,25 @@ func (c *Reconciler) reconcileHTTPRoute(ctx context.Context,

recorder := controller.GetEventRecorder(ctx)

httproute, err := c.httprouteLister.HTTPRoutes(ing.Namespace).Get(desired.Name)
httpRoute, err := c.httprouteLister.HTTPRoutes(ing.Namespace).Get(desired.Name)
if apierrs.IsNotFound(err) {

httproute, err = c.gwapiclient.GatewayV1beta1().HTTPRoutes(desired.Namespace).Create(ctx, desired, metav1.CreateOptions{})
httpRoute, err = c.gwapiclient.GatewayV1beta1().HTTPRoutes(desired.Namespace).Create(ctx, desired, metav1.CreateOptions{})
if err != nil {
recorder.Eventf(ing, corev1.EventTypeWarning, "CreationFailed", "Failed to create HTTPRoute: %v", err)
return nil, fmt.Errorf("failed to create HTTPRoute: %w", err)
}

recorder.Eventf(ing, corev1.EventTypeNormal, "Created", "Created HTTPRoute %q", httproute.GetName())
return httproute, nil
recorder.Eventf(ing, corev1.EventTypeNormal, "Created", "Created HTTPRoute %q", httpRoute.GetName())
return httpRoute, nil
} else if err != nil {
return nil, err
} else {
if !equality.Semantic.DeepEqual(httproute.Spec, desired.Spec) ||
!equality.Semantic.DeepEqual(httproute.Annotations, desired.Annotations) ||
!equality.Semantic.DeepEqual(httproute.Labels, desired.Labels) {
if !equality.Semantic.DeepEqual(httpRoute.Spec, desired.Spec) ||
!equality.Semantic.DeepEqual(httpRoute.Annotations, desired.Annotations) ||
!equality.Semantic.DeepEqual(httpRoute.Labels, desired.Labels) {

// Don't modify the informers copy.
origin := httproute.DeepCopy()
origin := httpRoute.DeepCopy()

Check warning on line 123 in pkg/reconciler/ingress/reconcile_resources.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/ingress/reconcile_resources.go#L123

Added line #L123 was not covered by tests
origin.Spec = desired.Spec
origin.Annotations = desired.Annotations
origin.Labels = desired.Labels
Expand All @@ -136,7 +135,7 @@ func (c *Reconciler) reconcileHTTPRoute(ctx context.Context,
}
}

return httproute, err
return httpRoute, err
}

func (c *Reconciler) reconcileTLS(
Expand Down
44 changes: 9 additions & 35 deletions pkg/reconciler/ingress/resources/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"sort"

"k8s.io/utils/pointer"

Check failure on line 23 in pkg/reconciler/ingress/resources/httproute.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

SA1019: "k8s.io/utils/pointer" is deprecated: Use functions in k8s.io/utils/ptr instead: ptr.To to obtain a pointer, ptr.Deref to dereference a pointer, ptr.Equal to compare dereferenced pointers. (staticcheck)
"knative.dev/pkg/kmap"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -75,16 +76,6 @@ func makeHTTPRouteSpec(

rules := makeHTTPRouteRule(rule)

gatewayConfig := config.FromContext(ctx).Gateway
namespacedNameGateway := gatewayConfig.Gateways[rule.Visibility].Gateway

gatewayRef := gatewayapi.ParentReference{
Group: (*gatewayapi.Group)(&gatewayapi.GroupVersion.Group),
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
Namespace: ptr.To(gatewayapi.Namespace(namespacedNameGateway.Namespace)),
Name: gatewayapi.ObjectName(namespacedNameGateway.Name),
}

return gatewayapi.HTTPRouteSpec{
Hostnames: hostnames,
Rules: rules,
Expand Down Expand Up @@ -163,14 +154,6 @@ func makeHTTPRouteRule(rule *netv1alpha1.IngressRule) []gatewayapi.HTTPRouteRule
backendRefs = append(backendRefs, backendRef)
}

pathPrefix := "/"
if path.Path != "" {
pathPrefix = path.Path
}
pathMatch := gatewayapi.HTTPPathMatch{
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: ptr.To(pathPrefix),
}
matches := matchesFromRulePath(path)
rule := gatewayapi.HTTPRouteRule{
BackendRefs: backendRefs,
Expand Down Expand Up @@ -236,11 +219,11 @@ func makeRedirectHTTPRouteRule(rule *netv1alpha1.IngressRule) []gatewayapi.HTTPR
for _, path := range rule.HTTP.Paths {
preFilters := []gatewayapi.HTTPRouteFilter{
{
Type: gatewayapi.HTTPRouteFilterRequestRedirect,
Type: gatewayapiv1.HTTPRouteFilterRequestRedirect,
RequestRedirect: &gatewayapi.HTTPRequestRedirectFilter{
Scheme: ptr("https"),
Port: portNumPtr(443),
StatusCode: ptr(http.StatusMovedPermanently),
Scheme: ptr.To("https"),
Port: ptr.To(gatewayapi.PortNumber(443)),
StatusCode: ptr.To(http.StatusMovedPermanently),
},
}}

Expand All @@ -260,24 +243,15 @@ func matchesFromRulePath(path netv1alpha1.HTTPIngressPath) []gatewayapi.HTTPRout
pathPrefix = path.Path
}
pathMatch := gatewayapi.HTTPPathMatch{
Type: ptr(gatewayapi.PathMatchPathPrefix),
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: pointer.String(pathPrefix),
}

var headerMatchList []gatewayapi.HTTPHeaderMatch
for k, v := range path.Headers {
headerMatch := gatewayapi.HTTPHeaderMatch{
Type: ptr.To(gatewayapiv1.HeaderMatchExact),
Name: gatewayapiv1.HTTPHeaderName(k),
Value: v.Exact,
}
headerMatchList = append(headerMatchList, headerMatch)
}
headerMatchList := []gatewayapi.HTTPHeaderMatch{}
var headerMatchList []gatewayapi.HTTPHeaderMatch

Check failure on line 250 in pkg/reconciler/ingress/resources/httproute.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

Consider pre-allocating `headerMatchList` (prealloc)
for k, v := range path.Headers {
headerMatch := gatewayapi.HTTPHeaderMatch{
Type: ptr(gatewayapi.HeaderMatchExact),
Name: gatewayapi.HTTPHeaderName(k),
Type: ptr.To(gatewayapiv1.HeaderMatchExact),
Name: gatewayapiv1.HTTPHeaderName(k),
Value: v.Exact,
}
headerMatchList = append(headerMatchList, headerMatch)
Expand Down
72 changes: 30 additions & 42 deletions pkg/reconciler/ingress/resources/httproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@ import (
"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
"knative.dev/net-gateway-api/pkg/reconciler/ingress/config"
"k8s.io/utils/pointer"

Check failure on line 26 in pkg/reconciler/ingress/resources/httproute_test.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

SA1019: "k8s.io/utils/pointer" is deprecated: Use functions in k8s.io/utils/ptr instead: ptr.To to obtain a pointer, ptr.Deref to dereference a pointer, ptr.Equal to compare dereferenced pointers. (staticcheck)
"k8s.io/utils/ptr"
"knative.dev/networking/pkg/apis/networking"
"knative.dev/networking/pkg/apis/networking/v1alpha1"
"knative.dev/pkg/kmeta"
"knative.dev/pkg/reconciler"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapi "sigs.k8s.io/gateway-api/apis/v1beta1"
)

const (
testNamespace = "test-ns"
testIngressName = "test-ingress"
testGatewayClass = "test-class"
testNamespace = "test-ns"
testIngressName = "test-ingress"
)

var (
Expand All @@ -57,9 +54,9 @@ var (
gatewayRef = gatewayapi.ParentReference{
Group: (*gatewayapi.Group)(pointer.String("gateway.networking.k8s.io")),
Kind: (*gatewayapi.Kind)(pointer.String("Gateway")),
Namespace: ptr[gatewayapi.Namespace]("test-ns"),
Namespace: ptr.To[gatewayapi.Namespace]("test-ns"),
Name: gatewayapi.ObjectName("foo"),
SectionName: ptr[gatewayapi.SectionName]("http"),
SectionName: ptr.To[gatewayapi.SectionName]("http"),
}
)

Expand Down Expand Up @@ -218,7 +215,6 @@ func TestMakeHTTPRoute(t *testing.T) {
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: ptr.To("/"),
},
Headers: HTTPHeaderMatchList{},
},
},
}},
Expand Down Expand Up @@ -298,7 +294,6 @@ func TestMakeHTTPRoute(t *testing.T) {
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: ptr.To("/"),
},
Headers: HTTPHeaderMatchList{},
}},
}},
CommonRouteSpec: gatewayapi.CommonRouteSpec{
Expand Down Expand Up @@ -476,12 +471,7 @@ func TestMakeHTTPRoute(t *testing.T) {
}}},
},
CommonRouteSpec: gatewayapi.CommonRouteSpec{
ParentRefs: []gatewayapi.ParentReference{{
Group: (*gatewayapi.Group)(ptr.To("gateway.networking.k8s.io")),
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
Namespace: ptr.To[gatewayapi.Namespace]("test-ns"),
Name: gatewayapi.ObjectName("foo"),
}},
ParentRefs: []gatewayapi.ParentReference{gatewayRef},
},
},
}},
Expand Down Expand Up @@ -566,20 +556,19 @@ func TestMakeRedirectHTTPRoute(t *testing.T) {
Hostnames: []gatewayapi.Hostname{externalHost},
Rules: []gatewayapi.HTTPRouteRule{{
Filters: []gatewayapi.HTTPRouteFilter{{
Type: gatewayapi.HTTPRouteFilterRequestRedirect,
Type: gatewayapiv1.HTTPRouteFilterRequestRedirect,
RequestRedirect: &gatewayapi.HTTPRequestRedirectFilter{
Scheme: ptr("https"),
Port: portNumPtr(443),
StatusCode: ptr(http.StatusMovedPermanently),
Scheme: ptr.To("https"),
Port: ptr.To(gatewayapi.PortNumber(443)),
StatusCode: ptr.To(http.StatusMovedPermanently),
},
}},
Matches: []gatewayapi.HTTPRouteMatch{
{
Path: &gatewayapi.HTTPPathMatch{
Type: ptr(gatewayapi.PathMatchPathPrefix),
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: pointer.String("/"),
},
Headers: HTTPHeaderMatchList{},
},
},
}},
Expand All @@ -601,19 +590,18 @@ func TestMakeRedirectHTTPRoute(t *testing.T) {
Hostnames: []gatewayapi.Hostname{localHostShortest, localHostShort, localHostFull},
Rules: []gatewayapi.HTTPRouteRule{{
Filters: []gatewayapi.HTTPRouteFilter{{
Type: gatewayapi.HTTPRouteFilterRequestRedirect,
Type: gatewayapiv1.HTTPRouteFilterRequestRedirect,
RequestRedirect: &gatewayapi.HTTPRequestRedirectFilter{
Scheme: ptr("https"),
Port: portNumPtr(443),
StatusCode: ptr(http.StatusMovedPermanently),
Scheme: ptr.To("https"),
Port: ptr.To(gatewayapi.PortNumber(443)),
StatusCode: ptr.To(http.StatusMovedPermanently),
},
}},
Matches: []gatewayapi.HTTPRouteMatch{{
Path: &gatewayapi.HTTPPathMatch{
Type: ptr(gatewayapi.PathMatchPathPrefix),
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: pointer.String("/"),
},
Headers: HTTPHeaderMatchList{},
}},
}},
CommonRouteSpec: gatewayapi.CommonRouteSpec{
Expand Down Expand Up @@ -682,43 +670,43 @@ func TestMakeRedirectHTTPRoute(t *testing.T) {
Rules: []gatewayapi.HTTPRouteRule{
{
Filters: []gatewayapi.HTTPRouteFilter{{
Type: gatewayapi.HTTPRouteFilterRequestRedirect,
Type: gatewayapiv1.HTTPRouteFilterRequestRedirect,
RequestRedirect: &gatewayapi.HTTPRequestRedirectFilter{
Scheme: ptr("https"),
Port: portNumPtr(443),
StatusCode: ptr(http.StatusMovedPermanently),
Scheme: ptr.To("https"),
Port: ptr.To(gatewayapi.PortNumber(443)),
StatusCode: ptr.To(http.StatusMovedPermanently),
},
}},
Matches: []gatewayapi.HTTPRouteMatch{
{
Path: &gatewayapi.HTTPPathMatch{
Type: ptr(gatewayapi.PathMatchPathPrefix),
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: pointer.String("/"),
},
Headers: []gatewayapi.HTTPHeaderMatch{{
Type: ptr(gatewayapi.HeaderMatchExact),
Name: gatewayapi.HTTPHeaderName("tag"),
Type: ptr.To(gatewayapiv1.HeaderMatchExact),
Name: gatewayapiv1.HTTPHeaderName("tag"),
Value: "goo",
}},
}},
}, {
Filters: []gatewayapi.HTTPRouteFilter{{
Type: gatewayapi.HTTPRouteFilterRequestRedirect,
Type: gatewayapiv1.HTTPRouteFilterRequestRedirect,
RequestRedirect: &gatewayapi.HTTPRequestRedirectFilter{
Scheme: ptr("https"),
Port: portNumPtr(443),
StatusCode: ptr(http.StatusMovedPermanently),
Scheme: ptr.To("https"),
Port: ptr.To(gatewayapi.PortNumber(443)),
StatusCode: ptr.To(http.StatusMovedPermanently),
},
}},
Matches: []gatewayapi.HTTPRouteMatch{
{
Path: &gatewayapi.HTTPPathMatch{
Type: ptr(gatewayapi.PathMatchPathPrefix),
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: pointer.String("/doo"),
},
Headers: []gatewayapi.HTTPHeaderMatch{{
Type: ptr(gatewayapi.HeaderMatchExact),
Name: gatewayapi.HTTPHeaderName("tag"),
Type: ptr.To(gatewayapiv1.HeaderMatchExact),
Name: gatewayapiv1.HTTPHeaderName("tag"),
Value: "doo",
}},
}},
Expand Down

0 comments on commit cb21626

Please sign in to comment.