Skip to content

Commit df378db

Browse files
committed
feat: add creation of ReferenceGrant objects for custom backend refs in Gateway API
This feature is used only when referencing Services in another namespace using custom backend refs in Gateway API. It ensures proper cross-namespace access by automatically creating the necessary ReferenceGrant objects. Signed-off-by: kahirokunn <okinakahiro@gmail.com>
1 parent f5c9687 commit df378db

File tree

15 files changed

+717
-1
lines changed

15 files changed

+717
-1
lines changed

charts/flagger/templates/rbac.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ rules:
226226
resources:
227227
- httproutes
228228
- httproutes/finalizers
229+
- referencegrants
230+
- referencegrants/finalizers
229231
verbs:
230232
- get
231233
- list
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Copyright 2021 The Kubernetes 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 v1beta1
18+
19+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
// +genclient
22+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
23+
// +kubebuilder:object:root=true
24+
// +kubebuilder:resource:categories=gateway-api,shortName=refgrant
25+
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
26+
// +kubebuilder:storageversion
27+
28+
// ReferenceGrant identifies kinds of resources in other namespaces that are
29+
// trusted to reference the specified kinds of resources in the same namespace
30+
// as the policy.
31+
//
32+
// Each ReferenceGrant can be used to represent a unique trust relationship.
33+
// Additional Reference Grants can be used to add to the set of trusted
34+
// sources of inbound references for the namespace they are defined within.
35+
//
36+
// All cross-namespace references in Gateway API (with the exception of cross-namespace
37+
// Gateway-route attachment) require a ReferenceGrant.
38+
//
39+
// ReferenceGrant is a form of runtime verification allowing users to assert
40+
// which cross-namespace object references are permitted. Implementations that
41+
// support ReferenceGrant MUST NOT permit cross-namespace references which have
42+
// no grant, and MUST respond to the removal of a grant by revoking the access
43+
// that the grant allowed.
44+
type ReferenceGrant struct {
45+
metav1.TypeMeta `json:",inline"`
46+
metav1.ObjectMeta `json:"metadata,omitempty"`
47+
48+
// Spec defines the desired state of ReferenceGrant.
49+
Spec ReferenceGrantSpec `json:"spec,omitempty"`
50+
51+
// Note that `Status` sub-resource has been excluded at the
52+
// moment as it was difficult to work out the design.
53+
// `Status` sub-resource may be added in future.
54+
}
55+
56+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
57+
// +kubebuilder:object:root=true
58+
// ReferenceGrantList contains a list of ReferenceGrant.
59+
type ReferenceGrantList struct {
60+
metav1.TypeMeta `json:",inline"`
61+
metav1.ListMeta `json:"metadata,omitempty"`
62+
Items []ReferenceGrant `json:"items"`
63+
}
64+
65+
// ReferenceGrantSpec identifies a cross namespace relationship that is trusted
66+
// for Gateway API.
67+
type ReferenceGrantSpec struct {
68+
// From describes the trusted namespaces and kinds that can reference the
69+
// resources described in "To". Each entry in this list MUST be considered
70+
// to be an additional place that references can be valid from, or to put
71+
// this another way, entries MUST be combined using OR.
72+
//
73+
// Support: Core
74+
//
75+
// +kubebuilder:validation:MinItems=1
76+
// +kubebuilder:validation:MaxItems=16
77+
From []ReferenceGrantFrom `json:"from"`
78+
79+
// To describes the resources that may be referenced by the resources
80+
// described in "From". Each entry in this list MUST be considered to be an
81+
// additional place that references can be valid to, or to put this another
82+
// way, entries MUST be combined using OR.
83+
//
84+
// Support: Core
85+
//
86+
// +kubebuilder:validation:MinItems=1
87+
// +kubebuilder:validation:MaxItems=16
88+
To []ReferenceGrantTo `json:"to"`
89+
}
90+
91+
// ReferenceGrantFrom describes trusted namespaces and kinds.
92+
type ReferenceGrantFrom struct {
93+
// Group is the group of the referent.
94+
// When empty, the Kubernetes core API group is inferred.
95+
//
96+
// Support: Core
97+
Group Group `json:"group"`
98+
99+
// Kind is the kind of the referent. Although implementations may support
100+
// additional resources, the following types are part of the "Core"
101+
// support level for this field.
102+
//
103+
// When used to permit a SecretObjectReference:
104+
//
105+
// * Gateway
106+
//
107+
// When used to permit a BackendObjectReference:
108+
//
109+
// * GRPCRoute
110+
// * HTTPRoute
111+
// * TCPRoute
112+
// * TLSRoute
113+
// * UDPRoute
114+
Kind Kind `json:"kind"`
115+
116+
// Namespace is the namespace of the referent.
117+
//
118+
// Support: Core
119+
Namespace Namespace `json:"namespace"`
120+
}
121+
122+
// ReferenceGrantTo describes what Kinds are allowed as targets of the
123+
// references.
124+
type ReferenceGrantTo struct {
125+
// Group is the group of the referent.
126+
// When empty, the Kubernetes core API group is inferred.
127+
//
128+
// Support: Core
129+
Group Group `json:"group"`
130+
131+
// Kind is the kind of the referent. Although implementations may support
132+
// additional resources, the following types are part of the "Core"
133+
// support level for this field:
134+
//
135+
// * Secret when used to permit a SecretObjectReference
136+
// * Service when used to permit a BackendObjectReference
137+
Kind Kind `json:"kind"`
138+
139+
// Name is the name of the referent. When unspecified, this policy
140+
// refers to all resources of the specified Group and Kind in the local
141+
// namespace.
142+
//
143+
// +optional
144+
Name *ObjectName `json:"name,omitempty"`
145+
}

pkg/apis/gatewayapi/v1beta1/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
3333
scheme.AddKnownTypes(SchemeGroupVersion,
3434
&HTTPRoute{},
3535
&HTTPRouteList{},
36+
&ReferenceGrant{},
37+
&ReferenceGrantList{},
3638
)
3739
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
3840
return nil

pkg/apis/gatewayapi/v1beta1/zz_generated.deepcopy.go

Lines changed: 125 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/clientset/versioned/typed/gatewayapi/v1beta1/fake/fake_gatewayapi_client.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)