Skip to content

Commit ed82622

Browse files
[release-1.17] Create a per ingress ReferenceGrant to avoid conflicts in the same namespace (#830)
* Create a per ingress ReferenceGrant to avoid conflicts in the same namespace Signed-off-by: Rajat Vig <[email protected]> * Format Signed-off-by: Rajat Vig <[email protected]> * Add a test for the MakeReferenceGrant method Signed-off-by: Rajat Vig <[email protected]> * Fix lint error Signed-off-by: Rajat Vig <[email protected]> * Use kmeta.ChildName and change test for long names Signed-off-by: Rajat Vig <[email protected]> --------- Signed-off-by: Rajat Vig <[email protected]> Co-authored-by: Rajat Vig <[email protected]>
1 parent b6da539 commit ed82622

File tree

3 files changed

+122
-16
lines changed

3 files changed

+122
-16
lines changed

pkg/reconciler/ingress/ingress_test.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func TestReconcile(t *testing.T) {
229229
func TestReconcileTLS(t *testing.T) {
230230
// The gateway API annoyingly has a number of
231231
secretName := "name-WE-STICK-A-LONG-UID-HERE"
232+
ingressName := "name"
232233
nsName := "ns"
233234
deleteTime := time.Now().Add(-10 * time.Second)
234235
table := TableTest{{
@@ -240,8 +241,8 @@ func TestReconcileTLS(t *testing.T) {
240241
gw(defaultListener),
241242
},
242243
WantCreates: []runtime.Object{
243-
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS())),
244-
rp(secret(secretName, nsName)),
244+
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS(), withName(ingressName))),
245+
rp(ingressName, secret(secretName, nsName)),
245246
},
246247
WantUpdates: []clientgotesting.UpdateActionImpl{{
247248
Object: gw(defaultListener, tlsListener("example.com", nsName, secretName)),
@@ -271,8 +272,8 @@ func TestReconcileTLS(t *testing.T) {
271272
ing(withBasicSpec, withFinalizer, withGatewayAPIClass, withTLS(), makeItReady),
272273
secret(secretName, nsName),
273274
gw(defaultListener, tlsListener("example.com", nsName, secretName)),
274-
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS()), httpRouteReady),
275-
rp(secret(secretName, nsName)),
275+
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS(), withName(ingressName)), httpRouteReady),
276+
rp(ingressName, secret(secretName, nsName)),
276277
},
277278
WantUpdates: []clientgotesting.UpdateActionImpl{
278279
// None
@@ -292,8 +293,8 @@ func TestReconcileTLS(t *testing.T) {
292293
}),
293294
secret(secretName, nsName),
294295
gw(defaultListener, tlsListener("secure.example.com", nsName, secretName)),
295-
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS())),
296-
rp(secret(secretName, nsName)),
296+
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS(), withName(ingressName))),
297+
rp(ingressName, secret(secretName, nsName)),
297298
},
298299
WantUpdates: []clientgotesting.UpdateActionImpl{{
299300
Object: gw(defaultListener),
@@ -307,8 +308,8 @@ func TestReconcileTLS(t *testing.T) {
307308
secret(secretName, nsName),
308309
},
309310
WantCreates: []runtime.Object{
310-
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS())),
311-
rp(secret(secretName, nsName)),
311+
httpRoute(t, ing(withBasicSpec, withGatewayAPIClass, withTLS(), withName(ingressName))),
312+
rp(ingressName, secret(secretName, nsName)),
312313
},
313314
WantUpdates: []clientgotesting.UpdateActionImpl{
314315
// None
@@ -2546,6 +2547,12 @@ func withTLS() IngressOption {
25462547
}
25472548
}
25482549

2550+
func withName(name string) IngressOption {
2551+
return func(i *v1alpha1.Ingress) {
2552+
i.Name = name
2553+
}
2554+
}
2555+
25492556
func secret(name, ns string) *corev1.Secret {
25502557
return &corev1.Secret{
25512558
ObjectMeta: metav1.ObjectMeta{
@@ -2560,11 +2567,11 @@ func secret(name, ns string) *corev1.Secret {
25602567
}
25612568
}
25622569

2563-
func rp(to *corev1.Secret) *gatewayapiv1beta1.ReferenceGrant {
2570+
func rp(ingressName string, to *corev1.Secret) *gatewayapiv1beta1.ReferenceGrant {
25642571
t := true
25652572
return &gatewayapiv1beta1.ReferenceGrant{
25662573
ObjectMeta: metav1.ObjectMeta{
2567-
Name: to.Name + "-" + testNamespace,
2574+
Name: ingressName + "-" + to.Name + "-" + testNamespace,
25682575
Namespace: to.Namespace,
25692576
OwnerReferences: []metav1.OwnerReference{{
25702577
APIVersion: "networking.internal.knative.dev/v1alpha1",

pkg/reconciler/ingress/resources/reference_grant.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ package resources
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
netv1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1"
2425
"knative.dev/pkg/kmeta"
2526
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
2627
)
2728

28-
// Grant the resource "to" access to the resource "from"
29+
// MakeReferenceGrant Grant the resource "to" access to the resource "from"
2930
func MakeReferenceGrant(_ context.Context, ing *netv1alpha1.Ingress, to, from metav1.PartialObjectMetadata) *gatewayv1beta1.ReferenceGrant {
30-
name := to.Name
31-
if len(name)+len(from.Namespace) > 62 {
32-
name = name[:62-len(from.Namespace)]
33-
}
34-
name += "-" + from.Namespace
31+
name := kmeta.ChildName(ing.Name, fmt.Sprintf("-%s-%s", to.Name, from.Namespace))
3532

3633
return &gatewayv1beta1.ReferenceGrant{
3734
ObjectMeta: metav1.ObjectMeta{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2021 The Knative Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package resources
18+
19+
import (
20+
"context"
21+
"strings"
22+
"testing"
23+
24+
"knative.dev/pkg/ptr"
25+
26+
"github.com/google/go-cmp/cmp"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
netv1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1"
29+
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
30+
)
31+
32+
func TestMakeReferenceGrantLongName(t *testing.T) {
33+
ing := &netv1alpha1.Ingress{
34+
ObjectMeta: metav1.ObjectMeta{
35+
Name: "test-ingress",
36+
Namespace: "test-namespace",
37+
},
38+
}
39+
40+
to := metav1.PartialObjectMetadata{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Name: "to-resource",
43+
Namespace: "to-namespace",
44+
Labels: map[string]string{
45+
"app": "test-app",
46+
},
47+
Annotations: map[string]string{
48+
"annotation-key": "annotation-value",
49+
},
50+
},
51+
TypeMeta: metav1.TypeMeta{
52+
Kind: "Service",
53+
APIVersion: "v1",
54+
},
55+
}
56+
57+
from := metav1.PartialObjectMetadata{
58+
ObjectMeta: metav1.ObjectMeta{
59+
Namespace: strings.Repeat("f", 63),
60+
},
61+
TypeMeta: metav1.TypeMeta{
62+
Kind: "Ingress",
63+
APIVersion: "networking.k8s.io/v1",
64+
},
65+
}
66+
67+
want := &gatewayv1beta1.ReferenceGrant{
68+
ObjectMeta: metav1.ObjectMeta{
69+
Name: "test-ingress0156b17a6c5096e2fdeb6058cc449e26-to-resource-ffffff",
70+
Namespace: "to-namespace",
71+
Labels: to.Labels,
72+
Annotations: to.Annotations,
73+
OwnerReferences: []metav1.OwnerReference{
74+
{
75+
APIVersion: "networking.internal.knative.dev/v1alpha1",
76+
Kind: "Ingress",
77+
Name: "test-ingress",
78+
Controller: ptr.Bool(true),
79+
BlockOwnerDeletion: ptr.Bool(true),
80+
},
81+
},
82+
},
83+
Spec: gatewayv1beta1.ReferenceGrantSpec{
84+
From: []gatewayv1beta1.ReferenceGrantFrom{{
85+
Group: gatewayv1beta1.Group("networking.k8s.io"),
86+
Kind: gatewayv1beta1.Kind("Ingress"),
87+
Namespace: gatewayv1beta1.Namespace(strings.Repeat("f", 63)),
88+
}},
89+
To: []gatewayv1beta1.ReferenceGrantTo{{
90+
Group: gatewayv1beta1.Group(""),
91+
Kind: gatewayv1beta1.Kind("Service"),
92+
Name: (*gatewayv1beta1.ObjectName)(&to.Name),
93+
}},
94+
},
95+
}
96+
97+
got := MakeReferenceGrant(context.TODO(), ing, to, from)
98+
99+
if diff := cmp.Diff(want, got); diff != "" {
100+
t.Errorf("MakeReferenceGrant (-want, +got):\n%s", diff)
101+
}
102+
}

0 commit comments

Comments
 (0)